服务器:CentOS-7.x
ip:192.168.21.100、192.168.21.101、192.168.21.128
vip(虚拟ip):192.168.21.102
实现目的:
1、三台服务器运行正常的情况下,vip(虚拟ip):192.168.21.102绑定在192.168.21.100上
2、192.168.21.100宕机,vip(虚拟ip):192.168.21.102随机飘逸到192.168.21.101或者192.168.21.128其中的一台上面
3、192.168.21.100、192.168.21.101宕机,vip(虚拟ip):192.168.21.102会飘逸到192.168.21.128上面
4、192.168.21.100、192.168.21.128宕机,vip(虚拟ip):192.168.21.102会飘逸到192.168.21.101上面
5、192.168.21.100恢复,vip(虚拟ip):192.168.21.102又会飘逸到192.168.21.100上面
1、下载安装包
https://www.keepalived.org/software/keepalived-2.2.8.tar.gz
上传keepalived-2.2.8.tar.gz到/data/soft目录
2、安装依赖包
sudo yum install openssl-devel popt-devel libnl3-devel libnfnetlink-devel kernel-devel gcc
3、编译安装keepalived
sudo mkdir -p /data/server/keepalived #创建安装目录
cd /data/soft
sudo tar zxvf keepalived-2.2.8.tar.gz
cd keepalived-2.2.8
sudo ./configure --prefix=/data/server/keepalived #配置,必须看到以下提示,说明配置正确,才能继续安装
Use IPVS Framework : Yes
Use VRRP Framework : Yes
sudo make #编译
sudo make install #安装
sudo /data/server/keepalived/sbin/keepalived -v #查看版本
4、配置keepalived
拷贝配置文件
sudo cp /data/server/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
sudo mkdir /etc/keepalived
sudo cp /data/server/keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/keepalived.conf
sudo cp /data/server/keepalived/sbin/keepalived /usr/sbin/
sudo mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
4.1主节点192.168.21.100配置文件
sudo vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
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 ens33 #绑定vip地址的主节点网卡名称
virtual_router_id 51 #虚拟路由的ID,3个节点要一致
priority 101 #优先级高,数字越大优先级越高,取值范围:0-254
advert_int 1
authentication {
auth_type PASS #VRRP验证类型:PASS、AH两种
auth_pass 1111 #VRRP验证密码,在同一个vrrp_instance下,主、从必须使用相同的密码才能正常通信
}
virtual_ipaddress {
192.168.21.102/24 #vip地址
}
unicast_src_ip 192.168.21.100 #本地(主节点)网卡ens33的IP地址
unicast_peer {
192.168.21.101 #从节点ip地址
192.168.21.128 #从节点ip地址
}
}
:wq! #保存退出
4.2从节点192.168.21.101配置文件
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
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 ens33 #绑定vip地址的主节点网卡名称
virtual_router_id 51 #虚拟路由的ID,3个节点要一致
priority 100 #值要小于主节点,取值范围:0-254
advert_int 1
authentication {
auth_type PASS #VRRP验证类型:PASS、AH两种
auth_pass 1111 #VRRP验证密码,在同一个vrrp_instance下,主、从必须使用相同的密码才能正常通信
}
virtual_ipaddress {
192.168.21.102/24 #vip地址
}
unicast_src_ip 192.168.21.101 #本地(从节点)网卡ens33的IP地址
unicast_peer {
192.168.21.100 #主节点ip地址
}
}
:wq! #保存退出
4.3从节点192.168.21.128配置文件
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
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 ens33 #绑定vip地址的主节点网卡名称
virtual_router_id 51 #虚拟路由的ID,3个节点要一致
priority 99 #值要小于主节点,取值范围:0-254
advert_int 1
authentication {
auth_type PASS #VRRP验证类型:PASS、AH两种
auth_pass 1111 #VRRP验证密码,在同一个vrrp_instance下,主、从必须使用相同的密码才能正常通信
}
virtual_ipaddress {
192.168.21.102/24 #vip地址
}
unicast_src_ip 192.168.21.128 #本地(从节点)网卡ens33的IP地址
unicast_peer {
192.168.21.100 #主节点ip地址
}
}
:wq! #保存退出
5、修改启动文件
sudo mv /usr/lib/systemd/system/keepalived.service /usr/lib/systemd/system/keepalived.service.bak
sudo vi /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
:wq! #保存退出
sudo systemctl daemon-reload #重新加载
sudo systemctl enable keepalived.service #设置开机自动启动
sudo systemctl disable keepalived.service #取消开机自动启动
sudo systemctl start keepalived.service #启动
sudo systemctl stop keepalived.service #停止
sudo systemctl status keepalived.service #查看状态
6、验证vip
tail -f /var/log/messages #查看日志
6.1当主节点上的keepalived服务停止时,VIP会自动绑定到从节点服务器
6.2主节点的keepalived服务恢复后,VIP又会自动绑定到主节点服务器
至此,Keepalived配置vip实现故障转移完成。