有四个不同的网络问题要解决:

  1. 高耦合的容器到容器的通信
  2. Pod 到 Pod 的通信
  3. Pod 与 Service 的通信
  4. 外部与内部的通信

    There are 4 distinct networking problems to solve:

    1. Highly-coupled container-to-container communications
    2. Pod-to-Pod communications
    3. Pod-to-Service communications
    4. External-to-internal communications

模型和动机 Model and motivation

Kubernetes 偏离了默认的 Docker 网络模型(尽管从 Docker 1.8 开始他们的网络插件变得更接近了)。
目标是在一个共享网络命名空间的平面中每个 pod 都有一个 IP ,这样可以跨网络与其他物理计算机和容器通信。每个 pod 一个 IP 的设计构建了一个干净且向后兼容的模型。 从端口分配、联网、命名、服务发现、负载平衡、应用程序配置和迁移的角度来看,pod 倾向于被视为 VM 或物理主机。

Kubernetes deviates from the default Docker networking model (though as of Docker 1.8 their network plugins are getting closer). The goal is for each pod to have an IP in a flat shared networking namespace that has full communication with other physical computers and containers across the network. IP-per-pod creates a clean, backward-compatible model where pods can be treated much like VMs or physical hosts from the perspectives of port allocation, networking, naming, service discovery, load balancing, application configuration, and migration.

另一方面,动态端口分配既需要静态端口的支持(例如外部可访问服务),也需要动态分配端口的支持,需要区分集中分配和本地获取的动态端口,使调度复杂化(因为端口是稀缺资源),对用户不方便,使应用程序配置复杂化,受到端口冲突、重用和耗尽的困扰,需要非标准方案命名(如 consul 或 etcd ,而非 DNS ),需要使用标准命名/寻址方案对程序进行代理和(或)重定向(如 web 浏览器),除了监测组成员关系改变,还需要额外单独监测实例的地址/端口改变并使缓存失效,阻碍容器/ pod 迁移(如使用 CRIU )。
NAT 碎片化的地址空间会引入额外的复杂度,会打破自注册机制,有可能还有其他问题。

Dynamic port allocation, on the other hand, requires supporting both static ports (e.g., for externally accessible services) and dynamically allocated ports, requires partitioning centrally allocated and locally acquired dynamic ports, complicates scheduling (since ports are a scarce resource), is inconvenient for users, complicates application configuration, is plagued by port conflicts and reuse and exhaustion, requires non-standard approaches to naming (e.g. consul or etcd rather than DNS), requires proxies and/or redirection for programs using standard naming/addressing mechanisms (e.g. web browsers), requires watching and cache invalidation for address/port changes for instances in addition to watching group membership changes, and obstructs container/pod migration (e.g. using CRIU). NAT introduces additional complexity by fragmenting the addressing space, which breaks self-registration mechanisms, among other problems.

容器到容器

一个 pod 内的所有的容器行为就像他们在相同的主机上,不需要考虑网络。他们都可以到达 localhost 上彼此的端口。这提供了简洁性(提前知道的端口)、安全性(端口绑定到 localhost 对 pod 内可见,但对外不可见)以及性能。这也减少了应用程序从物理或虚拟主机上的非容器化应用程序迁移的问题。在同一台主机上运行应用程序堆栈的人已经知道如何使端口不冲突,并安排客户端找到它们。

All containers within a pod behave as if they are on the same host with regard to networking. They can all reach each other’s ports on localhost. This offers simplicity (static ports know a priori), security (ports bound to localhost are visible within the pod but never outside it), and performance. This also reduces friction for applications moving from the world of uncontainerized apps on physical or virtual hosts. People running application stacks together on the same host have already figured out how to make ports not conflict and have arranged for clients to find them.

