Haproxy是目前比較流行的一種群集調(diào)度工具,同類群集調(diào)度工具有很多,如LVS和Nginx。相比較而言,LVS性能最好,但是搭建相對復(fù)雜;Nginx的upstream模塊支持群集功能,但是相對群集節(jié)點健康檢查功能不強,性能沒有Haproxy好,更多的是應(yīng)用在企業(yè)內(nèi)網(wǎng)環(huán)境中。
搭建LVS群集可以參考博文:Centos 7搭建LVS+Keepalived高可用Web服務(wù)群集
創(chuàng)新互聯(lián)專注于雞西梨樹企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城系統(tǒng)網(wǎng)站開發(fā)。雞西梨樹網(wǎng)站建設(shè)公司,為雞西梨樹等地區(qū)提供建站服務(wù)。全流程按需求定制設(shè)計,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
Nginx群集可以參考博文:基于centos 7安裝Tomcat服務(wù)及其負載均衡
關(guān)于Haproxy搭建Web群集原理概述參考博文:Haproxy搭建Web群集概述
部署環(huán)境如下:
準備工作
1)調(diào)通網(wǎng)絡(luò),防火墻放行相關(guān)流量(我這里直接將防火墻關(guān)閉了);
2)準備系統(tǒng)映像,配置本地yum(自行配置);
3)下載haproxy源碼包,可以從我提供的網(wǎng)盤鏈接下載使用:https://pan.baidu.com/s/1I8JCUhejz0VSe8Q4lhzUpQ
提取碼:th9x
4)web網(wǎng)站使用apache、Nginx、Tomcat搭建都可以,只要可以訪問就行,我這里部署兩臺Nginx和一臺Apache,web網(wǎng)站搭建可以參考:
APache網(wǎng)站服務(wù)配置訪問控制和構(gòu)建虛擬主機
Centos 7部署Nginx網(wǎng)站服務(wù)
基于centos 7安裝Tomcat服務(wù)及其負載均衡
關(guān)于Nginx詳細配置及說明可以參考: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 寫保護,將以只讀方式掛載
[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
[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
[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)在可以使用客戶端訪問Haproxy服務(wù)器的IP地址,第一次訪問到的頁面是第一臺Nginx服務(wù)器,第二次訪問到的頁面會是第二臺Nginx服務(wù)器
我這里就直接配置了,關(guān)于DNS詳細配置及配置項解釋及DNS工作原理概述請參考博文:CentOS7簡單搭建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地址,打開瀏覽器使用域名訪問即可
關(guān)于Apache網(wǎng)站詳細配置及工作原理概述參考博文: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
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)在可以使用客戶端訪問
[root@centos03 ~]# tail -f /var/log/haproxy/haproxy-info.log
—————— 本文至此結(jié)束,感謝閱讀 ——————