티스토리 뷰
♠ Docker swarm 에 대한 내용 참조
https://docs.docker.com/engine/swarm/
https://docs.docker.com/reference/cli/docker/swarm/init/
Swarm mode overview
Docker Engine Swarm mode overview
docs.docker.com
▶ Docker swarm 생성 ( manager 노드가 되었다 )
>> 두번째 장비에는 아래 내용중 docker swarm join --token ~ 명령어를 입력한다
>> join 을 위해서는 token 이 필요하다 >>> 그대로 복사 붙여넣기 ( 보안상 좋지는 않다 )
>> 명령어 그대로를 파일로 만들어서 넘겨주는게 낫다
[vagrant@docker1 ~]$ docker swarm init --advertise-addr 192.168.25.10
Swarm initialized: current node (ln6o25x1z658c6gwga1axt5wl) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1ks4brrs0tgvukyutgu7rsmv3tsaraz0um2afcrb71zym2x2p3-1ao8wy3buqmjqmxwfs0pu2pwa 192.168.25.10:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
▶ 파일로 만들어서 넘겨준다
[vagrant@docker1 ~]$ echo 'docker swarm join --token SWMTKN-1-1ks4brrs0tgvukyutgu7rsmv3tsaraz0um2afcrb71zym2x2p3-1ao8wy3buqmjqmxwfs0pu2pwa 192.168.25.10:2377' > swarm-token
>> scp 로 복사해준다
[vagrant@docker1 ~]$ scp swarm-token 192.168.25.20:/home/vagrant
vagrant@192.168.25.20's password:
swarm-token 100% 131 219.6KB/s 00:00
▶ ubuntu 에서 권한 줘서 실행한다
vagrant@docker2:~$ ll
-rw-rw-r-- 1 vagrant vagrant 131 Mar 7 10:05 swarm-token
vagrant@docker2:~$ chmod u+x swarm-token
-rwxrw-r-- 1 vagrant vagrant 131 Mar 7 10:05 swarm-token*
vagrant@docker2:~$ ./swarm-token
This node joined a swarm as a worker.
▶centos 에서의 포트 번호를 확인해보자
>> 2377 포트번호가 도커데몬으로 사용된다는 것을 확인 가능하다
[vagrant@docker1 ~]$ sudo yum install -y net-tools
[vagrant@docker1 ~]$ sudo netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 690/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 790/master
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 380/rpcbind
tcp6 0 0 :::22 :::* LISTEN 690/sshd
tcp6 0 0 ::1:25 :::* LISTEN 790/master
tcp6 0 0 :::2377 :::* LISTEN 750/dockerd
tcp6 0 0 :::7946 :::* LISTEN 750/dockerd
tcp6 0 0 :::111 :::* LISTEN 380/rpcbind
▷ docker node 들이 묶여 있는 걸 확인할 수 있다
>> * 표시가 있으면 manager
[vagrant@docker1 ~]$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ln6o25x1z658c6gwga1axt5wl * docker1.example.com Ready Active Leader 25.0.3
257jeo37jv4q0ewrbvi9e84l0 docker2 Ready Active 25.0.3
○ ubuntu 에서 같은 명령어 실행시, worker node 라고 한다
vagrant@docker2:~$ docker node ls
Error response from daemon: This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state. Please run this command on a manager node or promote the current node to a manager.
■ docker swarm 관련 명령어
docker swarm init : docker swarm 초기화
docker node ls : cluster node 확인
docker service create --name myalpine --replicas 2 alpine /bin/sleep 3600s
docker service ls
docker service rm myalpine
docker service create --name mysql --replicas 2 -e MYSQL_ROOT_PASSWORD=mypass -p 9900:3306 mysql
◎ 도커 클러스트 해제할 때에는 시간이 좀 걸린다
○ ubuntu 에서 떠난다
vagrant@docker2:~$ docker swarm leave
Node left the swarm.
○ centos 확인
[vagrant@docker1 ~]$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ln6o25x1z658c6gwga1axt5wl * docker1.example.com Ready Active Leader 25.0.3
257jeo37jv4q0ewrbvi9e84l0 docker2 Down Active 25.0.3
▷ 도커 서비스로 컨테이너를 만든다
>> replicas 몇개의 컨테이너를 띄울 것인지 결정
>> 서비스를 올리면 verify 로도 확인을 한다 ( 검증까지 한다 )
>> 자동으로 부하분산도 해준다
>>> 예를 들면, 4개의 서비스를 올리면, node worker 에 2개 manaver 에 2개를 올려준다
[vagrant@docker1 ~]$ docker service create --name alpine-service --replicas 2 alpine /bin/sleep 3600s
iwwk3qn9pn3zeqx6w5ouf2hbg
overall progress: 2 out of 2 tasks
1/2: running
2/2: running
verify: Service converged
□ docker service 옵션
[vagrant@docker1 ~]$ docker service
Usage: docker service COMMAND
Manage Swarm services
Commands:
create Create a new service
inspect Display detailed information on one or more services
logs Fetch the logs of a service or task
ls List services
ps List the tasks of one or more services
rm Remove one or more services
rollback Revert changes to a service's configuration
scale Scale one or multiple replicated services
update Update a service
[vagrant@docker1 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
▩ 도커 서비스를 지웠는데도 docker ps 에서는 출력되는데, 아마도 꺼지는데 시간이 걸리는듯 싶다
[vagrant@docker1 ~]$ docker service rm alpine-service
alpine-service
[vagrant@docker1 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc82f062282e alpine:latest "/bin/sleep 3600s" 2 minutes ago Up 2 minutes alpine-service.1.sym7ufyokqfoh110218dvxfes
8d1db1510090 alpine:latest "/bin/sleep 3600s" 2 minutes ago Up 2 minutes alpine-service.2.o4bbfm4b627n5qz3s75c38pd9
□ 부하 분산 확인을 위해서 다시 클러스터에 묶는다
vagrant@docker2:~$ ./swarm-token
This node joined a swarm as a worker.
[vagrant@docker1 ~]$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ln6o25x1z658c6gwga1axt5wl * docker1.example.com Ready Active Leader 25.0.3
257jeo37jv4q0ewrbvi9e84l0 docker2 Down Active 25.0.3
qn99hviik4t1hlh16bvot4ula docker2 Ready Active 25.0.3
● alpine-service 를 2개 올려보았다
>> 분산되었다
[vagrant@docker1 ~]$ docker service create --name alpine-service --replicas 2 alpine /bin/sleep 3600s
edsz3usp7n5zow5kee8t1tvd6
overall progress: 2 out of 2 tasks
1/2: running
2/2: running
verify: Service converged
[vagrant@docker1 ~]$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
edsz3usp7n5z alpine-service replicated 2/2 alpine:latest
vagrant@docker2:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
886075ccd787 alpine:latest "/bin/sleep 3600s" 23 seconds ago Up 23 seconds alpine-service.2.wevodxi53c4umjpyjp7rrfrwn
● 서비스 도중에 추가 업데이트도 가능하다
[vagrant@docker1 ~]$ docker service update alpine-service --replicas 4
alpine-service
overall progress: 4 out of 4 tasks
1/4: running
2/4: running
3/4: running
4/4: running
verify: Service converged
>> 확인이 된다
[vagrant@docker1 ~]$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
edsz3usp7n5z alpine-service replicated 4/4 alpine:latest
▷ 추가로 늘어난 2개 service 도 부하분산 되었다
[vagrant@docker1 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8269c211cc4c alpine:latest "/bin/sleep 3600s" About a minute ago Up About a minute alpine-service.3.zrzu6u8po4xq5kyciycpo98yi
21b930b09fa5 alpine:latest "/bin/sleep 3600s" 3 minutes ago Up 3 minutes alpine-service.1.4chr33kfwhorqrcsh9z0wijhy
vagrant@docker2:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7af2f1916d2d alpine:latest "/bin/sleep 3600s" About a minute ago Up About a minute alpine-service.4.h915m1ift4bpmpoxzpbhh1t5y
886075ccd787 alpine:latest "/bin/sleep 3600s" 2 minutes ago Up 2 minutes alpine-service.2.wevodxi53c4umjpyjp7rrfrwn
▶ 아파치 서비스를 1개 더 올려본다
[vagrant@docker1 ~]$ docker service create --name apache-service --replicas 1 httpd
sujvqf79xra3pan562250wf9b
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged
[vagrant@docker1 ~]$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
edsz3usp7n5z alpine-service replicated 4/4 alpine:latest
sujvqf79xra3 apache-service replicated 1/1 httpd:latest
▶ 서비스 확인
[vagrant@docker1 ~]$ docker service ps apache-service
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
7ds4fe3q8yeo apache-service.1 httpd:latest docker1.example.com Running Running 2 minutes ago
[vagrant@docker1 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e144adc96623 httpd:latest "httpd-foreground" 2 minutes ago Up 2 minutes 80/tcp apache-service.1.7ds4fe3q8yeodrjzbz73crp76
8269c211cc4c alpine:latest "/bin/sleep 3600s" 18 minutes ago Up 18 minutes alpine-service.3.zrzu6u8po4xq5kyciycpo98yi
21b930b09fa5 alpine:latest "/bin/sleep 3600s" 20 minutes ago Up 20 minutes alpine-service.1.4chr33kfwhorqrcsh9z0wijhy
▷아파치의 IP 확인
>> replica 이기 때문에 apache-service.1 뒤에 긴 내용이 적힌다
[vagrant@docker1 ~]$ docker container inspect apache-service.1.7ds4fe3q8yeodrjzbz73crp76
>> apache update
[vagrant@docker1 ~]$ docker service update apache-service --replicas 3
apache-service
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
▶ ubuntu 에 있는 service 가 늘어난 상황에서 ubuntu 장비를 강제로 종료해보자
vagrant@docker2:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b75b9bd5453 httpd:latest "httpd-foreground" 52 seconds ago Up 51 seconds 80/tcp apache-service.3.kbgprr1n5gxjsfxq4rddpgzy4
f452524f1024 httpd:latest "httpd-foreground" 52 seconds ago Up 51 seconds 80/tcp apache-service.2.aa5k0s1na2i7om1gps1n8y8uh
7af2f1916d2d alpine:latest "/bin/sleep 3600s" 27 minutes ago Up 27 minutes alpine-service.4.h915m1ift4bpmpoxzpbhh1t5y
886075ccd787 alpine:latest "/bin/sleep 3600s" 29 minutes ago Up 29 minutes alpine-service.2.wevodxi53c4umjpyjp7rrfrwn
>> centos 장비 service
[vagrant@docker1 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e144adc96623 httpd:latest "httpd-foreground" 10 minutes ago Up 10 minutes 80/tcp apache-service.1.7ds4fe3q8yeodrjzbz73crp76
8269c211cc4c alpine:latest "/bin/sleep 3600s" 27 minutes ago Up 27 minutes alpine-service.3.zrzu6u8po4xq5kyciycpo98yi
21b930b09fa5 alpine:latest "/bin/sleep 3600s" 29 minutes ago Up 29 minutes alpine-service.1.4chr33kfwhorqrcsh9z0wijhy
▷ centos 로 모든 서비스가 올라왔다
>> 장애 극복이 가능하다
[vagrant@docker1 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d9ad0dff125 httpd:latest "httpd-foreground" 7 seconds ago Up 1 second 80/tcp apache-service.2.sow07j8o5qpey8hfku7ywwuus
9c6a705b62b7 alpine:latest "/bin/sleep 3600s" 7 seconds ago Up 1 second alpine-service.2.kbugcuounh5d1tw0n46j6mlr9
a34c36c3facd alpine:latest "/bin/sleep 3600s" 7 seconds ago Up 1 second alpine-service.4.d0v0mwta9fe1i4mjacj9fatgx
461e35705bf0 httpd:latest "httpd-foreground" 7 seconds ago Up 1 second 80/tcp apache-service.3.na1p56atg8yrigc2nuf3114v3
e144adc96623 httpd:latest "httpd-foreground" 13 minutes ago Up 12 minutes 80/tcp apache-service.1.7ds4fe3q8yeodrjzbz73crp76
8269c211cc4c alpine:latest "/bin/sleep 3600s" 29 minutes ago Up 29 minutes alpine-service.3.zrzu6u8po4xq5kyciycpo98yi
21b930b09fa5 alpine:latest "/bin/sleep 3600s" 31 minutes ago Up 31 minutes alpine-service.1.4chr33kfwhorqrcsh9z0wijhy
'Docker 와 Container' 카테고리의 다른 글
오케스트레이션 [orchestration] (0) | 2024.03.06 |
---|---|
Docker compose 응용 (0) | 2024.03.06 |
Docker - compose (0) | 2024.03.06 |
Docker registry 배포 (0) | 2024.03.06 |
Docker image upload (0) | 2024.03.06 |