您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

Azure Kubernetes 服务 (AKS) 中应用程序的网络概念Network concepts for applications in Azure Kubernetes Service (AKS)

在基于容器的微服务应用程序开发方法中,应用程序组件必须协同工作才能处理其任务。In a container-based microservices approach to application development, application components must work together to process their tasks. Kubernetes 提供支持此应用程序通信的各种资源。Kubernetes provides various resources that enable this application communication. 可以在内部或外部连接应用程序并将其公开。You can connect to and expose applications internally or externally. 要生成高可用性应用程序,可以对应用程序进行负载均衡。To build highly available applications, you can load balance your applications. 更复杂的应用程序可能需要配置 SSL/TLS 终止的入口流量或多个组件的路由。More complex applications may require configuration of ingress traffic for SSL/TLS termination or routing of multiple components. 出于安全考虑,可能还需要限制流量流入或 Pod 和节点之间的流量。For security reasons, you may also need to restrict the flow of network traffic into or between pods and nodes.

本文介绍了为 AKS 中的应用程序提供网络的核心概念:This article introduces the core concepts that provide networking to your applications in AKS:

Kubernetes 基础知识Kubernetes basics

为允许访问应用程序或让应用程序组件相互通信,Kubernetes 为虚拟网络提供了抽象层。To allow access to your applications, or for application components to communicate with each other, Kubernetes provides an abstraction layer to virtual networking. Kubernetes 节点连接到虚拟网络,可为 Pod 提供入站和出站连接。Kubernetes nodes are connected to a virtual network, and can provide inbound and outbound connectivity for pods. kube-proxy 组件在每个节点上运行,以提供这些网络功能 。The kube-proxy component runs on each node to provide these network features.

在 Kubernetes 中,服务以逻辑方式对 Pod 进行分组,以允许通过 IP 地址或 DNS 名称以及特定端口进行直接访问 。In Kubernetes, Services logically group pods to allow for direct access via an IP address or DNS name and on a specific port. 此外,还可使用负载均衡器分发流量 。You can also distribute traffic using a load balancer. 使用入口控制器也可实现更复杂的应用程序流量路由 。More complex routing of application traffic can also be achieved with Ingress Controllers. 使用 Kubernetes 网络策略可提供安全性,还可筛选 Pod 网络流量(在 AKS 中,为预览版) 。Security and filtering of the network traffic for pods is possible with Kubernetes network policies (in preview in AKS).

Azure 平台还有助于简化 AKS 群集的虚拟网络。The Azure platform also helps to simplify virtual networking for AKS clusters. 创建 Kubernetes 负载均衡器时,将创建和配置基础 Azure 负载均衡器资源。When you create a Kubernetes load balancer, the underlying Azure load balancer resource is created and configured. 打开 Pod 的网络端口时,会配置相应的 Azure 网络安全组规则。As you open network ports to pods, the corresponding Azure network security group rules are configured. 对于 HTTP 应用程序路由,Azure 还可以在配置新的入口路由时配置外部 DNS 。For HTTP application routing, Azure can also configure external DNS as new ingress routes are configured.

服务Services

为简化应用程序工作负载的网络配置,Kubernetes 使用服务以逻辑方式对一组 Pod 进行分组并提供网络连接 。To simplify the network configuration for application workloads, Kubernetes uses Services to logically group a set of pods together and provide network connectivity. 可用的服务类型如下:The following Service types are available:

  • 群集 IP - 创建在 AKS 群集中使用的内部 IP 地址。Cluster IP - Creates an internal IP address for use within the AKS cluster. 适用于支持群集中其他工作负载的仅限内部使用的应用程序。Good for internal-only applications that support other workloads within the cluster.

    显示 AKS 群集中群集 IP 流量的示意图

  • NodePort - 在基础节点上创建端口映射,该映射允许使用节点 IP 地址和端口直接访问应用程序。NodePort - Creates a port mapping on the underlying node that allows the application to be accessed directly with the node IP address and port.

    显示 AKS 群集中 NodePort 流量的示意图

  • LoadBalancer - 创建 Azure 负载均衡器资源、配置外部 IP 地址并将请求的 Pod 连接到负载均衡器后端池。LoadBalancer - Creates an Azure load balancer resource, configures an external IP address, and connects the requested pods to the load balancer backend pool. 为允许客户流量发送到应用程序,要在所需端口上创建负载均衡规则。To allow customers traffic to reach the application, load balancing rules are created on the desired ports.

    显示 AKS 群集中负载均衡器流量的示意图

    针对入站流量的其他控制和路由,可能要改用入口控制器For additional control and routing of the inbound traffic, you may instead use an Ingress controller.

  • ExternalName - 创建特定的 DNS 条目,便于访问应用程序。ExternalName - Creates a specific DNS entry for easier application access.

