keepalived 单播结构
keepalived 主机 | 角色 | IP |
---|---|---|
node103 | MASTER | 192.168.2.103 |
node104 | BACKUP | 192.168.2.104 |
安装keepalived
yum -y install keepalived haproxy
配置MASTER
~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict 单节点不能开启,需要注释
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER #角色
interface enp3s0 #网卡名称
virtual_router_id 51 # ID需要一致
priority 100 #权重,MASTER要比BACKUP大
advert_int 2 # 时隔多久检查一次
authentication {
auth_type PASS
auth_pass 1234
}
unicast_src_ip 192.168.2.103 #本机
unicast_peer {
192.168.2.104 #发送 VRRP 主机IP
}
virtual_ipaddress {
192.168.2.105 dev enp3s0 label enp3s0:1 #VIP
}
}
配置BACKUP
~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict 单节点不能开启,需要注释
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP #角色
interface enp3s0 #网卡名称
virtual_router_id 51 # ID需要一致
priority 60 #权重,BACKUP要比MASTER小
advert_int 2 # 时隔多久检查一次
authentication {
auth_type PASS
auth_pass 1234
}
unicast_src_ip 192.168.2.104 #本机IP
unicast_peer {
192.168.2.103 #发送 VRRP 主机IP
}
virtual_ipaddress {
192.168.2.105 dev enp3s0 label enp3s0:1 #VIP
}
}
启动服务
systemctl start keepalived
systemctl enable keepalived
systemctl status keepalived
验证VIP是否成功漂移
所有节点都安装httpd,如果有WEB服务了也可自己测试
yum -y install httpd
systemctl start httpd
在各服务器分别在/var/www/html/路径下编写两个txt文件
MASTER
~]# cd /var/www/html/
echo 192.168.2.103 > 1.txt
BACKUP
~]# cd /var/www/html/
echo 192.168.2.104 > 1.txt
可以停止莫一台进行测试访问
]# curl 192.168.2.105/1.txt
192.168.2.103
查看VIP在那个服务器上,访问结果与之对应
~]# ifconfig enp3s0:1
enp3s0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.105 netmask 255.255.255.255 broadcast 0.0.0.0
ether 70:4d:7b:b5:93:ae txqueuelen 1000 (Ethernet)