天天看點

Haproxy搭建Web群集——超詳細的理論+實驗!!!一、常見的WEB叢集排程器二、Haproxy應用分析三、Haproxy排程算法原理四、使用Haproxy搭建Web群集

Haproxy搭建Web群集

  • 一、常見的WEB叢集排程器
  • 二、Haproxy應用分析
  • 三、Haproxy排程算法原理
    • 3.1、RR (Round Robin)
    • 3.2、LC (Least Connections)
    • 3.3、SH(Source Hashing)
  • 四、使用Haproxy搭建Web群集
    • 4.1、環境配置
    • 4.2、存儲伺服器 192.168.100.24
    • 4.2、Nginx伺服器1 192.168.100.22
    • 4.3、Nginx伺服器2 192.168.100.23
    • 4.4、Haproxy 伺服器 192.168.100.21
    • 4.5、驗證Haproxy搭建Web群集

一、常見的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伺服器,兩台Nginx伺服器,一台共享存儲(NFS)伺服器
主機 IP位址 作業系統 主要軟體
Haproxy伺服器 CentOS 7.6 x86_64 192.168.100.21 haproxy-1.4.24.tar
Nginx伺服器1 CentOS 7.6 x86_64 192.168.100.22 nginx-1.12.2.tar
Nginx伺服器2 CentOS 7.6 x86_64 192.168.100.23 nginx-1.12.2.tar
NFS伺服器 CentOS 7.6 x86_64 192.168.100.24 nfs-utils,rpcbind
  • 在做這個實驗的時候,必須先關閉所有伺服器的防火牆和核心防護!
[[email protected] examples]# systemctl stop firewalld
[[email protected] examples]# systemctl disable firewalld
[[email protected] examples]# vi /etc/selinux/config 
SELINUX=disabled   ## 關閉核心防護
= >> wq 儲存
           

4.2、存儲伺服器 192.168.100.24

## 做共享目錄實驗時,最好不要選擇7.4版本,會有很多bug
[[email protected] ~]# yum -y install rpcbind nfs-utils
[[email protected] ~]# systemctl start rpcbind   ## 啟動時,先啟動rpcbind
[[email protected] ~]# systemctl start nfs

[[email protected] ~]# vi /etc/exports  ## 做共享網段的目錄
/opt/Tom 192.168.100.0/24(rw,sync)
/opt/Jack 192.168.100.0/24(rw,sync)

## 設定重新開機和開機自啟rpcbind,nfs-utils
[[email protected] ~]# systemctl restart rpcbind
[[email protected] ~]# systemctl restart nfs
[[email protected] ~]# systemctl enable rpcbind
[[email protected] ~]# systemctl enable nfs

## 建立共享的首頁,并給首頁插入不同的内容,可以讓我們更直覺的看見後面的負載輪詢頁面
[[email protected] ~]# mkdir /opt/Tom /opt/Jack
[[email protected] ~]# echo "this is www.Tom.com" >/opt/Tom/index.html
[[email protected] ~]# echo "this is www.Jack.com" >/opt/Jack/index.html
           

4.2、Nginx伺服器1 192.168.100.22

  • 編譯安裝 Nginx,我們首先把nginx軟體包上傳至/opt下,可以用xftp或者别的軟體上傳(軟體包在我上傳的資源中)
[[email protected] ~]#yum -y install pcre-devel zlib-devel gcc-c++
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
[[email protected] ~]# cd /opt
[[email protected] ~]# tar zxvf nginx-1.12.2.tar.gz
[[email protected] ~]# cd nginx-1.12.2
[[email protected] nginx-1.12.2]# 
./configure \
--prefix=/usr/local/nginx \    ## 指定安裝路徑
--user=nginx \   ## 指定使用者賬号
--group=nginx    ## 指定組賬戶

[[email protected] nginx-1.12.2]# make && make install   ## 進行編譯安裝
[[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/   ## 做軟連接配接,并檢視
[[email protected] 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
           
  • Nginx 的運作控制

■檢查配置檔案

與 Apache 的主程式 httpd 類似, Nginx 的主程式也提供了“-t”選項用來對配置檔案進行

檢查, 以便找出不當或錯誤的配置。 配置檔案 nginx.conf 預設位于安裝目錄下的 conf/子目

錄中。 若要檢查位于其他位置的配置檔案, 可使用“-c”選項來指定路徑

[[email protected] ~]# 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
[[email protected] ~]# yum -y install psmisc    ## 安裝一下這條指令就好了
[[email protected] ~]# nginx                                ####啟動
[[email protected] ~]# yum install net-tools
[[email protected] ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
7180/nginx: master
           
  • 添加Nginx系統服務
■添加 Nginx 系統服務
[[email protected] ~]# vi /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
= >> wq 儲存

[[email protected] ~]# chmod 754 /lib/systemd/system/nginx.service
[[email protected] ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to
/usr/lib/systemd/system/nginx.service.
這樣一來, 就可以 systemctl 指令來啟動、 停止、 重新開機、 重載 Nginx 伺服器了, 方法是
在執行時添加相應的 start、 stop、 restart、 reload 參數

#######刷下指令,服務正常工作了#########
[[email protected] ~]# killall -3 nginx                                                     ###停止服務
[[email protected] ~]# systemctl start nginx.service
[[email protected] ~]# systemctl enable nginx.service
           
  • 安裝httpd 挂載測試頁
[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# showmount -e 192.168.100.24     ####如果還沒釋出,請到存儲伺服器釋出下,exportfs -rv
Export list for 192.168.100.24:
/opt/Tom (everyone)
/opt/Jack  (everyone)

[[email protected] ~]# vi /etc/fstab 
192.168.100.24:/opt/Tom/ /usr/local/nginx/html/ nfs     defaults,_netdev     0 0        ###開機自動挂載,注意格式對齊

[[email protected] nginx-1.12.2]# systemctl start nfs  ## 開啟nfs
[[email protected] nginx-1.12.2]# systemctl enable nfs  ## 設定開機自啟nfs
[[email protected] nginx-1.12.2]# init 6   ## 重新開機測試一下測試頁
[[email protected] ~]# curl 192.168.100.22   ## 可以使用這種方法驗證
this is www.Tom.com
## 也可以在浏覽輸入 192.168.100.22 測試一下
           

4.3、Nginx伺服器2 192.168.100.23

前面的步驟和Nginx伺服器 1 是一樣的

  • 安裝httpd 挂載測試頁
[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# showmount -e 192.168.100.24     ####如果還沒釋出,請到存儲伺服器釋出下,exportfs -rv
Export list for 192.168.100.24:
/opt/Tom  (everyone)
/opt/Jack (everyone)
 
[[email protected] ~]# vi /etc/fstab 
192.168.100.24:/opt/Jack/ /usr/local/nginx/html/ nfs     defaults     0 0        ###開機自動挂載,注意格式對齊

[[email protected] ~]# systemctl start nfs
[[email protected] ~]# systemctl enable nfs
[[email protected] nginx-1.12.2]# init 6

[[email protected] ~]# curl 192.168.100.23
this is www.Jack.com
## 也可以在浏覽輸入 192.168.100.23 測試一下
           

4.4、Haproxy 伺服器 192.168.100.21

  • 編譯安裝 Haproxy ,上傳 haproxy-1.4.24.tar.gz 到/opt目錄下
[[email protected] ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++   ## 安裝所需環境
[[email protected] ~]# cd /opt
[[email protected] opt]# tar xzvf haproxy-1.4.24.tar.gz 
[[email protected] opt]# cd haproxy-1.4.24/
[[email protected] haproxy-1.4.24]# make TARGET=linux26
[[email protected] haproxy-1.4.24]# make install
           
  • 配置Haproxy 服務
[[email protected] haproxy-1.4.24]# mkdir /etc/haproxy
[[email protected] haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[[email protected] haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg 
100dd ,把裡面的配置檔案都删除!輸入下面代碼!
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        ## 這裡IP改成節點伺服器 1 的IP
        server inst2 192.168.100.23:80 check inter 2000 fall 3        ## 這裡IP改成節點伺服器 2 的IP
        
[[email protected] haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy 
[[email protected] haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy
[[email protected] haproxy-1.4.24]# chkconfig --add haproxy
[[email protected] haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[[email protected] haproxy-1.4.24]# service haproxy start 

[[email protected] haproxy-1.4.24]# systemctl start haproxy.service 
[[email protected] haproxy-1.4.24]# systemctl enable haproxy.service 
           

4.5、驗證Haproxy搭建Web群集

  • 在浏覽器輸入 192.168.100.21驗證是否輪詢頁面。
  • 驗證成功!!!
Haproxy搭建Web群集——超詳細的理論+實驗!!!一、常見的WEB叢集排程器二、Haproxy應用分析三、Haproxy排程算法原理四、使用Haproxy搭建Web群集
Haproxy搭建Web群集——超詳細的理論+實驗!!!一、常見的WEB叢集排程器二、Haproxy應用分析三、Haproxy排程算法原理四、使用Haproxy搭建Web群集

繼續閱讀