"within the cluster" 在 ClusterIP 服务的上下文中意味着什么?

What does "within the cluster" mean in the context of ClusterIP service?

我有一个包含以下内容的 Kubernetes 集群:

现在,我有服务的集群IP:

NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP   5d3h
svc-clusterip   ClusterIP   10.98.148.55   <none>        80/TCP    16m

现在我可以看到我可以从主机 (!) 访问此服务 - 而不是在 Pod 或其他任何地方:

$ curl 10.98.148.55
Hello world ! Version 1

问题是我不确定此功能是否是 ClusterIP 服务定义的一部分 - 即无论我使用什么网络插件,它是否保证以这种方式工作,或者这个插件是否依赖。

Kubernetes docs 声明:

ClusterIP: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType

不清楚“在集群内”是什么意思 - 这是否意味着在集群中的容器 (pod) 内?或者甚至像上面的例子那样来自节点本身?

这取决于集群设置。如果您使用的是 GKE 集群并将其设置为 VPC 原生,那么您将能够从同一 VPC 中的主机访问 clusterIP 服务。

definition from the Kubernetes documentation 表示可以从集群内访问它:

  • ClusterIP: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType.

问题是,我们如何理解“集群”?

嗯,the Kubernetes cluster is:

A Kubernetes cluster is a set of node machines for running containerized applications.

所以回答你的问题:

It's not clear what is meant by "within the cluster" - does that mean within a container (pod) in the cluster? or even from the nodes themselves as in the example above?

是的,这意味着节点 + pods 在这些节点上 运行,因此您的行为是正常的和预期的。

另一个问题:

The thing is that I'm not sure if this capability is part of the definition of the ClusterIP service - i.e. is it guaranteed to work this way no matter what network plugin I use, or is this plugin-dependant.

基本上CNI插件负责network communication for the pods:

A CNI plugin is responsible for inserting a network interface into the container network namespace (e.g. one end of a veth pair) and making any necessary changes on the host (e.g. attaching the other end of the veth into a bridge). It should then assign the IP to the interface and setup the routes consistent with the IP Address Management section by invoking appropriate IPAM plugin.

我用新鲜的 kubadm cluster setup without CNI plugin. I created a sample Nginx deployment 做了一个快速测试,然后为它服务。我观察到了什么?

  • 服务已正确创建并分配了 IP 地址,但是...
    user@example-ubuntu-kubeadm-template-clear-1:~$ kubectl get svc
    NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
    kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   10m
    my-service   ClusterIP   10.109.245.27   <none>        80/TCP    6m52s
    
  • 部署中的 pods 未启动,因为节点有 a taint
    1 node(s) had taint {node.kubernetes.io/not-ready: }
    
    kubectl describe node {node-name} 我可以找到:
    container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:Network plugin returns error: cni plugin not initialized
    

所以是的,服务独立于 CNI 插件,但是没有 CNI 插件你无法启动任何 pods 所以服务是无用的。

does that mean within a container (pod) in the cluster? or even from the nodes themselves

您可以从 KubeNode 和 pods 访问 ClusterIP。该IP为虚拟IP,仅在集群内有效。它的一种工作方式是(除了 CNI),使用 Linux 内核的 iptables/IPVS 功能,它使用 Pod IP 地址和 pods 之间的负载平衡重写数据包。这些规则由 KubeProxy

维护