溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Haproxy搭建群集

發(fā)布時間:2020-07-22 08:44:02 來源:網(wǎng)絡(luò) 閱讀:303 作者:友引町 欄目:系統(tǒng)運(yùn)維

常見的web集群調(diào)度器

目前常見的web集群調(diào)度器分為軟件和硬件,軟件通常使用開源的LVS,Haproxy,Nginx,硬件一般使用比較多的是F5,也有很多人使用國內(nèi)的一些產(chǎn)品,如梭子魚,綠盟等

Haproxy應(yīng)用分析

LVS在企業(yè)應(yīng)用中抗負(fù)載能力很強(qiáng),但存在不足

LVS不支持正則處理,不能實現(xiàn)動靜分離
對于大型網(wǎng)站,LVS的實施配置復(fù)雜,維護(hù)成本相對較高

Haproxy是一款可提供高可用性,負(fù)載均衡,及基于TCP和HTTP應(yīng)用的代理的軟件

特別適用于負(fù)載特別大的web站點(diǎn)
運(yùn)行在當(dāng)前的硬件上可支持?jǐn)?shù)以萬計的并發(fā)連接連接請求

Haproxy調(diào)度算法原理

RR:最簡單常用的,輪詢調(diào)度

LC:最小連接數(shù)算法,根據(jù)后端的節(jié)點(diǎn)連接數(shù)大小動態(tài)分配前端請求

SH:來源訪問調(diào)度算法,用于有session會話記錄在服務(wù)器端,可以基于來源ip,cookie做群集調(diào)度

Haproxy日志管理

Haproxy的日志默認(rèn)是輸出到系統(tǒng)的syslog中,在生產(chǎn)環(huán)境中一般單獨(dú)定義出來

定義的方法步驟

修改Haproxy配置文件中關(guān)于日志配置的選項,加入配置:

log /devlog local0 info
log /dev/log local0 notice

修改rsyslog配置,將Haproxy相關(guān)的配置獨(dú)立定義到haproxy.conf,并放到/etc/rsyslog.d/下

保存配置文件并重啟rsyslog服務(wù),完成rsyslog配置

隨著企業(yè)網(wǎng)站負(fù)載增加,haproxy參數(shù)優(yōu)化相當(dāng)重要

maxconn:最大連接數(shù),根據(jù)應(yīng)用實際情況進(jìn)行調(diào)整,推薦使用10240
daemon:守護(hù)進(jìn)程模式,Haproxy可以使用非守護(hù)進(jìn)程模式啟動,建議使用守護(hù)進(jìn)程模式啟動
nbproc:負(fù)載均衡的并發(fā)進(jìn)程數(shù),建議與當(dāng)前服務(wù)器CPU核數(shù)相等或為其2倍
retries:重試次數(shù),主要用于對集群節(jié)點(diǎn)的檢查,如果節(jié)點(diǎn)多,且并發(fā)量大,設(shè)置為2次或3次
option http-server-close:主動關(guān)閉http請求選項,建議在生產(chǎn)環(huán)境中使用此選項
timeout http-keep-alive:長連接超時時間,設(shè)置長連接超時時間可以設(shè)置為10s
timeout http-request:http請求超時時間,建議將此時間設(shè)置為5~10s,增加http連接釋放速度
timeout client:客戶端超時時間,如果訪問量過大,節(jié)點(diǎn)響應(yīng)慢可以將此時間設(shè)置短一些,建議設(shè)置為1min左右就可以了

使用Haproxy搭建群集

實驗環(huán)境

Haporxy服務(wù)器:192.168.100.210
Nginx服務(wù)器1:192.168.100.201
Nginx服務(wù)器2:192.168.100.202
client測試機(jī):192.168.100.100

在三臺Linux服務(wù)器上安裝實驗所需環(huán)境包,將四臺虛擬機(jī)的網(wǎng)絡(luò)模式改為僅主機(jī)模式。

Nginx服務(wù)器1配置

