有两台虚拟机分别为:
node1 :192.168.226.134
node 2:192.168.226.147
虚拟ip
1.准备:设置主机名和基于key认证
(1)设置主机名:192.168.226.134
hostnamectl set-hostname node1
vim /etc/hosts
192.168.226.147 node2
exit
(2)设置主机名:192.168.226.147
hostnamectl set-hostname node2
vim /etc/hosts
192.168.226.134 node2
exit
基于key认证
node1
ssh-keygen #生成key
cd .ssh
ssh-copy -i id_rsa.pub nod2 #将公钥复制到node2
yum install keepalived
node2
ssh-keygen
cd .ssh
ssh-copy -i id_rsa.pub nod1
yum install keepalived
2配置keepalived的配置文件
node1:
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak 备份配置
vim keepalive.conf 修改配置
Configuration File for keepalived
global_defs { #全局定义
notification_email { #收件邮箱地址
root@localhost
}
notification_email_from node2@localhost 发件邮箱地址
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1 # 路由器名称
vrrp_mcast_group4 224.100.100.100 #多播地址
}
vrrp_instance VI_1 {
state MASTER #当前节点在此虚拟路由器上的初始状态;只能有一个是MASTER,余下的都应该为BACKUP
interface eth0 #绑定为当前虚拟路由器使用的物理接口
virtual_router_id 88 #当前虚拟路由器惟一标识,范围是0-255
priority 100 #当前物理节点在此虚拟路由器中的优先级;范围1-254
advert_int 1 #:vrrp通告的时间间隔,默认1s
authentication {
auth_type PASS
auth_pass 888888
}
virtual_ipaddress {
192.168.226.100/24 #虚拟路由地址
}
}
scp keepalived.conf node2:/etc/keepalived/ 将配置复制到node2
node2:
cd /etc/keepalived/
vim keepalived.conf
修改:
router_id node2 修改名称
state BACKUP 备用
priority 90 备用的优先级比主的低
3.测试
在node1 上: tcpdump -i ens33 -nn host 224.100.100.100
在新的虚拟机ping 192.168.226.100这个共同的虚拟ip
在node1: systemctl start keepalived.service
在node2上启动服务:systemctl start keepalived.service
然后将node1关闭,可以发现node2 将顶替上去,然后将node1开启,因为其定义的工作模式为非抢占模式,所以node1重新工作
网友评论