[docker] docker swarm mode 구성

docker swarm mode 구성

description:

To use Docker in swarm mode, install Docker. See installation instructions for all operating systems and platforms.

Current versions of Docker include swarm mode for natively managing a cluster of Docker Engines called a swarm. Use the Docker CLI to create a swarm, deploy application services to a swarm, and manage swarm behavior.

If you are using a Docker version prior to 1.12.0, you can use standalone swarm, but we recommend updating.

official page: https://docs.docker.com/engine/swarm/

 

vm 준비

docker-manager 192.168.192.10

docker-worker01 192.168.192.20

docker-worker02 192.168.192.30

3대의 vm 에는 docker가 설치 되어 있습니다.

Docker-manager
test@docker-manager:~$ docker -v
Docker version 18.02.0-ce, build fc4de44
test@docker-manager:~$

Docker-worker01
test@docker-worker01:~$ docker -v
Docker version 18.02.0-ce, build fc4de44
test@docker-worker01:~$

Docker-worker02
test@docker-worker02:~$ docker -v
Docker version 18.02.0-ce, build fc4de44
test@docker-worker02:~$

 

1. Create Docker Swarm cluster

docker-manager 에서 작업

command : docker swarm init –advertise-addr $docker-manager ip

test@docker-manager:~$ docker swarm init --advertise-addr 192.168.192.10
Swarm initialized: current node (mzsiydsvbyhafre4cppmfupvb) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5ar8gen44ij5gi7hlkmbbhdji2e06nvod2u86e5db7zwip9707-8f5nxu48rh841xdcj0na1vp0s 192.168.192.10:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

test@docker-manager:~$

 

docker manager node 확인

test@docker-manager:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
mzsiydsvbyhafre4cppmfupvb *   docker-manager      Ready               Active              Leader
test@docker-manager:~$

test@docker-manager:~$ docker info
Containers: 1
~중략
Swarm: active
 NodeID: mzsiydsvbyhafre4cppmfupvb
 Is Manager: true
 ClusterID: rnqp34pegl5rj9fqd1ex30x0h
 Managers: 1
 Nodes: 1
 Orchestration:
  Task History Retention Limit: 5
 Raft:
  Snapshot Interval: 10000
  Number of Old Snapshots to Retain: 0
  Heartbeat Tick: 1
  Election Tick: 3
 Dispatcher:
  Heartbeat Period: 5 seconds
 CA Configuration:
  Expiry Duration: 3 months
  Force Rotate: 0
 Autolock Managers: false
 Root Rotation In Progress: false
 Node Address: 192.168.192.10
 Manager Addresses:
  192.168.192.10:2377
~중략

join-token 생성

docker-manager host 에서 작업

test@docker-manager:~$ docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5ar8gen44ij5gi7hlkmbbhdji2e06nvod2u86e5db7zwip9707-8f5nxu48rh841xdcj0na1vp0s 192.168.192.10:2377

test@docker-manager:~$

 

 

docker-worker swarm 등록

docker-manager에서 생성한 join-token을 이용하여 worker node 를 등록 합니다.

docker-worker01 host 에서 작업

test@docker-worker01:~$ docker swarm join  --token SWMTKN-1-5ar8gen44ij5gi7hlkmbbhdji2e06nvod2u86e5db7zwip9707-8f5nxu48rh841xdcj0na1vp0s 192.168.192.10:2377
This node joined a swarm as a worker.
test@docker-worker01:~$ docker info |grep -i swarm
WARNING: No swap limit support
Swarm: active

 

docker-worker02 host 에서 작업

test@docker-worker02:~$ docker swarm join  --token SWMTKN-1-5ar8gen44ij5gi7hlkmbbhdji2e06nvod2u86e5db7zwip9707-8f5nxu48rh841xdcj0na1vp0s 192.168.192.10:2377
This node joined a swarm as a worker.
test@docker-worker02:~$ docker info |grep -i swarm
WARNING: No swap limit support
Swarm: active
test@docker-worker02:~$

 

docker-manager node 확인

test@docker-manager:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
mzsiydsvbyhafre4cppmfupvb *   docker-manager      Ready               Active              Leader
jvitfs6bmgr699p3nltzok0fw     docker-worker01     Ready               Active
hwkew1ktumxynqhqexjx5gn8l     docker-worker02     Ready               Active
test@docker-manager:~$

 

nginx test

docker run 의 경우 아래와 같이 사용하지만 docker swarm 의 경우 docker service create 명령어를 사용합니다.

docker run 실행시는 아래와 같이 사용 합니다. docker swarm 에서는 docker service create 명령어를 사용합니다. 🙂

test@docker-test:~$ docker run --name web-service -d -p 80:80 -p 443:443 nginx

 

docker service create 명령어

test@docker-manager:~$ docker service create --name web-service -p 80:80 -p 443:443 nginx
pgdc990uyesjmw399ayuraie9
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
test@docker-manager:~$

 

!!! docker service create 명령어 사용시 –publish 옵션을 사용해야 합니다. 

test@docker-master:~$ docker service create --name web-service --publish 80:80 nginx
md8pzk0owb4s8ida55voqzlrb
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
test@docker-master:~$

 

 

service 확인

test@docker-manager:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
pgdc990uyesj        web-service         replicated          1/1                 nginx:latest        *:80->80/tcp, *:443->443/tcp
test@docker-manager:~$ docker service ps web-service
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
wd1vyqp9eg2x        web-service.1       nginx:latest        docker-manager      Running             Running 29 seconds ago
test@docker-manager:~$

 

 

docker service scale 명령어를 사용하여 service 개수를 늘려 줍니다.

service 갯수를 줄일때도 동일하게 scale을 사용 합니다.

test@docker-manager:~$ docker service scale web-service=3
web-service scaled to 3
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
test@docker-manager:~$


test@docker-manager:~$ docker service ps web-service
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
wd1vyqp9eg2x        web-service.1       nginx:latest        docker-manager      Running             Running 20 minutes ago
bezi55mn78lf        web-service.2       nginx:latest        docker-worker01     Running             Running 42 seconds ago
vwpxbid1ez53        web-service.3       nginx:latest        docker-worker02     Running             Running 42 seconds ago
test@docker-manager:~$

 

docker service rm 

swarm 에서 사용중인 컨테이너 삭제시는 docker servier rm $service_name 으로 삭제 할수 있습니다.

test@docker-manager:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
pgdc990uyesj        web-service         replicated          1/1                 nginx:latest        *:80->80/tcp, *:443->443/tcp
test@docker-manager:~$ docker service rm web-service
web-service
test@docker-manager:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
test@docker-manager:~$

 

 

댓글 남기기