Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2803097
  • 博文数量: 389
  • 博客积分: 4177
  • 博客等级: 上校
  • 技术积分: 4773
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-16 23:29
文章分类

全部博文(389)

分类: Mysql/postgreSQL

2014-11-13 15:25:15

                              使用KeepAlived实现HAProxy高可用

     在以前的文章中我们实现了使用HAProxy来转发读请求到后端多台mysql从库上,但是对于HAProxy的高可用(HA)需
求没有配置,本文在以前的基础再增加一个HAProxy的备份机器.这样当主服务器的HAProxy挂掉后,备份机器能够迅
速的进行自动接管.我们通过KeepAlived来实现这样的需求

主HAProxy: 192.168.10.192
备HAProxy: 192.168.10.85
VIP:192.168.10.158


整个项目的拓扑结构如下.
           

            vip 
             |
      --主haproxy--  <--> --备haproxy--
          |     \
          |      \
      --mysql--  --mysql--

 

--在主,备HAProxy上安装keepalived软件
[root@cscscslocalhost 1109]# tar -xvf keepalived-1.2.1.tar.gz

[root@cscscslocalhost keepalived-1.2.1]# ./configure --prefix=/usr/local/keepalived
checking for gcc... gcc

[root@cscscslocalhost keepalived-1.2.1]# make

[root@cscscslocalhost keepalived-1.2.1]# make install

--在主,备HAProxy上把KeepAlived的安装目录下的sbin目录加到.bash_profile文件中,然后source生效

PATH=$PATH:$HOME/bin:/usr/local/haproxy/sbin:/sbin:/bin:/usr/local/keepalived/sbin


--在主,备HAProxy上查看keepalived,确保成功安装.
[root@cscscslocalhost sbin]# keepalived -v
Keepalived v1.2.1 (11/12,2014)


--在主,备HAProxy上修改KeepAlived的配置文件


主HAProxy的配置

[root@cscscslocalhost keepalived]# vi /usr/local/keepalived/etc/keepalived/keepalived.conf

global_defs {
   notification_email {
    
    
    
   }
   #notification_email_from
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_haproxy
  {
    script "/usr/local/keepalived/chk_haproxy.sh"
    interval 1
    weight 2
   }

 vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    mcast_src_ip 192.168.10.192

 authentication {
        auth_type PASS
        auth_pass 111111
    }

    track_script {
        chk_haproxy
    }

    virtual_ipaddress {
        192.168.10.158
    }
}


备用HAProxy服务器的配置

global_defs {
   notification_email {
    
    
    
   }
   #notification_email_from
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_haproxy
  {
    script "/usr/local/keepalived/chk_haproxy.sh"
    interval 1
    weight 2
   }


 vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    mcast_src_ip 192.168.10.85

  authentication {
        auth_type PASS
        auth_pass 111111
    }


    track_script {
        chk_haproxy
    }

 
    virtual_ipaddress {
        192.168.10.158
    }
}

--在主,备HAProxy上编写脚本文件,这个脚本的目的是检查HAProxy存活性,如果HAProxy
死掉了,那么KeepAlived也会被杀死,这样保证备HAProxy能确正常的接管

[root@cscscslocalhost ~]# vi /usr/local/keepalived/chk_haproxy.sh

#!/bin/bash

status=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)

   if [ ${status} = "0" ]; then
            /bin/kill -9 `/sbin/pidof keepalived`
    fi


--在主,备HAProxy上修改脚本执行权限

[root@cscscslocalhost ~]# chmod 755 /usr/local/keepalived/chk_haproxy.sh


--在主HAProxy上启动KeepAlived,通过/var/log/messages,可以看出已经功启动,并进入master状态

[root@cscscslocalhost keepalived]# keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf

[root@cscscslocalhost ~]# tail -f /var/log/messages

Nov 12 14:28:15 cscscslocalhost Keepalived: Starting Keepalived v1.2.1 (11/12,2014)
Nov 12 14:28:15 cscscslocalhost Keepalived_vrrp: Registering Kernel netlink reflector
Nov 12 14:28:15 cscscslocalhost Keepalived_vrrp: Registering Kernel netlink command channel
Nov 12 14:28:15 cscscslocalhost Keepalived_vrrp: Registering gratutious ARP shared channel
Nov 12 14:28:15 cscscslocalhost Keepalived_vrrp: Opening file '/usr/local/keepalived/etc/keepalived/keepalived.conf'.
Nov 12 14:28:15 cscscslocalhost Keepalived_vrrp: Configuration is using : 64914 Bytes
Nov 12 14:28:15 cscscslocalhost Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Nov 12 14:28:15 cscscslocalhost Keepalived: Starting VRRP child process, pid=17731
Nov 12 14:28:15 cscscslocalhost Keepalived_vrrp: VRRP_Script(chk_haproxy) succeeded
Nov 12 14:28:16 cscscslocalhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Nov 12 14:28:17 cscscslocalhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE

[root@cscscslocalhost keepalived]# ip a
1: lo: mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 1c:6f:65:d9:4a:6f brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.192/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.158/32 scope global eth0


--测试通过VIP来连接mysql

[root@localhost ~]# mysql -u root -p123456 --host=192.168.10.158 --port=23306
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 50484
Server version: 5.6.14-rel62.0 Percona Server with XtraDB (GPL), Release rel62.0, Revision 483

Copyright (c) 2009-2013 Percona LLC and/or its affiliates
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| login_sso         |
| zabbix             |
+--------------------+
6 rows in set (0.00 sec)


连接成功.

手动在主HAProxy上杀死haproxy,模拟haproxy故障,这样备份HAProxy会进行接管.


[root@cscscslocalhost keepalived]# ps  -eaf | grep 4852
nobody    4852     1  0 15:13 ?        00:00:00 haproxy -f /etc/haproxy.cfg
root      5019 12171  0 15:13 pts/0    00:00:00 grep 4852
[root@cscscslocalhost keepalived]# kill -9 4852


--备份HAProxy的/var/log/messages日志,可以看出备份HAProxy已经接管了VIP

Nov 12 14:27:56 db135 Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Nov 12 14:27:56 db135 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Nov 12 14:28:41 db135 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Nov 12 14:28:42 db135 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Nov 12 14:28:48 localhost.localdomain haproxy[12523]: Connect from 192.168.10.145:54535 to 192.168.10.158:23306 (mysql_proxy/TCP)

--重新show databases,发现已经重连了,整个过程在2-3秒的样子.
mysql> show databases;
No connection. Trying to reconnect...
Connection id:    50815
Current database: *** NONE ***

+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| login_sso         |
| zabbix             |
+--------------------+
6 rows in set (0.00 sec)

 

 

 

 

阅读(4477) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~