LVS+Nginx+Keepalived高可用集群

LVS+Nginx+Keepalived高可用集群

准备

四台服务器

192.168.1.105
192.168.1.104
192.168.1.120 VIP
192.168.1.103 LVS Master
192.168.1.102 LVS Backup

基础环境

102 和 103 节点安装 Keepalived,104 和 105 节点安装 Nginx 和 Keepalived,参考:/archives/nginxkeepalived实现双机主备和双主热备

配置 104 和 105 节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/etc/sysconfig/network-scripts
cp ifcfg-lo ifcfg-lo:1

vim ifcfg-lo:1
DEVICE=lo:1
IPADDR=192.168.1.120
NETMASK=255.255.255.255
NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback

service network restart
1
2
3
4
5
6
7
8
9
10
11
12
vim /etc/sysctl.conf
# configration for lvs
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2

sysctl -p
route add -host 192.168.1.120 dev lo:1
echo "route add -host 192.168.1.120 dev lo:1" >> /etc/rc.local

配置 103 节点

安装 ipvsadm 并配置 ip
1
yum -y install ipvsadm
1
2
3
4
5
6
7
8
9
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens33:1

vim ifcfg-ens33:1
BOOTPROTO=static
DEVICE=ens33:1
ONBOOT=yes
IPADDR=192.168.1.120
NETMASK=255.255.255.0
修改 Keepalived 配置文件后重启
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
global_defs {
router_id LVS_102
}

vrrp_instance VI_1 {
state MASTER
interface ens33:1
virtual_router_id 66
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.120
}
}

virtual_server 192.168.1.120 80 {
# 健康检查的时间,单位:秒
delay_loop 6
# 配置负载均衡算法,默认是轮询
lb_algo rr
# 设置LVS的模式 NAT|TUN|DR
lb_kind DR
# 设置会话持久化的时间
persistence_timeout 5
# 协议
protocol TCP

# 负载均衡的真实服务器,也就是Nginx节点的真实IP地址
real_server 192.168.1.104 80 {
# 轮询的默认权重配比设置为1
weight 1
# 设置健康检测
TCP_CHECK {
# 检查的80端口
connect_port 80
# 检查的超时时间
connect_timeout 2
# 重试次数 2次
nb_get_retry 2
# 间隔时间 3秒
delay_before_retry 3
}
}
real_server 192.168.1.105 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 2
nb_get_retry 2
delay_before_retry 3
}
}
}

配置 102 节点

安装 ipvsadm
1
yum -y install ipvsadm
修改 Keepalived 配置文件后重启
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
global_defs {
router_id LVS_102
}

vrrp_instance VI_1 {
state BACKUP
interface ens33:1
virtual_router_id 66
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.120
}
}

virtual_server 192.168.1.120 80 {
# 健康检查的时间,单位:秒
delay_loop 6
# 配置负载均衡算法,默认是轮询
lb_algo rr
# 设置LVS的模式 NAT|TUN|DR
lb_kind DR
# 设置会话持久化的时间
persistence_timeout 5
# 协议
protocol TCP

# 负载均衡的真实服务器,也就是Nginx节点的真实IP地址
real_server 192.168.1.104 80 {
# 轮询的默认权重配比设置为1
weight 1
# 设置健康检测
TCP_CHECK {
# 检查的80端口
connect_port 80
# 检查的超时时间
connect_timeout 2
# 重试次数 2次
nb_get_retry 2
# 间隔时间 3秒
delay_before_retry 3
}
}
real_server 192.168.1.105 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 2
nb_get_retry 2
delay_before_retry 3
}
}
}