Services, networking and DNS
Service types
| Type | What you get |
|---|---|
| ClusterIP (default) | A stable virtual IP reachable only inside the cluster |
| NodePort | ClusterIP + a port (30000–32767) opened on every node |
| LoadBalancer | NodePort + a cloud load balancer (on EKS: an NLB via the AWS LB Controller) |
| ExternalName | A DNS CNAME to an external name (no proxying) |
Headless (clusterIP: None) |
No virtual IP; DNS returns the pod IPs directly (StatefulSets, client-side load balancing) |
A Service selects pods by labels; the EndpointSlice controller records the IPs and ports of Ready pods. A Service with no selector can point at manually managed endpoints (e.g. an external database IP).
Useful fields:
externalTrafficPolicy: Local(NodePort/LoadBalancer) — only route to pods on the receiving node: preserves client source IP and avoids an extra hop, but nodes without pods fail health checks and load can be uneven.Cluster(default) spreads across all pods but SNATs the source IP.internalTrafficPolicy: Local— in-cluster traffic stays on the node (node-local caches/agents).sessionAffinity: ClientIP— sticky by client IP.publishNotReadyAddresses— include unready pods (peer discovery in StatefulSets).
How a ClusterIP connection flows (iptables mode)
A pod connects to 10.100.12.7:80 (the Service IP). Nothing "listens" on that IP — the node's iptables KUBE-SERVICES chain matches it, jumps to a KUBE-SVC-... chain that picks one KUBE-SEP-... endpoint by probability, which DNATs to podIP:8080. Conntrack remembers the choice so the connection sticks to that backend. That's also why long-lived connections (gRPC/HTTP2, DB pools) don't rebalance when you scale — load balancing happens per connection, not per request.
DNS with CoreDNS
CoreDNS runs as a Deployment behind the kube-dns Service. Records:
| Name | Resolves to |
|---|---|
orders.shop.svc.cluster.local |
The Service ClusterIP (A/AAAA) |
Headless kafka.data.svc.cluster.local |
All ready pod IPs |
kafka-0.kafka.data.svc.cluster.local |
One StatefulSet pod |
_http._tcp.orders.shop.svc.cluster.local |
SRV record with port |
Pods get /etc/resolv.conf with nameserver <kube-dns IP>, search domains shop.svc.cluster.local svc.cluster.local cluster.local and options ndots:5. A name with fewer than 5 dots is tried with each search suffix first — so api.github.com causes several failed lookups before the real one. Mitigate with FQDNs ending in a dot, a lower ndots via dnsConfig, and NodeLocal DNSCache. CoreDNS forwards non-cluster names upstream (on EKS, the VPC resolver).
Ingress and Gateway API
- Ingress — HTTP(S) routing rules (host, path → Service). It does nothing without an ingress controller (AWS Load Balancer Controller → ALB, Traefik, HAProxy, and the widely used community ingress-nginx — which was retired in March 2026 and no longer gets fixes, so new clusters should pick a maintained controller or Gateway API).
ingressClassNameselects the controller;pathTypeisPrefix,ExactorImplementationSpecific; TLS via akubernetes.io/tlsSecret or ACM annotations on ALB. - Gateway API — the successor: role-oriented resources (
GatewayClassby infra providers,Gatewayby platform teams,HTTPRoute/GRPCRoute/TCPRouteby app teams), with native traffic weighting, header matching and cross-namespace delegation. Supported by the AWS LB Controller (and VPC Lattice), Istio, Cilium, NGINX Gateway Fabric.
NetworkPolicy
By default all pods can talk to all pods. A NetworkPolicy selects pods (podSelector) and, once a pod is selected for a direction (Ingress/Egress), only explicitly allowed traffic is permitted in that direction. Policies are additive (union of allows); there are no deny rules in the core API.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: default-deny-all, namespace: shop }
spec:
podSelector: {} # all pods in the namespace
policyTypes: [Ingress, Egress]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: api-from-frontend, namespace: shop }
spec:
podSelector: { matchLabels: { app: api } }
policyTypes: [Ingress]
ingress:
- from:
- podSelector: { matchLabels: { app: frontend } }
ports: [{ protocol: TCP, port: 8080 }]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: allow-dns-egress, namespace: shop }
spec:
podSelector: {}
policyTypes: [Egress]
egress:
- to:
- namespaceSelector: { matchLabels: { kubernetes.io/metadata.name: kube-system } }
podSelector: { matchLabels: { k8s-app: kube-dns } }
ports: [{ protocol: UDP, port: 53 }, { protocol: TCP, port: 53 }]
Gotchas: after a default-deny egress policy, DNS breaks unless you allow it; selectors in one from item are ANDed, separate items are ORed; NetworkPolicy has no effect unless the CNI enforces it (VPC CNI now supports it when enabled; Calico/Cilium do).