这个方案减少了一个 pod 内容器之间的隔离——端口不能冲突,并且没有容器私有端口,但这些似乎是未来可行的解决方案中相对较小的问题。此外,pod 的前提是一个 pod 中的容器共享一些资源(卷、cpu 、ram 等),因此期望并能容忍隔离减少。进一步,用户可以控制容器是否属于相同的 pod ,而通常情况下,用户不能控制哪几个 pod 在同一主机上。

The approach does reduce isolation between containers within a pod — ports could conflict, and there can be no container-private ports, but these seem to be relatively minor issues with plausible future workarounds. Besides, the premise of pods is that containers within a pod share some resources (volumes, cpu, ram, etc.) and therefore expect and tolerate reduced isolation. Additionally, the user can control what containers belong to the same pod whereas, in general, they don’t control what pods land together on a host.

Pod to pod

因为每个 pod 获取一个 “真实”(非机器私有)的 IP 地址,pod 可以不用代理和转译进行通信。pod 可以使用已知的端口号,避免使用像 DNS-SD、Consul 或 Etcd 这样的高级服务发现系统。

Because every pod gets a “real” (not machine-private) IP address, pods can communicate without proxies or translations. The pod can use well-known port numbers and can avoid the use of higher-level service discovery systems like DNS-SD, Consul, or Etcd.

当容器调用 ioctl(SIOCGIFADDR) (获取接口的地址), 它看到的 IP 与任何对等容器都会看到的相同 —— 每个 Pod 都有一个其他 Pod 知道的 IP 地址。通过使 IP 地址和端口在 pod 内外均相同,我们创建一个无 NAT 的平面地址空间。运行 ip addr show 应该会如期望那样工作。这使得包括分发 IP 地址的自注册机制和应用在内的所有已存的命名/发现机制可以开箱即用。我们应该优化 pod 内部网络通信。在一个 pod 内,容器更倾向于用卷 (如 tmpfs )或 IPC 来通信。

When any container calls ioctl(SIOCGIFADDR) (get the address of an interface), it sees the same IP that any peer container would see them coming from — each pod has its own IP address that other pods can know. By making IP addresses and ports the same both inside and outside the pods, we create a NAT-less, flat
address space.Running “ip addr show” should work as expected. This would enable all existing naming/discovery mechanisms to work out of the box, including self-registration mechanisms and applications that distribute IP addresses. We should be optimizing for inter-pod network communication. Within a pod, containers are more likely to use communication through volumes (e.g., tmpfs) or IPC.

这不同于标准的 Docker 模型。在 Docker 模式中,每个容器获取 172.空间中的一个 IP ,通过 SIOCGIFADDR 也只能看到172.地址。如果这些容器连接到其他对等容器,对等容器将看到从一个不同 IP 过来的连接,但它自己不知道。简而言之 —— 你永远不能从一个容器进行自注册任何内容,因为无法通过其私有IP访问容器。

This is different from the standard Docker model. In that mode, each container gets an IP in the 172-dot space and would only see that 172-dot address from SIOCGIFADDR. If these containers connect to another container the peer would see the connect coming from a different IP than the container itself knows. In short — you can never self-register anything from a container, because a container can not be reached on its private IP.

我们考虑的另一种选择是增加一层寻址:每个容器以 pod 为中心的 IP 。每个容器将有它自己的本地 IP 地址,仅在 Pod 内可见。这可能使得容器化应用从物理/虚拟主机迁移到 pod 更简单,但可能在实现上更复杂(每个 pod 需要一个网桥,需要水平分割/虚拟专用 DNS),而且额外的地址转换层会破坏自注册和 IP 分发机制。

An alternative we considered was an additional layer of addressing: pod-centric IP per container. Each container would have its own local IP address, visible only within that pod. This would perhaps make it easier for containerized applications to move from physical/virtual hosts to pods, but would be more complex to implement (e.g., requiring a bridge per pod, split-horizon/VP DNS) and to reason about, due to the additional layer of address translation, and would break self-registration and IP distribution mechanisms.