可以动态分配负载均衡器和服务的 IP 地址,也可以指定要使用的现有静态 IP 地址。The IP address for load balancers and services can be dynamically assigned, or you can specify an existing static IP address to use. 可以分配内部和外部静态 IP 地址。Both internal and external static IP addresses can be assigned. 这个现有静态 IP 地址通常与 DNS 条目绑定。This existing static IP address is often tied to a DNS entry.

可以创建内部和外部负载均衡器 。Both internal and external load balancers can be created. 内部负载均衡器仅分配一个专用 IP 地址,因此无法从 Internet 访问。Internal load balancers are only assigned a private IP address, so can't be accessed from the Internet.

Azure 虚拟网络Azure virtual networks

在 AKS 中,可以部署使用以下两种网络模型之一的群集:In AKS, you can deploy a cluster that uses one of the following two network models:

  • Kubenet 网络 - 通常在部署 AKS 群集时创建和配置网络资源 。Kubenet networking - The network resources are typically created and configured as the AKS cluster is deployed.
  • Azure 容器网络接口 (CNI) 网络 - AKS 群集连接到现有的虚拟网络资源和配置 。Azure Container Networking Interface (CNI) networking - The AKS cluster is connected to existing virtual network resources and configurations.

Kubenet(基本)网络Kubenet (basic) networking

kubenet 网络选项是用于创建 AKS 群集的默认配置 。The kubenet networking option is the default configuration for AKS cluster creation. 使用 kubenet,节点从 Azure 虚拟网络子网获取 IP 地址 。With kubenet, nodes get an IP address from the Azure virtual network subnet. Pod 接收从逻辑上不同的地址空间到节点的 Azure 虚拟网络子网的 IP 地址。Pods receive an IP address from a logically different address space to the Azure virtual network subnet of the nodes. 然后配置网络地址转换 (NAT),以便 Pod 可以访问 Azure 虚拟网络上的资源。Network address translation (NAT) is then configured so that the pods can reach resources on the Azure virtual network. 流量的源 IP 地址是 NAT 到节点的主 IP 地址。The source IP address of the traffic is NAT'd to the node's primary IP address.

节点使用 kubenet Kubernetes 插件。Nodes use the kubenet Kubernetes plugin. 可以让 Azure 平台创建和配置虚拟网络,或选择将 AKS 群集部署到现有虚拟网络子网中。You can let the Azure platform create and configure the virtual networks for you, or choose to deploy your AKS cluster into an existing virtual network subnet. 同样,只有 Pod 和接收可路由 IP 地址的节点才能使用 NAT 与 AKS 群集外的其他资源进行通信。Again, only the nodes receive a routable IP address, and the pods use NAT to communicate with other resources outside the AKS cluster. 这种方法大大减少了需要在网络空间中保留供 Pod 使用的 IP 地址数量。This approach greatly reduces the number of IP addresses that you need to reserve in your network space for pods to use.

有关详细信息,请参阅为 AKS 群集配置 kubenet 网络For more information, see Configure kubenet networking for an AKS cluster.

Azure CNI(高级)网络Azure CNI (advanced) networking

借助 Azure CNI,每个 pod 都可以从子网获取 IP 地址,并且可以直接访问。With Azure CNI, every pod gets an IP address from the subnet and can be accessed directly. 这些 IP 地址在网络空间必须是唯一的,并且必须事先计划。These IP addresses must be unique across your network space, and must be planned in advance. 每个节点都有一个配置参数来表示它支持的最大 Pod 数。Each node has a configuration parameter for the maximum number of pods that it supports. 这样,就会为每个节点预留相应的 IP 地址数。The equivalent number of IP addresses per node are then reserved up front for that node. 此方法需要更多的计划,并且经常会耗尽 IP 地址或者在应用程序需求增长时需要在更大的子网中重新生成群集。This approach requires more planning, and often leads to IP address exhaustion or the need to rebuild clusters in a larger subnet as your application demands grow.

