环境说明:

IP主机名
192.168.111.152docker-node1
192.168.111.153docker-node2
192.168.111.154docker-node3

docker-node1作为swarm管理节点

创建集群

创建主节点

[[email protected] ~]# docker swarm init --advertise-addr 192.168.111.152
Swarm initialized: current node (ntnheq4yt2r4sr2aml1l13rkt) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-1f3thb4mu0rfzlh3mn14i2xkn2q8yvz1r5c438x3zqsbu3dybt-0z6ie4bn7yb20pc1xnsed8z1s \
    192.168.111.152:2377

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

将node节点加入集群

[[email protected]ker-node2 ~]# docker swarm join \
    --token SWMTKN-1-1f3thb4mu0rfzlh3mn14i2xkn2q8yvz1r5c438x3zqsbu3dybt-0z6ie4bn7yb20pc1xnsed8z1s \
    192.168.111.152:2377
    
[[email protected] ~]# docker swarm join \
    --token SWMTKN-1-1f3thb4mu0rfzlh3mn14i2xkn2q8yvz1r5c438x3zqsbu3dybt-0z6ie4bn7yb20pc1xnsed8z1s \
    192.168.111.152:2377

在主节点查看集群信息

[[email protected] ~]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
2mfuq3t5oidnxasut31689ow1     docker-node3        Ready               Active
cdp0z0tpqmfc5lpuzdd4gpk0x     docker-node2        Ready               Active
ntnheq4yt2r4sr2aml1l13rkt *   docker-node1        Ready               Active              Leader

集群中创建容器

[[email protected] ~]# docker service create --replicas 1 --name nginx -d nginx
  • replicas: 创建副本数量

查看各节点容器分布状况

[[email protected] ~]# docker service ps nginx
ID                  NAME                IMAGE              NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
js69hnpp9pf7        nginx.1             nginx:latest       docker-node3        Running             Running 27 minutes ago
qznuk4gl6osp        nginx.2             nginx:latest       docker-node2        Running             Running 27 minutes ago

将节点移出集群

[[email protected] ~]# docker swarm leave

swarm集群自定义网络

因集群中跨主机间的docker容器网络不通,所以需要通过自定义网络将各主机间的容器连到同一个网段

查看docker 默认网络

[[email protected] ~]# docker network list
NETWORK ID          NAME                DRIVER              SCOPE
2145cfc9514e        bridge              bridge              local
60a6c66c590d        docker_gwbridge     bridge              local
3ef8c4c1c66e        host                host                local
7wsqirbzvzvu        ingress             overlay             swarm
665ae0d15f1b        none                null                local

添加自定义网络

[[email protected] ~]# docker network create --driver overlay mynet

创建容器,并将容器加入自定义网络

[[email protected] ~]# docker service create --replicas 2 --network mynet --name nginx -d nginx

原文转载: https://www.jianshu.com/p/77c4c62d9afe

说点什么
支持Markdown语法
好耶,沙发还空着ヾ(≧▽≦*)o
Loading...