}
}
real_server 10.18.42.6 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
关于配置文件中的其它项:
HTTP_GET | SSL_GET{
url{ #根据url check,可以指定多个
path / #定义要检查的URI地址
digest <string> #检查后的摘要信息
#(可以使用genhash -s 目标服务器ip -p 端口号 status_code 200 -u uri地址得到摘要信息)
status_code 200 #检查的返回状态码
}
}
notify_master /path/xx.sh #指定当切换到master 时执行的脚本
notify_backup /path/xx.sh #指定当切换到backup 时执行的脚本
notify_fault "path/xx.sh VG_1" #故障时执行的脚本
notify /path/xx.sh
virtualhost <string> #检查的web服务器的虚拟主机
sorry_server <IPADDR> <port> #备用机的IP,所有的realserver失效后启用
notify_up <string> #检测到服务器up后执行的脚本
notify_down <string> #检测到服务器down后执行的脚本
实现MySQL的故障转移
1)、实现master与slave1两台主机的复制(AA复制)
2)、利用keepalived 的健康检查功能,检测本机的3306端口是否存活,如果端口失效,则自动执行自定义脚本
3)、自定义脚本的内容为:kill 本机的keepalived进程,并删除本机VIP;当本机keepalived进程被kill掉之后,另一台主机的keepalived进程即可获得虚拟IP,实现的故障转移
4)、测试:客户端连接keepalived提供的虚拟IP(mysql需要事先授权grant连接)
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id teacher
}
vrrp_instance lvs_inst {
state MASTER
interface ens33
virtual_router_id 51
priority 250
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.18.42.123
}
}
virtual_server 10.18.42.123 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 10.18.42.251 3306 {
weight 1
notify_down /etc/keepalived/kill.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@localhost ~]# cat /etc/keepalived/kill.sh
#!/bin/bash
pkill keepalived
#systemctl stop keepalived #尽量使用此方式关闭keepalived
ip addr del dev ens33 10.18.42.123/32
haproxy的故障转移
[root@localhost ~]# cat /etc/keepalived/keepalived.conf