节点使用 Azure 容器网络接口 (CNI) Kubernetes 插件。Nodes use the Azure Container Networking Interface (CNI) Kubernetes plugin.

显示两个节点的示意图,其中的网桥将每个节点连接到单个 Azure VNet

Azure CNI 通过 kubenet 网络提供以下功能:Azure CNI provides the following features over kubenet networking:

  • 在虚拟网络中,群集中的每个 Pod 都分配了一个 IP 地址。Every pod in the cluster is assigned an IP address in the virtual network. Pod 可以直接与群集中的其他 Pod 以及虚拟网络中的其他节点通信。The pods can directly communicate with other pods in the cluster, and other nodes in the virtual network.
  • 启用了服务终结点的子网中的 Pod 可以安全连接到 Azure 服务(例如 Azure 存储和 SQL DB)。Pods in a subnet that have service endpoints enabled can securely connect to Azure services, such as Azure Storage and SQL DB.
  • 可以创建用户定义的路由 (UDR) 将来自 Pod 的流量路由到网络虚拟设备。You can create user-defined routes (UDR) to route traffic from pods to a Network Virtual Appliance.

有关详细信息,请参阅为 AKS 群集配置 Azure CNIFor more information, see Configure Azure CNI for an AKS cluster.

入口控制器Ingress controllers

创建 LoadBalancer 类型服务时,系统会创建基础 Azure 负载均衡器资源。When you create a LoadBalancer type Service, an underlying Azure load balancer resource is created. 负载均衡器配置为在给定端口上将流量分发到服务中的 Pod。The load balancer is configured to distribute traffic to the pods in your Service on a given port. LoadBalancer 仅在第 4 层工作 - 服务不知道实际的应用程序,不会考虑任何其他路由。The LoadBalancer only works at layer 4 - the Service is unaware of the actual applications, and can't make any additional routing considerations.

入口控制器在第 7 层工作,可使用更智能的规则来分发应用程序流量 。Ingress controllers work at layer 7, and can use more intelligent rules to distribute application traffic. 入口控制器的一个常见用途是基于入站 URL 将 HTTP 流量路由到不同的应用程序。A common use of an Ingress controller is to route HTTP traffic to different applications based on the inbound URL.

显示 AKS 群集中入口流量的示意图

在 AKS 中,可以使用 NGINX 之类的服务器创建入口资源,或使用 AKS HTTP 应用程序路由功能。In AKS, you can create an Ingress resource using something like NGINX, or use the AKS HTTP application routing feature. 为 AKS 群集启用 HTTP 应用程序路由时,Azure 平台会创建入口控制器和 External-DNS 控制器 。When you enable HTTP application routing for an AKS cluster, the Azure platform creates the Ingress controller and an External-DNS controller. 在 Kubernetes 中创建新的入口资源时,系统会在特定于群集的 DNS 区域中创建所需的 DNS A 记录。As new Ingress resources are created in Kubernetes, the required DNS A records are created in a cluster-specific DNS zone. 有关详细信息,请参阅部署 HTTP 应用程序路由For more information, see deploy HTTP application routing.

入口的另一个常见功能是 SSL/TLS 终止。Another common feature of Ingress is SSL/TLS termination. 在通过 HTTPS 访问的大型 Web 应用程序上,TLS 终止可以由入口资源处理,而不是在应用程序自身内部处理。On large web applications accessed via HTTPS, the TLS termination can be handled by the Ingress resource rather than within the application itself. 要提供自动 TLS 认证生成和配置,可以将入口资源配置为使用 Let's Encrypt 之类的提供程序。To provide automatic TLS certification generation and configuration, you can configure the Ingress resource to use providers such as Let's Encrypt. 有关使用 Let's Encrypt 配置 NGINX 入口控制器的详细信息,请参阅 Ingress 和 TLS For more information on configuring an NGINX Ingress controller with Let's Encrypt, see Ingress and TLS.