与Docker一样,端口仍然可以发布到主机节点的接口,但这种需求已大大减少。

Like Docker, ports can still be published to the host node’s interface(s), but the need for this is radically diminished.

实现 Implementation

对于 GCE 集群配置脚本,我们使用高级路由规则和 支持 ip 转发 的 VM ,这样每个 VM 有额外的 256 个能路由到它的 IP 地址。这是除分配给虚拟机的“主” IP 地址之外的另一 个 IP 地址,该虚拟机通过 NAT 进行互联网访问。容器网桥(称为 cbr0以区别于docker0)设置在 Docker 本体外的。

For the Google Compute Engine cluster configuration scripts, we use advanced routing rules and ip-forwarding-enabled VMs so that each VM has an extra 256 IP addresses that get routed to it. This is in addition to the ‘main’ IP address assigned to the VM that is NAT-ed for Internet access. The container bridge (called cbr0 to differentiate it from docker0) is set up outside of Docker proper.

GCE 高级路由规则的例子:

Example of GCE’s advanced routing rules:

  1. gcloud compute routes add "${NODE_NAMES[$i]}" \
  2. --project "${PROJECT}" \
  3. --destination-range "${NODE_IP_RANGES[$i]}" \
  4. --network "${NETWORK}" \
  5. --next-hop-instance "${NODE_NAMES[$i]}" \
  6. --next-hop-instance-zone "${ZONE}" &

不过,GCE 本身对这些 IP 一无所知。这意味着,当一个 pod 试图 egress GCE 的项目时,数据包必须被 SNAT (伪装)到 VM 的 IP ,这样 GCE 可以识别到并放行。

GCE itself does not know anything about these IPs, though. This means that when a pod tries to egress beyond GCE’s project the packets must be SNAT’ed (masqueraded) to the VM’s IP, which GCE recognizes and allows.

其他实现 Other implementations

带着每个 pod 一个 IP 的主要目标,出现了服务 GCE 之外需要的其他实现。

  • OpenVSwitch with GRE/VxLAN(已不可访问)
  • Flannel
  • L2 networks (“带有 Linux 网桥设备”部分)
  • Weave 是另一种构建覆盖网络的方式,主要目标是 Docker 集成。
  • Calico 使用 BGP 以支持真实容器 IP 。
  • Cilium 支持覆盖网络模式( IPv4/IPv6 )和 直接路由模型(Direct Routing model)( IPv6 )

    With the primary aim of providing IP-per-pod-model, other implementations exist to serve the purpose outside of GCE.

    • OpenVSwitch with GRE/VxLAN
    • Flannel
    • L2 networks (“With Linux Bridge devices” section)
    • Weave is yet another way to build an overlay network, primarily aiming at Docker integration.
    • Calico uses BGP to enable real container IPs.
    • Cilium supports Overlay Network mode (IPv4/IPv6) and Direct Routing model (IPv6)

Pod 到服务 Pod to service

服务抽象提供了一种在公共访问策略(例如负载平衡)下对 pod 进行分组的方法。
这一实现创建了一个虚拟 IP ,客户端可以访问该虚拟 IP ,并透明地代理到服务中的 pod 。每个节点都运行一个kube-proxy 进程,该进程编辑 iptables 规则,从而捕获对服务 IP 的访问,并将其重定向到正确的后端。通过平衡来自同一节点的客户端流量,提供了一个高可用的负载均衡解决方案,且性能开销低。

The service abstraction provides a way to group pods under a common access policy (e.g. load-balanced). The implementation of this creates a virtual IP which clients can access and which is transparently proxied to the pods in a Service. Each node runs a kube-proxy process which programs iptables rules to trap access to service IPs and redirect them to the correct backends. This provides a highly-available load-balancing solution with low performance overhead by balancing client traffic from a node on that same node.

外到内 External to internal

