通過Haproxy的ACL規劃實現智慧負載均衡,並簡述tcp、http、health的配置示例
一、前言
本次實驗目:
1、通過haproxy實現負載均衡,並使用acl進行智慧排程。
2、配置演示haproxy的tcp模式和http模式。
3、對後端的伺服器進行相關的監控檢查。
二、配置nginx伺服器
1、配置nginx1
首先在nginx1上安裝nginx服務和PHP-fpm相關服務
[root@nginx1 ~]# yum install -y epel-release [root@nginx1 ~]# yum install -y nginx [root@nginx1 ~]# yum install -y php-fpm php-mysql php-mbstring php-mcrypt
接著建立相應的web根目錄
[root@nginx1 ~]# mkdir /data/nginx/html/ -pv
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/nginx’
mkdir: created directory ‘/data/nginx/html/’
編輯建立預設頁面:
[root@nginx1 ~]# vim /data/nginx/html/index.html
<h1>This is nginx1</h1>
下載wordpress包並安裝到指定的web目錄下,提供一個wordpress頁面。
[root@nginx1 ~]# cd /data/nginx/html/
[root@nginx1 html]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@nginx1 html]# tar xf wordpress-4.9.4-zh_CN.tar.gz
然後建立nginx配置檔案:
#注意需要刪除/etc/nginx/nginx.conf檔案中的預設listen 80的配置 [root@nginx1 html]# vim /etc/nginx/conf.d/vhost.conf server { listen 80; server_name www.ilinux.io; root /data/nginx/html; index index.htm index.phpl; location / { } location ~* \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /data/nginx/html/$fastcgi_script_name; } }
最後啟動nginx和php-fpm服務並調整firewalld和selinux 狀態。
[root@nginx1 html]# systemctl start nginx
[root@nginx1 ~]# systemctl start php-fpm
[root@nginx1 html]# systemctl stop firewalld
[root@nginx1 html]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@nginx1 html]# setenforce 0
2、配置nginx2
首先安裝nginx服務:
[root@nginx2 ~]# yum install -y epel-release
[root@nginx2 ~]# yum install -y nginx
建立nginx 的web根目錄:
[root@nginx2 ~]# mkdir -pv /data/nginx/html
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/nginx’
mkdir: created directory ‘/data/nginx/html’
編輯建立預設頁面:
[root@nginx2 ~]# vim /data/nginx/html/index.html
<h1>This is nginx2</h1>
接著在web根目錄下建立一個web2目錄,並提供一個測試頁面:
root@nginx2 html]# cd /data/nginx/html/
[root@nginx2 html]# mkdir web2/
[root@nginx2 html]# vim web2/index.html
<h1>This is test page</h1>
然後建立nginx配置檔案:
[root@nginx2 html]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name wwww.ilinux.io;
root /data/nginx/html;
index index.html;
}
最後啟動nginx服務並調整firewalld和selinux狀態:
[root@nginx2 html]# systemctl start nginx
[root@nginx2 html]# systemctl stop firewalld
[root@nginx2 html]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@nginx2 html]# setenforce 0
目前後端兩個nginx伺服器分別提供了兩個預設主頁,以及一個wordpress動態頁面和一個web2的測試頁面。
三、配置Haproxy伺服器
1、結合ACL做負載均衡
首先安裝haproxy服務:
[root@haproxy ~]# yum install -y haproxy
接著編輯配置/etc/haproxy/haproxy.cfg,新增下述配置段:
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend main *:80
acl url_blog path_reg ^/wordpress.* #此acl用於匹配訪問路徑帶有/wordpress的連線
acl url_web2 path_reg ^/web2.* #此acl用於匹配訪問路徑帶有/web2的連線
use_backend web2 if url_web2 #將acl url_web2匹配到的會話排程到後端伺服器組web2處理
use_backend blog if url_blog #將acl url_blog匹配到的會話排程到後端伺服器組blog處理
default_backend nginxsrvs #其餘會話預設使用nginxsrvs進行負載均衡排程
backend nginxsrvs
balance roundrobin
server nginx1 192.168.0.83:80 check
server nginx2 192.168.0.84:80 check
backend web2
balance roundrobin
server web2 192.168.0.84:80 check
backend blog
balance roundrobin
server blog1 192.168.0.83:80 check
隨後啟動haproxy服務並調整firewalld和selinux:
[root@haproxy ~]# systemctl start haproxy
[root@haproxy ~]# systemctl stop firewalld
[root@haproxy ~]# setenforce 0
[root@haproxy ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
此時訪問http://192.168.0.81/ 應該能夠正常輪詢到兩個後端的nignx 伺服器上
[root@client ~]# for i in {1..10} ; do curl http://192.168.0.81/ ; done
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
將後端其中一個伺服器的nginx服務手動停用後,訪問會話應可能正常負載到第二臺伺服器上。
[root@client ~]# for i in {1..10} ; do curl http://192.168.0.81/ ; done
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
<h1>This is nginx2</h1>
而由於ACL排程的緣故,訪問http://192.168.0.81/web2/ 應被排程到後端nginx2 上,而訪問http://192.168.0.81/wordpress應被排程到後端的nginx1上。
[root@client ~]# curl http://192.168.0.81/web2/
<h1>This is test page</h1>
此時基於ACL做的負載均衡排程就已經成功了。
2、配置haproxy的 tcp與http模式
haproxy的執行模式有三種tcp、http和health,其中http為haproxy的預設執行模式,而health基本上已經被haproxy的其他健康檢查引數所替代了,因此此處不再演示。
在/etc/haproxy/haproxy.cfg中新增tcp模式和http模式的配置段:
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen sshsrvs #配置listen段
bind *:22322 #監聽22322埠
balance leastconn #設定排程演算法的為leastconn
mode tcp #設定模式問tcp模式
server sshsrv1 192.168.0.83:22 check #反代到後端的22埠
server sshsrv2 192.168.0.84:22 check
listen websrvs
bind *:8080
balance roundrobin
mode http #設定模式為httpd
option httpchk GET /index.html #配置通過獲取後端主機的指定頁面來對後端主機做健康檢測
server websrv1 192.168.0.83:80 check
server websrv2 192.168.0.84:80 check
此時重啟haproxy服務後,相應的22322和8080埠應能正常監聽起來:
[root@haproxy ~]# systemctl restart haproxy
[root@haproxy ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:8080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22322 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
此時通過訪問http://192.168.0.81:8080 應能正常輪詢到後端主頁,也能通過ssh 192.168.0.81:22322連線到後端的伺服器上。
[root@client ~]# for i in {1..10} ; do curl http://192.168.0.81:8080 ; done
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
<h1>This is nginx2</h1>
<h1>This is nginx1</h1>
3、haproxy的健康檢查
haproxy的健康檢測主要可以分為下列三種:
- 基於四層,對監聽埠進行健康檢測。此方式,haproxy只會檢查後端server的埠是否存活,而不能保證服務的真正可用。
listen websrvs
bind *:8080
balance roundrobin
mode http
option httpchk
server websrv1 192.168.0.83:80 check
server websrv2 192.168.0.84:80 check
- 通過URI獲取來進行健康檢測,其檢查方式為通過去GET後端server 的web頁面來檢查後端server的服務是否為存活。
listen websrvs
bind *:8080
balance roundrobin
mode http
option httpchk GET /index.html
server websrv1 192.168.0.83:80 check
server websrv2 192.168.0.84:80 check
- 通過request來獲取後端server的http首部資訊來進行匹配檢測,此類檢查可用實現更高階的監控與檢查。
listen websrvs
bind *:8080
balance roundrobin
mode http
option httpchk HEAD /index.html HTTP/1.0
server websrv1 192.168.0.83:80 check
server websrv2 192.168.0.84:80 check
另外還可以啟用haproxy的監控頁面來管理檢視後端伺服器的執行情況,在haproxy的配置檔案中新增下述配置段並重啟服務:
listen stats
bind *:9099 #狀態頁的監聽埠為9099
stats enable #啟用狀態頁
stats uri /admin?stats #設定狀態頁的訪問URI
stats auth admin:admin #配置認證訪問
stats admin if TRUE #如果認證通過,則啟動狀態頁的管理功能
來自為知筆記(Wiz)