文章目錄
- 一、Haproxy 概述
- 1.1 Haproxy 與 LVS、Nginx 的比較
- 1.2 常見的 Web 集群調度器
- 1.3 Haproxy 應用分析
- 二、Haproxy 調度演算法
- 2.1 RR(Round Riobin)
- 2.2 LC(Least Connections)
- 2.3 SH(Source Hashing)
- 三、實驗
- 3.1 實驗準備
- 3.2 步驟
- 3.2.1 除錯存盤服務器(192.168.100.24)
- 3.2.2 編譯安裝Nginx服務器1(192.168.100.22)
- 3.2.3 編譯安裝Nginx服務器2(192.168.100.23)
- 3.2.4 配置Haproxy 服務器(192.168.100.21)
- 四、Haproxy日志檔案
- 五、Haproxy引數優化
一、Haproxy 概述
Haproxy是目前比較流行的一種集群調度工具
1.1 Haproxy 與 LVS、Nginx 的比較
- LVS 性能最好,但是搭建相對復雜
- Nginx 的 upstream 模塊支持集群功能,但是對集群節點健康檢查功能不強,性能沒有 Haproxy 好
1.2 常見的 Web 集群調度器
- 目前常見的 Web 集群調度器分為軟體和硬體
- 軟體通常使用開源的 LVS、 Haproxy、 Nginx
- 硬體一般使用比較多的是 F5,也有很多人使用國內的一些產品,如梭子魚、綠盟等
1.3 Haproxy 應用分析
LVS 雖然在企業應用中抗負載能力很強,但存在不足
- LVS 不支持正則處理,不能實作動靜分離
- 對于大型網站,LVS 的實施配置復雜,維護成本相對較高
Haproxy 是一款可提供高可用性、負載均衡、及基于 TCP 和 HTTP 應用的代理的軟體
- 特別適用于負載特別大的 Web 站點
- 運行在當前的硬體上可支持數以萬計的并發連接連接請求
二、Haproxy 調度演算法
Haproxy支持多種調度演算法,最常用的有三種:RR(Round Robin),LC(Least Connections),SH(Source Hashing)
2.1 RR(Round Riobin)
- RR演算法是最簡單最常用的一種演算法,即輪詢調度
- 理解舉例
◆ 有三個節點 A、B、C
◆ 第一個用戶訪問會被指派到節點A
◆ 第二個用戶訪問會被指派到節點B
◆ 第三個用戶訪問會被指派到C節點
◆ 第四個用戶訪問繼續指派到節點A,輪詢分配訪問請求實作負載均衡效果
2.2 LC(Least Connections)
- LC演算法即最小連接數演算法,根據后端的節點連接數大小動態分配前端請求
- 理解舉例
◆ 有三個節點A、B、C,各節點的連接數分別為A:4、B:5、C:6
◆ 第一個用戶連接請求,會被指派到A上,連接數變為A:5、B:5、C:6
◆ 第二個用戶請求會繼續分配到A上,連接數變為A6、B:5、C:6;再有新的請求會分配給B,每次將新的請求指派給連接數最小的客戶端
◆ 由于實際情況下A、B、C的連接數會動態釋放,很難會出現一樣連接數的情況
◆ 此演算法相比較rr演算法有很大改進,是目前用到比較多的一種演算法
2.3 SH(Source Hashing)
- SH即基于來源訪問調度演算法,此演算法用于一些有 Session會話記錄在服務器端的場景,可以基于來源的IP、Cookie等做集群調度
- 理解舉例
◆ 有三個節點A、B、C,第一個用戶第一次訪問被指派到了A,第二個用戶第次訪問被指派到了B
◆ 當第一個用戶第二次訪問時會被繼續指派到A,第二個用戶第二次訪問時依舊會被指派到B,只要負載均衡調度器不重啟,第一個用戶訪問都會被指派到A,第二個用戶訪問都會被指派到B,實作集群的調度
◆ 此調度演算法好處是實作會話保持,但某些IP訪問量非常大時會引起負載不均衡,部分節點訪問量超大,影響業務使用
三、實驗
3.1 實驗準備
| 主機 | 作業系統 | IP地址 | 主要軟體 |
|---|---|---|---|
| Haproxy服務器 | CentoS7.6 | 192.168.100.21 | haproxy-1.5.19.tar.gz |
| Nginx服務器1 | CentoS7.6 | 192.168.100.22 | Nginx-1.12.2.tar.gz |
| Nginx服務器2 | CentoS7.6 | 192.168.100.23 | Nginx-1.12.2.tar.gz |
| 存盤服務器 | CentoS7.6 | 192.168.100.24 | nfs-utils rpcbind |
3.2 步驟
3.2.1 除錯存盤服務器(192.168.100.24)
'rpm -q nfs-utils ###如果沒裝,yum -y install nfs-utils
rpm -q rpcbind ###如果沒裝,yum -y install rpcbind'
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# vi /etc/exports
/opt/51xit 192.168.100.0/24 (rw,sync)
/opt/52xit 192.168.100.0/24 (rw,sync)
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# mkdir /opt/51xit /opt/52xit
[root@localhost ~]# echo "this is www.51xit.com" >/opt/51xit/index.html
[root@localhost ~]# echo "this is www.52xit.com" >/opt/52xit/index.html
3.2.2 編譯安裝Nginx服務器1(192.168.100.22)
[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# ll
total 960
-rw-r--r-- 1 root root 981687 Sep 24 15:58 nginx-1.12.2.tar.gz
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]#
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 5 月 16 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
'■啟動、 停止 Nginx
killall -1 nginx ####安全重啟
killall -3 nginx ###停止服務
如果出現: -bash: killall: command not found'
[root@localhost nginx-1.12.2]# yum -y install psmisc
[root@localhost nginx-1.12.2]# nginx ####啟動
[root@localhost nginx-1.12.2]# yum -y install net-tools
[root@localhost nginx-1.12.2]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 20107/nginx: master
'###添加 Nginx 系統服務###'
[root@localhost nginx-1.12.2]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx ####描述
After=network.target ####描述服務類別
[Service]
Type=forking ####后臺運行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ####PID 檔案位置
ExecStart=/usr/local/nginx/sbin/nginx ####啟動服務
ExecReload=/usr/bin/kill -s HUP $MAINPID ####根據 PID 多載配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ####根據 PID 終止行程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost nginx-1.12.2]# systemctl status nginx
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@localhost nginx-1.12.2]# systemctl start nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@localhost nginx-1.12.2]# killall -3 nginx
[root@localhost nginx-1.12.2]# systemctl start nginx.service
[root@localhost nginx-1.12.2]# systemctl status nginx.service
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2020-09-24 17:08:01 CST; 6s ago
Process: 77233 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 77234 (nginx)
Tasks: 2
CGroup: /system.slice/nginx.service
├─77234 nginx: master process /usr/local/nginx/sbin/nginx
└─77235 nginx: worker process
Sep 24 17:08:01 localhost.localdomain systemd[1]: Starting nginx...
Sep 24 17:08:01 localhost.localdomain systemd[1]: PID file /usr/local/nginx/l...
Sep 24 17:08:01 localhost.localdomain systemd[1]: Started nginx.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost nginx-1.12.2]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service
'####刷下命令,服務正常作業了###
killall -3 nginx ###停止服務
systemctl start nginx.service
###上面命令一刷,下面就可以正常使用了###
systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl restart nginx.service
systemctl status nginx.service'
###安裝httpd 掛載測驗頁###
[root@localhost nginx-1.12.2]# yum -y install nfs-utils
[root@localhost nginx-1.12.2]# systemctl start nfs
[root@localhost nginx-1.12.2]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@localhost nginx-1.12.2]# showmount -e 192.168.100.24
Export list for 192.168.100.24:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
[root@localhost nginx-1.12.2]# mount 192.168.100.24:/opt/51xit /usr/local/nginx/html/ ###臨時掛載
[root@localhost nginx-1.12.2]# vi /etc/fstab ###永久掛載,需要重啟
192.168.100.24:/opt/51xit/ /usr/local/nginx/html/ nfs defaults,_netdev 0 0
[root@localhost nginx-1.12.2]# init 6
網頁登錄:192.168.100.22驗證

