keepalived实例--双主

keepalived实例–双主

环境:

RS:real_server 以下全部简称
两台RS配置IP
172.16.2.10/16
172.16.2.20/16
安装httpd,每台上面做一个不同的测试页,以便区分。实际中主备必须一样。



调度器:director
两台调度器
172.16.2.30/16
172.16.2.40/16
安装keepalived
安装ipvsadm(这里配置用不到,只做测试用)

vip:virtual ip 客户端发送请求的IP
vip组
172.16.2.88/16
172.16.2.99/16

以上主机全部同步时间

配置:

RS配置

两台RS使用脚本配置回环网卡IP(vip)

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
#!/bin/bash
#
vip='172.16.2.99' #dr模式下的必须操作,vip是什么,变量内vip就写什么。这里配置成88和99.
netmask='255.255.255.255'
iface='lo:0' #网卡别名。依次排序,不能重复
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $iface $vip netmask $netmask broadcast $vip up
route add -host $vip dev $iface
;;
stop)
ifconfig $iface down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
*)
echo "Usage : $(basename $0) start|stop"
exit 1
;;
esac

两台RS安装httpd

安装
yum install httpd
提供测试页
echo RS1 > /var/www/html/index.html

RS2 同上

完成后启动服务,在调度器主机上能访问即可。

调度器配置

安装好keepalived、ipvsadm后开始配置

配置keepalived

修改配置文件。添加vrrp和virtual_server,配置如下。

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.1.101.33
}
vrrp_instance VI_1 {
state MASTER
priority 100
interface eno16777736
virtual_router_id 33
advert_int 1
authentication {
auth_type PASS
auth_pass RT3SKUI2
}
virtual_ipaddress {
172.16.2.99/16 dev eno16777736 label eno16777736:0
}
# notify_master "/etc/keepalived/notify.sh master"
# notify_backup "/etc/keepalived/notify.sh backup"
# notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 172.16.2.99 80 {
delay_loop 1
lb_algo wrr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 172.16.2.10 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
nb_get_retry 3
delay_before_retry 2
connect_timeout 3
}
}
real_server 172.16.2.20 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
nb_get_retry 3
delay_before_retry 2
connect_timeout 3
}
}
}
}
vrrp_instance VI_2 {
state BACKUP
priority 95
interface eno16777736
virtual_router_id 34
advert_int 1
authentication {
auth_type PASS
auth_pass OT4SKOI2
}
virtual_ipaddress {
172.16.2.88/16 dev eno16777736 label eno16777736:1
}
# notify_master "/etc/keepalived/notify.sh master"
# notify_backup "/etc/keepalived/notify.sh backup"
# notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 172.16.2.88 80 {
delay_loop 1
lb_algo wrr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 172.16.2.10 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
nb_get_retry 3
delay_before_retry 2
connect_timeout 3
}
}
real_server 172.16.2.20 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
nb_get_retry 3
delay_before_retry 2
connect_timeout 3
}
}
}

附上代码中注释的邮件提醒服务脚本:

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
#!/bin/bash
#
contact='root@localhost'
notify() {
mailsubject="$(hostname) to be $1, vip floating"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac

启动keepalived

检查成功与否:

ifconfig查IP
    确保在主节点上可以看到设置的网卡别名,以及IP

ipvsadm -ln 查规则
    确保后端两台RS已经被加入到集群里。
    注意:这里的规则不是使用ipvsadm添加。是keepalived根据配置文件从内核中直接生成的。

systemctl status keepalived 查看keepalived的状态
    可以看到keepalived的详细状态信息。

确保以上三项全部没问题以后再使用一台客户端对vip进行访问就可以了。

还可以进行测试。停掉后端一台RS的http服务。查看访问结果。

<% if (theme.canvas_nest) { %> <% } %> s