클라우드 + DevOps/Docker

Docker 네트워크 실습 :: NginX 컨테이너 리버스 프록시 구성

gamjadori 2024. 2. 22. 17:14
728x90

<실습4 :: NginX 컨테이너 리버스 프록시 구성>

 

1. 컨테이너 구성 (nginx): proxy-container

ubuntu@host1:~/Labs/ch05$ docker container run -d -p 5000:80 --name=proxy-container nginx:1.25.0-alpine
Unable to find image 'nginx:1.25.0-alpine' locally
1.25.0-alpine: Pulling from library/nginx
f56be85fc22e: Pull complete 
97c80f11709c: Pull complete 
afb503c1f124: Pull complete 
f8c948b732dd: Pull complete 
d021bba29710: Pull complete 
cadcca1af197: Pull complete 
4aacde79cec4: Pull complete 
Digest: sha256:2e776a66a3556f001aba13431b26e448fe8acba277bf93d2ab1a785571a46d90
Status: Downloaded newer image for nginx:1.25.0-alpine
9a8418c7ee8e86ffe6f315feb4936d7b631cb21091e88f5bff52978b8bada1f8

<생성 확인>

ubuntu@host1:~/Labs/ch05$ docker container ps
CONTAINER ID   IMAGE                 COMMAND                   CREATED         STATUS         PORTS                                   NAMES
**9a8418c7ee8e   nginx:1.25.0-alpine   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   0.0.0.0:5000->80/tcp, :::5000->80/tcp   proxy-container**
4e49879f5e7f   nginx                 "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:8003->80/tcp, :::8003->80/tcp   alb-node03
5616dd359296   nginx                 "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:8002->80/tcp, :::8002->80/tcp   alb-node02
ccd6c942f9f5   nginx                 "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:8001->80/tcp, :::8001->80/tcp   alb-node01

 

2. curl을 이용한 컨테이너 확인

ubuntu@host1:~/Labs/ch05$ curl 192.168.56.101:8002
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

ubuntu@host1:~/Labs/ch05$ curl 192.168.56.101:5000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

3. proxy-container로 nginx.conf 이동

ubuntu@host1:~/Labs/ch05$ docker container cp ./nginx.conf proxy-container:/etc/nginx/
Successfully copied 2.05kB to proxy-container:/etc/nginx/

<적용>

ubuntu@host1:~/Labs/ch05$ docker container restart proxy-container
proxy-container

<재구동 상태 확인>

ubuntu@host1:~/Labs/ch05$ docker container ps
CONTAINER ID   IMAGE                 COMMAND                   CREATED         STATUS         PORTS                                   NAMES
9a8418c7ee8e   nginx:1.25.0-alpine   "/docker-entrypoint.…"   3 minutes ago   Up 5 seconds   0.0.0.0:5000->80/tcp, :::5000->80/tcp   proxy-container
4e49879f5e7f   nginx                 "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:8003->80/tcp, :::8003->80/tcp   alb-node03
5616dd359296   nginx                 "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:8002->80/tcp, :::8002->80/tcp   alb-node02
ccd6c942f9f5   nginx                 "/docker-entrypoint.…"   2 hours ago     Up 2 hours     0.0.0.0:8001->80/tcp, :::8001->80/tcp   alb-node01

 

4. 로드밸런싱 확인

  • curl localhost:5000을 하면 웹서버 1~3이 랜덤하게 나옴

 

<심화: 로드밸런싱 부하량 설정>

ubuntu@host1:~/Labs/ch05$ vi nginx.conf
  • weigt=(숫자); 을 추가하면 웹서버 부하량을 다르게 설정할 수 있음

<nginx.conf 적용>

ubuntu@host1:~/Labs/ch05$ docker container cp ./nginx.conf proxy-container:/usr/share/nginx/
Successfully copied 2.05kB to proxy-container:/usr/share/nginx/
ubuntu@host1:~/Labs/ch05$ docker container restart proxy-container
proxy-container
ubuntu@host1:~/Labs/ch05$ curl localhost:5000
>> curl localhost:5000을 하면 웹서버 1~3의 8001 = 8003 > 8002의 순으로 호출됨