099、如何访问Service (Swarm06)

 
前面已经学习了如何部署Service吗,也验证了swarm的failover特性,下面我们要学习的是如何访问Service。
 
下面我们删除之前的Service,重新部署一个Service
 
内部访问Service
 
root@host03:~# docker service  rm web_server    #    删除之前的Service web_server
web_server
root@host03:~# docker service create --name web_server --replicas=2 httpd    #    创建新的Service,并设置副本数为2
1hgm1w94dvuxlbsen9lmsefvz
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged
root@host03:~# docker service ps web_server    #    查看新建Service 容器分布情况
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
s4o9mtd8xvqe        web_server.1        httpd:latest        host01              Running             Running 12 seconds ago                       
kq66bz6omujf        web_server.2        httpd:latest        host02              Running             Running 12 seconds ago
root@host01:~# docker inspect web_server.1.s4o9mtd8xvqe8yqcnbyuom26f | jq .[0].NetworkSettings    #    查看某容器内部IP地址
{
  "Bridge": "",
  "SandboxID": "407fde9cdf85507b7e5c635ea3313e41e6ef54da9242837e292526cf0398000d",
  "HairpinMode": false,
  "LinkLocalIPv6Address": "",
  "LinkLocalIPv6PrefixLen": 0,
  "Ports": {
    "80/tcp": null
  },
  "SandboxKey": "/var/run/docker/netns/407fde9cdf85",
  "SecondaryIPAddresses": null,
  "SecondaryIPv6Addresses": null,
  "EndpointID": "a10f1d6f81f492f8b7911997a0837d499f8bbc733c274c955e479c0fedeec433",
  "Gateway": "172.17.0.1",
  "GlobalIPv6Address": "",
  "GlobalIPv6PrefixLen": 0,
  "IPAddress": "172.17.0.2",
  "IPPrefixLen": 16,
  "IPv6Gateway": "",
  "MacAddress": "02:42:ac:11:00:02",
  "Networks": {
    "bridge": {
      "IPAMConfig": null,
      "Links": null,
      "Aliases": null,
      "NetworkID": "d4b0862f06c8f2f16802b3fe9175743e7afdc2396322ff19e656bebca4d9a940",
      "EndpointID": "a10f1d6f81f492f8b7911997a0837d499f8bbc733c274c955e479c0fedeec433",
      "Gateway": "172.17.0.1",
       "IPAddress": "172.17.0.2",
      "IPPrefixLen": 16,
      "IPv6Gateway": "",
      "GlobalIPv6Address": "",
      "GlobalIPv6PrefixLen": 0,
      "MacAddress": "02:42:ac:11:00:02",
      "DriverOpts": null
    }
  }
}
root@host01:~# curl http://172.17.0.2    #    内部访问验证
<html><body><h1>It works!</h1></body></html>
 
外部访问Service
 
root@host03:~# docker service update --publish-add 8080:80 web_server    #    更新Service配置,添加端口映射,将容器的80端口映射到host的8080端口
web_server
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged
root@host03:~# curl http://10.12.31.211:8080    #    访问 host01 IP 验证外部访问
<html><body><h1>It works!</h1></body></html>
root@host03:~# curl http://10.12.31.212:8080    #    访问 host02 IP 验证外部访问
<html><body><h1>It works!</h1></body></html>
root@host03:~# curl http://10.12.31.213:8080    #    访问 host03 IP 验证外部访问(host03上没有容器,也可以访问成功,这是swarm的一个特性 routing mesh)
<html><body><h1>It works!</h1></body></html>
 
 

猜你喜欢

转载自www.cnblogs.com/www1707/p/10872741.html
099