docker-swarm常用命令

1 初始化为docker swarm manager

eval $(docker-machine env node-1)
docker swarm init \
    --advertise-addr $(docker-machine ip node-1)

2 获取swarm集群的token

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)

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)

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集群所有结点状态

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用的)

docker network create --driver overlay network-name

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

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

docker service ls

9 查看service下所有task

docker service ps service-name

10 扩容service

docker service scale service-name=num

11 删除service

docker service remove service-name

12 列出所有task名字

docker service ps --format "{{.Name}}" service-name

13 列出所有task的id

docker service ps --format "{{.ID}}" service-name

14 列出所有task的node

docker service ps --format "{{.Node}}" service-name

13 找到task对应的container-id

docker inspect --format "{{.Status.ContainerStatus.ContainerID}}" $TASK_ID

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published.