keepalived 双主模型
双主模型是两台服务器互为主备,即一台为主备,另一台为备主,让两台服务器并行运行,也可以实现减轻单台keepalived主机上的压力。
双主模型需要注意此时需要有2个VIP地址
keepalived 双主模型实现
准备主机2台
server | hostname | ip |
---|---|---|
keepalived | s1 | 172.20.27.10 |
keepalived | s2 | 172.20.27.11 |
s1 节点配置
修改配置文件
[root@s1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id s1.mylinuxops.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
# 双主模型需要定义两个VIP配置端,一个为主的配置端一个为备的配置端
vrrp_instance VI_1 { #配置第一个实例
state MASTER #定义角色为主
interface ens33
virtual_router_id 27 #双主时需要注意虚拟路由必须与另一个虚拟路由ID不同
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 #定义单播的目标地址
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0 #在一个实例中,vip可以是多个,此处以绑定两个地址为例子,双主模型中如果每个主的实例中都有两个vip,那么当其中一个主挂了之后另一个将获取4个地址。
172.20.27.101 dev ens33 label ens33:1
}
}
vrrp_instance VI_2 { #定义第二个实例
state BACKUP #双主模型中第二个实例的角色就为备
interface ens33
virtual_router_id 37 #虚拟路由ID需要改为和第一个实例不同
priority 80 #因为为备服务器所以优先级需要降低
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.10 #设置单播的源地址
unicast_peer {
172.20.27.11 #设置单薄的目标地址
}
virtual_ipaddress {
172.20.27.200 dev ens33 label ens33:2 #配置虚拟IP
172.20.27.201 dev ens33 label ens33:3
}
}
重启服务
[root@s1 ~]# systemctl restart keepalived
s2节点配置
修改keepalived配置文件
[root@s2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
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_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
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
172.20.27.101 dev ens33 label ens33:1
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 37
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.11
unicast_peer {
172.20.27.10
}
virtual_ipaddress {
172.20.27.200 dev ens33 label ens33:2
172.20.27.201 dev ens33 label ens33:3
}
}
重启服务
[root@s2 ~]# systemctl restart keepalived
测试
查看s1 s2节点上绑定的虚拟IP信息
s1节点
[root@s1 ~]# ifconfig
ens33: flags=4163 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
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
RX packets 159893 bytes 13229687 (12.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 36390 bytes 8038414 (7.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# S1节点中 100,101为主
ens33:0: flags=4163 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)
ens33:1: flags=4163 mtu 1500
inet 172.20.27.101 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:c5:12:3c txqueuelen 1000 (Ethernet)
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
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
s2节点
[root@s2 ~]# ifconfig
ens33: flags=4163 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
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
RX packets 146792 bytes 12377811 (11.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5501 bytes 486871 (475.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#s2中 200,201 为主
ens33:2: flags=4163 mtu 1500
inet 172.20.27.200 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
ens33:3: flags=4163 mtu 1500
inet 172.20.27.201 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
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
测试s1停止服务
[root@s1 ~]# systemctl stop keepalived
在s2节点上查看
[root@s2 ~]# ifconfig
ens33: flags=4163 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
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
RX packets 162656 bytes 13744995 (13.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6334 bytes 551143 (538.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#s2节点上获取了所有的4个地址
ens33:0: flags=4163 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)
ens33:1: flags=4163 mtu 1500
inet 172.20.27.101 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
ens33:2: flags=4163 mtu 1500
inet 172.20.27.200 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
ens33:3: flags=4163 mtu 1500
inet 172.20.27.201 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
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