欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

haproxy+keepalived实现高可用负载均衡(实例配置)

程序员文章站 2022-06-19 20:10:30
作者:split_two 环境四台机器都是redhat4.8(64位)版本: ip地址       ...

作者: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地址?
总结:我这测试都没问题,如果有问题请给我留言!原文: