HAProxy 安裝及基本配置

0. 下載并安裝

HAProxy 官網,最新穩定版本

$ wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.9.tar.gz
$ tar -zxvf haproxy-1.8.9.tar.gz
$ make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy
$ sudo make install PREFIX=/usr/local/haproxy

參數說明

TARGET: 使用 uname -r 查看內核

  • linux22 for Linux 2.2
  • linux24 for Linux 2.4 and above (default)
  • linux24e for Linux 2.4 with support for a working epoll (> 0.21)
  • linux26 for Linux 2.6 and above
  • linux2628 for Linux 2.6.28, 3.x, and above (enables splice and proxy)
$ uname -r
3.10.0-693.5.2.el7.x86_64

ARCH: ARCH=x86_64,系統位數

PREFIX: PREFIX=/usr/local/haprpxy,haprpxy安裝路徑

1. 基本配置

$ sudo mkdir -p /etc/haproxy
$ sudo touch /etc/haproxy/haproxy.cfg
global                                          # 全局屬性
    daemon                                      # 以daemon方式在后臺運行
    maxconn 256                                 # 最大同時256連接
    pidfile /usr/local/haproxy/conf/haproxy.pid # 指定保存HAProxy進程號的文件

defaults                                        # 默認參數
    mode http                                   # http模式
    timeout connect 5000ms                      # 連接server端超時5s
    timeout client 50000ms                      # 客戶端響應超時50s
    timeout server 50000ms                      # server端響應超時50s

frontend http-in                                # 前端服務http-in
    bind *:8080                                 # 監聽8080端口
    default_backend nginx-server                # 請求轉發至名為nginx-server的后端服務

backend nginx-server                            # 后端服務
    server server1 127.0.0.1:80 maxconn 32      # nginx 80 端口服務,HAProxy同時最多向這個服務發起32個連接

2. 測試

$ /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
$ sudo systemctl start nginx
$ curl http://127.0.0.1:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

3. more

添加統計頁面

...

listen haproxy_stats                            # haproxy 統計頁面
    bind *:8081
    mode http
    stats refresh 3s                            # 頁面刷新間隔
    stats uri /haproxy-stats                    # uri
    stats auth admin:admin                      # 授權賬戶
HAProxy 統計頁面

4. 參考資料

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容