#
#keepalived 官网
#keepalived 中文文档
#keepalived 下载
#
#逻辑图
主机名 | keeoalived角色 | ip地址 |
Storage_1 | Master | 192.168.174.137 Vip:192.168.174.100 |
Storage_2 | Backup | 192.168.174.138 |
#均关闭防火墙和selinux
下载软件 编译安装
1
2
3
4
5
|
[root@Storage_2 ~]
# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
[root@Storage_2 ~]
#tar -xf keepalived-1.3.5.tar.gz
[root@Storage_2 ~]
#cd keepalived-1.3.5
[root@Storage_2 keepalived-1.3.5]
# ./configure --prefix=/usr/local/keepalived
[root@Storage_2 keepalived-1.3.5]
# make && make install
|
1
2
|
[root@Storage_1 ~]
# wget
#同理
|
或者yum 安装 (系统镜像内有)
1
|
[root@Storage_1 ~]
# yum install -y keepalived
|
1
|
[root@Storage_2 ~]
# yum install -y keepalived
|
本文采用的是yum安装
nginx安装
#前面的文章已经安装过很多次了,这里就不再重复
#开启nginx
1
|
[root@Storage_1 ~]
# /usr/local/nginx/sbin/nginx
|
1
|
[root@Storage_2 ~]
# /usr/local/nginx/sbin/nginx
|
#修改keepalived配置文件(Master节点)
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
|
[root@Storage_1 ~]
# vim /etc/keepalived/keepalived.conf
[root@Storage_1 ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
test
@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.01
smtp_connect_timeout 30
router_id Stroage_1
}
vrrp_script chk_nginx {
#脚本
script
"/opt/chk_nginx_status.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 150
#优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.100
/24
}
track_script {
chk_nginx
}
}
|
编写检查nginx状态的脚本
1
2
3
4
5
6
7
8
9
|
[root@Storage_1 ~]
# vim /opt/chk_nginx_status.sh
[root@Storage_1 ~]
# cat /opt/chk_nginx_status.sh
#!/bin/bash
if
[ `
netstat
-lntup |
grep
"nginx: master"
|
wc
-l` -
eq
0 ]
then
systemctl stop keepalived
#如果nginx挂掉,就关闭keepalived
fi
[root@Storage_1 ~]
# chmod +x /opt/chk_nginx_status.sh
|
#修改keepalived配置文件(Backup节点)
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
|
[root@Storage_2 ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
test
@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.01
smtp_connect_timeout 30
router_id Stroage_2
#在同一个局域网内唯一
}
vrrp_instance VI_1 {
#实例名称
state BACKUP
#角色
interface eno16777736
#接口
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.100
/24
#vip
}
}
|
#开启服务
1
|
[root@Storage_1 ~]
# systemctl start keepalived.service
|
1
|
[root@Storage_2 ~]
# systemctl start keepalived.service
|
#测试
#查看ip(vip出现在master上)
1
2
3
4
5
6
7
8
9
10
|
[root@Storage_1 ~]
# ip addr show eno16777736
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:87:00:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.174.137
/24
brd 192.168.174.255 scope global dynamic eno16777736
valid_lft 1684sec preferred_lft 1684sec
inet 192.168.174.100
/24
scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe87:97
/64
scope link
valid_lft forever preferred_lft forever
[root@Storage_1 ~]
#
|
1
2
3
4
5
6
7
8
|
[root@Storage_2 ~]
# ip addr show eno16777736
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:78:f4:d9 brd ff:ff:ff:ff:ff:ff
inet 192.168.174.138
/24
brd 192.168.174.255 scope global dynamic eno16777736
valid_lft 1595sec preferred_lft 1595sec
inet6 fe80::20c:29ff:fe78:f4d9
/64
scope link
valid_lft forever preferred_lft forever
[root@Storage_2 ~]
#
|
#测试
#关闭主上的nginx
1
|
[root@Storage_1 ~]
# /usr/local/nginx/sbin/nginx -s quit
|
#主上的vip不见了
1
2
3
4
5
6
7
|
[root@Storage_1 ~]
# ip addr show eno16777736
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:87:00:97 brd ff:ff:ff:ff:ff:ff
inet 192.168.174.137
/24
brd 192.168.174.255 scope global dynamic eno16777736
valid_lft 1326sec preferred_lft 1326sec
inet6 fe80::20c:29ff:fe87:97
/64
scope link
valid_lft forever preferred_lft forever
|
#备上出现了vip
1
2
3
4
5
6
7
8
9
|
[root@Storage_2 ~]
# ip addr show eno16777736
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:78:f4:d9 brd ff:ff:ff:ff:ff:ff
inet 192.168.174.138
/24
brd 192.168.174.255 scope global dynamic eno16777736
valid_lft 1397sec preferred_lft 1397sec
inet 192.168.174.100
/24
scope global secondary eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe78:f4d9
/64
scope link
valid_lft forever preferred_lft forever
|
#访问测试
#vip成功漂移
本文转自 chaunceyjiang 51CTO博客,原文链接:http://blog.51cto.com/cqwujiang/1918990,如需转载请自行联系原作者