LVS+Keepalived部署

1.环境说明

        六台服务器,两台作为director,四台作为两组real server。director为10.8.8.110(keepalived master)和10.8.8.111(keepalived backup),vip:10.8.8.112。两组real server为10.8.8.101:80,10.8.8.102:80和10.8.8.105:8889,10.8.8.106:8889

2.安装和配置LVS

        两台director上安装ipvadm

1
yum install -y ipvsadm

        两台director上编写DR模式脚本

1
vim /usr/local/sbin/lvs_dr.sh

        添加内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=10.8.8.112
rs1=10.8.8.101
rs2=10.8.8.102
rs3=10.8.8.105
rs4=10.8.8.106
$ipv -C
$ipv -A -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
$ipv -A -t $vip:8889 -s rr
$ipv -a -t $vip:8889 -r $rs3:8889 -g -w 1
$ipv -a -t $vip:8889 -r $rs4:8889 -g -w 1

        然后在四台real server上编写脚本

1
vim /usr/local/sbin/lvs_dr_rs.sh

        添加内容

1
2
3
4
5
6
7
8
#! /bin/bash
vip=10.8.8.112
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

3.安装Keepalived

        两台director上执行

1
yum install -y keepalived

        两台director上编辑配置文件

1
vim /etc/keepalived/keepalived.conf

        加入配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
vrrp_instance VI_1 {
state MASTER //备上为BACKUP
interface eno16777984 //备的网卡名
#lvs_sync_daemon_inteface eno16777984
virtual_router_id 51
priority 100 //备为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.8.8.112
}
}
virtual_server 10.8.8.112 80 {
delay_loop 3
lb_algo wlc
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 60
protocol TCP
real_server 10.8.8.101 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.8.8.102 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
virtual_server 10.8.8.112 8889 {
delay_loop 3
lb_algo wlc
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 60
protocol TCP
real_server 10.8.8.105 8889 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8889
}
}
real_server 10.8.8.106 8889 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8889
}
}
}

4.启动LVS+Keepalived

        两台director执行脚本

1
sh /usr/local/sbin/lvs_dr.sh

        两台director启动keepalived

1
systemctl start keepalived

        四台real server执行脚本

1
sh /usr/local/sbin/lvs_dr_rs.sh

        LVS+Keepalived部署完成