1、配置固定IP地址

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #dhcp改為static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=8ecd53ce-afdb-46f8-b7ff-b2f428a3bc8f
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.201 #IP地址
NETMASK=255.255.255.0 #子網(wǎng)掩碼
GATEWAY=192.168.100.1 #網(wǎng)關(guān)
[root@localhost ~]# systemctl restart network #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost ~]# ifconfig #查看網(wǎng)卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.201 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功
inet6 fe80::c776:9d00:618:88f2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:eb:34:07 txqueuelen 1000 (Ethernet)
RX packets 51789 bytes 75688349 (72.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7987 bytes 543001 (530.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

掛載工具包,解壓Nginx源碼包

[root@localhost ~]# mkdir /mnt/tools #創(chuàng)建掛載點(diǎn)
[root@localhost ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ #掛載
Password for root@//192.168.100.100/tools:
[root@localhost ~]# cd /mnt/tools/LNMP/
[root@localhost LNMP]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.gz
mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2
[root@localhost LNMP]# tar zxf nginx-1.12.2.tar.gz -C /opt/ #解壓源碼包
[root@localhost LNMP]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.2 rh
[root@localhost opt]#

編譯安裝Nginx服務(wù)

[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx #創(chuàng)建nginx用戶
[root@localhost nginx-1.12.2]# ./configure \ #配置Nginx服務(wù)

--prefix=/usr/local/nginx \ #安裝路徑
--user=nginx \ #屬主
--group=nginx \ #屬組
--with-http_stub_status_module
#啟用ngx_http_stub_status_module支持(獲取nginx自上次啟動以來的工作狀態(tài))

[root@localhost nginx-1.12.2]# make && make install #編譯安裝Nginx服務(wù)

配置Nginx服務(wù)站點(diǎn)首頁,并開啟服務(wù)

[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html/
[root@localhost html]# echo "this is abc web" > index.html #創(chuàng)建站點(diǎn)首頁
[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #建立軟鏈接
[root@localhost html]# 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
[root@localhost html]# nginx #開啟服務(wù)
[root@localhost html]# netstat -ntap | grep 80 #查看端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18683/nginx: master
[root@localhost html]# systemctl stop firewalld.service #關(guān)閉防火墻
[root@localhost html]# setenforce 0 #關(guān)閉增強(qiáng)型安全功能
[root@localhost html]#

測試訪問Nginx服務(wù)

使用Haproxy搭建群集

Nginx服務(wù)器2配置

配置固定IP地址

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #dhcp改為static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=09073596-a7cf-404c-a098-28c3ff58c44b
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.202 #IP地址
NETMASK=255.255.255.0 #子網(wǎng)掩碼
GATEWAY=192.168.100.1 #網(wǎng)關(guān)
[root@localhost ~]# systemctl restart network #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost ~]# ifconfig #查看網(wǎng)卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.202 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功
inet6 fe80::5134:22f5:842b:5201 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0d:f1:75 txqueuelen 1000 (Ethernet)
RX packets 50790 bytes 74736905 (71.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7366 bytes 466513 (455.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

掛載工具包,解壓Nginx源碼包

[root@localhost ~]# mkdir /mnt/tools #創(chuàng)建掛載點(diǎn)
[root@localhost ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ #掛載
Password for root@//192.168.100.100/tools:
[root@localhost ~]# cd /mnt/tools/LNMP/
[root@localhost LNMP]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.gz
mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2
[root@localhost LNMP]# tar zxf nginx-1.12.2.tar.gz -C /opt/ #解壓源碼包
[root@localhost LNMP]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.2 rh
[root@localhost opt]#

編譯安裝Nginx服務(wù)

[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx #創(chuàng)建nginx用戶
[root@localhost nginx-1.12.2]# ./configure \ #配置Nginx服務(wù)

--prefix=/usr/local/nginx \ #安裝路徑
--user=nginx \ #屬主
--group=nginx \ #屬組
--with-http_stub_status_module
#啟用ngx_http_stub_status_module支持(獲取nginx自上次啟動以來的工作狀態(tài))

[root@localhost nginx-1.12.2]# make && make install #編譯安裝Nginx服務(wù)

配置Nginx服務(wù)站點(diǎn)首頁,并開啟服務(wù)

[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html/
[root@localhost html]# echo "this is xyz web" > index.html #創(chuàng)建站點(diǎn)首頁
[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #建立軟鏈接
[root@localhost html]# 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
[root@localhost html]# nginx #開啟服務(wù)
[root@localhost html]# netstat -ntap | grep 80 #查看端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18683/nginx: master
[root@localhost html]# systemctl stop firewalld.service #關(guān)閉防火墻
[root@localhost html]# setenforce 0 #關(guān)閉增強(qiáng)型安全功能
[root@localhost html]#

測試訪問Nginx服務(wù)

使用Haproxy搭建群集

Haproxy服務(wù)器配置

配置固定IP地址

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #dhcp改為static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=3ceed540-b04c-48d6-a4f7-79951f09ea1d
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.210 #IP地址
NETMASK=255.255.255.0 #子網(wǎng)掩碼
GATEWAY=192.168.100.1 #網(wǎng)關(guān)
[root@localhost ~]# systemctl restart network #重啟網(wǎng)絡(luò)服務(wù)
[root@localhost ~]# ifconfig #查看網(wǎng)卡信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.210 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功
inet6 fe80::3e1d:31ba:f66a:6f80 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:95:9b:1b txqueuelen 1000 (Ethernet)
RX packets 51747 bytes 75871654 (72.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7407 bytes 470471 (459.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

編譯安裝haproxy服務(wù)

[root@localhost ~]# mkdir /mnt/tools #創(chuàng)建掛載點(diǎn)
[root@localhost ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ #掛載
Password for root@//192.168.100.100/tools:
[root@localhost ~]# cd /mnt/tools/MySQL/
[root@localhost MySQL]# ls
amoeba-mysql-binary-2.2.0.tar.gz haproxy-1.5.19.tar.gz mysql-5.5.24.tar.gz
boost_1_59_0.tar.gz jdk-6u14-linux-x64.bin mysql-5.7.17.tar.gz
[root@localhost MySQL]# tar zxf haproxy-1.5.19.tar.gz -C /opt/ #解壓haproxy源碼包
[root@localhost MySQL]# cd /opt/
[root@localhost opt]# ls
haproxy-1.5.19 rh
[root@localhost opt]#
[root@localhost opt]# cd haproxy-1.5.19/
[root@localhost haproxy-1.5.19]# make TARGET=linux26 #編譯haproxy服務(wù)

[root@localhost haproxy-1.5.19]# make install #安裝haproxy服務(wù)

配置haproxy服務(wù)

[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy #創(chuàng)建配置文件目錄
[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/ #模板復(fù)制到配置目錄下
[root@localhost haproxy-1.5.19]# vim /etc/haproxy/haproxy.cfg #編輯配置文件

    chroot /usr/share/haproxy  ##注釋掉

    redispatch  ##注釋掉

#為全局配置
global
log 127.0.0.1 local0 #配置日志記,local0為日志設(shè)備,默認(rèn)存放到系統(tǒng)日志
log 127.0.0.1 local1 notice #notice為 日志級別,通常有24個級別
#log loghost local0 info
maxconn 4096 #最大連接數(shù)
uid 99 #用戶uid
gid 99 #用戶gid
daemon
#debug
#quiet

#defaults配置項配置默認(rèn)參數(shù),一般會被應(yīng)用組件繼承,如果在應(yīng)用組件中沒有特別聲明,將安裝默認(rèn)配置參數(shù)設(shè)置
defaults
log global #定義日志為global配置中的日志定義
mode http #模式為http
option httplog #采用http日志格式記錄日志
option dontlognull #日志不為空
retries 3 #檢查節(jié)點(diǎn)服務(wù)器失敗連續(xù)達(dá)到三次則認(rèn)為節(jié)點(diǎn)不可用
maxconn 2000 #最大連接數(shù)
contimeout 5000 #連接超時時間
clitimeout 50000 #客戶端超時時間
srvtimeout 50000 #服務(wù)器超時時間

listen webcluster 0.0.0.0:80
option httpchk GET /index.html #檢查服務(wù)器的index.html文件
balance roundrobin #負(fù)載均衡調(diào)度算法使用輪詢算法
server inst1 192.168.100.201:80 check inter 2000 fall 3 #定義備份節(jié)點(diǎn),健康檢查請求三次
server inst2 192.168.100.202:80 check inter 2000 fall 3

[root@localhost haproxy-1.5.19]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy #啟動文件
[root@localhost haproxy-1.5.19]# chmod +x /etc/init.d/haproxy #添加執(zhí)行權(quán)限
[root@localhost haproxy-1.5.19]# chkconfig --add haproxy #添加到service
[root@localhost haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/ #便于系統(tǒng)識別
[root@localhost haproxy-1.5.19]#
[root@localhost haproxy-1.5.19]# service haproxy start #開啟服務(wù)
Starting haproxy (via systemctl): [ 確定 ]
[root@localhost haproxy-1.5.19]# systemctl stop firewalld.service #關(guān)閉防火墻
[root@localhost haproxy-1.5.19]# setenforce 0 #關(guān)閉增強(qiáng)型安全功能
[root@localhost haproxy-1.5.19]#

用測試主機(jī)訪問haproxy服務(wù)器IP地址

使用Haproxy搭建群集

使用Haproxy搭建群集

日志定義,修改haproxy配置文件

[root@localhost haproxy-1.5.19]# cd /etc/haproxy/
[root@localhost haproxy]# vim haproxy.cfg
log /dev/log local0 info #添加
log /dev/log local0 notice #添加
#log 127.0.0.1 local0 #注釋
#log 127.0.0.1 local1 notice #注釋
[root@haproxy haproxy]# service haproxy restart ##重啟服務(wù)
[root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf ##創(chuàng)建系統(tǒng)日志haproxy配置文件
[root@haproxy haproxy]# vim /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info') ##根據(jù)級別創(chuàng)建不同的日志文件
then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
[root@haproxy haproxy]# systemctl restart rsyslog.service ##重啟系統(tǒng)日志服務(wù)
[root@haproxy haproxy]# cd /var/log/ ##此時是沒有haproxy日志
##重新訪問網(wǎng)頁
[root@haproxy haproxy]# cd /var/log/haproxy/
[root@haproxy haproxy]# ls ##此時就生成了info級別的日志文件
haproxy-info.log

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI