Keepalived, HAProxy and varnish

From Skytech
Jump to navigation Jump to search

Setting up Keepalived, HAProxy and Varnish

Assuming debian install, but packages should exists for popular distroes as well

Install packages

aptitude install keepalived varnish haproxy


Prepare system configs

edit

/etc/sysctl.conf

Add:

net.ipv4.ip_forward=1
net.ipv4.ip_nonlocal_bind=1

Setup keepalived

MASTER: Edit

/etc/keepalived/keepalived.conf

vrrp_script chk_haproxy {           # Requires keepalived-1.1.13
        script "killall -0 haproxy"     # cheaper than pidof
        interval 2                      # check every 2 seconds
        weight 2                        # add 2 points of prio if OK
}

vrrp_script chk_nginx {           # Requires keepalived-1.1.13
        script "killall -0 nginx"     # cheaper than pidof
        interval 2                      # check every 2 seconds
        weight 2                        # add 2 points of prio if OK
}

vrrp_instance VI_1 {
        interface eth0
        state MASTER
        virtual_router_id 152
        priority 101                    # 101 on master, 100 on backup
        virtual_ipaddress {
            172.30.1.248
        }
        track_script {
            chk_haproxy
        }
}

vrrp_instance VI_2 {
        interface eth0
        state MASTER
        virtual_router_id 162
        priority 101                    # 101 on master, 100 on backup
        virtual_ipaddress {
            172.30.1.247
        }
        track_script {
            chk_nginx
        }
}


SLAVE:

vrrp_script chk_haproxy {           # Requires keepalived-1.1.13
        script "killall -0 haproxy"     # cheaper than pidof
        interval 2                      # check every 2 seconds
        weight 2                        # add 2 points of prio if OK
}

vrrp_script chk_nginx {           # Requires keepalived-1.1.13
        script "killall -0 nginx"     # cheaper than pidof
        interval 2                      # check every 2 seconds
        weight 2                        # add 2 points of prio if OK
}

vrrp_instance VI_1 {
        interface eth0
        state BACKUP
        virtual_router_id 152
        priority 100                    # 101 on master, 100 on backup
        virtual_ipaddress {
            172.30.1.248
        }
        track_script {
            chk_haproxy
        }
}

vrrp_instance VI_2 {
        interface eth0
        state BACKUP
        virtual_router_id 162
        priority 100                    # 101 on master, 100 on backup
        virtual_ipaddress {
            172.30.1.247
        }
        track_script {
            chk_nginx
        }
}


Setup HAProxy

MASTER:

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        maxconn 8196
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option redispatch
        maxconn 20000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen varnish_farm 172.30.1.248:80
        mode http
        cookie SERVERID insert indirect nocache
        balance roundrobin
        option http-server-close
        option forwardfor
        stats enable
        stats auth admin:Toklon
        server varnish1 172.30.1.23:80 cookie varnish11 check
        server varnish2 172.30.1.75:80 cookie varnish12 check

SLAVE:

Setup Varnish

MASTER & SLAVE are equal on both servers.