in 未分类

高可用haproxy+keepalived(一)

首先是haproxy,在192.168.1.90和91上分别安装haproxy和设置haproxy.cfg

[root@node0190 keepalived]# cat /etc/haproxy/haproxy.cfg
global
        log /dev/log    local0
        log /dev/log    local1 notice
        user haproxy
        group haproxy
        daemon
        maxconn 100000
        spread-checks 5
        pidfile /etc/haproxy/logs/haproxy.pid
defaults
        log     global
        mode    http
        balance roundrobin
       contimeout 5000
       clitimeout 50000
       srvtimeout 50000
       option abortonclose
                 option httpclose
                 option forwardfor
frontend http-in
        bind *:10001
       option forwardfor except 127.0.0.1
       option http-server-close
       option httplog
       option dontlognull
       #option redispatch
       monitor-uri /hamon # Returns 200 if it's up
        #capture request header Host len 500
       #capture request header Content-Length len 500
        #capture request header Referer len 500
        default_backend honidmaster
        maxconn 100000

backend honidmaster
        server ms01 192.168.1.92:80  maxconn 700
        server ms02 192.168.1.93:80  maxconn 700

listen stats :1936
        mode http
       stats enable
       stats uri /
       stats realm Haproxy\ Statistics
       stats auth admin:admin

访问192.168.1.90或者91,正确的话可以轮询到92和93的web上.

现在,来设置keepalived,通过一个Vip来访问. Vip为 192.168.1.136
90作为keepalived的master,91作为backup

90设置

# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
   jack.zou@jingle.cn
   }
   notification_email_from jackzou19@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

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

91的设置就是将state MASTER 改成state BACKUP,将priority 100改成99

ip -o -f inet addr show #查看IP
ip -f inet addr delete 192.168.1.136/32 dev eth0 #删除Vip
/sbin/ifconfig tunl0 180.97.80.136 broadcast 180.97.80.136 netmask 255.255.255.0 up

/sbin/ifconfig tunl0 180.97.80.136 broadcast 180.97.80.136 netmask 255.255.255.0 down #这种down的方式是不行的,ifconfig 看不到Vip了,但是ip -o -f inet addr show还是能查看到的,需要通过ip -f inet addr delete 192.168.1.136/32 dev eth0 #删除Vip

经测试,同时开启90和91,Vip在90上面. 如果stop 90的keepalived,136的IP会漂移到91上面,期间web访问不受影响
如果90的keepalived恢复,IP会漂移回来,期间web访问不受影响

OK,达到了haproxy高可用的目的,那么还有2点要测试
1. 后端web出故障了,haproxy自动剔除
2. keepalived自动检测异常后切换keepalived的服务. 上面测试是我手动启动keepalived来测试效果的.比如90的haproxy出问题了,keepalived应该将Vip漂移到91上面,继续提供web服务.

Write a Comment

Comment