目前为止讨论了集群内的 pod 或 服务如何相互访问。从集群外部访问 pod 更难办一点。我们想为目标 Kubernetes Services 提供高可用,高性能负载均衡。许多公有云提供的很简单,但不够灵活。

So far the discussion has been about how to access a pod or service from within the cluster. Accessing a pod from outside the cluster is a bit more tricky. We want to offer highly-available, high-performance load balancing to target Kubernetes Services. Most public cloud providers are simply not flexible enough yet.

通常对实现方式是设置指向集群内所有目标节点的外部负载均衡器(例如 GCE 的 ForwardingRules 或 AWS 的 ELB )。当流量到达节点时,它被识别为特定服务的一部分并路由到对应的后端 Pod 。这意味着一些流量在网络上会受到double-bounced。一旦云提供商有了更好的服务,我们就可以利用这些服务。

The way this is generally implemented is to set up external load balancers (e.g. GCE’s ForwardingRules or AWS’s ELB) which target all nodes in a cluster. When traffic arrives at a node it is recognized as being part of a particular Service and routed to an appropriate backend Pod. This does mean that some traffic will get double-bounced on the network. Once cloud providers have better offerings we can take advantage of those.

挑战和未来工作 Challenges and future work

Docker API

现在,docker inspect 不展示容器的网络配置,因为它们是从另一个容器派生的。那些信息应该以某种方式暴露出来。

Right now, docker inspect doesn’t show the networking configuration of the containers, since they derive it from another container. That information should be exposed somehow.

外部 IP 分配 External IP assignment

我们期望能分配外部 IP 地址 (Docker#6743),这样我们就不需要为每个节点分配固定容量的 IP 地址段,以便在pod infra容器重新启动期间使IP地址稳定(#2801),方便 pod 迁移。现在,如果 pod infra 容器死亡,则必须停止并重新启动所有用户容器,因为 pod infa 容器的网络将在重新启动时更改,并且任何后续的用户容器重新启动都将加入新的网络,从而无法看到其同级容器。此外,IP 地址的更改会遇到 DNS 缓存/ TTL 问题。外部 IP 分配还将简化 DNS 支持(见下文)。

We want to be able to assign IP addresses externally from Docker #6743 so that we don’t need
to statically allocate fixed-size IP ranges to each node, so that IP addresses can be made stable across pod infra container restarts (#2801), and to facilitate pod migration. Right now, if the pod infra container dies, all the user containers must be stopped and restarted because the netns of the pod infra container will change on restart, and any subsequent user container restart will join that new netns, thereby not being able to see its peers. Additionally, a change in IP address would encounter DNS caching/TTL problems. External IP assignment would also simplify DNS support (see below).

IPv6

IPv6 支持很 nice ,但需要在一些地方进行重大的内部更改。pods 应该能够报告多个 IP 地址( Kubernetes issue #27398 ),并且 Kubernets 使用的网络插件架构也需要能返回IPv6地址( CNI issue #245 )。必须对处理 IP 地址的 Kubernetes 代码进行审计和修复,以支持 IPv4 和 IPv6 地址,而不是假定只有 IPv4 。AWS 开始推出基本的 ipv6 支持,但其他主要云提供商(如 GCE )似乎还不支持将 ipv6 直接分配给实例。不过,我们很乐意接受裸机上运行 Kubernetes 的用户的 PR 。 :-)

IPv6 support would be nice but requires significant internal changes in a few areas. First pods should be able to report multiple IP addresses Kubernetes issue #27398 and the network plugin architecture Kubernetes uses needs to allow returning IPv6 addresses too CNI issue #245. Kubernetes code that deals with IP addresses must then be audited and fixed to support both IPv4 and IPv6 addresses and not assume IPv4. AWS started rolling out basic ipv6 support, but direct ipv6 assignment to instances doesn’t appear to be supported by other major cloud providers (e.g. GCE) yet. We’d happily take pull requests from people running Kubernetes on bare metal, though. :-)