Sub topic 8
Connecting to the Pod on the local node
A DaemonSet gives you one Pod per node. Often the workload that uses it, such as an application sending metrics to a node agent, should talk to the agent on its own node, not one on another node. This chapter covers the ways to do that. It assumes you can write a DaemonSet manifest and know how Services and Pod environment variables work.
Why a normal Service is not enough
A regular Service load-balances across all matching Pods. For a DaemonSet that means a client on node A may be sent to the agent on node C. That adds network hops, and the agent may see the wrong node's data. You need a way to pin traffic to the local node.
Option 1: node IP and hostPort
Expose a port on the node itself by adding hostPort to the agent container:
containers:
- name: agent
image: busybox:1.36
ports:
- containerPort: 8125
hostPort: 8125
protocol: TCPEach node now accepts connections on port 8125 and forwards them to the agent Pod running there. The client needs its node's IP address. The downward API supplies it as an environment variable:
apiVersion: v1
kind: Pod
metadata:
name: client
spec:
containers:
- name: app
image: busybox:1.36
command:
- sh
- -c
- while true; do echo ping | nc -w 1 $NODE_IP 8125; sleep 30; done
env:
- name: NODE_IP
valueFrom:
fieldRef:
fieldPath: status.hostIPstatus.hostIP is the IP of the node the client Pod was scheduled on, so the connection stays on that node.
A hostPort binds a port on the node. Two Pods cannot claim the same host port on one node, and the scheduler will leave a Pod unscheduled if the port is taken. Keep a DaemonSet's host ports few and documented.
Option 2: a Service with internalTrafficPolicy Local
If you prefer a stable DNS name, create a Service that only routes to endpoints on the same node as the caller:
apiVersion: v1
kind: Service
metadata:
name: node-log-agent
namespace: kube-system
spec:
selector:
app: node-log-agent
internalTrafficPolicy: Local
ports:
- port: 8125
targetPort: 8125Clients use node-log-agent.kube-system.svc and kube-proxy sends their traffic only to the agent on their own node.
Other approaches
| Approach | How the client connects | Main trade-off |
|---|---|---|
| `hostPort` plus `status.hostIP` | Node IP and port | Uses a node port. No DNS name. |
| Service with `internalTrafficPolicy: Local` | Service DNS name | Fails if the node has no ready agent. |
| `hostNetwork: true` on the agent | Node IP, agent's own port | Agent shares the node's network namespace. Port conflicts are likely. |
| Shared `hostPath` socket | Unix socket file on the node | No network at all, but both Pods need host access. |
| Headless Service | DNS returns every agent Pod IP | The client must pick the right Pod itself. |
For high-volume traffic such as metrics or logs, the unix socket and hostPort options avoid extra network hops. For ordinary request traffic, the Service with the Local policy is easier to manage.
Checking that it works
kubectl get pods -n kube-system -l app=node-log-agent -o wide
kubectl get pod client -o wide
kubectl logs clientCompare the NODE column of the two Pod listings. If they differ, the client is on a node where the agent Pod you are looking at does not run, and the traffic goes to a different agent Pod, the one on the client's node.
Exercise
A client Pod uses a Service with internalTrafficPolicy: Local to reach a DaemonSet agent. During a rolling update, the client on one node starts failing for a short time. Why, and which approach would not change this behavior?
Show answer
While the agent Pod on that node is being replaced, the node has no ready endpoint. With the Local policy the Service does not send traffic to other nodes, so connections fail until the new Pod is ready. A hostPort approach fails the same way, because the Pod behind the port is also gone. Reduce the gap by tuning maxUnavailable and a readiness probe, or have clients retry.