Haproxy搭建Web群集
- 一、常見的WEB集群調度器
- 二、Haproxy應用分析
- 三、Haproxy調度演算法原理
- 3.1、RR (Round Robin)
- 3.2、LC (Least Connections)
- 3.3、SH(Source Hashing)
- 四、使用Haproxy搭建Web群集
- 4.1、實驗配置
一、常見的WEB集群調度器
- 目前常見的WEB集群調度器分為軟體和硬體
- 軟體通常使用LVS、Haproxy、Nginx
- 硬體一般使用比較多的是F5,也有很多人使用國內的一些產品,如梭子魚、綠盟等,
二、Haproxy應用分析
■LVS在企業應用中抗負載能力很強,但存在不足
- LVS不支持正則處理,不能實作動靜分離
- 對于大型網站,LVS的實施配置復雜,維護成本相對于較高
■Haproxy是一款可提供高可用性、負載均衡、及基于TCP和HTTP應用代理的軟體
- 適用于負載大的web站點
- 運行在硬體上可支持數以萬計的并發連接的連接請求
三、Haproxy調度演算法原理
3.1、RR (Round Robin)
■ Haproxy支持多種調度演算法,最常用的有三種
-
RR (Round Robin)
◆ RR演算法是最簡單最常用的一種演算法,即輪詢調度 -
理解舉例
◆ 有三個節點A、B、C
◆ 第一個用戶訪問會被指派到節點A
◆ 第二個用戶訪問會被指派到節點B
◆ 第三個用戶訪問會被指派到節點C
◆ 第四個用戶訪問繼續指派到節點A,輪詢分配訪問請求實作負載均衡效果
3.2、LC (Least Connections)
■ LC (Least Connections)
- 最小連接數演算法,根據后端的節點連接數大小動態分配前端請求
■ 理解舉例
- 有三個節點A、B、C,各節點的連接數分別為A:4、B:5、C:6
- 第一個用戶連接請求,會被指派到A上,連接數變為A:5、B:5、C:6
- 第二個用戶請求會繼續分配到A上,連接數變為A:6、B:5、C:6;再有新的請求會分配給B,每次將新的請求指派給連接數最小的客戶端
- 由于實際情況下A、B、C的連接數會動態釋放,很難會出現一樣連接數的情況
- 此演算法相比較rr演算法有很大改進,是目前用到比較多的一種演算法
3.3、SH(Source Hashing)
■ SH(Source Hashing)
- 基于來源訪問調度演算法,用于一些有Session會話記錄在服務器端的場景,可以基于來源的IP、Cookie等做集群調度
■理解舉例
- 有三個節點A、B、C,第一個用戶第一次訪問被指派到了A,第二個用戶第一次訪問被指派到了B
- 當第一個用戶第二次訪問時會被繼續指派到A,第二個用戶第二次訪問時依舊會被指派到B,只要負載均衡調度器不重啟,第一個用戶訪問都會被指派到A,第二個用戶訪問都會被指派到B,實作集群的調度
- 此調度演算法好處是實作會話保持,但某些IP訪問量非常大時會引起負載不均衡,部分節點訪問量超大,影響業務使用
四、使用Haproxy搭建Web群集
4.1、實驗配置
########### 使用Haproxy搭建Web群集 #######
場景介紹:
主機 作業系統 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
######除錯存盤服務器 192.168.100.24 #####
[root@localhost ~]#rpm -q nfs-utils ###如果沒裝,yum -y install nfs-utils
[root@localhost ~]#rpm -q rpcbind ###如果沒裝,yum -y install rpcbind
[root@localhost ~]# mkdir /opt/51xit /opt/52xit ###創建51和52目錄
[root@localhost ~]# echo "this is www.51xit.com" >/opt/51xit/index.html ###建一個51xit首頁并寫入東西
[root@localhost ~]# echo "this is www.52xit.com" >/opt/52xit/index.html ###建一個52xit首頁并寫入東西
[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 start rpcbind ###啟動rpcbind 先啟動rpcbind在啟動nfs
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
######編譯安裝Nginx服務器1 192.168.100.22 ######
1、編譯安裝 Nginx
Nginx 安裝檔案可以從官方網站 http://www.nginx.org/下載,
下面以穩定版 Nginx 1.12.2為例 上傳至/opt下
[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 ~]# 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 && make install ###編譯安裝
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# nginx ### 啟動nginx
[root@localhost nginx-1.12.2]# netstat -anutp |grep nginx ### 查看啟動情況
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21135/nginx: master
■啟動、 停止 Nginx
[root@localhost nginx-1.12.2]# killall -3 nginx ###停止服務 -1是安全重啟
如果出現: -bash: killall: command not found
[root@localhost nginx-1.12.2]# yum -y install psmisc ###如果出現上面報錯就代表沒有安裝killall功能,yum安裝一下
[root@localhost nginx-1.12.2]# killall -3 nginx ###然后停止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 系統服務
[root@localhost ~]# 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
################下面是刷的#############
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.12.2]# systemctl start nginx ### 啟動nginx
[root@localhost nginx-1.12.2]# systemctl enable nginx ### 開機自啟nginx
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service ### 權限也要給一下
[root@localhost nginx-1.12.2]# yum -y install nfs-utils ### 把nfs裝一下,有的已經裝了的就不用裝了
[root@localhost nginx-1.12.2]# showmount -e 192.168.100.24 ###測驗一下
[root@localhost ~]# mount 192.168.100.24:/opt/51xit /usr/local/nginx/html/ ###臨時掛載,可以直接永久掛載
[root@localhost ~]# vi /etc/fstab ###編輯永久掛載
192.168.100.24:/opt/51xit/ /usr/local/nginx/html/ nfs defaults,_netdev 0 0 ###開機自動掛載,注意格式對齊
[root@localhost nginx-1.12.2]# init6 ###重啟服務器
[root@localhost nginx-1.12.2]# systemctl restart nginx ###重啟nginx
######編譯安裝Nginx服務器2 192.168.100.23 ######
1、編譯安裝 Nginx
Nginx 安裝檔案可以從官方網站 http://www.nginx.org/下載,
下面以穩定版 Nginx 1.12.2為例 上傳至/opt下
[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 ~]# 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 && make install ###編譯安裝
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# nginx ### 啟動nginx
[root@localhost nginx-1.12.2]# netstat -anutp |grep nginx ### 查看啟動情況
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21135/nginx: master
■啟動、 停止 Nginx
[root@localhost nginx-1.12.2]# killall -3 nginx ###停止服務 -1是安全重啟
如果出現: -bash: killall: command not found
[root@localhost nginx-1.12.2]# yum -y install psmisc ###如果出現上面報錯就代表沒有安裝killall功能,yum安裝一下
[root@localhost nginx-1.12.2]# killall -3 nginx ###然后停止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 系統服務
[root@localhost ~]# 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
################下面是刷的#############
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.12.2]# systemctl start nginx ### 啟動nginx
[root@localhost nginx-1.12.2]# systemctl enable nginx ### 開機自啟nginx
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service ### 權限也要給一下
[root@localhost nginx-1.12.2]# yum -y install nfs-utils ### 把nfs裝一下,有的已經裝了的就不用裝了
[root@localhost nginx-1.12.2]# showmount -e 192.168.100.24 ###測驗一下
[root@localhost ~]# mount 192.168.100.24:/opt/51xit /usr/local/nginx/html/ ###臨時掛載,可以直接永久掛載
[root@localhost ~]# vi /etc/fstab ###編輯永久掛載
192.168.100.24:/opt/52xit/ /usr/local/nginx/html/ nfs defaults,_netdev 0 0 ###開機自動掛載,注意格式對齊
[root@localhost nginx-1.12.2]# init 6 ###重啟服務器
[root@localhost nginx-1.12.2]# systemctl restart nginx ###重啟nginx
#####配置Haproxy 服務器 192.168.100.21 #####
1、編譯安裝 Haproxy
上傳 haproxy-1.4.24.tar.gz 到/opt目錄下
[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
2、配置Haproxy 服務
[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
[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 重繪會發現來回切換兩個網站頁面,說明已經實作了負載均衡
注意:不要用7.4版本的做存盤服務器,會出現bug不能共享!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/128751.html
標籤:其他
