服务是否首先将流量发送到本地 pods?

Do services send traffic to local pods first?

我有一个带有指向它的服务的 DaemonSet。 当一个 pod 将访问 my 服务的 ClusterIP 时,它会获取同一节点上的本地 pod 运行 还是服务中的任何 pod?

有什么办法可以实现吗?我的理解是,它与 externaltrafficpolicy: local 相同,但用于内部流量。

By default, traffic sent to a ClusterIP or NodePort Service may be routed to any backend address for the Service. Since Kubernetes 1.7 it has been possible to route "external" traffic to the Pods running on the Node that received the traffic, but this is not supported for ClusterIP Services, and more complex topologies — such as routing zonally — have not been possible. The Service Topology feature resolves this by allowing the Service creator to define a policy for routing traffic based upon the Node labels for the originating and destination Nodes.

您需要使用:Service topology

一个更喜欢本地的示例服务pods:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
  topologyKeys:
    - "kubernetes.io/hostname"
    - "*"

UPD 1:

还有另一个选项可以确保发送到某个特定节点端口的请求将在同一节点上处理 - 它是 hostPort

一个例子:

kind: Pod
apiVersion: v1
metadata:
  name: test-api
  labels:
    app: test-api
spec:
  containers:
  - name: testapicontainer
    image: myprivaterepo/testapi:latest
    ports:
    - name: web
      hostPort: 55555
      containerPort: 80      
      protocol: TCP

上面的 pod 将在 hostPort: 55555 上公开容器端口 80 - 如果你有 DaemonSet 用于那些 pods - 那么你可以肯定,他们会在每个节点上是 运行,每个请求将在接收它的节点上处理。

但是,请谨慎使用并阅读以下内容:Configuration Best Practices