ITChap
ITChap

Reputation: 4732

Do services send traffic to local pods first?

I have a DaemonSet with a service pointing to it. When a pod will access the ClusterIP of the my service, will it get the local pod running on the same node or any pod in the service?

Is there any way to achieve this? My understanding is that it would be the same thing than externaltrafficpolicy: local but for internal traffic.

Upvotes: 1

Views: 1802

Answers (1)

Vüsal
Vüsal

Reputation: 2706

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.

You need to use: Service topology

An example service which prefers local 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:

There is another option to make sure, that requests sent to a port of some particular node will be handled on the same node - it's hostPort.

An example:

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

The above pod will expose container port 80 on a hostPort: 55555 - if you have DaemonSet for those pods - then you can be sure, that they will be run on each node and each request will be handled on the node which received it.

But, please be careful using it and read this: Configuration Best Practices

Upvotes: 3

Related Questions