haproxy主备

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

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

启动haproxy-02服务

[root@haproxy-02 haproxy]# /etc/init.d/haproxy restart
Stopping haproxy:                                          [FAILED]
Starting haproxy:                                          [  OK  ]

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

客户端访问haproxy-02的ip地址 
 haproxy高可用以及双主模式(二)_第1张图片

安装keepalived

在haproxy机器上分别安装keepalived

[root@haproxy-01 ~]# yum install keepalived -y
[root@haproxy-02 ~]# yum install keepalived -y

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

[root@haproxy-01 ~]# 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配置文件内容如下

[root@haproxy-02 ~]# 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服务

[root@haproxy-01 ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

[root@haproxy-02 keepalived]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]


haproxy-01情况

[root@haproxy-01 ~]# ip a|grep 130
    inet 192.168.33.130/32 scope global eth0

haproxy-02情况

[root@haproxy-02 ~]# ip a|grep 130

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

客户端访问虚拟VIP情况

 haproxy高可用以及双主模式(二)_第2张图片 
通过访问得知,虚拟vip访问正常。轮询访问

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

[root@haproxy-01 ~]# /etc/init.d/haproxy stop
Stopping haproxy:                                          [  OK  ]
 
[root@haproxy-01 ~]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK

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

[root@haproxy-02 ~]# ip a|grep 130
    inet 192.168.33.130/32 scope global eth0

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

客户端访问虚拟VIP情况 
 haproxy高可用以及双主模式(二)_第3张图片

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

haproxy高可用以及双主模式(二)_第4张图片

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

haproxy高可用以及双主模式(二)_第5张图片

以上就是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完整配置文件如下

[root@haproxy-01 ~]# 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完整配置文件如下

[root@haproxy-02 ~]# 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机器情况如下

[root@haproxy-01 ~]# /etc/init.d/haproxy start
Starting haproxy:                                          [  OK  ]
 
[root@haproxy-01 ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]

haproxy-02机器情况如下

[root@haproxy-02 ~]# /etc/init.d/haproxy restart
Stopping haproxy:                                          [  OK  ]
Starting haproxy:                                          [  OK  ]
 
[root@haproxy-02 ~]# /etc/init.d/keepalived restart
Stopping keepalived:                                       [  OK  ]
Starting keepalived:                                       [  OK  ]

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

[root@haproxy-01 ~]# ip a|grep 130
    inet 192.168.33.130/32 scope global eth0

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

[root@haproxy-02 ~]# ip a|grep 33.150
    inet 192.168.33.150/32 scope global eth0

客户端访问虚拟VIP情况如下 
虚拟VIP:130访问情况 
 
haproxy高可用以及双主模式(二)_第6张图片

虚拟VIP:150访问情况 
 haproxy高可用以及双主模式(二)_第7张图片

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

[root@haproxy-01 ~]# /etc/init.d/haproxy stop
Stopping haproxy:                                          [  OK  ]
 
[root@haproxy-01 ~]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]

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

[root@haproxy-01 ~]# ip a|grep 130

查看haproxy-01机器日志情况

haproxy高可用以及双主模式(二)_第8张图片

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

查看haproxy-02机器虚拟VIP情况 
 haproxy高可用以及双主模式(二)_第9张图片虚拟VIP已经漂移在haproxy-02机器上

查看haproxy-02机器上的日志 
wKiom1d5OMzy5LUqAAC6aZ4cuWA674.jpg-wh_50

客户端浏览器访问情况如下 
虚拟VIP130 
 
haproxy高可用以及双主模式(二)_第10张图片

虚拟VIP150 
 haproxy高可用以及双主模式(二)_第11张图片

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