~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email { # 指定 keepalived 在发生事件,比如切换 IP 的时候需要发生邮件的地址,可以有多个,一行一个即可
acassen
}
notification_email_from Alexandre.Cassen@firewall.loc # 指定 smtp 服务器设置
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL # 运行 keepalived 的标示,可以写主机名等信息
vrrp_iptables # 不生成 iptables 规则
}
vrrp_instance IP1 { # 监听一组 VIP
state BACKUP # 指定初始状态为 BACKUP 即备份状态
interface br0 # 监听的本地网卡名称,此处是一块桥接网卡
virtual_router_id 10 # 路由 id,多个网卡环境下,id 一定不能一样,范围为 1-255
priority 90 # 优先级,高的为 MASTER,范围为 1-255
advert_int 1 # 进行通告
# unicast_src_ip 192.168.10.128 # 定义单播源地址,实现定向传递心跳信息,以减少网络中的广播,优化网络环境
# unicast_peer {
# 192.168.10.129 # 目标地址,可定义多个
# }
authentication { # 配置认证机制
auth_type PASS # 认证类型为密码
auth_pass 5555 # 认证密码
}
virtual_ipaddress {
192.168.10.100
}
}
vrrp_instance IP2 {
state MASTER # 第二组 IP 初始状态是 MASTER
interface br0
virtual_router_id 20
priority 100
advert_int 1
# unicast_src_ip 192.168.10.128
# unicast_peer {
# 192.168.10.129
# }
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.101 # 具体的虚拟 IP 地址,两个 keepalived 设置要一样,否则服务不生效
}
}
~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_iptables
}
vrrp_instance IP1 {
state MASTER # Server 1 的 MASTER
interface eth1
virtual_router_id 10
priority 150
advert_int 1
# unicast_src_ip 192.168.10.129
# unicast_peer {
# 192.168.10.128
# }
authentication {
auth_type PASS
auth_pass 5555
}
virtual_ipaddress {
192.168.10.100
}
}
vrrp_instance IP2 {
state BACKUP # 是 Server 1 的 BACKUP
interface eth1
virtual_router_id 20
priority 50
advert_int 1
# unicast_src_ip 192.168.10.129
# unicast_peer {
# 192.168.10.128
# }
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.101
}
}
在 Server 1关闭 keepalived
~]# /etc/init.d/keepalived stop
Stopping keepalived (via systemctl): [ OK ]
在 Server 2 查看日志
image.png
验证 Server 2 的 IP 地址
image.png
开启 Server 1 的 keepalived
~]# /etc/init.d/keepalived start
Starting keepalived (via systemctl): [ OK ]
验证 Server 2 的日志
image.png
验证 Server 2 的 IP 地址
image.png
验证 Server 1 的 IP 地址
image.png
网友评论