Centos7部署HAproxy實現Nginx群集

  • 2019 年 12 月 5 日
  • 筆記

前言

Haproxy是一個使用C語言編寫的自由及開放程式碼軟體,其提供高可用性、負載均衡,以及基於TCP和HTTP的應用程式代理,可以運行於大部分主流的Linux作業系統上。 本次實驗用到四台伺服器,一台伺服器安裝haproxy實現調度,另三台伺服器搭建nginx提供web服務,一台客戶機測試訪問。

環境介紹

主機

系統

ip地址

功能

HAproxy

Centos7

192.168.128.130

haproxy

node1

Centos7

192.168.128.131

Openresty

node2

Centos7

192.168.128.132

Openresty

node3

Centos7

192.168.128.133

Openresty

Client

Windows10

192.168.3.2

chrome

nginx部署

PS:由於有3台nginx web伺服器,我這邊使用ansible統一安裝了。

wget https://openresty.org/download/openresty-1.13.6.1.tar.gz  tar -zxvf openresty-1.13.6.1.tar.gz  cd openresty-1.13.6.1.tar  useradd -m www  yum -y install gcc gcc-c++ zlib-devel  pcre-devel openssl-devel  ./configure --prefix=/usr/local --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module  gmake && gmake install  /usr/local/nginx/sbin/nginx -t  /usr/local/nginx/sbin/nginx

測試一下nginx安裝是否正確

[root@node1 html]# curl -I http://192.168.128.131  HTTP/1.1 200 OK  Server: openresty/1.13.6.1  Date: Mon, 18 Mar 2019 14:06:59 GMT  Content-Type: text/html  Content-Length: 556  Last-Modified: Mon, 18 Mar 2019 13:39:22 GMT  Connection: keep-alive  ETag: "5c8f9f8a-22c"  Accept-Ranges: bytes    [root@node2 openresty-1.13.6.1]# curl -I http://192.168.128.132  HTTP/1.1 200 OK  Server: openresty/1.13.6.1  Date: Mon, 18 Mar 2019 14:07:28 GMT  Content-Type: text/html  Content-Length: 556  Last-Modified: Mon, 18 Mar 2019 13:38:49 GMT  Connection: keep-alive  ETag: "5c8f9f69-22c"  Accept-Ranges: bytes    [root@node3 openresty-1.13.6.1]# curl -I http://192.168.128.133  HTTP/1.1 200 OK  Server: openresty/1.13.6.1  Date: Mon, 18 Mar 2019 14:07:32 GMT  Content-Type: text/html  Content-Length: 556  Last-Modified: Mon, 18 Mar 2019 13:39:08 GMT  Connection: keep-alive  ETag: "5c8f9f7c-22c"  Accept-Ranges: bytes

haproxy部署

nginx部署成功後,我們開始部署haproxy,這裡選擇1.8.19版本安裝。

編譯安裝

wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.19.tar.gz  tar -zxvf haproxy-1.8.19.tar.gz  cd haproxy-1.8.19  make TARGET=linux310 ARCH=x86_64 PREFIX=/usr/local/haproxy  make install PREFIX=/usr/local/haproxy  [root@haproxy conf]# /usr/local/haproxy/sbin/haproxy -v  HA-Proxy version 1.8.19 2019/02/11  Copyright 2000-2019 Willy Tarreau <[email protected]>
  • TARGET=linux310,內核版本,使用uname-r查看內核,如:3.10.0-514.el7,此時該參數就為linux310;kernel 大於2.6.28的可以用:TARGET=linux2628;
  • ARCH=x86_64,系統位數;
  • PREFIX=/usr/local/haprpxy #/usr/local/haprpxy,為haprpxy安裝路徑。

配置

由於新版本的haproxy已經沒有在examples目錄裡面保存配置文件了,我就招了一個老版本的配置文件來使用。

添加配置文件

global      log 127.0.0.1 local0   #日誌          maxconn 1000 # 最大連接數          daemon    defaults          log     global # 採用全局定義的日誌          mode    http # 默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK          option  httplog # 日誌類型為http日誌          option  dontlognull # 不記錄健康檢查的日誌資訊          retries 3   # 3次連接失敗就認為服務不可用,也可以在下面設置          timeout connect 5000   # 連接超時時間          timeout client  50000   # 客戶端連接超時時間          timeout server 50000    # 伺服器連接超時時間    listen admin_stats          bind 0.0.0.0:1080   # 監聽埠          mode http          option httplog          maxconn 10          stats refresh 30s  # 統計頁面刷新時間          stats uri /stats  # 統計頁面url          stats realm XingCloud Haproxy   # 統計頁面密碼框的提示文本          stats auth admin:admin   # 統計頁面的驗證資訊          stats auth  Frank:Frank          stats hide-version          stats  admin if TRUE  listen webcluster         bind 0.0.0.0:80         option httpchk GET /index.html         balance roundrobin  # 負載均衡模式輪詢         server inst1 192.168.128.131:80 check inter 2000 fall 3  # 定義後端伺服器,每2000毫秒檢查一次         server inst2 192.168.128.132:80 check inter 2000 fall 3         server inst3 192.168.128.133:80 check inter 2000 fall 3

處理日誌

默認haproxy不帶日誌,我們需要rsyslog來配合生成日誌,如果你的系統中沒有rsyslog,可以使用yum 安裝一下。

在/etc/rsyslog.conf最末尾添加如下兩行:

# haproxy.log  local0.*          /var/log/haproxy.log  local3.*          /var/log/haproxy.log

修改/etc/sysconfig/rsyslog 文件中配置為如下配置:

# SYSLOGD_OPTIONS=""  SYSLOGD_OPTIONS="-r -m 0"

重啟rsyslog

systemctl restart rsyslog

測試

上述配置完成後先檢查一下haproxy的埠和進程是否正常

看到埠和進程都處於正常狀態了,下面來測試一下負載均衡是否可以正常工作。