---
# System prepended metadata

title: Lab Docker SWARM (docker service)

---

# Lab Docker SWARM (docker service)


![](https://i.imgur.com/PFbfeCd.png)


### Cài đặt docker swarm visualizer

```
docker run -it -d -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer
```
Truy cập  docker swarm visualizer theo địa chỉ http://master-node-ip:8080


## 1. Manager node
### Tạo manager node
``` 
docker swarm init
---------------------
Swarm initialized: current node (ckeawi94gefztsgmlhzehlv6z) is now a manager.

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

    docker swarm join --token SWMTKN-1-1jepva9466vm9jv4io0m3n1cmtjs621qtfh1ithmpo66a03nux-ec83kj23obl947eqaafz7icrn 10.0.1.76:2377

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

### Thêm manager node
```
docker swarm join-token manager
----------------------------
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-1jepva9466vm9jv4io0m3n1cmtjs621qtfh1ithmpo66a03nux-c68slgwpsmdfkj4u4lhemfqws 10.0.1.76:2377
```

### Chuyển worker node thành manager node
```
docker node promote worker1
```
### Chuyển manager node thành worker node
```
docker node demote worker1
```
## 2. Tạo Worker node

```
docker swarm join --token [token] 10.0.1.76:2377
```

## 3. Triển khai dịch vụ trên SWARM


### Service replicas
```
docker service create -d --name nginx_service -p 8081:80 --replicas 2 nginx:latest

docker service ls
docker service ps nginx_service
```
### Service global
```
docker service create -d --name nginx_service_global -p 80:80 --mode global nginx:latest

docker service ls
docker service ps  nginx_service_global
```
### Scale
```
docker service scale nginx_service=3
docker service scale nginx_service_global=3
```
### Update service
```
# Change port
docker service update --publish-add published=8082,target=80 nginx_service_global

# Check
docker service inspect --format '{{json .Endpoint.Ports}}' nginx_service_global

# update limit resource
docker service update --limit-cpu="0.5" --limit-memory=150MB nginx_service

# Check
docker service inspect --format '{{json .Spec.TaskTemplate.Resources}}' nginx_service

# update image
docker service update --image nginx:1.23.3 nginx_service --update-delay 10s
docker service update --image nginx:latest nginx_service --update-delay 10s

docker service update --image nginx:1.23.3 nginx_service_global --update-delay 10s
```
### Kiểm tra khả năng tự khôi phục
```
docker stop $(docker ps -a -q)
```

### Kiểm tra khả năng rollback
```
docker service update --image nginx:1.23.3 nginx_service --update-delay 10s

docker service update --image nginx:latest nginx_service --update-delay 10s

docker service update --rollback nginx_service

```
## Một số lệnh khác
```
docker node ls
docker swarm leave -f
docker node rm <node-name>
docker node update --availability drain node1
docker node update --availability active node1
# Xóa tất cả các container & images

docker rm -f $(docker ps -a -q)
docker rmi -f $(docker images -a -q)

# Kiểm tra
docker ps -a
docker images -a
```

