Kubernetes 中的 'endpoint' 是什么?

What is an 'endpoint' in Kubernetes?

我是 Kubernetes 新手,刚开始阅读文档。 经常使用术语 'endpoint',但文档缺乏明确的定义。

就 Kubernetes 而言,'endpoint' 是什么?它位于哪里?

我可以想象 'endpoint' 是个人 'node' 的某种接入点,但这只是一个猜测。

虽然您在 glossary there's indeed no entry for endpoint, it is a well defined Kubernetes network concept or abstraction. Since it's of secondary nature, you'd usually not directly manipulate it. There's a core resource Endpoint 中的定义是正确的,并且在命令行中也受支持:

$ kubectl get endpoints
NAME         ENDPOINTS            AGE
kubernetes   192.168.64.13:8443   10d

您会看到它实际上是什么:一个 IP 地址和一个端口。通常,您会让服务管理端点(服务将流量路由到每个 pod 一个 EP),但如果您有需要它的用例,您也可以 manually manage 它们。

Pods 通过端点将自己暴露给服务。 如果你愿意成为一个 pod 的一部分。

资料来源:Services and Endpoints

  1. 端点跟踪服务向其发送流量的对象的 IP 地址。
  2. 当服务选择器与 pod 标签匹配时,该 IP 地址将添加到您的端点。

来源:https://theithollow.com/2019/02/04/kubernetes-endpoints/

在k8s中,Endpoints由一个分布式的API组成,比如“[IP]:[Port]”等等。但是,在 SOAP 中,Endpoint 是一个分布式的 API 就像 URL.

from Google Cloud:

Endpoints is a distributed API management system. It provides an API console, hosting, logging, monitoring, and other features to help you create, share, maintain, and secure your APIs.

端点是一种资源,可以获取动态分配给它的一个或多个 pods IP 地址以及端口。可以使用 kubectl get endpoints.

查看端点

端点资源被 kubernetes 服务引用,因此该服务记录了 pods 的内部 IP,以便能够与它们通信。

我们需要端点作为抽象层,因为 kubernetes 中的 'service' 充当编排的一部分,以确保将流量分配到 pods(包括仅将流量发送到健康的 pods ).例如,如果一个 pod 死亡,将生成一个具有新 IP 地址的替换 pod。从概念上讲,dead pod IP 将从端点对象中移除,并添加新创建的 pod 的 IP,以便更新服务并 'knows' 连接到 pods。

阅读'Exposing pods to the cluster',然后在此处'Creating a Service' - https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#exposing-pods-to-the-cluster

调查和查看关系的简单方法是:

  • kubectl describe pods - 并观察您的 pods
  • 的 IP 地址
  • kubectl get ep - 观察分配给您端点的 IP 地址
  • kubectl describe service myServiceName - 并观察与您的服务关联的 Endpoints

所以不,端点与单个节点的 IP 无关。我发现了解 kubernetes 的整体结构以及集群、节点、服务、端点和 pods 之间的关系很有用。该图很好地总结了它,并显示了导致 OSI 第 2 层(TCP 层)到达后端节点 1 的入口流,OSI 第 7 层(http 层)入口最终到达 Pod 1 中的 'Web Container 1' :

我会一一回答你的问题:


What is an 'endpoint' in terms of Kubernetes?

(K8S中资源名称为Endpoints).

Endpoints 是 kubernetes 中的一个对象,代表一个……端点列表。
这些端点可以是:

  1. 集群内的内部pod 运行——这是比较熟悉的形式。
    当我们创建服务和 pods 并将服务标签选择器与 pods 标签匹配时,它会在幕后自动为我们创建。

  2. 一个 不是 pod 的外部 IP - 这是最不为人知的选项。

外部 IP 可以位于集群外部 - 例如外部 Web 服务器或数据库。
它也可以驻留在不同的命名空间中——如果你想将你的服务指向集群内不同命名空间中的服务。

关于外部端点——如果你没有在你的服务中指定标签选择器——Kubernetes 无法创建端点列表,因为他不知道服务应该包含和代理哪个 pods .


Where is it located?

就像此处提供的出色图表所示 - 它位于服务和内部(pods)或外部(网络服务器、数据库等)之间 资源。


I could image the 'endpoint' is some kind of access point for an individual 'node' Its an access point to a resource that sits inside one of the nodes in your cluster.

Endpoint 可以位于集群中的一个节点内,也可以位于集群/环境之外。

如果它是一个 内部 端点(这意味着 pod 标签与服务标签选择器匹配)- 您可以通过以下方式访问它:

$kubectl describe svc/my-service


Name:                     my-service
Namespace:                default
Labels:                   <none>
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":" my-service","namespace":"...
Selector:                 run=some-run
Type:                     NodePort
IP:                       10.100.92.162
Port:                     <unset>  8080/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  31300/TCP
Endpoints:                172.21.21.2:80,172.21.38.56:80,172.21.39.160:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

或直接与:

$kubectl get endpoints my-service

NAME           ENDPOINTS                                       AGE
my-service   172.21.21.2:80,172.21.38.56:80,172.21.39.160:80   63d

关于外部积分:

您创建了一个没有标签选择器的服务:

apiVersion: v1
kind: Service
metadata:
  name: my-service #<------ Should match the name of Endpoints object
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 9376

所以不会自动创建相应的Endpoint对象,需要手动添加Endpoints对象,并将Service映射到外部资源所在的所需网络地址和端口运行:

apiVersion: v1
kind: Endpoints
metadata:
  name: my-service #<------ Should match the name of Service
subsets:
  - addresses:
      - ip: 192.0.2.45
    ports:
      - port: 9376

(注意:) 我使用术语 internal 来表示 pods 的 auto-generated 端点与手动创建的端点的标签选择器和术语 external 匹配。

我可以使用术语 auto-generatedmanual 来代替 - 这样会更准确,但我认为更令人困惑还有。

在大多数情况下,当端点与集群内的 pods 相关时——我们希望它们也由 K8S 管理——在这种情况下,它们也需要由 K8S 生成。

将端点视为“到达应用程序的最终目的地”或'smth at the very end'

正如你在下面的例子中看到的:pod-IP = 10.32.0.2, service-Port* = 3306, endpoint = [pod-IP]:[服务端口]

因此,用户 Bob 要访问 MySql 应用程序,它应该寻址到 10.32.0.2:3306,这是网络中他可以找到所需信息的最后一个节点。

简单示例:我想访问 Google 邮件,在这种情况下,me/browser 端点将是 gmail.com:443 类似上面的例子 [pod-IP]:[service-Port]