docker-swarm常用命令

1 初始化为docker swarm manager

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
eval $(docker-machine env node-1)
docker swarm init \
--advertise-addr $(docker-machine ip node-1)
eval $(docker-machine env node-1) docker swarm init \ --advertise-addr $(docker-machine ip node-1)
eval $(docker-machine env node-1)
docker swarm init \
    --advertise-addr $(docker-machine ip node-1)

2 获取swarm集群的token

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
eval $(docker-machine env node-1)
SWARM_MANAGER_TOKEN=$(docker swarm join-token -q manager)
SWARM_WORKER_TOKEN=$(docker swarm join-token -q worker)
eval $(docker-machine env node-1) SWARM_MANAGER_TOKEN=$(docker swarm join-token -q manager) SWARM_WORKER_TOKEN=$(docker swarm join-token -q worker)
eval $(docker-machine env node-1)
SWARM_MANAGER_TOKEN=$(docker swarm join-token -q manager)
SWARM_WORKER_TOKEN=$(docker swarm join-token -q worker)

3 其他机器加入集群(作为manager)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
eval $(docker-machine env node-2)
docker swarm join \
--token $SWARM_MANAGER_TOKEN \
--advertise-addr $(docker-machine ip node-2) \
$(docker-machine ip node-1):2377
eval $(docker-machine env node-2) docker swarm join \ --token $SWARM_MANAGER_TOKEN \ --advertise-addr $(docker-machine ip node-2) \ $(docker-machine ip node-1):2377
eval $(docker-machine env node-2)
docker swarm join \
    --token $SWARM_MANAGER_TOKEN \
    --advertise-addr $(docker-machine ip node-2) \
    $(docker-machine ip node-1):2377

4 其他机器加入集群(作为worker)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
eval $(docker-machine env node-3)
docker swarm join \
--token $SWARM_WORKER_TOKEN \
--advertise-addr $(docker-machine ip node-2) \
$(docker-machine ip node-1):2377
eval $(docker-machine env node-3) docker swarm join \ --token $SWARM_WORKER_TOKEN \ --advertise-addr $(docker-machine ip node-2) \ $(docker-machine ip node-1):2377
eval $(docker-machine env node-3)
docker swarm join \
    --token $SWARM_WORKER_TOKEN \
    --advertise-addr $(docker-machine ip node-2) \
    $(docker-machine ip node-1):2377

5 查看swarm集群所有结点状态

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
mwa8584n72ranq628a7mbzzk8 node-2 Ready Active Reachable
qqvl6z8b64myh0h7wlpc7hs4n * node-1 Ready Active Leader
zqw7tt2f9oiu5y0lv2oo6pqyk node-3 Ready Active
docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS mwa8584n72ranq628a7mbzzk8 node-2 Ready Active Reachable qqvl6z8b64myh0h7wlpc7hs4n * node-1 Ready Active Leader zqw7tt2f9oiu5y0lv2oo6pqyk node-3 Ready Active
docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
mwa8584n72ranq628a7mbzzk8     node-2              Ready               Active              Reachable
qqvl6z8b64myh0h7wlpc7hs4n *   node-1              Ready               Active              Leader
zqw7tt2f9oiu5y0lv2oo6pqyk     node-3              Ready               Active

6 创建一个overlay网络(service用的)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker network create --driver overlay network-name
docker network create --driver overlay network-name
docker network create --driver overlay network-name

7 创建一个service(注意image需要在所有swarm node上都能访问到)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker service create --name service-name --network network-name -p 8080:8080 image-name
docker service create --name service-name --network network-name -p 8080:8080 image-name
docker service create --name service-name --network network-name -p 8080:8080 image-name

注意上面的8080将service的port 8080 暴露给所有node上(host)。

当访问curl "http://node-ip:8080"时候,service会做负载均衡,并转发请求到每个子task上

8 查看service

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker service ls
docker service ls
docker service ls

9 查看service下所有task

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker service ps service-name
docker service ps service-name
docker service ps service-name

10 扩容service

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker service scale service-name=num
docker service scale service-name=num
docker service scale service-name=num

11 删除service

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker service remove service-name
docker service remove service-name
docker service remove service-name

12 列出所有task名字

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker service ps --format "{{.Name}}" service-name
docker service ps --format "{{.Name}}" service-name
docker service ps --format "{{.Name}}" service-name

13 列出所有task的id

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker service ps --format "{{.ID}}" service-name
docker service ps --format "{{.ID}}" service-name
docker service ps --format "{{.ID}}" service-name

14 列出所有task的node

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker service ps --format "{{.Node}}" service-name
docker service ps --format "{{.Node}}" service-name
docker service ps --format "{{.Node}}" service-name

13 找到task对应的container-id

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker inspect --format "{{.Status.ContainerStatus.ContainerID}}" $TASK_ID
docker inspect --format "{{.Status.ContainerStatus.ContainerID}}" $TASK_ID
docker inspect --format "{{.Status.ContainerStatus.ContainerID}}" $TASK_ID

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *