美文网首页
haproxy+keepalived配置

haproxy+keepalived配置

作者: bluexiii | 来源:发表于2019-07-31 10:51 被阅读0次

    防火墙配置

    firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens33 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
    firewall-cmd --zone=public --add-port=8080/tcp --permanent
    firewall-cmd --zone=public --add-port=18010/tcp --permanent
    firewall-cmd --reload
    

    systemctl disable firewalld 
    systemctl stop firewalld 
    

    关闭selinux

    vi /etc/sysconfig/selinux
    SELINUX=disabled
    setenforce 0
    getenforce
    

    启动测试WEB服务

    yum install nc
    

    vi index.html

    <!doctype html>
    <html>
        <head>
          <meta charset="utf-8">
          <title>Test Page</title>
        </head>
        <body>
          <h1>It Works!</h1>
        </body>
    </html>
    
    while true;do { printf '%b\r\n' 'HTTP/1.1 200 OK' '%b\r\n';cat index.html; }|nc -l 8080;done
    

    安装haproxy

    yum install -y haproxy
    

    配置haproxy

    vi /etc/haproxy/haproxy.cfg 
    
    global
            user haproxy
            group haproxy
            daemon
            maxconn 4096
    defaults
            mode    tcp
            balance roundrobin #leastconn
            timeout client      30000ms
            timeout server      30000ms
            timeout connect      3000ms
            retries 3
    frontend fr_server1
            bind 0.0.0.0:18010
            default_backend bk_server1
    backend bk_server1
        server  srv1 10.211.55.11:8080
        server  srv2 10.211.55.12:8080
    listen stats
        mode http
        bind *:9090
        stats enable
        stats refresh 3s
        stats uri     /
        stats auth    admin:123456
        stats admin if TRUE
    

    启动haproxy

    systemctl enable haproxy
    systemctl start haproxy
    

    安装keepalived

    yum install keepalived ipset-libs libnl3-devel psmisc
    

    配置keepalived

    vi /etc/keepalived/keepalived.conf

    vrrp_script chk_proc {
      script "killall -0 haproxy" # check the haproxy process
      interval 2 # every 2 seconds
      weight 2 # add 2 points if OK
    }
    
    vrrp_instance VI_1 {
      interface eth0 # interface to monitor
      state MASTER # MASTER on haproxy1, BACKUP on haproxy2
      virtual_router_id 51
      priority 101 # 101 on haproxy1, 100 on haproxy2
      virtual_ipaddress {
        10.211.55.100/24 # virtual ip address 
      }
      track_script {
        chk_proc
      }
    }
    

    启动keepalived

    systemctl enable keepalived
    systemctl start keepalived
    

    查看日志

    tail -f /var/log/messages
    

    参考文档

    相关文章

      网友评论

          本文标题:haproxy+keepalived配置

          本文链接:https://www.haomeiwen.com/subject/ullzrctx.html