haproxy高可用以及双主模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了haproxy高可用以及双主模式相关的知识,希望对你有一定的参考价值。

haproxy主备

虚拟VIP:192.168.33.130 
把haproxy-01的配置文件拷贝到haproxy-02机器相应的目录即可

[email protected] haproxy]# scp haproxy.cfg [email protected]:/etc/haproxy/
haproxy.cfg                                     100% 3520     3.4KB/s   00:00

启动haproxy-02服务

[[email protected] haproxy]# /etc/init.d/haproxy restart
Stopping haproxy:                                          [FAILED]
Starting haproxy:                                          [  OK  ]

关于haproxy-02日志记录和haproxy-01记录方式一样

客户端访问haproxy-02的ip地址 
技术分享 技术分享技术分享

安装keepalived

在haproxy机器上分别安装keepalived

[[email protected] ~]# yum install keepalived -y
[[email protected] ~]# yum install keepalived -y

haproxy-01机器上keepalived配置文件内容如下

[[email protected] ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
    [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_MASTER
}
 
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.33.130
    }
}

haproxy-02机器上keepalived配置文件内容如下

[[email protected] ~]# cat /etc/keepalived/keepalived.conf 
global_defs {
  notification_email {
   [email protected]
  }
  notification_email_from [email protected]
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_BACKUP
}

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.33.130
   }
}


分别在haproxy-01/02机器启动keepalived服务

[[email protected] ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

[[email protected] keepalived]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]


haproxy-01情况

[[email protected] ~]# ip a|grep 130
    inet 192.168.33.130/32 scope global eth0

haproxy-02情况

[[email protected] ~]# ip a|grep 130

通过以上查看情况得知虚拟VIP在haproxy-01机器上

客户端访问虚拟VIP情况

技术分享 技术分享技术分享 
通过访问得知,虚拟vip访问正常。轮询访问

模拟故障把haproxy-01机上的haproxy和keepalived服务停止

[[email protected] ~]# /etc/init.d/haproxy stop
Stopping haproxy:                                          [  OK  ]
 
[[email protected] ~]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK

在haproxy-02机器上查看虚拟VIP情况

[[email protected] ~]# ip a|grep 130
    inet 192.168.33.130/32 scope global eth0

虚拟VIP已经漂移到haproxy-02机器上了

客户端访问虚拟VIP情况 
技术分享 技术分享技术分享

haproxy-01机器查看keepalived剔除过程日志

技术分享

haproxy-02机器查看keepalived切换获取VIP过程日志

技术分享

以上就是haproxy+keepalived主备模式

haproxy+keepalived双主模式

增加虚拟VIP:192.168.33.150

haproxy-01机器虚拟VIP:192.168.33.130
haproxy-02机器虚拟VIP:192.168.33.150

haproxy-01机器上keepalived完整配置文件如下

[[email protected] ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
    [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_MASTER
}
 
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.33.130
    }
}
 
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.33.150
 }
}

haproxy-02器上keepalived完整配置文件如下

[[email protected] ~]# cat /etc/keepalived/keepalived.conf 
global_defs {
   notification_email {
    [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_BACKUP
}
 
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.33.130
    }
}
 
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.33.150
    }
}

分别启动haproxy-01/02机器的haproxy和keepalived服务即可 
haproxy-01机器情况如下

[[email protected] ~]# /etc/init.d/haproxy start
Starting haproxy:                                          [  OK  ]
 
[[email protected] ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

haproxy-02机器情况如下

[[email protected] ~]# /etc/init.d/haproxy restart
Stopping haproxy:                                          [  OK  ]
Starting haproxy:                                          [  OK  ]
 
[[email protected] ~]# /etc/init.d/keepalived restart
Stopping keepalived:                                       [  OK  ]
Starting keepalived:                                       [  OK  ]

查看haproxy-01机器虚拟VIP情况

[[email protected] ~]# ip a|grep 130
    inet 192.168.33.130/32 scope global eth0

查看haproxy-02机器虚拟VIP情况

[[email protected] ~]# ip a|grep 33.150
    inet 192.168.33.150/32 scope global eth0

客户端访问虚拟VIP情况如下 
虚拟VIP:130访问情况 
技术分享 
技术分享技术分享

虚拟VIP:150访问情况 
技术分享 技术分享技术分享

模拟故障,吧haproxy-01机器haproxy和keepalived服务down掉

[[email protected] ~]# /etc/init.d/haproxy stop
Stopping haproxy:                                          [  OK  ]
 
[[email protected] ~]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]

查看haproxy-01机器虚拟VIP情况

[[email protected] ~]# ip a|grep 130

查看haproxy-01机器日志情况

技术分享

从上面日志中看出虚拟VIP:192.168.33.130已经剔除了

查看haproxy-02机器虚拟VIP情况 
技术分享 技术分享虚拟VIP已经漂移在haproxy-02机器上

查看haproxy-02机器上的日志 
技术分享技术分享

客户端浏览器访问情况如下 
虚拟VIP130 
技术分享 
技术分享技术分享

虚拟VIP150 
技术分享 技术分享技术分享

以上就haproxy+keepalived双主模式,先这样吧


本文出自 “村里的男孩” 博客,请务必保留此出处http://noodle.blog.51cto.com/2925423/1795448

以上是关于haproxy高可用以及双主模式的主要内容,如果未能解决你的问题,请参考以下文章

Mysq+Haproxy+Keepalived高可用

Haproxy+Keepalived实现网站双主高可用-理论篇

Heartbeat + Haproxy + MySQL双主复制 实现读写负载均衡及高可用

案例二(构建双主高可用HAProxy负载均衡系统)

Linux运维:构建双主高可用的HAProxy负载均衡系统

[转] HaproxyKeepalived双主高可用负载均衡