一、環(huán)境說(shuō)明
- 使用haproxy對(duì)apache進(jìn)行負(fù)載均衡。
主機(jī)IP | 角色 | 安裝服務(wù) |
---|---|---|
192.168.161.131 | 后端服務(wù)器1 | httpd,80端口 |
192.168.161.132 | 后端服務(wù)器2 | httpd,8080端口 |
192.168.161.133 | 調(diào)度服務(wù)器 | haproxy,8189端口 |
二、安裝配置httpd
- 參考文章,需要對(duì)兩臺(tái)后端服務(wù)器安裝httpd服務(wù),并配置https。
1.使用http訪(fǎng)問(wèn)。
2.使用https訪(fǎng)問(wèn)。
三、安裝配置haproxy
- haproxy下載地址
1.安裝依賴(lài)包,創(chuàng)建系統(tǒng)用戶(hù)。
//安裝依賴(lài)包。
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//創(chuàng)建用戶(hù)。
useradd -r -M -s /sbin/nologin haproxy
2.解壓安裝包,編譯安裝。
tar zxf haproxy-2.9-dev1.tar.gz
cd haproxy-2.9-dev1
//編譯安裝。
make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
TARGET=linux-glibc \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 \
USE_SYSTEMD=1
make install PREFIX=/usr/local/haproxy
3.配置各個(gè)負(fù)載的內(nèi)核參數(shù)。
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
//重新讀取。
sysctl -p
4.提供配置文件。
mkdir /etc/haproxy
//添加以下內(nèi)容。
cat > /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------統(tǒng)計(jì)頁(yè)面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s
#---------------web設(shè)置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 172.16.103.130:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
EOF
//修改配置文件。
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
# cookie SESSION_COOKIE insert indirect nocache //將其注釋掉
server web01 192.168.161.131:80 check inter 2000 fall 5 //添加后端服務(wù)器ip,注意這里的80端口就是httpd的監(jiān)聽(tīng)端口。
server web02 192.168.161.132:80 check inter 2000 fall 5
5.設(shè)置系統(tǒng)服務(wù)。
cat > /usr/lib/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
EOF
//重新加載。
systemctl daemon-reload
6.啟用日志。
vim /etc/rsyslog.conf
local0.* /var/log/haproxy.log //添加此行
//重啟服務(wù)。
systemctl restart rsyslog
7.啟動(dòng)haproxy。
systemctl start haproxy
四、驗(yàn)證http負(fù)載均衡
1.使用harproxy的IP訪(fǎng)問(wèn)網(wǎng)頁(yè),訪(fǎng)問(wèn)一次顯示RS1,刷新一下顯示RS2,再刷新一下RS1。
2.命令訪(fǎng)問(wèn)。
五、配置https負(fù)載均衡
1.需要提前配置兩臺(tái)后端服務(wù)器的httpd為https模式。
2.修改haproxy配置文件。
vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
defaults
mode tcp //將此處改成tcp協(xié)議
#---------------web設(shè)置-----------------------
listen webcluster
bind 0.0.0.0:443 //將端口改成443
mode tcp //使用tcp協(xié)議
# cookie SESSION_COOKIE insert indirect nocache
server web01 192.168.161.131:443 check inter 2000 fall 5 //將端口改成443
server web02 192.168.161.132:443 check inter 2000 fall 5
//重啟服務(wù)。
systemctl restart haproxy
3.驗(yàn)證。
六、haproxy網(wǎng)頁(yè)監(jiān)控
6.1 監(jiān)控參數(shù)詳解
1.可以通過(guò)haproxy的web頁(yè)面,查看監(jiān)聽(tīng)負(fù)載均衡集群狀態(tài),包括調(diào)度器、后端服務(wù)器。
2.監(jiān)控頁(yè)面參數(shù)詳解。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-568022.html
#####################################################
Queue列表
Cur: current queued requests //當(dāng)前的隊(duì)列請(qǐng)求數(shù)量
Max:max queued requests //最大的隊(duì)列請(qǐng)求數(shù)量
Limit: //隊(duì)列限制數(shù)量
#####################################################
Session rate (每秒的連接回話(huà)列表)
scur: current sessions //每秒的當(dāng)前回話(huà)的限制數(shù)量
smax: max sessions //每秒的新的最大的回話(huà)量
slim: sessions limit //每秒的新回話(huà)的限制數(shù)量
#####################################################
Sessions
Total: //總共回話(huà)量
Cur: //當(dāng)前的回話(huà)
Max: //最大回話(huà)
Limit: //回話(huà)限制
Lbtot: //選中一臺(tái)服務(wù)器所用的總時(shí)間
#####################################################
Bytes
In: //網(wǎng)絡(luò)的字節(jié)數(shù)輸入總量
Out: //網(wǎng)絡(luò)的字節(jié)數(shù)輸出總量
#####################################################
Denied
Req: //拒絕請(qǐng)求量
Resp: //拒絕回應(yīng)
#####################################################
Errors
Req:request errors //錯(cuò)誤請(qǐng)求
Conn:connection errors //錯(cuò)誤的連接
Resp: response errors (among which srv_abrt) ///錯(cuò)誤的回應(yīng)
#####################################################
Warnings
Retr: retries (warning) //重新嘗試
Redis:redispatches (warning) //再次發(fā)送
#####################################################
Server列表
Status:狀態(tài),包括up(后端機(jī)活動(dòng))和down(后端機(jī)掛掉)兩種狀態(tài)
LastChk: 持續(xù)檢查后端服務(wù)器的時(shí)間
Wght: (weight) : 權(quán)重
Act: server is active (server), number of active servers (backend) //活動(dòng)鏈接數(shù)量
Bck: server is backup (server), number of backup servers (backend) //backup:備份的服務(wù)器數(shù)量
Down: //后端服務(wù)器連接后都是down的數(shù)量
Downtime: downtime: total downtime (in seconds) //總的downtime 時(shí)間
Throttle: warm up status //設(shè)備變熱狀態(tài)
6.2 頁(yè)面操作
1.查看RS1的httpd服務(wù)狀態(tài)。
2.手動(dòng)關(guān)閉網(wǎng)頁(yè)上的服務(wù)。
3.過(guò)一會(huì),頁(yè)面顯示的服務(wù)自動(dòng)起來(lái)了。
4.關(guān)閉服務(wù)器上的服務(wù)。
5.查看監(jiān)控網(wǎng)頁(yè)狀態(tài),顯示該服務(wù)已宕機(jī)。此時(shí)即時(shí)在網(wǎng)頁(yè)上啟動(dòng)該服務(wù),也沒(méi)有效果,只能在服務(wù)器上啟動(dòng)服務(wù)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-568022.html
到了這里,關(guān)于集群基礎(chǔ)3——haproxy負(fù)載均衡apache的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!