[개발자가 하는 일 8] 로드밸런싱을 위한 Haproxy 디자인 및 개발
2016.09.27. 11:01

[개발자가 하는 일 8] 로드밸런싱을 위한 Haproxy


이전화에 도메인에 대해 알아보았습니다. 


이제 도메인과 IP를 연결(각 도메인 회사마다 조금씩 차이가 있어 생략합니다)


각 회사의 DNS 설정을 통해 진행하였습니다.


이제 mobileflow.co.kr 로 접속하게 되면 특정 IP로 이동하도록 DNS설정을 해두었습니다.


예를 들어 우리가 mobileflow.co.kr 을 치게되면 아래 그림과 같은 흐름으로 이동하게 됩니다.




그동안 설치했던 익숙한 그림들을 바탕으로 처음 보는 프로그램이 하나 있습니다.


바로 Haproxy입니다.  Haproxy에 대한 개념은 아래의 주소가 정리가 잘되어 있습니다.

http://d2.naver.com/helloworld/284659



Haproxy에 대한 개념은 위의 링크를 참고하고 설치는 간단한 Yum으로 진행하겠습니다.


1) Haproxy Yum 설치

[root@mobileflow ~]# yum -y install haproxy

2) 설치 위치 확인

[root@mobileflow ~]# whereis haproxy

haproxy: /usr/sbin/haproxy /etc/haproxy /usr/local/sbin/haproxy /usr/share/haproxy /usr/share/man/man1/haproxy.1.gz

3) 설정 파일 확인

[root@mobileflow haproxy]# ls /etc/haproxy

haproxy.cfg

4) 설정 수정

[root@mobileflow haproxy]# vi /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------

#   가이드 문서 주소

#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt

#

#---------------------------------------------------------------------


#---------------------------------------------------------------------

# Global settings

#---------------------------------------------------------------------

global

    # to have these messages end up in /var/log/haproxy.log you will

    # need to:

    #


    log         127.0.0.1 local2   #로그설정


    chroot      /var/lib/haproxy

    pidfile     /var/run/haproxy.pid

    maxconn     4000

    user        haproxy

    group       haproxy

    daemon


    # turn on stats unix socket

    stats socket /var/lib/haproxy/stats


#---------------------------------------------------------------------

# common defaults that all the 'listen' and 'backend' sections will

# use if not designated in their block

#---------------------------------------------------------------------

defaults

    mode                    http

    log                       global

    option                  httplog

    option                  dontlognull

    option http-server-close

    option forwardfor       except 127.0.0.0/8

    option                  redispatch

    retries                 3

    timeout http-request    10s

    timeout queue           1m

    timeout connect         10s

    timeout client          1m

    timeout server          1m

    timeout http-keep-alive 10s

    timeout check           10s

    maxconn                 3000       #최대 동접 설정


#---------------------------------------------------------------------

# main frontend which proxys to the backends

#---------------------------------------------------------------------

frontend  PassThrough1 #

    acl url_static       path_beg       -i /static /images /javascript /stylesheets /css /js /imgs

    acl url_static       path_end       -i .jpg .gif .png .css .js


    use_backend static          if url_static

    default_backend             static


frontend http-in 

    mode  http

    bind *:80

    option httplog

    acl host_sub1 hdr(host) -i mobileflow-homepage.s3.ap-northeast-2.amazonaws.com/uploads/img/www

    acl host_sub3 hdr(host) -i www.mobileflow.co.kr

    acl host_sub5 hdr(host) -i demo.mobileflow.co.kr

    use_backend static     if host_sub1

    use_backend main_app   if host_sub3

    use_backend demo_app   if host_sub5

        stats enable                                 #통계

        stats hide-version

        stats uri /stats

        stats realm Haproxy\ Statistics

        stats auth haproxy:admin

    default_backend             main_app  #기본 설정

#---------------------------------------------------------------------

# static backend for serving up images, stylesheets and such

#---------------------------------------------------------------------

backend static  #이미지 서버 설정

    balance     roundrobin

    server      static 127.0.0.1:4000 check 


#---------------------------------------------------------------------

# round robin balancing between the various backends

#---------------------------------------------------------------------

backend www_cluster  #mobileflow.co.kr 접속하면 www 이동시켜준다.

   option forwardfor except 127.0.0.1 header My-X-Forwarded-For 

    redirect prefix http://www.mobileflow.co.kr code 301 if { hdr(host) -i mobileflow.co.kr }



backend main_app  #메인 어플리케이션 서버

    option forwardfor except 127.0.0.1 header My-X-Forwarded-For

    balance     roundrobin

    server  app1 127.0.0.1:8080 check


backend demo_app #데모서버

    balance     roundrobin

    server  app1 127.0.0.1:8081 check

5) Haproxy 시작

[root@mobileflow haproxy]# service haproxy start
Starting haproxy: [ OK ]
이후 들어가서 접속해보면 정상적으로 사이트가 연결되어 나옵니다

Haproxy 설정에 통계 설정을 해두면 아래와 같이 통계도 확인 할 수 있습니다.



Haproxy를 왜 설치 할까요?

저는 두가지 목적으로 Haproxy를 설치했습니다.

첫째로는 이미지 서버와,어플리케이션 서버,데이터 베이스 서버의 의 분리를 위함
둘째로는 추후 부하 분산에 대한 대비

이렇게 프로그램을 미리 설치해 두면 서버에 문제발생시 
부하가 일어나는 곳을 찾아 서버를 증설하거나 서버를 분할시에 대처 시간이 줄어 들기 때문입니다.

이제 서버에 대한 기본적인 설치 및 셋팅을 진행하였습니다.



이전 내용은 이곳에서 보시면 됩니다.


 

참조

개발의뢰