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<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 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<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)
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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<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
s2節點
[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 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<UP,BROADCAST,RUNNING,MULTICAST> 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<UP,BROADCAST,RUNNING,MULTICAST> 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<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
測試s1停止服務
[root@s1 ~]# systemctl stop keepalived
在s2節點上查看
[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 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<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)
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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<UP,BROADCAST,RUNNING,MULTICAST> 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<UP,BROADCAST,RUNNING,MULTICAST> 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<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