此外可以配置要保留到 AKS 群集中的容器的请求上的客户端源 IP 为入口控制器。You can also configure your ingress controller to preserve the client source IP on requests to containers in your AKS cluster. 当客户端的请求路由到通过入口控制器在 AKS 群集中的容器时,该请求的原始源 ip 将不能对目标容器。When a client's request is routed to a container in your AKS cluster via your ingress controller, the original source ip of that request will not be available to the target container. 如果你启用客户端源 IP 保留,客户端的源 IP 是在请求标头中X-转发-对于When you enable client source IP preservation, the source IP for the client is available in the request header under X-Forwarded-For. 如果入口控制器上使用客户端源 IP 保留,不能使用 SSL 传递身份验证。If you are using client source IP preservation on your ingress controller, you cannot use SSL pass-through. 客户端源 IP 保留和 SSL 直通可与其他服务,如负载均衡器类型。Client source IP preservation and SSL pass-through can be used with other services, such as the LoadBalancer type.

网络安全组Network security groups

网络安全组筛选 VM(例如 AKS 节点)的流量。A network security group filters traffic for VMs, such as the AKS nodes. 创建服务(如 LoadBalancer)时,Azure 平台会自动配置所需的任何网络安全组规则。As you create Services, such as a LoadBalancer, the Azure platform automatically configures any network security group rules that are needed. 请勿手动配置网络安全组规则,以筛选 AKS 群集中 Pod 的流量。Don't manually configure network security group rules to filter traffic for pods in an AKS cluster. 定义任何所需端口并作为 Kubernetes 服务清单的一部分转发,让 Azure 平台创建或更新相应的规则。Define any required ports and forwarding as part of your Kubernetes Service manifests, and let the Azure platform create or update the appropriate rules. 还可以使用网络策略(如下一部分中所述)来自动向 Pod 应用流量筛选器规则。You can also use network policies, as discussed in the next section, to automatically apply traffic filter rules to pods.

网络策略Network policies

默认情况下,AKS 群集中的所有 Pod 都可以无限制地发送和接收流量。By default, all pods in an AKS cluster can send and receive traffic without limitations. 为了提高安全性,你可能想要定义用来控制流量流的规则。For improved security, you may want to define rules that control the flow of traffic. 后端应用程序通常只向所需的前端服务公开,或者数据库组件仅可由连接到它们的应用程序层访问。Backend applications are often only exposed to required frontend services, or database components are only accessible to the application tiers that connect to them.

网络策略是提供可用于控制 pod 之间的流量流的 AKS 中的 Kubernetes 功能。Network policy is a Kubernetes feature available in AKS that lets you control the traffic flow between pods. 可选择基于分配的标签、命名空间或流量端口等设置来允许或拒绝流量。You can choose to allow or deny traffic based on settings such as assigned labels, namespace, or traffic port. 网络安全组更多是针对 AKS 节点,而不是针对 Pod。Network security groups are more for the AKS nodes, not pods. 使用网络策略是一种更合适的用来控制流量流的云本机方式。The use of network policies is a more suitable, cloud-native way to control the flow of traffic. 因为 Pod 是在 AKS 群集中动态创建的,则可以动态应用所需的网络策略。As pods are dynamically created in an AKS cluster, the required network policies can be automatically applied.

有关详细信息,请参阅在 Azure Kubernetes 服务 (AKS) 中使用网络策略保护 Pod 之间的流量For more information, see Secure traffic between pods using network policies in Azure Kubernetes Service (AKS).

后续步骤Next steps

要开始使用 AKS 网络,请使用 kubenetAzure CNI 创建和配置具有自己的 IP 地址范围的 AKS 群集。To get started with AKS networking, create and configure an AKS cluster with your own IP address ranges using kubenet or Azure CNI.

如需相关的最佳做法,请参阅 AKS 中的网络连接和安全性的最佳做法For associated best practices, see Best practices for network connectivity and security in AKS.

有关核心 Kubernetes 和 AKS 概念的详细信息,请参阅以下文章:For additional information on core Kubernetes and AKS concepts, see the following articles: