抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

ingress overlay网络

以下是来至官方文档——Use overlay networks 的一段

When you initialize a swarm or join a Docker host to an existing swarm, two new networks are created on that Docker host:

  • an overlay network called ingress, which handles control and data traffic related to swarm services. When you create a swarm service and do not connect it to a user-defined overlay network, it connects to the ingress network by default.
  • a bridge network called docker_gwbridge, which connects the individual Docker daemon to the other daemons participating in the swarm.

大致意思就是说ingress是集群服务默认使用的overlay网络,用于处理与集群服务有关的控制(负载)和数据流量。

在容器内的应用无法使用此网络进行通信,比如:在只有一个leader的集群上开两个服务:tomcatv1tomcatv2,以交互的方式进入tomcatv2,并使用此网络去请求tomcat8080端口,是无法访问的(即便两个IP能够ping通)

创建两个tomcat服务

1
2
docker service create -p 8080:8080 --name tomcatv1 tomcat
docker service create -p 80:8080 --name tomcatv2 tomcat

交互进入tomcatv2

1
docker exec -it tomcatv2.1.i5v2a2vnocgxguoup6fh7jt55 bash

请求tomcatv1

1
2
3
4
5
6
7
8
//ping
root@3a5b888f644a:/usr/local/tomcat# ping 10.255.0.18
PING 10.255.0.18 (10.255.0.18) 56(84) bytes of data.
64 bytes from 10.255.0.18: icmp_seq=1 ttl=64 time=0.067 ms
64 bytes from 10.255.0.18: icmp_seq=2 ttl=64 time=0.080 ms

//curl 这里不会有任何结果,可以和 curl localhost:8080 对比一下结果
root@3a5b888f644a:/usr/local/tomcat# curl localhost:8080

自定义overlay网络

Prerequisites:

  • Firewall rules for Docker daemons using overlay networks

    You need the following ports open to traffic to and from each Docker host participating on an overlay network:

    • TCP port 2377 for cluster management communications
    • TCP and UDP port 7946 for communication among nodes
    • UDP port 4789 for overlay network traffic
  • Before you can create an overlay network, you need to either initialize your Docker daemon as a swarm manager using docker swarm init or join it to an existing swarm using docker swarm join. Either of these creates the default ingress overlay network which is used by swarm services by default. You need to do this even if you never plan to use swarm services. Afterward, you can create additional user-defined overlay networks.

两个先决条件:宿主机加入集群和配置防火墙规则(测试使用直接关闭就行)

创建

注意:必须是集群管理节点

1
docker network create -d overlay my-overlay

这里给出部分参数说明:

名称 描述
–attachable 独立容器也能使用此网络
–driver , -d 网络驱动
–subnet 网段的CIDR格式的子网
–gateway 主子网的IPv4或IPv6网关
–ingress 自定义ingress类型的时候指定,只能有一个ingress网络
–ip-range 从子范围(相对于子网)分配容器ip

更多参数参见:docker network create

测试

这次使用创建的my-overlay网络启动服务

1
2
docker service create -p 8080:8080 --network my-overlay  --name tomcatv1 tomcat
docker service create -p 80:8080 --network my-overlay --name tomcatv2 tomcat

进入tomcatv2通过my-overlay请求tomcatv1的8080端口

1
2
3
//这里404的原因是我拉取的tomcat镜像webapps下没有任何东西
root@48b4ca6c276c:/usr/local/tomcat# curl 10.0.0.3:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Not found</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/8.5.50</h3></body></html>

发现,服务间已能通过此网络正常访问。

注意:当管理节点创建的服务被部署到工作节点,或者工作节点使用此网络部署独立的服务(docker run -p 8080:8080 --network my-overlay -d --name tomcat tomcat),工作节点会在此网络(my-overlay)自动创建一个端点(Endpoint),用于连接工作节点所在宿主机与其他集群内的宿主机。该端点(Endpoint)将占用此网络(my-overlay)一个随机的IP地址

Docker 网络模型如下:

评论