发布于 2016-02-11 23:06:34 | 419 次阅读 | 评论: 0 | 来源: 网友投递
Keepalived 服务器状态监测
keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
上一篇文章以理论介绍居多,这篇文章就以实际的应用配置为主,需要的朋友可以参考下这两篇文章
作者:split_two
环境四台机器都是RedHat4.8(64位)版本:
IP地址 用处
192.168.5.55 MASTER
192.168.2.73 BACKUP
192.168.5.54 负载A
192.168.5.57 负载B
192.168.2.100 VIP
1、MASTER上安装haproxy
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz /root
解压到当前目录
tar zxvf haproxy-1.3.20.tar.gz
进到此目录
cd haproxy-1.3.20
编译及安装,安装到/usr/local目录,但是安装完此目录下没有haproxy文件夹,我也很奇怪!
make TARGET=linux26 prefix=/usr/local/haproxy install
但是出来的结果会提示如下信息
install -d /usr/local/sbin
install haproxy /usr/local/sbin
install -d /usr/local/share/man/man1
install -m 644 doc/haproxy.1 /usr/local/share/man/man1
install -d /usr/local/doc/haproxy
for x in configuration architecture haproxy-en haproxy-fr; do \
install -m 644 doc/$x.txt /usr/local/doc/haproxy ; \
done
提示你启动haproxy在sbin目录,其余的在doc目录
进入到haproxy目录
cd /usr/local/doc/haproxy
新建一个haproxy主配置文件
vi haproxy.cfg
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/doc/haproxy
uid 501
gid 501
daemon
nbproc 1
pidfile /usr/local/doc/haproxy/haproxy.pid
defaults
log 127.0.0.1 local3
mode http
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2
maxconn 2000
balance roundrobin
stats uri /haproxy-status 查看状态
stats hide-version 隐藏haproxy版本
stats realm Gemini\ Haproxy 查看状态需要口令
stats auth admin:admin 用户名和密码
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web_proxy 192.168.2.100:80
server web1 192.168.5.54:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
server web2 192.168.5.57:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
2、MASTER安装keepalived
tar zxvf keepalived-1.1.15.tar.gz
vi /usr/src/kernels/2.6.9-89.EL-smp-x86_64/include/linux/types.h
将如下两行注释掉,否则编译会出错,跟我这个版本的系统有关系,你的也许不要!
/*
typedef __u16 __bitwise __sum16;
typedef __u32 __bitwise __wsum;
*/
cd keepalived-1.1.15
./configure
make
make install
将keepalived作为系统服务启动
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived/
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
vi /etc/keepalived/keepalived.conf内容如下:
! Configuration File for keepalived
global_defs {
router_id LVA_DEVEL
}
vrrp_script chk_http_port {
script "/root/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.2.100
}
}
上面调用了一个脚本check_haproxy.sh,内容如下:
#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`
if [ $A -eq 0 ];then
/usr/local/sbin/haproxy -f /usr/local/doc/haproxy/haproxy.cfg
echo "haproxy start"
sleep 3
if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
echo "keepalived stop"
fi
fi
3、BACKUP上安装haproxy,步骤就不详细介绍,和MASTER上面一样的。
haproxy.conf内容如下
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/doc/haproxy
uid 501
gid 501
daemon
nbproc 1
pidfile /usr/local/doc/haproxy/haproxy.pid
defaults
log 127.0.0.1 local3
mode http
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2
maxconn 2000
balance roundrobin
stats uri /haproxy-status
stats hide-version
stats realm Gemini\ Haproxy
stats auth admin:admin
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web_proxy 192.168.2.100:80
server web1 192.168.5.54:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
server web2 192.168.5.57:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
4、BACKUP上安装keepalived,步骤也不多介绍,keepalived.conf文件内容就两处有变化,红色字体标出
! Configuration File for keepalived
global_defs {
router_id LVA_DEVEL
}
vrrp_script chk_http_port {
script "/root/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.2.100
}
}
调用脚本check_haproxy.sh内容:
#!/bin/bash
A=`ip a | grep 192.168.2.100 | wc -l`
B=`ps -ef | grep haproxy | grep -v grep | awk '{print $2}'`
if [ $A -gt 0 ];then
/usr/local/sbin/haproxy -f /usr/local/doc/haproxy/haproxy.cfg
else
kill -9 $B
fi
5、两台负载机器我就不多介绍了,用的是系统自带的apache
登录192.168.5.54上操作:
echo 'this is 192.168.5.54!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start
登录192.168.5.57上操作:
echo 'Hello,This is 192.168.5.57!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start
6、测试步骤
启动MASTER上的keepalived服务,再启动BACKUP上的keepalived服务。
确定MASTER上是否有192.168.2.100地址,用ip a查看即可!
然后手动杀掉MASTER上的haproxy进程,看看是否能马上恢复进程?
停止MASTER上的keepalived服务,确认BACKUP是否接管VIP地址?
最后再启动MASTER上的keepalived服务,再确认MASTER是否再接管VIP地址?
总结:我这测试都没问题,如果有问题请给我留言!
原文:http://blog.chinaunix.net/uid-23916356-id-3387261.html