Haproxy 優化

  1. Haproxy 自身健康檢查
    vi /usr/local/haproxy/sbin/check_haproxy.sh
    #!/bin/sh
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
                                
    [[ -e "/etc/rc.d/init.d/haproxy" ]] || exit 1
    [[ -z `ps aux | grep /usr/sbin/haproxy | grep -v grep` ]] && service haproxy start && exit 1
                                
    ETH1_ADDR=`/sbin/ifconfig eth0 | awk -F ':' '/inet addr/{print $2}' | sed 's/[a-zA-Z ]//g'`
    [[ -z `curl -I -s "http://${ETH1_ADDR}" | grep "200 OK"` ]] && service haproxy restart
    check harpy Code

    chmox +x /usr/local/haproxy/sbin/check_haproxy.sh
    設置定時檢查任務
    crontab -e
    */5 * * * * /usr/local/haproxy/sbin/check_haproxy.sh >/dev/null 2>&1
    備註:若沒法運行,請根據haproxy安裝路徑和綁定的網卡對代碼作簡單修改php

  2. 進階配置
    mode http
    # 設置爲http模式
    balance source
    # 設置haproxy的調度算法爲源地址hash
    cookie SERVERID
    # 容許向cookie插入SERVERID,每臺服務器的SERVERID可在下面使用cookie關鍵字定義
    option httpchk GET /test/index.php
    # 開啓對後端服務器的健康檢測,經過GET /test/index.php來判斷後端服務器的健康狀況
    server php_server_1 10.12.25.68:80 cookie 1 check inter 2000 rise 3 fall 3 weight 2
    server php_server_2 10.12.25.72:80 cookie 2 check inter 2000 rise 3 fall 3 weight 1
    server php_server_bak 10.12.25.79:80 cookie 3 check inter 1500 rise 3 fall 3 backup
    # server語法:server [:port] [param*]
    # 使用server關鍵字來設置後端服務器;爲後端服務器所設置的內部名稱[php_server_1],該名稱將會呈如今日誌或警報中、後端服務器的IP地址
    # 支持端口映射[10.12.25.68:80]、指定該服務器的SERVERID爲1[cookie 1]
    # 接受健康監測[check]、監測的間隔時長,單位毫秒[inter 2000]
    # 監測正常多少次後被認爲後端服務器是可用的[rise 3]
    # 監測失敗多少次後被認爲後端服務器是不可用的[fall 3]
    # 分發的權重[weight 2]、最後爲備份用的後端服務器,當正常的服務器所有都宕機後,纔會啓用備份服務器[backup]

  3. 優化網絡
    vim /etc/sysctl.conf
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_keepalive_time = 1200
                                     
    net.ipv4.ip_local_port_range = 1024 65000
    net.ipv4.tcp_max_syn_backlog = 8192
    net.ipv4.tcp_max_tw_buckets = 80000
                                     
    net.core.somaxconn = 32768
                                     
    net.ipv4.tcp_keepalive_probes = 5
    net.ipv4.tcp_keepalive_intvl = 20
                                     
    net.core.wmem_default = 8388608
    net.core.rmem_default = 8388608
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216
                                     
    net.ipv4.tcp_rmem = 4096 87380 16777216
    net.ipv4.tcp_wmem = 4096 65536 16777216
                                     
    net.core.netdev_max_backlog = 32768
                                     
    net.ipv4.tcp_timestamps = 0
    net.ipv4.tcp_synack_retries = 2
    net.ipv4.tcp_syn_retries = 2
    net.ipv4.tcp_retries2 = 5
                                     
    net.ipv4.tcp_mem = 41943040 73400320 94371840
    net.ipv4.tcp_max_orphans = 3276800
    fs.file-max = 1300000

    sysctl -p 算法

  4. 參考文檔
    http://sofar.blog.51cto.com/353572/1291013/
    http://leejia.blog.51cto.com/4356849/1421882
相關文章
相關標籤/搜索