title: 【Linux】Keepalived实现多实例高可用配置 categories:
- Linux
- Keepalived tags:
- Keepalived urlname: use-keepalived-in-two-instance
keepalived 双实例配置
实验环境
- server_1(tomcat_1+mysql_1),IP:192.168.68.221
- server_2(tomcat_2+mysql_2),IP:192.168.68.222
- tomcat_VIP:192.168.68.223
- mysql_VIP:192.168.68.224
期望效果:在正常工作时tomcat_1作为主用,mysql_2作为主用。当tomcat_1应用异常时,将对外提供tomcat服务的虚拟IP漂移至tomcat_2,实现tomcat故障转移。当mysql_2应用出现异常时,将tomcat应用连接mysql时的虚拟IP漂移至mysql_1,实现mysql的故障转移。
keepalived配置方法
- server_1 keepalived配置详解
# 在server_1中安装keepalived
# 安装相关依赖包
yum install kernel-devel openssl-devel gcc make -y
tar -xvf keepalived.tar.gz
cd keepalived-1.2.19
./configure --prefix=/ --with-kernel-dir/usr/src/kernels/2.6.32-642.el6.x86_64/
make && make install
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@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
}
# 配置mysql_check脚本
vrrp_script mysql_check_port {
script "/etc/keepalived/doublecheck/check.sh"
interval 3
weight 2
}
# 配置tomcat_check配置脚本
vrrp_script web_check_port {
script "/etc/keepalived/check/check.sh"
interval 3
weight 2
}
# 配置server_1中tomcat_1为主用,且优先级较高,并且设置VIP为192.168.68.223
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
macst_src_ip 192.168.68.221
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
web_check_port
}
virtual_ipaddress {
192.168.68.223/24
}
}
# 配置server_1中的mysql_1为备用,且优先级较低,并设置VIP为192.168.68.224
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
macst_src_ip 192.168.68.221
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
mysql_check_port
}
virtual_ipaddress {
192.168.68.224/24
}
}
- server_2keepalived配置详解
# 在server_2中安装keepalived
# 安装相关依赖包
yum install kernel-devel openssl-devel gcc make -y
tar -xvf keepalived.tar.gz
cd keepalived-1.2.19
./configure --prefix=/ --with-kernel-dir/usr/src/kernels/2.6.32-642.el6.x86_64/
make && make install
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@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
}
# 配置mysql_check脚本
vrrp_script mysql_check_port {
script "/etc/keepalived/doublecheck/check.sh"
interval 2
weight 2
}
# 配置tomcat_check配置脚本
vrrp_script web_check_port {
script "/etc/keepalived/check/check.sh"
interval 3
weight 2
}
# 配置server_2中tomcat_2为备用,且优先级较低,并且设置VIP为192.168.68.223
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
macst_src_ip 192.168.68.222
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
web_check_port
}
virtual_ipaddress {
192.168.68.223/24
}
}
# 配置server_2中的mysql_2为主用,且优先级较高,并设置VIP为192.168.68.224
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
macst_src_ip 192.168.68.222
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
mysql_check_port
}
virtual_ipaddress {
192.168.68.224/24
}
}
- 定义在vrrp_script中的相关check脚本
# 相关脚本
# mkdir /etc/keepalived/doublecheck/
# mkdir /etc/keepalived/check/
# vim /etc/keepalived/doublecheck/check.sh
#!/bin/bash
MYSQL_ALIVE=`ps -C mysqld |grep -v PID|wc -l`
if [ $MYSQL_ALIVE -eq 0 ];then
service mysql restart
sleep 3
if [ `ps -C mysqld |grep -v PID |wc -l` -eq 0 ];then
killall keepalived
fi
fi
# vim /etc/keepalived/check/check.sh
if [ $(ps aux | grep tomcat | grep -v grep | wc -l) != 1 ];then
cd /usr/local/tomcat/bin;./startup.sh >> /dev/null 2>&1
sleep 2
if [ `ps aux | grep tomcat | grep -v grep | wc -l` != 1 ];then
killall keepalived
fi
fi
# chmod +x /etc/keepalived/check/check.sh /etc/keepalived/doublecheck/check.sh
- 启动keepalived服务
# service keepalived start
- 相关调试信息
通过查看/var/log/message