一、安装软件
yum list keepalived*
yum install keepalived.x86_64
二、配置keepalived.
修改“/etc/keepalived/keepalived.conf”目录下主、备两个节点配置文件(两台服务器)。
sudo vi /etc/keepalived/keepalived.conf
(一)、同一网段、keepalived服务中断后切换
主节点参考如下进行配置:
[root@ha80 ~]# cat /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_2 {
state MASTER
interface team0
virtual_router_id 83
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.10.53
}
}
备节点参考如下进行配置:
[root@ha81 ~]# cat /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_2 {
state BACKUP
interface team0
virtual_router_id 83
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.10.53
}
}
/
(二)、同一网段、条件切换
主节点参考如下进行配置:cat /etc/keepalived/keepalived.conf
global_defs {
router_id master01
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mysql236
}
virtual_ipaddress {
10.10.10.18
}
}
virtual_server 10.10.10.18 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.10.10.16 3306 {
weight 1
notify_down /etc/keepalived/bin/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
备节点参考如下进行配置:cat /etc/keepalived/keepalived.conf
global_defs {
router_id master02
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
nopreempt
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass mysql236
}
virtual_ipaddress {
10.10.10.18
}
}
virtual_server 10.10.10.18 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.10.10.17 3306 {
weight 1
notify_down /etc/keepalived/bin/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
(三)、不同网段、条件切换
主节点参考如下进行配置:cat /etc/keepalived/keepalived.conf
global_defs {
router_id master01
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mysql236
}
virtual_ipaddress {
10.10.10.18
}
}
virtual_server 10.10.10.18 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.10.103.16 3306 {
weight 1
notify_down /etc/keepalived/bin/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
备节点参考如下进行配置:cat /etc/keepalived/keepalived.conf
global_defs {
router_id master02
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
nopreempt
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass mysql236
}
virtual_ipaddress {
10.10.10.18
}
}
virtual_server 10.10.10.18 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.10.103.17 3306 {
weight 1
notify_down /etc/keepalived/bin/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
1、停止开机自启动:sudo systemctl disable keepalived.service
2、查看服务当前状态:sudo systemctl status keepalived.service
3、重新启动某服务:sudo systemctl restart keepalived.service
4、停止某服务:sudo systemctl stop keepalived.service
5、执行ip a命令,可查看虚拟IP信息。
6、防火墙影响导致不能访问
完全关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service