注:在manage上创建overlay网络后,各node上并没有同步创建该网络,只有当使用该overlay网络的容器在node节点上运行时,才会在node节点上自动创建,容器删除后,网络也会在node节点上删除。
(4)将容器绑定到自定义的网络中
$docker service create --network net3 --replicas 4 --name time-php registry.cntv.net/heqin/tvtime-php:v0.85dongsi
5qiv4hhv7ra5p65fpq2v6ok0x
$docker service ls
5qiv4hhv7ra5 time-php 4/4 registry.cntv.net/heqin/tvtime-php:v0.85dongsi
$docker service ps time-php
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
529kuyazgxxl8l0eds4f740nu time-php.1 registry.cntv.net/heqin/tvtime-php:v0.85dongsi swarm3 Running Running 32 seconds ago
brdelpbuwm3p9cujy6k78cpzo time-php.2 registry.cntv.net/heqin/tvtime-php:v0.85dongsi swarm3 Running Running 33 seconds ago
3csp6fmrsg5cvski270732q98 time-php.3 registry.cntv.net/heqin/tvtime-php:v0.85dongsi swarm2 Running Running 34 seconds ago
2ismj5zt9o3t2hfe1j1lx7dcw time-php.4 registry.cntv.net/heqin/tvtime-php:v0.85dongsi swarm4 Running Running 33 seconds ago
进入swarm3 ,可以看到net3网络下有两个容器。
$docker docker network inspect net3
[
{
"Name": "net3",
"Id": "d83qc9rgkkj1ws8kvmod4x759",
"Scope": "swarm",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "10.88.0.0/24",
"Gateway": "10.88.0.1"
}
]
},
"Internal": false,
"Containers": {
"d465611683b69f20c28ec9eb276a9e4088867e35508ecf2bf28c07c5d3e1b33a": {
"Name": "time-php.1.529kuyazgxxl8l0eds4f740nu",
"EndpointID": "ca667c89550f88eabf54eeaaaa3c55c678ec8184f121fc845bbaab1cd4be20ff",
"MacAddress": "02:42:0a:58:00:05",
"IPv4Address": "10.88.0.5/24",
"IPv6Address": ""
},
"d8fa64c4f8357418304ed1f19a049e4aac196c94ef50a27a0686ebe4ee734225": {
"Name": "time-php.2.brdelpbuwm3p9cujy6k78cpzo",
"EndpointID": "ab45bcf8e73d114e9bdf97cac128750a6c7a073149783746a9962c6cd4c599a8",
"MacAddress": "02:42:0a:58:00:06",
"IPv4Address": "10.88.0.6/24",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "257"
},
"Labels": {}
}
用swarm3的容器去ping swarm2主机上的容器,是可以ping通的
$docker exec -it d8fa64c4f835 ping 10.88.0.3
PING 10.88.0.3 (10.88.0.3) 56(84) bytes of data.
64 bytes from 10.88.0.3: icmp_seq=1 ttl=64 time=0.611 ms
64 bytes from 10.88.0.3: icmp_seq=2 ttl=64 time=5.45 ms
64 bytes from 10.88.0.3: icmp_seq=3 ttl=64 time=0.439 ms
64 bytes from 10.88.0.3: icmp_seq=4 ttl=64 time=0.586 ms
64 bytes from 10.88.0.3: icmp_seq=5 ttl=64 time=0.603 ms
8:swarm基本命令
swarm集群创建与管理
docker swarm Command
Commands:
init Initialize a swarm
join Join a swarm as a node and/or manager
join-token Manage join tokens
update Update the swarm
leave Leave a swarm
swarm服务创建与管理
docker service Command
Commands:
create Create a new service
inspect Display detailed information on one or more services
ps List the tasks of a service
ls List services
rm Remove one or more services
scale Scale one or multiple services
update Update a service
swarm服务创建选项
docker service create [OPTIONS] IMAGE
Options:
--constraint value Placement constraints (default [])
--container-label value Container labels (default [])
--endpoint-mode string Endpoint mode (vip or dnsrr)
-e, --env value Set environment variables (default [])
--help Print usage
-l, --label value Service labels (default [])
--limit-cpu value Limit CPUs (default 0.000)
--limit-memory value Limit Memory (default 0 B)
--log-driver string Logging driver for service
--log-opt value Logging driver options (default [])