# 필요환경
- docker 가 설치된 인스턴스 (2대 이상)
- 인스턴스간에 네트워크가 연결되어 있어야 함.
# plan
- 1대의 manager node
- n대의 worker node
# 구성하기
– manager 노드에서 docker swarm init 실행 (아이피가 여러개일 경우 –advertise-addr 옵션을 이용해 ip 지정)
ubuntu@ip-10-0-10-175:~$ docker swarm init --advertise-addr=10.0.10.175
Swarm initialized: current node (ysc8hse6ug9erplpbyole0vrk) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-4x40s3vahcusgm2e5im6l1dpr1cbsqxxhxjgvmn4d6uoog4e9n-50i4oabb2qagdv2g1klbow086 10.0.10.175:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
– worker 노드에 join 명령 실행 (위 manager에서 init 시에 나온 명령)
ubuntu@ip-10-0-50-90:~$ docker swarm join --token SWMTKN-1-4x40s3vahcusgm2e5im6l1dpr1cbsqxxhxjgvmn4d6uoog4e9n-50i4oabb2qagdv2g1klbow086 10.0.10.175:2377
This node joined a swarm as a worker.
ubuntu@ip-10-0-50-89:~$ docker swarm join --token SWMTKN-1-4x40s3vahcusgm2e5im6l1dpr1cbsqxxhxjgvmn4d6uoog4e9n-50i4oabb2qagdv2g1klbow086 10.0.10.175:2377
This node joined a swarm as a worker.
– 연결된 노드 확인
ubuntu@ip-10-0-10-175:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ysc8hse6ug9erplpbyole0vrk * ip-10-0-10-175 Ready Active Leader 19.03.6
9duven5prcw1so9ml7s3f1hyu ip-10-0-50-89 Ready Active 19.03.6
nh4vdukkdpm9kcswyzel5mkoa ip-10-0-50-90 Ready Active 19.03.6
jt78plqlkcrmcb27nx1v8klf6 ip-10-0-50-105 Ready Active 19.03.6
4t9r1m1ee6gidj720ww8ibmc5 ip-10-0-50-117 Ready Active 19.03.6
ymwovr8eztucql8gcfi5qt0un ip-10-0-50-158 Ready Active 19.03.6
p320doyyx2chwrb9zxb7yinjw ip-10-0-50-181 Ready Active 19.03.6
senx8ljz73kunjwd592jiyawr ip-10-0-50-220 Ready Active 19.03.6
7gzwbybb7h2y8lg0loojawtpx ip-10-0-50-241 Ready Active 19.03.6
– 클러스터 만들기 – nginx cluster
ubuntu@ip-10-0-10-175:~$ docker service create --name nginx-cluster -p 8080:80 --replicas 2 nginx:latest
okbw4y4hqe7btwzqbn56r7puu
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged
– 상태확인
ubuntu@ip-10-0-10-175:~$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
okbw4y4hqe7b nginx-cluster replicated 2/2 nginx:latest *:8080->80/tcp
8080 -> 80 으로 포워딩 해주는 docker 컨테이너가 실행되었다.
2대의 노드에만 container 가 올라갔지만 모든 노드에서 8080번이 호출가능하다. (자동으로 해당 서버로 라우팅 해준다)
– replica 변경
ubuntu@ip-10-0-10-175:~$ docker service update --replicas=3 nginx-cluster
nginx-cluster
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
– 어떤 노드들에서 실행되었는지 확인
ubuntu@ip-10-0-10-175:~$ docker service ps nginx-cluster
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
lkwjt03yjbai nginx-cluster.1 nginx:latest ip-10-0-50-90 Running Running 2 hours ago
f3ynriukyg2y nginx-cluster.2 nginx:latest ip-10-0-50-117 Running Running 2 hours ago
fxhrqhlej97k nginx-cluster.3 nginx:latest ip-10-0-50-158 Running Running about a minute ago
** 해당 노드수보다 replica가 많을 경우 한 서버에 여러개가 뜰 수 있음.
– 서비스 제거
ubuntu@ip-10-0-10-175:~$ docker service rm nginx-cluster
nginx-cluster