Swarm Mode

Introduction

  1. ๋„์ปค ์‹œ์Šคํ…œ ์ •๋ณด ํ™•์ธ

    docker system info
  2. Swarm ํ™œ์„ฑํ™”

    docker swarm init
  3. ๋„์ปค ์‹œ์Šคํ…œ ์ •๋ณด ํ™•์ธ

    docker system info
  4. Node ๋ชฉ๋ก ํ™•์ธ

    docker node ls
  5. ํ˜ธ์ŠคํŠธ์˜ ํ˜ธ์ŠคํŠธ ์ด๋ฆ„ ํ™•์ธ

    hostname
  6. Service ์ƒ์„ฑ

    docker service create --name nginx nginx 
  7. ์ƒ์„ฑ๋œ Service ํ™•์ธ

    docker service ls
  8. ์ƒ์„ฑ๋œ Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    docker service ps nginx
  9. Swarm Node์— ์ƒ์„ฑ๋œ ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    docker node ps
  10. ํ˜ธ์ŠคํŠธ์— ์ƒ์„ฑ๋œ ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    docker container ls
  11. Service์˜ ๋ณต์ œ๋ณธ ๊ฐฏ์ˆ˜๋ฅผ 3๊ฐœ๋กœ ์ˆ˜์ •

    docker service update nginx --replicas=3
  12. Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    docker service ps nginx
  13. ์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ์— ์ˆ˜์ • ๊ฐ€๋Šฅํ•œ ์„ค์ • ํ™•์ธ

    docker container update --help
  14. ์ƒ์„ฑ๋œ Service์— ์ˆ˜์ • ๊ฐ€๋Šฅํ•œ ์„ค์ • ํ™•์ธ

    docker service update --help
  15. ์ƒ์„ฑ๋œ Service์˜ ์ƒ์„ธ ๋‚ด์šฉ ํ™•์ธ

    docker service inspect nginx
  16. ์ƒˆ๋กœ์šด ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  Docker ์ด๋ฒคํŠธ ์ˆ˜์‹ 

    docker events
  17. ๊ธฐ์กด ํ„ฐ๋ฏธ๋„๋กœ ๋Œ์•„์™€์„œ Service์˜ ์ด๋ฏธ์ง€ ํƒœ๊ทธ ๋ณ€๊ฒฝ

    docker service update nginx --image=nginx:alpine
  18. ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ ํ™•์ธ

    docker events
  19. ๊ธฐ์กด ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ Service์˜ ์ƒ์„ธ ๋‚ด์šฉ ํ™•์ธ

    docker service inspect nginx
  20. Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    docker service ps nginx
  21. ํ˜ธ์ŠคํŠธ์— ์ƒ์„ฑ๋œ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ

    docker container ls -a
  22. ์ด์ „ ๋ฒ„์ „์œผ๋กœ ๋กค๋ฐฑ

    docker service update nginx --rollback
  23. Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    docker service ps nginx
  24. ํ˜ธ์ŠคํŠธ์— ์ƒ์„ฑ๋œ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ ํ™•์ธ

    docker container ls -a
  25. Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ์ค‘์— ํ•œ๊ฐœ๋ฅผ ์‚ญ์ œ

    docker container rm --force $(docker container ls --filter name=^nginx -q -l)
  26. Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    docker service ps nginx
  27. ํ˜ธ์ŠคํŠธ์˜ 8080 ํฌํŠธ๋ฅผ Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ 80 ํฌํŠธ์™€ ์—ฐ๋™

    docker service update nginx --publish-add 8080:80
  28. Service์— ์ •๋ณด ํ™•์ธ

    docker service ls
  29. Service์— ์†ํ•˜๋Š” ๊ฐ ์ปจํ…Œ์ด๋„ˆ๋ณ„ ํฌํŠธ ๋งตํ•‘ ํ™•์ธ

    docker container ls --filter name=^nginx
  30. Service์˜ ์ƒ์„ธ ๋‚ด์šฉ ํ™•์ธ

    docker service inspect nginx
  31. Service์— ์†ํ•˜๋Š” ๊ฐ ์ปจํ…Œ์ด๋„ˆ์˜ IP ์ฃผ์†Œ ํ™•์ธ

    docker container ls --filter name=^nginx -q | xargs -n 1 docker container inspect --format '{{ .Name }} {{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' | sed 's#^/##';
  32. Network ๋ชฉ๋ก ํ™•์ธ

    docker network ls
  33. ๋กœ์ปฌํ˜ธ์ŠคํŠธ์— ์—ฐ๋™๋œ ํฌํŠธ๋ฅผ ํ†ตํ•ด์„œ Service๋กœ ์ ‘๊ทผ์ด ๋˜๋Š”์ง€ ํ™•์ธ

    curl localhost:8080
  34. DOCKER-INGRESS ๊ทœ์น™ ํ™•์ธ

    sudo iptables -t nat -L DOCKER-INGRESS -n  | column -t
  35. ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ Service ๋กœ๊ทธ ์ˆ˜์‹ 

    docker service logs nginx -n 0 -f
  36. ๊ธฐ์กด ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ ๋กœ์ปฌํ˜ธ์ŠคํŠธ์— ์—ฐ๋™๋œ ํฌํŠธ๋ฅผ ํ†ตํ•ด์„œ Service ํ˜ธ์ถœ

    for i in `seq 1 20`; do curl localhost:8080; done
  37. ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ ๋‹ค์ˆ˜์˜ ์ปจํ…Œ์ด๋„ˆ๋กœ ๋ถ€ํ•˜ ๋ถ„์‚ฐ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  Ctrl+C๋ฅผ ์ž…๋ ฅ

  38. ๊ธฐ์กด ํ„ฐ๋ฏธ๋„๋กœ ๋Œ์•„์™€์„œ Service ์ˆ˜์ •

    docker service update nginx --reserve-memory 1.5gb 
  39. Service ์‚ญ์ œ

    docker service rm nginx
  40. Swarm ๋น„ํ™œ์„ฑํ™” ์‹œ๋„

    docker swarm leave
  41. Swarm ๋น„ํ™œ์„ฑํ™”

    docker swarm leave --force
  42. ๋„์ปค ์‹œ์Šคํ…œ ์ •๋ณด ํ™•์ธ

    docker system info

