Ubuntu 16.04 haproxy 簡單配置應用

轉載自:http://www.bubuko.com/infodetail-1948987.html

HAproxy

HAProxy是一個使用C語言編寫的自由及開放源代碼軟件,其提供高可用性負載均衡,以及基於TCPHTTP的應用程序代理
 

測試環境

ubuntu16.04 192.168.20.182 haproxy
ubuntu16.04 192.168.20.178 web1
ubuntu16.04 192.168.20.179 web2

安裝haproxy

~#apt-get install haproxy -y

haproxy配置文件

安裝完成後,配置文件路徑:/etc/haproxy/haproxy.cfg

修改配置文件內容如下:

 

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        pidfile /var/run/haproxy.pid  #haproxy的pid存放路徑,啓動進程的用戶必須有權限訪問此文件
        stats socket /run/haproxy/admin.sock mode 660 level admin
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        # Default ciphers to use on SSL-enabled listening sockets.
        # For more information, see ciphers(1SSL). This list is from:
        #  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
        ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
        ssl-default-bind-options no-sslv3

defaults
        log     global
        mode    http    #默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK
        option  httplog
        option  dontlognull
        maxconn 60000
        retries 3
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

listen stats    # 管理界面,統計信息參數配置
        bind 0.0.0.0:1080  #監聽端口
        stats refresh 30s  #統計頁面自動刷新時間
        stats uri /stats  #統計url
        stats realm Haproxy Manager  #統計頁面密碼框上提示文本
        stats auth admin:admin    #統計頁面登錄的用戶和密碼

frontend main    # 前端haproxy服務器配置
        bind 0.0.0.0:80
        acl url_static path_beg -i /static /images /javascript /stylesheets
        acl url_static path_end -i .jpg .gif .png .css .js
        use_backend static if url_static  #滿足策略要求,則響應策略定義的backend頁面
        default_backend dynamic    #不滿足則響應backend的默認頁面

backend static
        balance roundrobin  #負載均衡模式輪詢
        server static 127.0.0.1:80 check  #後端服務器定義

backend dynamic
        balance roundrobin
        server websrv1 192.168.20.178:80 check maxconn 1000  #後端web server
        server websrv2 192.168.20.179:80 check maxconn 1000  #後端web server

啓動haproxy

~#/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg

設置開機啓動

利用shell編程實現

訪問測試

 

測試:

 

訪問192.168.20.182:1080/stats進入haproxy的web stats界面

 

訪問192.168.20.182:80

 

會顯示178的web界面

 

再次刷新會訪問179的web界面
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章