利用keepalived可以实现对linux服务器的高可用性,即主从备份,一台线上服务器出故障另外一台服务器顶替它,当出故障的服务器恢复时又自动转换成主服务器,顶替它的服务器再次转换为备胎,当主服务器宕机、主服务器网卡坏掉、甚至主服务器服务挂掉(要用脚本监控实现转换)都可以自动切换到从服务器,下面针对这三种故障进行模拟。
实验环境:2台rhel5.6,keepalived-1.1.20.tar.gz
实验过程:
本实验以web服务为例,两台linux都安装Apache服务。
配置yum源,做准备工作,因为本次模拟的是主从备份,所以除了keepalived配置文件有区别外其他配置均相同。在实验前,将selinux和防火墙都关掉。
主服务器配置:
[root@web1_master ~]# yum install gcc gcc-c++ openssl-devel httpd kernel-devel -y
[root@web1_master ~]# echo "master" > /var/www/html/index.html
[root@web1_master ~]# tar zxf keepalived-1.1.20.tar.gz
[root@web1_master ~]# cd keepalived-1.1.20/
#./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.18-238.el5-i686/
#make &&make install
#cp /usr/local/sbin/keepalived /usr/sbin/
[root@web1_master ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
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
}
virtual_ipaddress {
192.168.65.65
}
}
####将keepalived.conf配置文件修改成以上配置,即修改虚拟地址为192.168.65.65,这个虚拟地址是虚拟不存在的,用户在访问网页的时候访问的就是这个虚拟地址,本配置文件要注意修改服务器的主从状态、优先级、组id以及虚拟ip,还可以修改Email地址通知自己等。
# service keepalived start
# service httpd start
# chkconfig --add httpd
# chkconfig --add keepalived
# chkconfig httpd on
# chkconfig keepalived on
从服务器配置:
[root@web1_slave keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
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 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.65
}
}
#####从服务器keepalived.conf的主配置文件,优先级改为比主的优先级(100)小,这里是90.
# echo "slave" > /var/www/html/index.html
######其他配置内容和主服务器相同
当主服务器宕机或是网卡出问题,从服务器都会取代它变成主服务器
上图是当主服务器网卡重启时,在从服务器日志记录上显示从服务器先是切换成主服务器,后又切换成从服务器。
当web站点挂了时,还想让从服务器自动切换成主服务器就需要脚本来执行。
下面新建一个脚本,名叫check_httpd.sh ,其位置都放在主从服务器的/etc/keepalived/下面,(主从服务器使用相同的脚本,脚本放的位置也都相同,在keepalived.conf修改的地方、内容也都相同)
下面是check_httpd.sh 脚本的内容
[root@web1_master keepalived]# cat check_httpd.sh
#!/bin/bash
CHECK_TIME=2
check()
{
curl http://127.0.0.1/ >/dev/null 2>&1
ret=$?
if [ $ret -ne 0 ];then
return $ret;
fi
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check
HTTP_OK=$?
if [ $HTTP_OK -eq 0 ];then
exit 0
fi
if [ $HTTP_OK -ne 0 ] && [ $CHECK_TIME -eq 0 ]
then
exit 1
fi
done
修改主服务器keepalived.conf配置文件,结果如下:
[root@web1_master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_http {
script "/etc/keepalived/check_httpd.sh"
weight -5
interval 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
track_script {
check_http
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.65
}
}
修改从服务器keepalived.conf配置文件,结果如下:
[root@web1_slave keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_http {
script "/etc/keepalived/check_httpd.sh"
weight -5
interval 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
track_script {
check_http
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.65
}
}
分别重启两台服务器的keepalived服务
#service keepalivedrestart
到这里,如果主服务器的web程序出现故障,则从服务器会在1秒钟后自动切换成主服务器,当主服务器恢复时,从服务器会自动切换成备胎,主服务器依然是主服务器。
至此,linux高可用性(HA_keepalived)配置完成。
本文出自 “个人感受” 博客,谢绝转载!