1. 程式人生 > >Haproxy搭建web群集

Haproxy搭建web群集

實驗準備:
一臺Haproxy伺服器,兩臺Nginx伺服器,一臺客戶端(可用本地電腦)


Nginx伺服器:
###注:兩臺都要進行類似操作

 [[email protected] ~]# yum -y install pcre-devel bzip2-devel zlib-devel
    [[email protected] ~]# tar -zxvf nginx-1.12.0.tar.gz -C /usr/src/
    [[email protected] ~]# cd /usr/src/nginx-1.12.0
    [[email protected]
~]# useradd -M -s /sbin/nologin nginx [[email protected] nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --without-http_gzip_module [[email protected] nginx-1.12.0]# make && make install [[email protected] ~]# cd /usr/local/nginx/html/ [
[email protected]
html]# rm -rf * [[email protected] html]# vim index.html <h1>welcome to text1.html</h1> :wq! [[email protected] html]# ln -s /usr/local/nginx/sbin/* /usr/local/bin [[email protected] html]# vim /etc/init.d/nginx #!/bin/bash #chkconfig: - 99 20 PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) kill -s HUP $(cat $PIDF_FPM) ;; *) echo "Usage: $0 { start | stop | restart | reload }" exit 1 ;; esac exit 0 [
[email protected]
~]# chmod +x /etc/init.d/nginx

Haproxy服務:

[[email protected] ~]# yum -y install pcre-devel bzip2-devel
[[email protected] ~]# tar -zxvf haproxy-1.5.19.tar.gz
[[email protected] ~]# cd haproxy-1.5.19
[[email protected] haproxy-1.5.19]# make TARGET=linux26 && make install
[[email protected] haproxy-1.5.19]# mkdir /etc/haproxy
[[email protected] haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
global
[[email protected] haproxy-1.5.19]# vim /etc/haproxy/haproxy.cfg 
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
        log 127.0.0.1 local0
        log 127.0.0.1 local1 notice
        maxconn 4096
        uid 99
        gid 99
        daemon
defaults
        log global
        mode http
        option httplog
        option dontlognull
        retries 3
        maxconn 4096
        contimeout 5000
        clitimeout 50000
        srvtimeout 50000
listen webcluster 0.0.0.0:80
        option httpchk GET /index.html
        balance roundrobin
        server inst1 192.168.3.1:80 check inter 2000 fall 3
        server inst2 192.168.3.2:80 check inter 2000 fall 3
:wq!
[[email protected] ~]# cp ~/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy	#新增啟動關閉指令碼
[[email protected] ~]# chmod +x /etc/init.d/haproxy 
[[email protected] ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[[email protected] ~]# chkconfig --add /etc/init.d/haproxy 
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 				#配置Haproxy日誌
...
    log /dev/log local0 info
    log /dev/log local0 notice
...
:wq!
[[email protected] ~]# touch /etc/rsyslog.d/haproxy.conf
[[email protected] ~]# vim /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~
:wq!
[[email protected] ~]# systemctl restart rsyslog.service 
[[email protected] ~]# tail -f /var/log/haproxy/haproxy-info.log			#檢視日誌檔案

測試:
客戶端多次訪問Ha伺服器裡面的內容發生是其中兩臺節點伺服器的內容;
關掉其中一臺節點伺服器的Nginx服務,客戶端繼續訪問Ha伺服器IP,仍然能訪問到另一臺節點伺服器