docker swarm 集群搭建

创建一个集群

[vagrant@node1 ~]$ docker swarm init --advertise-addr 192.168.9.101
Swarm initialized: current node (9fv7wb4zve235hltmchxprez7) is now a manager.

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

    docker swarm join --token SWMTKN-1-1wk8aduuxnay6ek866xaj31ixqnsib56b3p65hpzpcbbzo1qla-alcf8l1n7qpv7h7d6hg35scni 192.168.9.101:2377

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

查看网络

[vagrant@node1 ~]$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
83c5df0f97ac        bridge              bridge              local
7d81897af218        docker_gwbridge     bridge              local
b13ca5d9c552        host                host                local
0vfhc5c85dwi        ingress             overlay             swarm
c427f9d683f4        none                null                local

分别在另外两台上面执行

[vagrant@node2 ~]$ docker swarm join --token SWMTKN-1-1wk8aduuxnay6ek866xaj31ixqnsib56b3p65hpzpcbbzo1qla-alcf8l1n7qpv7h7d6hg35scni 192.168.9.101:2377
This node joined a swarm as a worker.
[vagrant@node3 ~]$ docker swarm join --token SWMTKN-1-1wk8aduuxnay6ek866xaj31ixqnsib56b3p65hpzpcbbzo1qla-alcf8l1n7qpv7h7d6hg35scni 192.168.9.101:2377
This node joined a swarm as a worker.

查看集群节点

[vagrant@node1 ~]$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
9fv7wb4zve235hltmchxprez7 *   node1               Ready               Active              Leader              19.03.5
lypcp8z6s2boi2897ni0g5oc5     node2               Ready               Active                                  19.03.5
o0hmpgi59tms2lt65wlgzivpn     node3               Ready               Active                                  19.03.5

提升node2,node3 为备用master,高可用

[vagrant@node1 ~]$ docker node promote node2
[vagrant@node1 ~]$ docker node promote node3

查看效果:

[vagrant@node1 ~]$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
9fv7wb4zve235hltmchxprez7 *   node1               Ready               Active              Leader              19.03.5
lypcp8z6s2boi2897ni0g5oc5     node2               Ready               Active              Reachable           19.03.5
o0hmpgi59tms2lt65wlgzivpn     node3               Ready               Active                                  19.03.5

创建服务

[vagrant@node1 ~]$ docker service create --name test1 alpine ping www.baidu.com
i5lip5az4w5ehv28q2fn3jxji
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
[vagrant@node1 ~]$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
i5lip5az4w5e        test1               replicated          1/1                 alpine:latest

创建nginx服务

vagrant@node1 ~]$ docker service create --name nginx nginx
m511a51xd6js4lflrrtr91vf7
overall progress: 0 out of 1 tasks
overall progress: 0 out of 1 tasks
overall progress: 0 out of 1 tasks
overall progress: 0 out of 1 tasks
overall progress: 0 out of 1 tasks
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged

爆过 8080服务

[vagrant@node1 ~]$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
m511a51xd6js        nginx               replicated          1/1                 nginx:latest
i5lip5az4w5e        test1               replicated          1/1                 alpine:latest
[vagrant@node1 ~]$ docker service update --publish-add 8080:80 nginx
nginx
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
[vagrant@node1 ~]$

扩容:

[vagrant@node1 ~]$ docker service scale nginx=3
nginx scaled to 3
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]

删除服务:

[vagrant@node1 ~]$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
m511a51xd6js        nginx               replicated          3/3                 nginx:latest        *:8080->80/tcp
i5lip5az4w5e        test1               replicated          1/1                 alpine:latest
[vagrant@node1 ~]$ docker service rm nginx
nginx
[vagrant@node1 ~]$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
i5lip5az4w5e        test1               replicated          1/1                 alpine:latest

创建网络

[vagrant@node1 ~]$ docker network ls
NETWORK ID          NAME                 DRIVER              SCOPE
83c5df0f97ac        bridge               bridge              local
7d81897af218        docker_gwbridge      bridge              local
b13ca5d9c552        host                 host                local
0vfhc5c85dwi        ingress              overlay             swarm
c427f9d683f4        none                 null                local
meps4tilzidi        test_tanzi-overlay   overlay             swarm
[vagrant@node1 ~]$ docker network
connect     create      disconnect  inspect     ls          prune       rm
[vagrant@node1 ~]$ docker network create -d overlay tanzi-overlay
p3ssr7hpilfr1k6qry6tp8nc5

创建服务并指定网络

[vagrant@node1 ~]$ docker service create --network tanzi-overlay --name nginx -p 8080:80 nginx
p3b79k42mvex1op5mwk9zfg1z
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
[vagrant@node1 ~]$ docker service create --network tanzi-overlay --name alpine alpine ping www.baidu.com
34tkwuppcgb5pqppj1zx22hhr
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged

定义一种不给外面访问的dnsrr的访问模式

[vagrant@node1 ~]$ docker service create --name nginx-b --endpoint-mode dnsrr nginx
u9snzqigr65x9xwb131jkm1ao
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged

更新一个服务

[vagrant@node1 ~]$ docker service update --network-add tanzi-overlay nginx-b
nginx-b
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged

 

定一个组,确定依赖关系:

[vagrant@node1 ~]$ docker stack services
nginx  test2
[vagrant@node1 ~]$ docker stack services nginx
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
7ti9w26od1i3        nginx_nginx         replicated          1/1                 nginx:latest        *:8080->80/tcp
cl1awlwzmxaq        nginx_alpine        replicated          1/1                 alpine:latest
[vagrant@node1 ~]$ docker stack deploy -c test.yml nginx
Updating service nginx_alpine (id: cl1awlwzmxaqybwyz09a3sr5t)
Updating service nginx_nginx (id: 7ti9w26od1i3yec6jmxcalwfz)
[vagrant@node1 ~]$ docker stack services
"docker stack services" requires exactly 1 argument.
See 'docker stack services --help'.

Usage:  docker stack services [OPTIONS] STACK

List the services in the stack
[vagrant@node1 ~]$ docker stack services ls
Nothing found in stack: ls
[vagrant@node1 ~]$ docker stack services
nginx  test2
[vagrant@node1 ~]$ docker stack services test2
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
celxgkxf3z31        test2_alpine        replicated          1/1                 alpine:latest
[vagrant@node1 ~]$ docker stack services nginx
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
7ti9w26od1i3        nginx_nginx         replicated          1/1                 nginx:latest        *:8080->80/tcp
cl1awlwzmxaq        nginx_alpine        replicated          1/1                 alpine:latest
version: "3.7"
services:
  nginx:
    image: nginx
    networks:
      - "tanzi-overlay"
    ports:
      - "8080:80"

  alpine:
    image: alpine
    networks:
      - "tanzi-overlay"
    command:
      - "ping"
      - "nginx"
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
    depends_on:
      - nginx

networks:
  tanzi-overlay:
    external: true

 

上一篇:vagrant+virtualbox


下一篇:使用PHP通知未定义变量