nginx 1개는 로드밸런스
nginx 2개는 각각 웹서버 역할을 한다.
웹서버 1개는 마지막에 aaaaaaaaaa 문자를 넣어주고
웹서버 1개는 마지막에 bbbbbbbbbb 문자를 넣어준다
localhost:8081 을 호출하면 aaaaaaaaaa 웹서버가 나오고
localhost:8082 을 호출하면 bbbbbbbbbb 웹서버가 나오고
localhost:8080 를 계속 호출하면 aaaaaaaaaa, bbbbbbbbbb가 순차적으로 나오도록 한다.
파일구조
/docker/loadbalance/Dockerfile
/docker/loadbalance/entrypoint.sh
/docker/loadbalance/default
/docker/nginx1_ubuntu/Dockerfile
/docker/nginx1_ubuntu/entrypoint.sh
/docker/nginx2_ubuntu/Dockerfile
/docker/nginx2_ubuntu/entrypoint.sh
/docker/docker-compose.yml
/docker/loadbalance/Dockerfile
FROM ubuntu:18.04
RUN apt update -y
RUN apt install vim -y
RUN apt install net-tools -y
RUN apt install iputils-ping -y
RUN apt install nginx -y
WORKDIR /
COPY ./loadbalance/entrypoint.sh /
RUN chmod 755 /entrypoint.sh
RUN rm -rf /etc/nginx/sites-available/default
COPY ./loadbalance/default /etc/nginx/sites-available/default
ENTRYPOINT ["./entrypoint.sh"]
/docker/loadbalance/entrypoint.sh
#!/bin/bash
sleep 1
service nginx start
sleep 1
/bin/bash
/docker/loadbalance/default
upstream myserver {
server nginx1_ubuntu:80;
server nginx2_ubuntu:80;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
}
/docker/nginx1_ubuntu/Dockerfile
FROM ubuntu:18.04
RUN apt update -y
RUN apt install vim -y
RUN apt install net-tools -y
RUN apt install iputils-ping -y
RUN apt install nginx -y
WORKDIR /
COPY ./nginx1_ubuntu/entrypoint.sh /
RUN chmod 755 /entrypoint.sh
RUN echo "<p><em>aaaaaaaaaa</p></em>" >> /var/www/html/index.nginx-debian.html
ENTRYPOINT ["./entrypoint.sh"]
/docker/nginx1_ubuntu/entrypoint.sh
#!/bin/bash
sleep 1
service nginx start
sleep 1
/bin/bash
/docker/nginx2_ubuntu/Dockerfile
FROM ubuntu:18.04
RUN apt update -y
RUN apt install vim -y
RUN apt install net-tools -y
RUN apt install iputils-ping -y
RUN apt install nginx -y
WORKDIR /
COPY ./nginx2_ubuntu/entrypoint.sh /
RUN chmod 755 /entrypoint.sh
RUN echo "<p><em>bbbbbbbbbb</p></em>" >> /var/www/html/index.nginx-debian.html
ENTRYPOINT ["./entrypoint.sh"]
/docker/nginx2_ubuntu/entrypoint.sh
#!/bin/bash
sleep 1
service nginx start
sleep 1
/bin/bash
/docker/docker-compose.yml
version: '2'
services:
loadbalance:
hostname: loadbalance
container_name: loadbalance
build:
context: .
dockerfile: ./loadbalance/Dockerfile
stdin_open: true
tty: true
ports:
- 8080:80
nginx1_ubuntu:
hostname: nginx1_ubuntu
container_name: nginx1_ubuntu
build:
context: .
dockerfile: ./nginx1_ubuntu/Dockerfile
stdin_open: true
tty: true
ports:
- 8081:80
nginx2_ubuntu:
hostname: nginx2_ubuntu
container_name: nginx2_ubuntu
build:
context: .
dockerfile: ./nginx2_ubuntu/Dockerfile
stdin_open: true
tty: true
ports:
- 8082:80
docker-compose.yml 파일이 있는 위치에서
컨테이너 만드는 명령어 실행
ls
pwd
docker-compose up -d
로컬에서 웹브라우저를 열어서
localhost:8081에 접근
localhost:8082에 접근
localhost:8080에 접근하고
새로고침을 5번 해보자
로드밸런스 역할을 잘 하는 것을 볼 수 있다