Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Docker Swarm

Nguyễn Hàn Duy


duy@techmaster.vn
Nội dung

● Swarm cluster
● Service & tasks
● Ingress overlay network
● Internal overlay network
● Stack
Swarm cluster
Standalone host Swarm cluster

Cont Cont Cont Cont Cont Cont Cont Cont


Cont

Cont Cont Cont Cont Cont Cont Cont Cont Cont

Cont Cont Cont Cont Cont

Cont Cont
Cont Cont Cont Cont Cont

Cont Cont
Docker
Manager & worker nodes
Task #1: Tạo Swarm cluster
Các bước tiến hành

● Chuẩn bị 3 máy ảo đã cài Docker (có thể sử dụng https://labs.play-with-


docker.com/ )
● Trên máy 1, khởi tạo Swarm cluster bằng lệnh:
docker swarm init --advertise-addr IP-của-máy-ảo
● Hệ thống tạo ra 1 đoạn code để thêm các worker node vào Swarm cluster.
Copy paste đoạn code đó và chạy trên 2 node còn lại
● Trên máy 1, kiểm tra bằng lệnh: docker node ls
Service & tasks
Standalone container Swarm service

● App được triển khai theo từng ● App được triển khai dưới dạng
container service
● Mỗi app là 1 contaner chạy ● Mỗi app có thể là nhiều task
trên 1 node (container) chạy trên nhiều node
● docker run … image ● docker service create … image
Standalone container Swarm service

Nginx Container
Task #2: Triển khai 1 service
chạy image Nginx trên
Swarm cluster
Các bước tiến hành

● Trên node manager: Sử dụng docker service create với image nginx,
expose ra cổng 8080, đặt tên service là nginx-app
● Kiểm tra xem service đang chạy ở node nào:
docker service ps nginx-app
● Gọi vào cổng 8080 từ một node bất kỳ trong Swarm
Task #3: Triển khai service
chạy image jwilder/whoami
gồm 5 tasks
Các bước tiến hành

1. Trên node manager: Sử dụng docker service create với image


jwilder/whoami, expose ra cổng 8081, gồm 5 tasks (--replicas=5), đặt
tên service là whoami-app
2. Gọi vào cổng 8081 từ một node bất kỳ trong Swarm
Service: global vs replicated
Task #4: Tạo global service
Tạo service có tên monitor, mode global, expose ra cổng 8080

docker service create \


--mount type=bind,source=/,destination=/rootfs:ro \
--mount type=bind,source=/var/run,destination=/var/run:rw \
--mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock \
--mount type=bind,source=/sys,destination=/sys:ro \
--mount type=bind,source=/var/lib/docker/,destination=/var/lib/docker:ro \
--mode global \
--name monitor \
-p 8080:8080 \
google/cadvisor:latest
Kiểm tra trên node manager

● Kiểm tra trạng thái service: docker service ps monitor


● Truy cập cổng 8080 từ các node trong Swarm
Ingress overlay network
Ingress overlay network
Host mode published port
Task #5: Host-mode
published port cho global
service
Các bước tiến hành

● Remove published port:


docker service update --publish-rm 8080 monitor
● Kiểm tra trạng thái service: docker service ps monitor
● Add host-mode published port:
docker service update --publish-add mode=host,published=8080,target=8080 monitor

● Kiểm tra lại trạng thái service monitor


● Truy cập cổng 8080 từ các node trong cluster
Internal overlay network
Internal overlay network
Task #6: Tạo internal
overlay network kết nối các
service
Các bước tiến hành

● Tạo 1 overlay network:


docker network create –d overlay my-app
● Tạo service chạy image nginx: gồm 2 tasks, expose cổng 80 của container ra cổng 9001
trên swarm cluster, tên service là my-nginx, đặt trong network my-app
● Tạo service chạy image jwilder/whoami: gồm 5 tasks, tên service là hello-whoami, đặt
trong network my-app
● Docker exec vào 1 container chạy nginx, sau đó chạy 2 lệnh:
ping hello-whoami và curl hello-whoami:80
Task #7: Triển khai ứng dụng
NodeJS to-do list trên Swarm
Stack
Stack – Service - Tasks
Triển khai stack với docker-compose.yml

● Sử dụng chính file docker-compose.yml ở phần docker-compose


● File compose phải để version 3
● Tham khảo: https://docs.docker.com/compose/compose-file/
● Triển khai stack bang file docker-compose.yml:
docker stack deploy -c docker-compose.yml stack-name

You might also like