본문 바로가기

클라우드/도커(Docker)

도커 Docker 로드밸런싱

https://hub.docker.com/_/haproxy

 

haproxy

We and third parties use cookies or similar technologies ("Cookies") as described below to collect and process personal data, such as your IP address or browser information. You can learn more about how this site uses Cookies by reading our privacy policy

hub.docker.com

더보기

# 로드밸런서 컨테이너 생성
vagrant@docker:~$ docker run -d --name lb haproxy:2.4.1

# 컨테이너 상태 확인
vagrant@docker:~$ docker ps
CONTAINER ID   IMAGE   COMMAND   CREATED    STATUS    PORTS     NAMES

-> lb 컨테이너가 실행 되지 않은것을 확인

# 로그 확인
vagrant@docker:~$ docker logs lb
[NOTICE]   (1) : haproxy version is 2.4.1-1ce7d49
[NOTICE]   (1) : path to executable is /usr/local/sbin/haproxy
[ALERT]    (1) : Cannot open configuration file/directory /usr/local/etc/haproxy/haproxy.cfg : No such file or directory

# 상세 설정 확인
vagrant@docker:~$ docker inspect lb
"Cmd":       "/usr/local/etc/haproxy/haproxy.cfg"
-> haproxy.cfg 파일을 실행해야 하는데 없다


# haproxy.cfg 파일 (haproxy 기본 설정 파일) 생성, 작성
vagrant@docker:~$ mkdir haproxy
vagrant@docker:~$ vi haproxy/haproxy.cfg
global
        daemon
        maxconn 1024

defaults
        balance roundrobin
        timeout client 60s
        timeout connect 60s
        timeout server 60s

frontend web-in
        bind *:80
        default_backend web-back

backend web-back
        server s1 web1:80 check
        server s2 web2:80 check

#index.html 파일 생성
vagrant@docker:~$ mkdir index
vagrant@docker:~$ echo "web1" > index/index.html
vagrant@docker:~$ echo "web2" > index/index2.html

# web서버 생성
vagrant@docker:~$ docker run -d --name web1 -v /home/vagrant/index/index.html:/usr/local/apache2/htdocs/index.html httpd
vagrant@docker:~$ docker run -d --name web2 -v /home/vagrant/index/index2.html:/usr/local/apache2/htdocs/index.html httpd


# 기존 컨테이너 제거
# haproxy 디렉토리를 연결한(bind mount) 컨테이너를 새로 생성
vagrant@docker:~$ docker rm -f lb
vagrant@docker:~$ docker run -d --name lb -v /home/vagrant/haproxy/:/usr/local/etc/haproxy/ --link web1:web1
                  --link web2:web2 -p 80:80 haproxy:2.4.1


# 로드밸런싱 확인
vagrant@docker:~$ curl localhost
web1
vagrant@docker:~$ curl localhost
web2