본문 바로가기

리눅스

Docker Swarm 클러스터에 서비스를 배포하고 업데이트하며 전역 스케일을 조절하는 방법

반응형

Docker Swarm 클러스터에 서비스를 배포하고 업데이트하며 전역 스케일을 조절하는 방법

Docker Swarm 클러스터에서 서비스를 배포하고 업데이트하며 전역 스케일을 조절하고, 마지막으로 서비스와 스택을 삭제하는 과정입니다.

Docker Swarm 초기화

docker swarm init

1. Docker Swarm 클러스터에 서비스 배포

  • Dockerfile 작성
vim Dockerfile
# 베이스 이미지 정의
FROM nginx:latest

# 작업 디렉토리 설정
WORKDIR /usr/share/nginx/html

# 복사할 파일 추가 (예: index.html)
COPY index.html .

# 노출할 포트 설정
EXPOSE 80

# 컨테이너 시작 시 실행할 명령어 (예: Nginx 시작)
CMD ["nginx", "-g", "daemon off;"]
  • index.html 작성
vim index.html
<html>
        <body>
                <h1>Web Server : Version 0.0.1</h1>
        </body>
</html>
  • 이미지 빌드
docker buildx create --use
$ docker buildx create --use
elated_mcnulty
docker buildx build --platform linux/amd64,linux/arm64 --tag anti1346/mynginx:v0.0.1 --no-cache --push .
$ docker buildx build --platform linux/amd64,linux/arm64 --tag anti1346/mynginx:v0.0.1 --no-cache --push .
[+] Building 43.1s (15/15) FINISHED                                                                                 
 => [internal] booting buildkit                                                                                     3.4s
 => => pulling image moby/buildkit:buildx-stable-1                                                                  2.0s
 => => creating container buildx_buildkit_elated_mcnulty0                                                           1.4s
 => [internal] load build definition from Dockerfile                                                                0.3s
 => => transferring dockerfile: 348B                                                                                0.0s
 => [linux/arm64 internal] load metadata for docker.io/library/nginx:latest                                         3.4s
 => [linux/amd64 internal] load metadata for docker.io/library/nginx:latest                                         3.3s
 => [auth] library/nginx:pull token for registry-1.docker.io                                                        0.0s
 => [internal] load .dockerignore                                                                                   0.3s
 => => transferring context: 2B                                                                                     0.0s
 => [internal] load build context                                                                                   0.3s
 => => transferring context: 135B                                                                                   0.0s
 => [linux/amd64 1/3] FROM docker.io/library/nginx:latest@sha256:80cff90527ee35ef185571e1c327beac                  17.4s
 => [linux/amd64 2/3] WORKDIR /usr/share/nginx/html                                                                 4.6s
 => [linux/amd64 3/3] COPY index.html .                                                                             0.8s
 => [linux/arm64 2/3] WORKDIR /usr/share/nginx/html                                                                 1.1s
 => [linux/arm64 3/3] COPY index.html .                                                                             0.4s
 => exporting to image                                                                                              7.7s
 => => exporting layers                                                                                             2.0s
 => => exporting manifest sha256:8d6ef872708a460dd114e0cd16b23ea1682cc0450a1a595aa5b243be329417bc                   0.1s
 => => exporting config sha256:9256de5c19cc842e626bb084fa3c18ea473e31fb841f47955f88ac18f3e4d36e                     0.1s
 => => exporting manifest sha256:8073bc22a6d790aeac27f9e751880e9b5e9242ba89d846a0f7004b4f758f3e01                   0.1s
 => => exporting config sha256:10d382c7dc0e36574a485d2de101e122cb5f8865b04765659758e8c18b1413bf                     0.1s
 => => exporting manifest list sha256:dedaf51bddb54861b3450788261fc                                                 0.1s
 => => pushing layers                                                                                               3.4s
 => => pushing manifest for docker.io/anti1346/mynginx:v0.0.1@sha256:dedaf51bddb54861b3450788261fc                  1.7s
 => [auth] anti1346/mynginx:pull,push token for registry-1.docker.io                                                0.0s
더보기

---

$ docker buildx build --platform linux/amd64,linux/arm64 --tag anti1346/mynginx:v0.0.1 --no-cache --push .
[+] Building 43.1s (15/15) FINISHED                                                                                 
 => [internal] booting buildkit                                                                                     3.4s
 => => pulling image moby/buildkit:buildx-stable-1                                                                  2.0s
 => => creating container buildx_buildkit_elated_mcnulty0                                                           1.4s
 => [internal] load build definition from Dockerfile                                                                0.3s
 => => transferring dockerfile: 348B                                                                                0.0s
 => [linux/arm64 internal] load metadata for docker.io/library/nginx:latest                                         3.4s
 => [linux/amd64 internal] load metadata for docker.io/library/nginx:latest                                         3.3s
 => [auth] library/nginx:pull token for registry-1.docker.io                                                        0.0s
 => [internal] load .dockerignore                                                                                   0.3s
 => => transferring context: 2B                                                                                     0.0s
 => [internal] load build context                                                                                   0.3s
 => => transferring context: 135B                                                                                   0.0s
 => [linux/amd64 1/3] FROM docker.io/library/nginx:latest@sha256:80cff90527ee35ef185571e1c327beac                  17.4s
 => => resolve docker.io/library/nginx:latest@sha256:80cff90527ee35ef185571e1c327beac                               0.3s
 => => sha256:8056d2bcf3b682573ee5b0c176c1209df285d5be0df98ec6ae08bf7421179b74 1.40kB / 1.40kB                      0.5s
 => => sha256:2d455521f76cee8b8b2e21457075cc500c60373d70acb217f12838818fc3da90 366B / 366B                          0.5s
 => => sha256:dc9c4fdb83d69ef5986ec344c6b75606b3a417c7434268cb6995962be5312f14 1.21kB / 1.21kB                      0.3s
 => => sha256:36664b6ce66b304efa7ba48eb960133a085c2ec800a9f8887df94a82679334c1 955B / 955B                          0.5s
 => => sha256:35497dd96569b9139cd388fd7107df32ccdc1449b205536bce0968b2dec3e7dc 628B / 628B                          0.3s
 => => sha256:2f44b7a888fa005d07c031d3cfad2a1c0344207def2ab9dbb97712425ff812c1 29.13MB / 29.13MB                    6.8s
 => => sha256:8b7dd3ed1dc34cc1caba38bbbf22bceda5bd2e7c4e1b3c11ca64cda2ed186a2c 41.37MB / 41.37MB                    8.6s
 => => extracting sha256:2f44b7a888fa005d07c031d3cfad2a1c0344207def2ab9dbb97712425ff812c1                           2.5s
 => => extracting sha256:8b7dd3ed1dc34cc1caba38bbbf22bceda5bd2e7c4e1b3c11ca64cda2ed186a2c                           2.1s
 => => extracting sha256:35497dd96569b9139cd388fd7107df32ccdc1449b205536bce0968b2dec3e7dc                           0.3s
 => => extracting sha256:36664b6ce66b304efa7ba48eb960133a085c2ec800a9f8887df94a82679334c1                           0.7s
 => => extracting sha256:2d455521f76cee8b8b2e21457075cc500c60373d70acb217f12838818fc3da90                           0.3s
 => => extracting sha256:dc9c4fdb83d69ef5986ec344c6b75606b3a417c7434268cb6995962be5312f14                           0.1s
 => => extracting sha256:8056d2bcf3b682573ee5b0c176c1209df285d5be0df98ec6ae08bf7421179b74                           0.2s
 => [linux/arm64 1/3] FROM docker.io/library/nginx:latest@sha256:80cff90527ee35ef185571e1c327beac                  25.0s
 => => resolve docker.io/library/nginx:latest@sha256:80cff90527ee35ef185571e1c327beac                               0.3s
 => => sha256:e5a7e61f6ff45ab19ba3bad8bfc4e38cdafbbca0cefcf1f81516bb14b6476557 1.40kB / 1.40kB                      0.4s
 => => sha256:64fb762834ec1a0ca5d5980d4f810342d9fe5f1cf96b4d4bf2d4a27a564778ca 1.21kB / 1.21kB                      0.4s
 => => sha256:aeb2f3db77c35ab11eccb3643b18e72e1085fcc42343078da8d72b76a6fd5b44 363B / 363B                          0.4s
 => => sha256:06f386eb918297ce19936107ccb010187b2d6a95ba8bd60073610d371289b46c 955B / 955B                          0.4s
 => => sha256:fbc138d1d206d59261b82b2c537e7819ca8f46c63ce80b4906394a8b83ea896a 627B / 627B                          0.3s
 => => sha256:a5573528b1f0cf2f5d87c94fe0aee9d8967d5de98258be9303c3c6fa477824ec 29.16MB / 29.16MB                   13.0s
 => => sha256:8897d65c841707a2f02937de91c6644ba9699bd3ad194bdd84d2b61a93ca12f2 38.04MB / 38.04MB                   14.0s
 => => extracting sha256:a5573528b1f0cf2f5d87c94fe0aee9d8967d5de98258be9303c3c6fa477824ec                           5.9s
 => => extracting sha256:8897d65c841707a2f02937de91c6644ba9699bd3ad194bdd84d2b61a93ca12f2                           2.1s
 => => extracting sha256:fbc138d1d206d59261b82b2c537e7819ca8f46c63ce80b4906394a8b83ea896a                           0.2s
 => => extracting sha256:06f386eb918297ce19936107ccb010187b2d6a95ba8bd60073610d371289b46c                           0.2s
 => => extracting sha256:aeb2f3db77c35ab11eccb3643b18e72e1085fcc42343078da8d72b76a6fd5b44                           0.2s
 => => extracting sha256:64fb762834ec1a0ca5d5980d4f810342d9fe5f1cf96b4d4bf2d4a27a564778ca                           0.2s
 => => extracting sha256:e5a7e61f6ff45ab19ba3bad8bfc4e38cdafbbca0cefcf1f81516bb14b6476557                           0.2s
 => [linux/amd64 2/3] WORKDIR /usr/share/nginx/html                                                                 4.6s
 => [linux/amd64 3/3] COPY index.html .                                                                             0.8s
 => [linux/arm64 2/3] WORKDIR /usr/share/nginx/html                                                                 1.1s
 => [linux/arm64 3/3] COPY index.html .                                                                             0.4s
 => exporting to image                                                                                              7.7s
 => => exporting layers                                                                                             2.0s
 => => exporting manifest sha256:8d6ef872708a460dd114e0cd16b23ea1682cc0450a1a595aa5b243be329417bc                   0.1s
 => => exporting config sha256:9256de5c19cc842e626bb084fa3c18ea473e31fb841f47955f88ac18f3e4d36e                     0.1s
 => => exporting manifest sha256:8073bc22a6d790aeac27f9e751880e9b5e9242ba89d846a0f7004b4f758f3e01                   0.1s
 => => exporting config sha256:10d382c7dc0e36574a485d2de101e122cb5f8865b04765659758e8c18b1413bf                     0.1s
 => => exporting manifest list sha256:dedaf51bddb54861b3450788261fc                                                 0.1s
 => => pushing layers                                                                                               3.4s
 => => pushing manifest for docker.io/anti1346/mynginx:v0.0.1@sha256:dedaf51bddb54861b3450788261fc                  1.7s
 => [auth] anti1346/mynginx:pull,push token for registry-1.docker.io                                                0.0s

---

  • Docker Compose 작성
vim docker-compose.yml
version: '3.8'

services:
  web:
    image: anti1346/mynginx:v0.0.1 # 새로운 이미지로 업데이트
    ports:
      - "80:80"
  • 서비스 배포
docker stack deploy -c docker-compose.yml mywebapp
$ docker stack deploy -c docker-compose.yml mywebapp
Creating network mywebapp_default
Creating service mywebapp_web
docker stack ls
$ docker stack ls
NAME       SERVICES
mywebapp   1
docker service ls
$ docker service ls
ID             NAME           MODE         REPLICAS   IMAGE                     PORTS
5ogkkm7utfed   mywebapp_web   replicated   1/1        anti1346/mynginx:v0.0.1   *:80->80/tcp
  • curl 명령어를 사용하여 서비스 확인
curl http://localhost
$ curl http://localhost     
<html>
        <body>
                <h1>Web Server : Version 0.0.1</h1>
        </body>
</html>
  • 웹 브라우저에서 서비스 확인

browser1

  • docker hub에서 빌드된 이미지 확인

docker_hub1

728x90

2. Docker Swarm 클러스터에 서비스 업데이트

  • Dockerfile 또는 index.html 편집
    • index.html 업데이트(0.0.1 -> 0.0.2)
vim index.html
<html>
        <body>
                <h1>Web Server : Version 0.0.2</h1>
        </body>
</html>
  • 이미지 빌드(Dockerfile 업데이트)
docker buildx build --platform linux/amd64,linux/arm64 --tag anti1346/mynginx:v0.0.2 --no-cache --push .
$ docker buildx build --platform linux/amd64,linux/arm64 --tag anti1346/mynginx:v0.0.2 --no-cache --push .
[+] Building 9.5s (14/14) FINISHED
...
 => [auth] anti1346/mynginx:pull,push token for registry-1.docker.io
  • Docker Compose 업데이트
vim docker-compose.yml
version: '3.8'

services:
  web:
    image: anti1346/mynginx:v0.0.2 # 새로운 이미지로 업데이트
    ports:
      - "80:80"
  • 서비스 업데이트(재배포)
docker stack deploy -c docker-compose.yml mywebapp
$ docker stack deploy -c docker-compose.yml mywebapp
Updating service mywebapp_web (id: 5ogkkm7utfed97wzie4uuoso6)
docker stack ls
$ docker stack ls
NAME       SERVICES
mywebapp   1
docker service ls
$ docker service ls
ID             NAME           MODE         REPLICAS   IMAGE                     PORTS
5ogkkm7utfed   mywebapp_web   replicated   1/1        anti1346/mynginx:v0.0.2   *:80->80/tcp
  • curl 명령어를 사용하여 서비스 확인
curl http://localhost
$ curl http://localhost
<html>
        <body>
                <h1>Web Server : Version 0.0.2</h1>
        </body>
</html>
  • 웹 브라우저에서 서비스 확인

browser2

  • docker hub에서 빌드된 이미지 확인

docker_hub2

3. Docker Swarm 클러스터에 서비스 전역 스케일 조절

  • 서비스 전역 스케일 조절
docker service scale mywebapp_web=3
$ docker service scale mywebapp_web=3
mywebapp_web scaled to 3
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged
docker stack ls
$ docker stack ls
NAME       SERVICES
mywebapp   1
docker service ls
$ docker service ls
ID             NAME           MODE         REPLICAS   IMAGE                     PORTS
5ogkkm7utfed   mywebapp_web   replicated   3/3        anti1346/mynginx:v0.0.2   *:80->80/tcp

4. Docker Swarm 서비스와 스택을 삭제

  • 서비스 삭제
docker service ls
$ docker service ls
ID             NAME           MODE         REPLICAS   IMAGE                     PORTS
5ogkkm7utfed   mywebapp_web   replicated   3/3        anti1346/mynginx:v0.0.2   *:80->80/tcp
docker service rm mywebapp_web
$ docker service rm mywebapp_web
mywebapp_web
  • 스택 삭제
docker stack ls
$ docker stack ls
NAME      SERVICES
docker stack rm mywebapp

 

이러한 명령어들을 통해 Docker Swarm 클러스터에서 서비스를 배포하고 업데이트하며 전역 스케일을 조절하고, 마지막으로 서비스와 스택을 삭제할 수 있습니다.

 

728x90
반응형