Swarm网络
swarm服务创建出来之后默认使用是bridge网络,能满足容器层次的网络通信,如果想外部访问service就需要暴露服务端口
docker service create --name my-web --replicas=3 --publish 8080:80 192.168.1.10:5000/httpd
创建服务时暴露端口给外部网络即可完成访问
curl 192.168.1.10:8080
<html><body><h1>It works!</h1></body></html>
routing mesh
当我们访问任何节点的8080端口时,swarm内部的load balancer 会将请求转发给web_server其中的一个副本。这就是routing mesh的作用。所以,无论访问那个节点,及时该节点上没有运行service的副本,最终都能访问到service。
ingress网络
当使用 --publish 8080:80 参数暴露服务端口之后,swarm会重新配置service
docker network ls
NETWORK ID NAME DRIVER SCOPE
3dfcfe7b40db bridge bridge local
09a7a09d1a26 docker_gwbridge bridge local
1f1ddcbcbed0 host host local
mbi9c518a10d ingress overlay swarm
usmsavjddv2h my_net overlay swarm
8db7892d6d39 none null local
会使用一个默认名字为ingress的overlay的网络来保证外部的通信
网段为10.0.0.0
docker network inspect -f {{.IPAM.Config}} ingress
[{10.0.0.0/24 10.0.0.1 map[]}]
查看 service的网络模式
docker service inspect my-web --pretty
ID: yud8sfn1uk0dkj3uzv702hys9
Name: my-web
Service Mode: Replicated
Replicas: 3
Placement:
UpdateConfig:
Parallelism: 1
On failure: pause
Monitoring Period: 5s
Max failure ratio: 0
Update order: stop-first
RollbackConfig:
Parallelism: 1
On failure: pause
Monitoring Period: 5s
Max failure ratio: 0
Rollback order: stop-first
ContainerSpec:
Image: 192.168.1.10:5000/httpd:latest@sha256:ad116b4faf32a576572c1501e3c83ecae52ed3ba161de2f50a89d24b796bd3eb
Init: false
Resources:
Endpoint Mode: vip
Ports:
PublishedPort = 8080
Protocol = tcp
TargetPort = 80
PublishMode = ingress
查看服务的ip
docker service inspect -f {{.Endpoint.VirtualIPs}} my-web
[{mbi9c518a10dan34qe5eodsd4 10.0.0.5/24}]
在node1上查看容器IP
docker inspect my-web.1.obag1ykqlqoa65s1le7wcvnge
"Networks": {
"ingress": {
"IPAMConfig": {
"IPv4Address": "10.0.0.6"
},
service之间互相通信
通过服务发现就可以实现服务与服务之间的互相通信
创建overlay网络
docker network create --driver overlay my_net
docker network ls
NETWORK ID NAME DRIVER SCOPE
3dfcfe7b40db bridge bridge local
09a7a09d1a26 docker_gwbridge bridge local
1f1ddcbcbed0 host host local
mbi9c518a10d ingress overlay swarm
usmsavjddv2h my_net overlay swarm
8db7892d6d39 none null local
使用新创建的网络创建服务
docker service create --name web1 --replicas=3 --network my_net 192.168.1.10:5000/httpd
nfovhu3bqs56xm35ltxwiotg1
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
再创建一个服务用于测试
docker service create --name webtest --replicas=1 --network my_net 192.168.1.10:5000/busybox sleep 1000000000
2yevf5q1m3rck0omocpbccmk1
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
查看服务所在节点
docker service ps webtest
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
e3fsmw8pcuk2 webtest.1 192.168.1.10:5000/busybox:latest swarm Running Running 54 seconds ago
在swarm上查看服务名称
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18d449c4a415 192.168.1.10:5000/busybox:latest "sleep 1000000000" 2 minutes ago Up 2 minutes webtest.1.e3fsmw8pcuk23lbcjpao9vvsm
ping web1进行测试
docker exec -it webtest.1.e3fsmw8pcuk23lbcjpao9vvsm ping -c 3 web1
PING web1 (10.0.1.2): 56 data bytes
64 bytes from 10.0.1.2: seq=0 ttl=64 time=0.078 ms
64 bytes from 10.0.1.2: seq=1 ttl=64 time=0.068 ms
64 bytes from 10.0.1.2: seq=2 ttl=64 time=0.086 ms
可以看到ping的ip为 10.0.1.2 这个是容器的vip 对应服务网络的ip
docker network inspect my_net
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "10.0.1.0/24",
"Gateway": "10.0.1.1"
}
]
},
访问vip时会把请求发给副本
服务的更新与回滚
下载 httpd:2.2 与 2.4
docker pull httpd:2.2
docker pull httpd:2.4
打包到私有仓库
创建一个httpd:2.2的服务
docker service create --replicas=3 --name test 192.168.1.10:5000/httpd:2.2
ngx4a3lyacal0jvhukd8imvw1
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
对服务进行镜像更新
docker service update --image 192.168.1.10:5000/httpd:2.4 test
test
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
查看
docker service ps test
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
x1wmlcsw0doa test.1 192.168.1.10:5000/httpd:2.4 node2 Running Running 28 seconds ago
s0s07gphqhw0 \_ test.1 192.168.1.10:5000/httpd:2.2 node2 Shutdown Shutdown 29 seconds ago
8kn2mc6b69a1 test.2 192.168.1.10:5000/httpd:2.4 swarm Running Running 26 seconds ago
xkyqfogi0qjl \_ test.2 192.168.1.10:5000/httpd:2.2 swarm Shutdown Shutdown 27 seconds ago
zavwy03ro7sa test.3 192.168.1.10:5000/httpd:2.4 node1 Running Running 24 seconds ago
zvrl48h7iqb4 \_ test.3 192.168.1.10:5000/httpd:2.2 node1 Shutdown Shutdown 25 seconds ago
更新设置
再创建一个服务
docker service create --replicas=6 --name test1 192.168.1.10:5000/httpd:2.2
3at50vxgwwub8vw46wm35cg3g
overall progress: 6 out of 6 tasks
1/6: running [==================================================>]
2/6: running [==================================================>]
3/6: running [==================================================>]
4/6: running [==================================================>]
5/6: running [==================================================>]
6/6: running [==================================================>]
verify: Service converged
设置总共更新4个 一次更新1个 每次间隔1分钟
docker service update --image 192.168.1.10:5000/httpd:2.4 --replicas=4 --update-parallelism 1 --update-delay 1m test1
###--update-parallelism 更新个数
###--update-delay 更新间隔
test1
overall progress: 1 out of 4 tasks
1/4: running [==================================================>]
2/4:
3/4:
4/4:
1分钟后
docker service update --image 192.168.1.10:5000/httpd:2.4 --replicas=4 --update-parallelism 1 --update-delay 1m test1
test1
overall progress: 2 out of 4 tasks
1/4: running [==================================================>]
2/4: running [==================================================>]
对服务进行回滚
docker service update --rollback test1
test1
rollback: manually requested rollback
overall progress: rolling back update: 6 out of 6 tasks
1/6: running [> ]
2/6: running [> ]
3/6: running [> ]
4/6: running [> ]
5/6: running [> ]
6/6: running [> ]
verify: Service converged
docker service ps test1
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
w5w06fi6rxh1 test1.1 192.168.1.10:5000/httpd:2.2 node1 Running Running 28 seconds ago
vx2025zy1ozl \_ test1.1 192.168.1.10:5000/httpd:2.4 node1 Shutdown Shutdown 28 seconds ago
p6e9jx6kp6sy \_ test1.1 192.168.1.10:5000/httpd:2.2 swarm Shutdown Shutdown 5 minutes ago
05r430txtg0v test1.2 192.168.1.10:5000/httpd:2.2 node1 Running Running 33 seconds ago
vhwq0ty0y7au \_ test1.2 192.168.1.10:5000/httpd:2.4 node1 Shutdown Shutdown 33 seconds ago
aks10m44cvsd \_ test1.2 192.168.1.10:5000/httpd:2.2 node1 Shutdown Shutdown 4 minutes ago
ebchjgfd3dug test1.3 192.168.1.10:5000/httpd:2.2 swarm Running Running 30 seconds ago
hpy36f42d0hq \_ test1.3 192.168.1.10:5000/httpd:2.4 node2 Shutdown Shutdown 31 seconds ago
mqc1ednxyqtx \_ test1.3 192.168.1.10:5000/httpd:2.2 node2 Shutdown Shutdown 3 minutes ago
z0wcpq6y7sfw test1.4 192.168.1.10:5000/httpd:2.2 node2 Running Running 35 seconds ago
57ae6rjp4p17 \_ test1.4 192.168.1.10:5000/httpd:2.4 swarm Shutdown Shutdown 36 seconds ago
1v90jou0ak5n \_ test1.4 192.168.1.10:5000/httpd:2.2 swarm Shutdown Shutdown 2 minutes ago
rjtxba4plza8 test1.5 192.168.1.10:5000/httpd:2.2 swarm Running Running 38 seconds ago
cpldu6tnr4ek test1.6 192.168.1.10:5000/httpd:2.2 node2 Running Running 38 seconds ago
版本回到了2.2