keepalived 多节点配置方法
准备3台主机
server | hostname | ip |
---|---|---|
keepalived | s1 | 172.20.27.10 |
keepalived | s2 | 172.20.27.11 |
keepalived | s3 | 172.20.27.12 |
S1节点配置
1.修改配置文件
[root@s1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@mylinuxops.com
}
notification_email_from root@mylinuxops.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id s1.mylinuxops.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables #需要关闭让keepalived产生iptables规则
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state Master
interface ens33 #需要注意网卡名称,不一定都是ens33
virtual_router_id 27
priority 100 #需要注意各个节点的优先级,当主机点挂了之后,其余的节点会根据,优先级的高低选举出,新的主机点。
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.10
unicast_peer {
172.20.27.11 #单播配置时需要将所有的节点都加入
172.20.27.12
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
}
}
2.启动服务
[root@s1 ~]# systemctl start keepalived
s2节点配置
1.修改配置文件
[root@s2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@mylinuxops.com
}
notification_email_from root@mylinuxops.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id s2.mylinuxops.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 27
priority 80
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.11
unicast_peer {
172.20.27.10
172.20.27.12
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
}
track_script {
check_nginx
}
}
2.启动服务
[root@s2 ~]# systemctl start keepalived
s3节点配置
1.修改配置文件
[root@s3 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@mylinuxops.com
}
notification_email_from root@mylinuxops.com
smtp_server 172.0.0.1
smtp_connect_timeout 30
router_id s3.mylinuxops.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 27
priority 50
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.12
unicast_peer {
172.20.27.10
172.20.27.11
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
}
}
2.启动服务
[root@s2 ~]# systemctl start keepalived
测试
查看当前VIP所在主机
[root@s1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.10 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::20c:29ff:fec5:123c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
RX packets 283462 bytes 22421914 (21.3 MiB)
RX errors 0 dropped 12 overruns 0 frame 0
TX packets 50515 bytes 9459257 (9.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.100 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 2 bytes 140 (140.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2 bytes 140 (140.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vip在s1主机上,当s1节点挂了之后,由于s2节点的优先级高于s3,所以s2获得了vip
[root@s2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.11 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::20c:29ff:fe4d:1ce3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
RX packets 271011 bytes 21681786 (20.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16217 bytes 1380138 (1.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.100 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 6 bytes 482 (482.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 482 (482.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
当s2节点也挂了之后,s3节点获得vip
[root@s3 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.12 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::20c:29ff:fe99:608a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:99:60:8a txqueuelen 1000 (Ethernet)
RX packets 20519 bytes 1491603 (1.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1775 bytes 215565 (210.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.100 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:99:60:8a txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 106 bytes 8744 (8.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 106 bytes 8744 (8.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
评论区