主机名称 | 服务器IP | |
server01 | 10.1.1.3 | master |
server02 | 10.1.1.4 | backup |
第一步:master和backup都进行安装keepalived
yum -y install keepalived
文件或者目录 | 作用 |
/etc/keepalived/keepalived.conf | 生效的配置文件 |
/etc/init.d/keepalived | 服务器管理脚本 |
/var/log/messages | 日志信息 |
第二步:配置keepalived
①备份主备服务器的配置文件
shell > cd /etc/keepalived
shell > cp keepalived.conf keepalived.conf_bak
②分别修改主备服务器配置文件
master
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#master默认只需要修改使用VIP即可
virtual_ipaddress {
10.1.1.6
}
}
slave
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
#修改工作模式为备
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#注意修改VIP
virtual_ipaddress {
10.1.1.6
}
}
③分别按照顺序启动主服务器和备服务器的keepalived
service keepalived start
④查看主备服务器的网卡信息
ip a s
示例配置文件
! Configuration File for keepalived
#发送邮件的配置
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#vrrp协议的配置
vrrp_instance VI_1 {
#工作模式
state MASTER
#监听的网卡
interface eth0
#虚拟路由id 需要和备服务器一致
virtual_router_id 51
#权重 优先级
priority 100
#vrrp包的发送周期 1s
advert_int 1
#权限验证
authentication {
auth_type PASS
auth_pass 1111
}
#需要绑定切换的VIP
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}
非抢占模式
①nopreempt
在主备服务器的配置文件,vrrp_instance段中
②设置state工作模式为BACKUP两个keepalived节点都启动后,默认都是BACKUP状态,双方在发送组播信息后,会根据优先级来选举一个MASTER出来。由于两者都配置了nopreempt,所以MASTER从故障中恢复后,不会抢占vip。这样会避免VIP切换可能造成的服务延迟。
单播模式
master上配置
#在vrrp_instace段中加入
#本地IP
unicast_src_ip 10.1.1.3
unicast_peer {
#对象IP 发送vrrp包给备服务器
10.1.1.4
}
slave上配置
#在vrrp_instace段中加入标签:loc,firewall,配置,keepalived,vrrp,服务器,安装,id From: https://blog.51cto.com/u_13708834/5731659
#本地IP
unicast_src_ip 10.1.1.4
unicast_peer {
#对象IP 发送vrrp包给备服务器
10.1.1.3
}