新聞中心
Haproxy是目前比較流行的一種群集調(diào)度工具,同類(lèi)群集調(diào)度工具有很多,如LVS和Nginx。相比較而言,LVS性能最好,但是搭建相對(duì)復(fù)雜;Nginx的upstream模塊支持群集功能,但是相對(duì)群集節(jié)點(diǎn)健康檢查功能不強(qiáng),性能沒(méi)有Haproxy好,更多的是應(yīng)用在企業(yè)內(nèi)網(wǎng)環(huán)境中。
搭建LVS群集可以參考博文:Centos 7搭建LVS+Keepalived高可用Web服務(wù)群集
Nginx群集可以參考博文:基于centos 7安裝Tomcat服務(wù)及其負(fù)載均衡
關(guān)于Haproxy搭建Web群集原理概述參考博文:Haproxy搭建Web群集概述
一、部署基于Haproxy搭建高可用Nginx群集
部署環(huán)境如下:
準(zhǔn)備工作
1)調(diào)通網(wǎng)絡(luò),防火墻放行相關(guān)流量(我這里直接將防火墻關(guān)閉了);
2)準(zhǔn)備系統(tǒng)映像,配置本地yum(自行配置);
3)下載haproxy源碼包,可以從我提供的網(wǎng)盤(pán)鏈接下載使用:https://pan.baidu.com/s/1I8JCUhejz0VSe8Q4lhzUpQ
提取碼:th9x
4)web網(wǎng)站使用apache、Nginx、Tomcat搭建都可以,只要可以訪問(wèn)就行,我這里部署兩臺(tái)Nginx和一臺(tái)Apache,web網(wǎng)站搭建可以參考:
APache網(wǎng)站服務(wù)配置訪問(wèn)控制和構(gòu)建虛擬主機(jī)
Centos 7部署Nginx網(wǎng)站服務(wù)
基于centos 7安裝Tomcat服務(wù)及其負(fù)載均衡
1、部署第一臺(tái)Nginx網(wǎng)站
關(guān)于Nginx詳細(xì)配置及說(shuō)明可以參考:Centos 7部署Nginx網(wǎng)站服務(wù)
[root@centos01 ~]# yum -y install prce-devel zlib-devel
[root@centos01 ~]# useradd -M -s /sbin/nologin nginx
[root@centos01 ~]# umount /mnt/
[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 寫(xiě)保護(hù),將以只讀方式掛載
[root@centos01 ~]# scp /mnt/nginx-1.6.0.tar.gz root@192.168.100.20:/root
The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts.
root@192.168.100.20's password:
nginx-1.6.0.tar.gz 100% 784KB 68.2MB/s 00:00
[root@centos01 ~]# scp /mnt/haproxy-1.4.24.tar.gz root@192.168.100.30:/root
The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.30' (ECDSA) to the list of known hosts.
root@192.168.100.30's password:
haproxy-1.4.24.tar.gz 100% 817KB 31.1MB/s 00:00 00:00
[root@centos01 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/
[root@centos01 ~]# cd /usr/src/nginx-1.6.0/
[root@centos01 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx
--group=nginx --with-http_stub_status_module
[root@centos01 nginx-1.6.0]# make && make install
[root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos01 ~]# echo "192.168.100.10:nginx" > /usr/local/nginx/html/index.html
[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3685/ngin: master
2、部署第二臺(tái)Nginx網(wǎng)站
[root@centos02 ~]# yum -y install pcre-devel zlib-devel
[root@centos02 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx-1.6.0.tar.gz
[root@centos02 ~]# tar zxvf nginx-1.6.0.tar.gz -C /usr/src/
[root@centos02 ~]# useradd -M -s /sbin/nologin nginx
[root@centos02 ~]# cd /usr/src/nginx-1.6.0/
[root@centos02 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx
--group=nginx --with-http_stub_status_module
[root@centos02 nginx-1.6.0]# make && make install
[root@centos02 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos02 ~]# echo "192.168.100.20:nginx" > /usr/local/nginx/html/index.html
[root@centos02 ~]# nginx
[root@centos02 ~]# netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6059/ngin: master
3、配置客戶端
1)客戶端添加VM1網(wǎng)卡,配置和服務(wù)器同網(wǎng)段IP地址
2)訪問(wèn)第一臺(tái)nginx服務(wù)器
3)訪問(wèn)第二臺(tái)nginx服務(wù)器
4、部署Haproxy服務(wù)器
[root@centos03 ~]# yum -y install pcre-devel bzip2-devel
[root@centos03 ~]# ls
anaconda-ks.cfg haproxy-1.4.24.tar.gz initial-setup-ks.cfg
[root@centos03 ~]# tar zxvf haproxy-1.4.24.tar.gz -C /usr/src/
[root@centos03 ~]# cd /usr/src/haproxy-1.4.24/
[root@centos03 haproxy-1.4.24]# make install
[root@centos03 ~]# mkdir /etc/haproxy
[root@centos03 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@centos03 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@centos03 ~]# chmod +x /etc/init.d/haproxy
[root@centos03 ~]# chkconfig --add haproxy
[root@centos03 ~]# chkconfig --level 35 haproxy on
[root@centos03 ~]# cp /usr/src/haproxy-1.4.24/haproxy /usr/sbin/
[root@centos03 ~]# mkdir -p /usr/share/haproxy
[root@centos03 ~]# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 10
clitimeout 10
srvtimeout 10
listen nginx 192.168.100.30:80
balance roundrobin
server web01 192.168.100.10:80 check inter 2000 fall 3
server web02 192.168.100.20:80 check inter 2000 fall 3
[root@centos03 ~]# /etc/init.d/haproxy start
Starting haproxy (via systemctl): [ 確定 ]
至此Haproxy群集部署完成,現(xiàn)在可以使用客戶端訪問(wèn)Haproxy服務(wù)器的IP地址,第一次訪問(wèn)到的頁(yè)面是第一臺(tái)Nginx服務(wù)器,第二次訪問(wèn)到的頁(yè)面會(huì)是第二臺(tái)Nginx服務(wù)器
5、部署DNS
我這里就直接配置了,關(guān)于DNS詳細(xì)配置及配置項(xiàng)解釋及DNS工作原理概述請(qǐng)參考博文:CentOS7簡(jiǎn)單搭建DNS服務(wù)
[root@centos03 ~]# yum -y install bind bind-chroot bind-utils
[root@centos03 ~]# echo "" > /etc/named.conf
[root@centos03 ~]# vim /etc/named.conf
options {
listen-on port 53 { 192.168.100.0/24; };
directory "/var/named";
};
zone bdqn.com IN {
type master;
file "bdqn.com.zone";
};
[root@centos03 ~]# named-checkconf -z /etc/named.conf
[root@centos03 ~]# vim /var/named/bdqn.com.zone
$TTL 86400
@ SOA bdqn.com. root.bdqn.com.(
2019112201
1H
15M
1W
1D
)
@ NS centos03.bdqn.com.
centos03 A 192.168.100.30
www A 192.168.100.30
[root@centos03 ~]# named-checkzone bdqn.com /var/named/bdqn.com.zone
zone bdqn.com/IN: loaded serial 2019112201
OK
[root@centos03 ~]# chmod +x /var/named/bdqn.com.zone
[root@centos03 ~]# chown named:named /var/named/bdqn.com.zone
[root@centos03 ~]# systemctl start named
[root@centos03 ~]# systemctl enable named
至此DNS已經(jīng)配置完成,客戶端添加DNS地址,打開(kāi)瀏覽器使用域名訪問(wèn)即可
二、使用Haproxy搭建Apache網(wǎng)站群集
1、部署apache網(wǎng)站
關(guān)于Apache網(wǎng)站詳細(xì)配置及工作原理概述參考博文:CentOS 7.4搭建Apache網(wǎng)站服務(wù)
[root@centos04 ~]# tar zxvf /mnt/httpd-2.2.17.tar.gz -C /usr/src/
[root@centos04 ~]# cd /usr/src/httpd-2.2.17/
[root@centos04 httpd-2.2.17]# ./configure --prefix=/usr/local/httpd --enable-so
–enable-rewrite --enable-charset-lite --enable-cgi
[root@centos04 httpd-2.2.17]# make && make install
[root@centos04 ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@centos04 ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@centos04 ~]# chmod +x /etc/init.d/httpd
[root@centos04 ~]# vim /etc/init.d/httpd
#!/bin/sh
#chkconfig 35 80 20
#description:apache server
[root@centos04 ~]# chkconfig --add httpd
[root@centos05 ~]# echo "192.168.100.40:apache" > /usr/local/httpd/htdocs/index.html
[root@centos04 ~]# apachectl -t
[root@centos04 ~]# systemctl start httpd
[root@centos04 ~]# systemctl enable httpd
[root@centos04 ~]# netstat -anptu | grep httpd
tcp6 0 0 :::80 :::* LISTEN 53801/httpd
[root@centos04 ~]# systemctl is-enabled httpd.service
enabled
2、部署Haproxy服務(wù)
listen nginx 192.168.100.30:80
balance roundrobin
server web01 192.168.100.10:80 check inter 2000 fall 3
server web02 192.168.100.20:80 check inter 2000 fall 3
server apache01 192.168.100.40:80 check inter 2000 fall 3 weight 1
[root@centos03 ~]# /etc/init.d/haproxy restart
Restarting haproxy (via systemctl): [ 確定 ]
至此基于Apache網(wǎng)站群集已經(jīng)部署完成,現(xiàn)在可以使用客戶端訪問(wèn)
[root@centos03 ~]# tail -f /var/log/haproxy/haproxy-info.log
—————— 本文至此結(jié)束,感謝閱讀 ——————
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:Centos7基于Haproxy搭建高可用Web群集-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://www.ef60e0e.cn/article/dsjdpe.html