Haproxy 웹 클러스터 구축

실험 준비: Haproxy 서버 1대, Nginx 서버 2대, 클라이언트 1대(로컬 컴퓨터 사용)
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
    

welcome to text1.html

: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에 계속 액세스하여 다른 노드 서버에 액세스할 수 있음

좋은 웹페이지 즐겨찾기