Opscraft
Kubernetes in depth · Lesson 5 of 11 · 7 min read

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)
Enlarge diagramLoadBalancer: + a cloud load balancer (NLB on EKS)NodePort: + port 30000–32767 on every nodeClusterIP: a stable virtual IP inside the clusterselects Ready pods by label; the default type
Figure 1. Service types build on each other: a LoadBalancer is a NodePort, which is a ClusterIP.
Swipe sideways to see all of it, or tap to open it full screen.

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.

Enlarge diagramclient podconnects10.100.12.7:80node kernel rulesKUBE-SERVICES → pickone endpoint → DNATorders pod10.0.3.21:8080orders pod10.0.5.9:8080orders pod10.0.7.44:8080
Figure 2. How a ClusterIP connection works: nothing listens on the Service IP. Kernel rules on the node rewrite the destination to one real pod.
Swipe sideways to see all of it, or tap to open it full screen.

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).

Enlarge diagrampod in shopcurl http://ordersCoreDNSkube-dns Service10.100.12.7the orders Service IPqueryanswerorders → orders.shop.svc.cluster.local (the pod's search domains fill in the rest)ndots:5 means short names try every search domain first: use FQDNs with a trailing dot for external names.
Figure 3. Cluster DNS: a pod asks CoreDNS for orders; search domains turn it into orders.shop.svc.cluster.local and the answer is the Service IP.
Swipe sideways to see all of it, or tap to open it full screen.

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). ingressClassName selects the controller; pathType is Prefix, Exact or ImplementationSpecific; TLS via a kubernetes.io/tls Secret or ACM annotations on ALB.
  • Gateway API — the successor: role-oriented resources (GatewayClass by infra providers, Gateway by platform teams, HTTPRoute/GRPCRoute/TCPRoute by 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.
Enlarge diagramusersshop.acme.devALBcreated by the AWSLB ControllerService orderspath /apiService paymentspath /payService webpath /
Figure 4. Ingress: one load balancer, many services. The ingress controller turns host and path rules into ALB listener rules.
Swipe sideways to see all of it, or tap to open it full screen.

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.

Enlarge diagramdb podspolicy: allow from app=apion 5432api podsapp=apiother podsapp=weballowedblockedRemember to allow DNS (UDP/TCP 53 to kube-dns) when you lock down egress.
Figure 5. NetworkPolicy: by default every pod can reach every pod. Once a policy selects the db pods, only what it allows gets in.
Swipe sideways to see all of it, or tap to open it full screen.
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).