「Kubernetes」- 网络调试(Testing,Troubleshooting)

问题描述

https://kubernetes.io/docs/concepts/cluster-administration/networking/

Networking is a central part of Kubernetes, but it can be challenging to understand exactly how it is expected to work. There are 4 distinct networking problems to address:

Highly-coupled container-to-container communications: this is solved by Pods and localhost communications.

Pod-to-Pod communications: this is the primary focus of this document.

Pod-to-Service communications: this is covered by Services.

External-to-Service communications: this is also covered by Services.

Kubernetes is all about sharing machines between applications. Typically, sharing machines requires ensuring that two applications do not try to use the same ports. Coordinating ports across multiple developers is very difficult to do at scale and exposes users to cluster-level issues outside of their control.

Dynamic port allocation brings a lot of complications to the system – every application has to take ports as flags, the API servers have to know how to insert dynamic port numbers into configuration blocks, services have to know how to find each other, etc.

解决方案

Kubernetes 网络插件是一种扩展组件,用于主要实现 Kubernetes 集群内部 Pod 间的通信。随着网络插件的发展及演进,不同的网络模型还能实现内部和外部的网络通信。

在 Kubernetes 集群中部署网络插件将启用集群内不同组件之间的通信。网络插件可以在节点之间路由流量,允许集群中的 pod 发送和接收流量。

总之,Kubernetes 网络插件是 Kubernetes 集群的一个核心组件,它能够保证应用程序的网络通信畅通,并且实现网络隔离和负载均衡等功能,从而提高 Kubernetes 集群的可靠性和稳定性。

原理简述

虽然是 Kubernetes 环境,但是网络通信的基本原理还是 Tunnel(隧道)与 Routing(路由),诸如 BGP、VXLAN、IPinIP 等等技术;

特性特征

它的作用包括:
1)提供网络虚拟化:Kubernetes 网络插件可以为 Kubernetes 集群提供网络虚拟化,使得不同节点之间的 Pod 可以像在同一个网络中一样通信。
2)实现网络隔离:Kubernetes 网络插件可以实现不同命名空间、不同节点之间的网络隔离,确保不同应用之间的网络互不干扰。
3)支持不同的网络模型:Kubernetes 网络插件可以支持不同的网络模型,比如 Overlay 网络、SDN 网络等。
4)实现负载均衡和服务发现:Kubernetes 网络插件可以实现负载均衡和服务发现,确保应用程序可以动态地扩展和缩减,而不会影响其它应用程序的正常运行。

应用场景

多租户应用场景:在多租户应用场景中,Kubernetes 网络插件可以为每个租户提供独立的网络隔离和安全性,使每个租户的应用都运行在自己的虚拟网络中,并且可以通过网络策略来控制不同租户之间的流量。

跨云数据中心应用场景:在跨云数据中心应用场景中,Kubernetes 网络插件可以为不同云数据中心提供统一的网络管理和连接,使得应用可以跨越多个数据中心运行,同时也可以通过网络策略来控制不同数据中心之间的流量。

容器网络函数应用场景:容器网络函数是一种利用 Kubernetes 网络插件构建的轻量级网络服务,它可以在容器网络中实现网络转发、负载均衡、安全防护等功能,可以应用于网络边缘、IoT、CDN 等场景。

大规模容器应用场景:在大规模容器应用场景中,Kubernetes 网络插件可以为大量的容器提供高效的网络连接和管理,保证应用的稳定性和可靠性,并且可以通过网络策略来控制不同容器之间的流量,提高应用的安全性。

常见网络插件

Alibaba Cloud: Terway, Flannel

Huawei Cloud: https://support.huaweicloud.com/usermanual-cce/cce_10_0281.html

Private Cloud: Calico, Cilium, Flannel

调试工具

kubectl apply -f ./network-multitool.yaml

技术细节

TODO ! kubernetes 为什么无法 ping 通过容器
Cluster Networking | Kubernetes
Why you can’t ping a Kubernetes Service – Nigel Poulton
coreos – kubernetes service IPs not reachable – Stack Overflow

TODO ! Kubernetes Service 的负载均衡实现方式(iptables)
Kubernetes 中的负载均衡原理 ———— 以 iptables 模式为例
./k8s-service-iptables.png

参考文献

praqma/network-multitool – Docker Image | Docker Hub