56341 发表于 2016-9-12 09:46:18

Keepalived + Haproxy 实现高可用负载均衡

keepalived是一个用于做双机热备(HA)的软件,常和haproxy联合起来做热备+负载均衡,达到高可用1. 实验环境Master:10.20.2.233
Backup:10.20.2.235
虚拟IP:10.20.2.238
2. Keepalived安装1) 通过yum安装软件依赖包
1
yum install -y wget make gcc openssl-devel popt-devel




2) Keepalived源码包下载安装下载地址:http://www.keepalived.org/software/keepalived-1.2.23.tar.gz

1
2
3
4
5
tar -zxf keepalived-1.2.23.tar.gz -C /usr/src
cd /sru/src/keepalived-1.2.23/
./configure --prefix=/usr/local/keepalived/
Make
make install




3) 安装成功后做成服务模式
1
2
3
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/




3. 编辑配置文件
1
2
mkdir -p /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/





1) Master配置文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
! Configuration File for keepalived

global_defs {
   router_id HA_TEST
}

#监测haproxy进程状态,每2秒执行一次
vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER # 从为BACKUP
    priority 200 # 从为100
    virtual_router_id 51 #路由ID,可通过#tcpdump vrrp查看。
    garp_master_delay 1 #主从切换时间,单位为秒。
    interface eth0
    advert_int 1
    mcast_src_ip 10.20.2.233    #MASTER服务器IP,从服务器写从服务器的IP
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      10.20.2.238    #虚拟IP
    }
    track_script {
      chk_haproxy    #监测haproxy进程状态
    }
}




2) BACKUP配置文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
! Configuration File for keepalived

global_defs {
   router_id HA_TEST
}

#监测haproxy进程状态,每2秒执行一次
vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP # 主为MASTER
    priority 100 # 主为101
    virtual_router_id 51 #路由ID,可通过#tcpdump vrrp查看。
    garp_master_delay 1 #主从切换时间,单位为秒。
    interface eth0
    advert_int 1
    mcast_src_ip 10.20.2.235    #BACKUP服务器IP,主服务器写主服务器的IP
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      10.20.2.238    #虚拟IP
    }
    track_script {
      chk_haproxy    #监测haproxy进程状态
    }
}




3) haproxy检测脚本(check_haproxy.sh)内容如下
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
/etc/init.d/haproxyd start
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
Fi

chmod 755 /etc/keepalived/check_haproxy.sh   #脚本编写完成后需要赋上权限




4) Keepalived的启停
1
2
3
service keepalived start   #停止
service keepalived stop      #启动
service keepalived restart    #重启




4. 测试:1) 启动MASTER和BACKUP上的keepalived
1
2
3
4
5
service keepalived start (keepalived启动的同时会将haproxy启动)
# service keepalived start
Starting keepalived:                                       
# service haproxyd status
HAProxy(pid 32077) is running!




MASTER绑定了VIP,BACKUP未绑定VIP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:85:46:b2 brd ff:ff:ff:ff:ff:ff
    inet 10.20.2.233/24 brd 10.20.2.255 scope global eth0
    inet 10.20.2.238/32 scope global eth0
    inet6 fe80::250:56ff:fe85:46b2/64 scope link
       valid_lft forever preferred_lft forever
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff
    inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0
    inet6 fe80::250:56ff:fe85:73ce/64 scope link
       valid_lft forever preferred_lft forever




2) 停止主上的haproxy,3秒后,keepalived会将其启动
1
2
3
4
5
# service haproxyd stop
STOP HAProxy Listen
STOP HAProxy process
# service haproxyd status
HAProxy(pid 32368) is running!




3) 停止主上的keepalived,备机马上接管服务BACKUP上绑定了VIP

1
2
3
4
5
6
7
8
9
10
11
12
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff
    inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0
    inet 10.20.2.238/32 scope global eth0
    inet6 fe80::250:56ff:fe85:73ce/64 scope link
       valid_lft forever preferred_lft forever




4) 启动主上的keepalived,MASTER绑定VIP,BACKUP释放了VIP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# service keepalived start
Starting keepalived:                                       
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff
    inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0
    inet 10.20.2.238/32 scope global eth0
    inet6 fe80::250:56ff:fe85:73ce/64 scope link
       valid_lft forever preferred_lft forever







页: [1]
查看完整版本: Keepalived + Haproxy 实现高可用负载均衡