参考:
示例:
1、manager:
➜ ~ docker swarm init
Swarm initialized: current node (y53c3ts1ycqknb6cfvu56jg2w) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-18kbws8xv6mnfcsvbwo1iwbvdeu139lxbibc8r609t2quwpvtu-e8l9cms7q2rie9uagdvls1eka \
192.168.10.209:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
➜ ~ docker network create -d=overlay onet
rys5x3bro7uyol04dvwex3k1g
➜ ~ docker network ls
NETWORK ID NAME DRIVER SCOPE
c3bfd973008e bridge bridge local
4a6d69a117e1 docker_gwbridge bridge local
a5e6a37dff91 host host local
wc0pv6sc2ac7 ingress overlay swarm
18a245c8f8dc none null local
rys5x3bro7uy onet overlay swarm
➜ ~ docker network inspect onet
[
{
"Name": "onet",
"Id": "rys5x3bro7uyol04dvwex3k1g",
"Created": "0001-01-01T00:00:00Z",
"Scope": "swarm",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": []
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"Containers": null,
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "4097"
},
"Labels": null
}
]
➜ ~ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
y53c3ts1ycqknb6cfvu56jg2w * hub162 Ready Active Leader
z9qpkd74pvdkrt7zjutus07p3 zookeeper-g3 Ready Active
➜ ~ docker service create \
> --replicas 3 \
> --name my-web \
> --network onet \
> nginx:1.11
image nginx:1.11 could not be accessed on a registry to record
its digest. Each node will access nginx:1.11 independently,
possibly leading to different nodes running different
versions of the image.
l4n94rxn4540h96r9zgotxqhj
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.
➜ ~ docker network inspect onet
[
{
"Name": "onet",
"Id": "rys5x3bro7uyol04dvwex3k1g",
"Created": "2017-08-02T12:22:05.548253892+08:00",
"Scope": "swarm",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "10.0.0.0/24",
"Gateway": "10.0.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"Containers": {},
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "4097"
},
"Labels": {},
"Peers": [
{
"Name": "hub162-c0c6d4fedbc9",
"IP": "192.168.10.209"
},
{
"Name": "zookeeper-g3-6aa8c197af12",
"IP": "192.168.10.212"
}
]
}
]
➜ ~ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
l4n94rxn4540 my-web replicated 3/3 nginx:1.11
➜ ~ docker service ps my-web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
npd3duiub491 my-web.1 nginx:1.11 zookeeper-g3 Running Running 5 minutes ago
kl0x0egf8ide my-web.2 nginx:1.11 hub162 Running Running 4 minutes ago
wvotp9pn4jdl my-web.3 nginx:1.11 zookeeper-g3 Running Running 5 minutes ago
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09b076da0dab nginx:1.11 "nginx -g 'daemon ..." 4 minutes ago Up 4 minutes 80/tcp, 443/tcp my-web.2.kl0x0egf8ideavrukmuxkkuvs
➜ ~ docker exec -it 09 bash
root@09b076da0dab:/# ping 10.0.0.3
PING 10.0.0.3 (10.0.0.3): 56 data bytes
64 bytes from 10.0.0.3: icmp_seq=0 ttl=64 time=1.370 ms
64 bytes from 10.0.0.3: icmp_seq=1 ttl=64 time=0.722 ms
64 bytes from 10.0.0.3: icmp_seq=2 ttl=64 time=0.874 ms
^C--- 10.0.0.3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.722/0.989/1.370/0.277 ms
root@09b076da0dab:/# ping 10.0.0.5
PING 10.0.0.5 (10.0.0.5): 56 data bytes
64 bytes from 10.0.0.5: icmp_seq=0 ttl=64 time=0.994 ms
64 bytes from 10.0.0.5: icmp_seq=1 ttl=64 time=0.504 ms
64 bytes from 10.0.0.5: icmp_seq=2 ttl=64 time=0.756 ms
^C--- 10.0.0.5 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.504/0.751/0.994/0.200 ms
root@09b076da0dab:/#
2、worker:
[root@zookeeper-g3 ~]# docker swarm join \
> --token SWMTKN-1-18kbws8xv6mnfcsvbwo1iwbvdeu139lxbibc8r609t2quwpvtu-e8l9cms7q2rie9uagdvls1eka \
> 192.168.10.209:2377
This node joined a swarm as a worker.
[root@zookeeper-g3 ~]# docker network inspect onet
[
{
"Name": "onet",
"Id": "rys5x3bro7uyol04dvwex3k1g",
"Created": "2017-08-02T12:22:05.365075116+08:00",
"Scope": "swarm",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "10.0.0.0/24",
"Gateway": "10.0.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"Containers": {
"a9680a11da29532db9f5b5b2abd85545809ad73112101c1e5378d8bb820f6d01": {
"Name": "my-web.1.npd3duiub4915d24r4ibhqjva",
"EndpointID": "a83574e59970320a25765122d1f3f0bc5a86f60198d2c75aa2fccc21ee18b9f4",
"MacAddress": "02:42:0a:00:00:03",
"IPv4Address": "10.0.0.3/24",
"IPv6Address": ""
},
"dd66dc76b5c5841f669c8c2c8b96f80266c9e4bd90db29455236b009e7278d32": {
"Name": "my-web.3.wvotp9pn4jdlx9se84557dsc7",
"EndpointID": "e450fe8082e20225476d62716ec046b395afdbe429fbeaa181e6607f4e6f7b8c",
"MacAddress": "02:42:0a:00:00:05",
"IPv4Address": "10.0.0.5/24",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "4097"
},
"Labels": {},
"Peers": [
{
"Name": "zookeeper-g3-6aa8c197af12",
"IP": "192.168.10.212"
},
{
"Name": "hub162-c0c6d4fedbc9",
"IP": "192.168.10.209"
}
]
}
]
[root@zookeeper-g3 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd66dc76b5c5 nginx:1.11 "nginx -g 'daemon ..." 33 seconds ago Up 27 seconds 80/tcp, 443/tcp my-web.3.wvotp9pn4jdlx9se84557dsc7
a9680a11da29 nginx:1.11 "nginx -g 'daemon ..." 33 seconds ago Up 28 seconds 80/tcp, 443/tcp my-web.1.npd3duiub4915d24r4ibhqjva
[root@zookeeper-g3 ~]# docker exec -it dd bash
root@dd66dc76b5c5:/# ping 10.0.0.4
PING 10.0.0.4 (10.0.0.4): 56 data bytes
64 bytes from 10.0.0.4: icmp_seq=0 ttl=64 time=0.638 ms
64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=0.648 ms
^C--- 10.0.0.4 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.638/0.643/0.648/0.000 ms
3、promote:
➜ ~ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
y53c3ts1ycqknb6cfvu56jg2w * hub162 Ready Active Leader
z9qpkd74pvdkrt7zjutus07p3 zookeeper-g3 Ready Active
➜ ~ docker node promote zookeeper-g3
Node zookeeper-g3 promoted to a manager in the swarm.
➜ ~ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
y53c3ts1ycqknb6cfvu56jg2w * hub162 Ready Active Leader
z9qpkd74pvdkrt7zjutus07p3 zookeeper-g3 Ready Active Reachable
➜ ~
网友评论