Swarm Cluster

  1. ์‹ค์Šต ํ™˜๊ฒฝ ์ƒ์„ฑ

    {
        cd ~/environment/container-labs
        git pull
        terraform init
        terraform apply --target=module.swarm_cluster --auto-approve
    }
  2. ์ธ์Šคํ„ด์Šค ์ •๋ณด ํ™•์ธ

    terraform show -json \
    | jq -r '.values.root_module.child_modules | .[].resources | .[] | select(.address | contains("module.swarm_cluster.aws_instance.this")).values.public_ip'
  3. ์ƒˆ๋กœ์šด ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  ์ฒซ๋ฒˆ์งธ ์ธ์Šคํ„ด์Šค์— SSH ์—ฐ๊ฒฐ - ๋งค๋‹ˆ์ € ๋…ธ๋“œ๋กœ ์‚ฌ์šฉ

    ssh ubuntu@์œ„์—์„œ ํ™•์ธํ•œ ์•„์ดํ”ผ ์ฃผ์†Œ
  4. Swarm ํ™œ์„ฑํ™”

    sudo docker swarm init
  5. ์ƒˆ๋กœ์šด ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  ๋‘๋ฒˆ์งธ, ์„ธ๋ฒˆ์งธ ์ธ์Šคํ„ด์Šค๋กœ SSH ์ ‘์†ํ•ด์„œ ํด๋Ÿฌ์Šคํ„ฐ ์กฐ์ธ ๋ช…๋ น์–ด ์‹คํ–‰

  6. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ Swarm ๋…ธ๋“œ ๋ชฉ๋ก ํ™•์ธ

    sudo docker node ls
  7. Service ์ƒ์„ฑ

    sudo docker service create --name nginx --replicas 3 --reserve-memory 1.5gb nginx
  8. ์ƒ์„ฑ๋œ Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    sudo docker service ps nginx
  9. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ๋ฐฐํฌ๋œ NGINX ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒ์„ธ ๋‚ด์šฉ ํ™•์ธ

    sudo docker container inspect $(sudo docker container ls -q -f name=nginx)
  10. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ์ƒ์„ฑ๋œ Network ๋ชฉ๋ก ํ™•์ธ

    sudo docker network ls
  11. ๋‘๋ฒˆ์งธ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ ํ•ด๋‹น ๋…ธ๋“œ์— ๋ฐฐํฌ๋œ NGINX ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒ์„ธ ๋‚ด์šฉ ํ™•์ธ

    sudo docker container inspect $(sudo docker container ls -q -f name=nginx)
  12. ํ•ด๋‹น ๋…ธ๋“œ์— ์ƒ์„ฑ๋œ Network ๋ชฉ๋ก ํ™•์ธ

    sudo docker network ls
  13. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ Service ์‚ญ์ œ

    sudo docker service rm nginx
  14. Overlay ํ˜•์‹์˜ Network ์ƒ์„ฑ

    sudo docker network create --driver overlay  my-app 
  15. Network๊ฐ€ ์ƒ์„ฑ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

    sudo docker network ls
  16. ๋‹ค๋ฅธ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ Network ๋ชฉ๋ก ํ™•์ธ

    sudo docker network ls
  17. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ Service ์ƒ์„ฑ

    sudo docker service create --name nginx --replicas 3 --reserve-memory 1.5gb \
    --network my-app nginx
  18. ์ƒ์„ฑ๋œ Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ™•์ธ

    sudo docker service ps nginx
  19. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ๋ฐฐํฌ๋œ NGINX ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒ์„ธ ๋‚ด์šฉ ํ™•์ธ

    sudo docker container inspect $(sudo docker container ls -q -f name=nginx)
  20. ๋‹ค๋ฅธ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ ํ•ด๋‹น ๋…ธ๋“œ์— ๋ฐฐํฌ๋œ NGINX ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒ์„ธ ๋‚ด์šฉ ํ™•์ธ

    sudo docker container inspect $(sudo docker container ls -q -f name=nginx)
  21. Network ๋ชฉ๋ก ํ™•์ธ

    sudo docker network ls
  22. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™

  23. ํ˜ธ์ŠคํŠธ์˜ 8080 ํฌํŠธ๋ฅผ Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ 80 ํฌํŠธ์™€ ์—ฐ๋™

    sudo docker service update nginx --publish-add 8080:80
  24. ๋กœ์ปฌํ˜ธ์ŠคํŠธ์— ์—ฐ๋™๋œ ํฌํŠธ๋ฅผ ํ†ตํ•ด์„œ Service๋กœ ์ ‘๊ทผ์ด ๋˜๋Š”์ง€ ํ™•์ธ

    curl localhost:8080
  25. ๋กœ์ปฌํ˜ธ์ŠคํŠธ์— ์—ฐ๋™๋œ ํฌํŠธ๋ฅผ ํ†ตํ•ด์„œ Service ํ˜ธ์ถœ

    for i in `seq 1 20`; do curl localhost:8080; done
  26. Service ๋กœ๊ทธ ํ™•์ธ

    sudo docker service logs nginx --since 1m
  27. ๋‹ค๋ฅธ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ ํ•ด๋‹น ๋…ธ๋“œ์˜ 8080 ํฌํŠธ๋ฅผ ํ†ตํ•ด์„œ Service๋กœ ์ ‘๊ทผ์ด ๋˜๋Š”์ง€ ํ™•์ธ

    curl localhost:8080
  28. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ Service์— ๋ถ€์—ฌ๋œ ๊ฐ€์ƒ IP ํ™•์ธ

    sudo docker service inspect nginx --format '{{json .Endpoint.VirtualIPs}}'
  29. ์ƒˆ๋กœ์šด Service ์ƒ์„ฑ

    sudo docker service create --name busybox praqma/network-multitool sleep infinity
  30. ์ƒ์„ฑ๋œ Service์— ์†ํ•˜๋Š” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์–ด๋–ค ๋…ธ๋“œ์— ๋ฐฐํฌ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

    sudo docker service ps busybox
  31. ํ•ด๋‹น ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋ฐฐํฌ๋œ ๋…ธ๋“œ๋กœ ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™

  32. ํ•ด๋‹น ๋…ธ๋“œ์— ๋ฐฐํฌ๋œ NGINX ์ปจํ…Œ์ด๋„ˆ์˜ IP ์ฃผ์†Œ ํ™•์ธ

    sudo docker container inspect $(sudo docker container ls -q -f name=nginx) \
    --format '{{ $network := index .NetworkSettings.Networks "my-app" }}{{ $network.IPAddress}}'
  33. busybox ์ปจํ…Œ์ด๋„ˆ๋กœ bash ํ”„๋กœ์„ธ์Šค ์‹คํ–‰

    sudo docker container exec -it $(sudo docker container ls -q -f name=busybox) bash 
  34. ์„œ๋น„์Šค์— ๋ถ€์—ฌ๋œ ๊ฐ€์ƒ IP๋กœ NGINX ์ปจํ…Œ์ด๋„ˆ์— ์ ‘๊ทผ์ด ๋˜๋Š”์ง€ ํ™•์ธ

    curl SERVICE_VIRTUAL_IP
  35. ํ•ด๋‹น ๋…ธ๋“œ์— ๋ฐฐํฌ๋œ NGINX ์ปจํ…Œ์ด๋„ˆ์˜ IP๋กœ ์ ‘๊ทผ์ด ๋˜๋Š”์ง€ ํ™•์ธ

    curl NGINX_CONTAINER_IP
  36. bash ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ

    exit
  37. ๋งค๋‹ˆ์ € ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™

  38. busybox ์„œ๋น„์Šค์— NGINX ์„œ๋น„์Šค๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” overlay ๋„คํŠธ์›Œํฌ ์—ฐ๋™

    sudo docker service update --network-add my-app busybox
  39. ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์–ด๋–ค ๋…ธ๋“œ์— ๋ฐฐํฌ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

    sudo docker service ps busybox
  40. busybox ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋ฐฐํฌ๋œ ๋…ธ๋“œ๋กœ ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™

  41. busybox ์ปจํ…Œ์ด๋„ˆ๋กœ bash ํ”„๋กœ์„ธ์Šค ์‹คํ–‰

    sudo docker container exec -it $(sudo docker container ls -q -f name=busybox) bash 
  42. ์„œ๋น„์Šค์— ๋ถ€์—ฌ๋œ ๊ฐ€์ƒ IP๋กœ NGINX ์ปจํ…Œ์ด๋„ˆ์— ์ ‘๊ทผ์ด ๋˜๋Š”์ง€ ํ™•์ธ

    curl SERVICE_VIRTUAL_IP
  43. ์œ„์—์„œ ํ™•์ธํ•œ NGINX ์ปจํ…Œ์ด๋„ˆ์˜ IP๋กœ ์ ‘๊ทผ์ด ๋˜๋Š”์ง€ ํ™•์ธ

    curl NGINX_CONTAINER_IP
  44. ์„œ๋น„์Šค ์ด๋ฆ„์œผ๋กœ NGINX ์ปจํ…Œ์ด๋„ˆ์— ์ ‘๊ทผ์ด ๋˜๋Š”์ง€ ํ™•์ธ

    curl nginx
  45. ์„œ๋น„์Šค ์ด๋ฆ„์œผ๋กœ ๋„๋ฉ”์ธ ์ฃผ์†Œ ๊ฒ€์ƒ‰ ์‹œ๋„

    dig nginx
  46. bash ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ

    exit
  47. Cloud9์— ์—ฐ๊ฒฐ๋œ ํ„ฐ๋ฏธ๋„๋กœ ์ด๋™ํ•ด์„œ ๋ฆฌ์†Œ์Šค ์‚ญ์ œ

    terraform destroy --target=module.swarm_cluster --auto-approve

Last updated