3.2.3 編譯安裝Nginx服務器2(192.168.100.23)
操作與 3.2.2 相同,只需要將 51xit 改為 52xit 就可以了,可以參考 3.2.2
網頁登錄:192.168.100.23驗證

3.2.4 配置Haproxy 服務器(192.168.100.21)
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xzvf haproxy-1.4.24.tar.gz
[root@localhost opt]# cd haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26
[root@localhost haproxy-1.4.24]# make install
[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
#chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
#redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server inst1 192.168.100.22:80 check inter 2000 fall 3
server inst2 192.168.100.23:80 check inter 2000 fall 3
'###上述組態檔解釋###
1、Haproxy組態檔通常分為三個部分
global:為全域配置
defaults:為默認配置
listen:為應用組件配置
global配置引數
log 127.0.0.1 local0:配置日志記錄,配置日志記錄,local0為日志設備,默認存放到系統日志
log 127.0.0.1 local1 notice:notice為日志級別,通常有24個級別
maxconn 4096:最大連接數
uid 99:用戶uid gid 99:用戶gid
2、defaults配置項配置默認引數,一般會被應用組件繼承,如果在應用組件中沒有特別宣告,將安裝默認配置引數設定
log global:定義日志為global配置中的日志定義
mode http:模式為http
option httplog:采用http日志格式記錄日志
option dontlognull :保證HAProxy不記錄上級負載均衡發送過來的用于檢測狀態沒有資料的心跳包
retries 3:檢查節點服務器失敗連續達到三次則認為節點不可用
maxconn 2000:最大連接數
contimeout 5000:連接超時時間
clitimeout 50000:客戶端超時時間
srvtimeout 50000:服務器超時時間
3、listen配置專案一般為配置應用模塊引數
listen appli4-backup 0.0.0.0:10004:定義一個appli4-backup的應用
option httpchk /index.html:檢查服務器的index.html檔案
option persist :強制將請求發送到已經down掉的服務器
balance roundrobin:負載均衡調度演算法使用輪詢演算法
server inst1 192.168.114.56:80 check inter 2000 fall 3:定義在線節點
server inst2 192.168.114.56:81 check inter 2000 fall 3 backup:定義備份節點'
[root@localhost haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chkconfig --add haproxy
[root@localhost haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.4.24]# service haproxy start
[root@localhost haproxy-1.4.24]# systemctl stop haproxy.service
[root@localhost haproxy-1.4.24]# systemctl start haproxy.service
網頁登錄:192.168.100.21驗證
切換,會發現不同的網站頁面,說明已經實作了負載均衡


四、Haproxy日志檔案
[root@localhost haproxy-1.4.24]# mkdir -p var/log/haproxy ###創建日志檔案存放點
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global ###下面log開頭的 統統去掉換下面的配置
log /dev/log local0 info
log /dev/log local1 notice
[root@localhost haproxy-1.4.24]# systemctl restart haproxy.service
[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy-1.4.24]# vi /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~
[root@localhost haproxy-1.4.24]# systemctl restart rsyslog.service
[root@localhost haproxy]# tail -f /var/log/haproxy/haproxy-info.log
Sep 24 17:23:14 localhost haproxy[65722]: 192.168.100.2:62258 [24/Sep/2020:17:22:21.206] webcluster webcluster/inst1 0/0/0/1/52926 200 1700 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"
Sep 24 17:23:54 localhost haproxy[65722]: 192.168.100.2:62275 [24/Sep/2020:17:23:41.993] webcluster webcluster/inst2 0/0/0/0/12236 200 258 - - ---- 0/0/0/0/0 0/0 "GET / HTTP/1.1"
Sep 24 17:24:53 localhost haproxy[65722]: 192.168.100.2:62293 [24/Sep/2020:17:24:03.530] webcluster webcluster/inst1 0/0/0/1/50041 200 984 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"
[root@localhost haproxy-1.4.24]# cd /var/log/haproxy
[root@localhost haproxy]# ll
total 4
-rw------- 1 root root 538 Sep 24 17:24 haproxy-info.log
網頁登錄:192.168.32.21
然后查看日志檔案不斷生成情況
五、Haproxy引數優化
隨著企業網站負載增加,haproxy引數優化相當重要:
- maxconn:最大連接數,根據應用實際情況進行調整,推薦使用10240
- daemon:守護行程模式,Haproxy可以使用非守護行程模式啟動,建議使用守護行程模式啟動
- nbproc:負載均衡的并發行程數,建議與當前服務器CPU核數相等或為其2倍
- retries:重試次數,主要用于對集群節點的檢查,如果節點多,且并發量大,設定為2次或3次
- option http-server-close:主動關閉http請求選項,建議在生產環境中使用此選項
- timeout http-keep-alive:長連接超時時間,設定長連接超時時間,可以設定為10s
- timeout http-request:http請求超時時間,建議將此時間設定為5~10s,增加http連接釋放速度
- timeout client:客戶端超時時間,如果訪問量過大,節點回應慢,可以將此時間設定短一些,建議設定為1min左右就可以了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/128761.html
標籤:其他
