Haproxy是目前比較流行的一種叢集排程工具
LVS 雖然在企業應用中抗負載能力很強,但存在不足
Haproxy 是一款可提供高可用性、負載均衡、及基於 TCP 和 HTTP 應用的代理的軟體
Haproxy支援多種排程演演算法,最常用的有三種:RR(Round Robin),LC(Least Connections),SH(Source Hashing)
主機 | 作業系統 | 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 |
'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
[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.2 相同,只需要將 51xit 改為 52xit 就可以了,可以參考 3.2.2
網頁登入:192.168.100.23驗證
[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驗證
切換,會發現不同的網站頁面,說明已經實現了負載均衡
[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引數優化相當重要: