데이터센터프로그래밍14

서유리·2022년 4월 27일
1
post-thumbnail

14-Docker Swarm(2)

🔴 Manual Scale Up & Down

  • Scale Up & Down은 컨테이너 여러개를 띄울 때, replicas 명령을 주어서 실제로 docker compose file(infrastructure as code)을 통해서 띄우는 것을 의미함

🔴 Manual Scale Up & Down : code (1)

# 서비스 생성
# web-1(이름)이라는 서비스를 create하고, nginx 3개, port number는 80:80으로 실행
sudo docker service create --replicas 3 -p 80:80 --name web-1 nginx
# 서비스 확인
sudo docker service ls
# 서비스 들어가 있는 process들(컨테이너 3개)이 떠 있는지 확인
sudo docker service ps web-1
# 도커 컨테이너 확인
docker container ls

🔴 Manual Scale Up & Down : code (2)

  • web-1을 늘리고, 줄이는 것을 손으로 직접 해봄
# web-1에 있는 working하는 컨테이너를 4개 올릴 것 (참고로, 앞에서는 3개 였음)
sudo docker service scale web-1=4
# 서비스 확인
sudo docker service ls
sudo docker service ps web-1
# ps 확인
sudo docker ps
# web-1을 5개로 올릴 것
sudo docker service scale web-1=5
# ps 확인
sudo docker ps
# web-1을 2개로 내릴 것
sudo docker service scale web-1=2

🔴 Service Update : code

  • 최신버전을 했다가, 서비스가 이상하게 돌아가는 경우에 보통 다운을 한다
  • 따라서, 아래의 코드를 통해서 다운을 실행함 (= 원생복구)
# 띄운 service가 nginx latest(최신버전)이고, 1.14.0으로 downgrade 할 것임
sudo docker service update --image nginx:1.14.0 web-1
# ps 확인
sudo docker service ps web-1

💗 코드 및 화면결과 (1)

  • 3개의 다른 machine에 swarm 띄우기 위해 docker-machine을 사용함
docker-machine create node1
docker-machine create node2
docker-machine create node3
docker-machine ls
# docker-machine ls 결과화면
NAME      ACTIVE   DRIVER       STATE     URL   SWARM   DOCKER    ERRORS
manager   *        virtualbox   Running                 Unknown   ssh command error:
output  : 
node1     -    virtualbox   Running   tcp://192.168.99.103:2376        v19.03.12   
node2     -    virtualbox   Running   tcp://192.168.99.104:2376        v19.03.12   
node3     -    virtualbox   Running   tcp://192.168.99.105:2376        v19.03.12   

💗 코드 및 화면결과 (2)

  • ssh 명령어를 사용해서 node1에 접속하기
docker-machine ssh node1
# 결과화면 
( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net
docker@node1:~$ ls
docker@node1:~$ exit
# node1에서 docker swram init 입력
docker@node1:~$ docker swarm init
# 결과화면 --> "--advertise-addr" 옵션을 통해서 여러 addresses 중에 사용할 ip를 선택하라는 의미
Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (10.0.2.15 on eth0 and 192.168.99.103 on eth1) - specify one with --advertise-addr
# node2, node3와 연동을 해야함으로 "192.168.99.103"의 ip를 선택하여 다시 init을 실행
docker@node1:~$ sudo docker swarm init --advertise-addr 192.168.99.103
# 결과화면
Swarm initialized: current node (893rbw5c19rogvpaxsbxpp54s) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-40w906gt4mra8507x6hmxidexqy3fyx8ooli9r1y83utwnz45c-2f3ffdfebd9nota4uddhuq56r 192.168.99.103:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

💗 코드 및 화면결과 (3)

  • command를 복사하여 node2에서 join하기
# node2에 swarm join 명령어 입력
docker@node2:~$ docker swarm join --token SWMTKN-1-40w906gt4mra8507x6hmxidexqy3fyx8ooli9r1y83utwnz45c-2f3ffdfebd9nota4uddhuq56r 192.168.99.103:2377
# swarm에 worker로 접속한 결과 메세지
This node joined a swarm as a worker.

💗 코드 및 화면결과 (4)

  • manager & worker 확인
    🦒 핵심은 node2에서는 worker이기 때문에 swarm command 사용이 불가능함
docker@node1:~$ docker node ls
# docker node ls 결과화면
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
893rbw5c19rogvpaxsbxpp54s *   node1               Ready               Active              Leader              19.03.12
vxpvanegserunthyoz8ff93sh     node2               Ready               Active                                  19.03.12

💗 코드 및 화면결과 (5)

  • node2를 매니저로 업데이트하면, swarm 명령어를 입력할 수 있음
  • node2는 Reachable, node1은 Leader임을 확인 할 수 있음
# docker node update
docker@node1:~$ docker node update --role manager node2
# docker node ls
docker@node1:~$ docker node ls
# 결과화면 
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
893rbw5c19rogvpaxsbxpp54s *   node1               Ready               Active              Leader              19.03.12
vxpvanegserunthyoz8ff93sh     node2               Ready               Active              Reachable           19.03.12
# manager 권한의 토큰 발행
docker@node1:~$ docker swarm join-token manager 
# 토큰
docker swarm join --token SWMTKN-1-40w906gt4mra8507x6hmxidexqy3fyx8ooli9r1y83utwnz45c-7is96i79c2unz5h9emn5w6xx7 192.168.99.103:2377
# 토큰을 "node3"에 입력하면 manager 권한 발행 가능
docker@node3:~$ docker swarm join --token SWMTKN-1-40w906gt4mra8507x6hmxidexqy3fyx8ooli9r1y83utwnz45c-7is96i79c2unz5h9emn5w6xx7 192.168.99.103:237

💗 코드 및 화면결과 (6)

  • docker service 실행
# docker service
docker@node1:~$ docker service create --replicas 3 -p 80:80 --name web-1 nginx
# 결과화면
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged
# docker service ls
docker@node1:~$ docker service ls
# 결과화면 
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
h6c57nbc4cei        web-1               replicated          3/3                 nginx:latest        *:80->80/tcp
# docker service ps web-1
docker@node1:~$ docker service ps web-1
# 결과화면
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
xv095synxe5z        web-1.1             nginx:latest        node2               Running             Running about a minute ago                       
pgshro45bbfy        web-1.2             nginx:latest        node3               Running             Running about a minute ago                       
3fczd9pu3v42        web-1.3             nginx:latest        node1               Running             Running about a minute ago   
# docker container ls
docker@node1:~$ docker container ls
# 결과화면
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
d35574ca7ce5        nginx:latest        "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp              web-1.3.3fczd9pu3v42gsrzylepdfwj6

💗 코드 및 화면결과 (7)

  • web-1을 늘리고, 줄이기
# docker service scale web-1=4
docker@node1:~$ docker service scale web-1=4
# 결과화면
web-1 scaled to 4
overall progress: 4 out of 4 tasks 
1/4: running   [==================================================>] 
2/4: running   [==================================================>] 
3/4: running   [==================================================>] 
4/4: running   [==================================================>] 
verify: Service converged 
# docker service ls
docker@node1:~$ docker service ls
# 결과화면
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
h6c57nbc4cei        web-1               replicated          4/4                 nginx:latest        *:80->80/tcp
# docker service ps web-1   
docker@node1:~$ docker service ps web-1  
# 결과화면
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
xv095synxe5z        web-1.1             nginx:latest        node2               Running             Running 2 minutes ago                        
pgshro45bbfy        web-1.2             nginx:latest        node3               Running             Running 2 minutes ago                        
3fczd9pu3v42        web-1.3             nginx:latest        node1               Running             Running 2 minutes ago                        
t3pby37p9995        web-1.4             nginx:latest        node1               Running             Running 24 seconds ago   
# docker ps
docker@node1:~$ docker ps
# 결과화면
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
896614422d4c        nginx:latest        "/docker-entrypoint.…"   35 seconds ago      Up 34 seconds       80/tcp              web-1.4.t3pby37p9995hblawwt94183b
d35574ca7ce5        nginx:latest        "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes        80/tcp              web-1.3.3fczd9pu3v42gsrzylepdfwj6
# docker service scale web-1=5 
docker@node1:~$ docker service scale web-1=5 
# 결과화면
web-1 scaled to 5
overall progress: 5 out of 5 tasks 
1/5: running   [==================================================>] 
2/5: running   [==================================================>] 
3/5: running   [==================================================>] 
4/5: running   [==================================================>] 
5/5: running   [==================================================>] 
verify: Service converged 
# docker ps
docker@node1:~$ docker ps
# 결과화면
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
896614422d4c        nginx:latest        "/docker-entrypoint.…"   56 seconds ago      Up 55 seconds       80/tcp              web-1.4.t3pby37p9995hblawwt94183b
d35574ca7ce5        nginx:latest        "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes        80/tcp              web-1.3.3fczd9pu3v42gsrzylepdfwj6
# docker service scale web-1=2
docker@node1:~$ docker service scale web-1=2 
# 결과화면
web-1 scaled to 2
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged

💗 코드 및 화면결과 (8)

  • Service Update
# docker service update 
docker@node1:~$ docker service update --image nginx:1.14.0 web-1
# 결과화면
web-1
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged
# docker service ps web-1
docker@node1:~$ docker service ps web-1  
# 결과화면
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE             ERROR               PORTS
lfvp0edvky5i        web-1.1             nginx:1.14.0        node2               Running             Running 18 seconds ago                        
xv095synxe5z         \_ web-1.1         nginx:latest        node2               Shutdown            Shutdown 23 seconds ago                       
ip215vebmwwu        web-1.2             nginx:1.14.0        node1               Running             Running 26 seconds ago                        
pgshro45bbfy         \_ web-1.2         nginx:latest        node3               Shutdown            Shutdown 30 seconds ago  
profile
best of best

0개의 댓글

관련 채용 정보