Haproxy搭建web群集

版权声明:转载请通知 https://blog.csdn.net/qq_41674452/article/details/84934694

实验准备:
一台Haproxy服务器,两台Nginx服务器,一台客户端(可用本地电脑)


Nginx服务器:
###注:两台都要进行类似操作

 [root@Nginx ~]# yum -y install pcre-devel bzip2-devel zlib-devel
    [root@Nginx ~]# tar -zxvf nginx-1.12.0.tar.gz -C /usr/src/
    [root@Nginx ~]# cd /usr/src/nginx-1.12.0
    [root@Nginx ~]# useradd -M -s /sbin/nologin nginx
    [root@Nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --without-http_gzip_module
    [root@Nginx nginx-1.12.0]# make && make install
    [root@Nginx ~]# cd /usr/local/nginx/html/
    [root@Nginx html]# rm -rf *
    [root@Nginx html]# vim index.html
    <h1>welcome to text1.html</h1>
    :wq!
    [root@Nginx html]# ln -s /usr/local/nginx/sbin/* /usr/local/bin
    [root@Nginx 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
    [root@Nginx ~]# chmod +x /etc/init.d/nginx

Haproxy服务:

[root@Haproxy ~]# yum -y install pcre-devel bzip2-devel
[root@Haproxy ~]# tar -zxvf haproxy-1.5.19.tar.gz
[root@Haproxy ~]# cd haproxy-1.5.19
[root@Haproxy haproxy-1.5.19]# make TARGET=linux26 && make install
[root@Haproxy haproxy-1.5.19]# mkdir /etc/haproxy
[root@Haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
global
[root@Haproxy 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!
[root@Haproxy ~]# cp ~/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy	#添加启动关闭脚本
[root@Haproxy ~]# chmod +x /etc/init.d/haproxy 
[root@Haproxy ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@Haproxy ~]# chkconfig --add /etc/init.d/haproxy 
[root@Haproxy ~]# vim /etc/haproxy/haproxy.cfg 				#配置Haproxy日志
...
    log /dev/log local0 info
    log /dev/log local0 notice
...
:wq!
[root@Haproxy ~]# touch /etc/rsyslog.d/haproxy.conf
[root@Haproxy ~]# 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!
[root@Haproxy ~]# systemctl restart rsyslog.service 
[root@Haproxy ~]# tail -f /var/log/haproxy/haproxy-info.log			#查看日志文件

测试:
客户端多次访问Ha服务器里面的内容发生是其中两台节点服务器的内容;
关掉其中一台节点服务器的Nginx服务,客户端继续访问Ha服务器IP,仍然能访问到另一台节点服务器

猜你喜欢

转载自blog.csdn.net/qq_41674452/article/details/84934694