Kubernetes Services & Networking
Master Kubernetes Services — stable virtual IPs, internal DNS discovery, ClusterIP, NodePort, LoadBalancer, and EndpointSlice load balancing.
Kubernetes Services & Networking
Pods are ephemeral. When a Pod crashes or a Deployment performs a rolling update, new Pods receive new internal IP addresses. A Service provides a single, permanent virtual IP and DNS name that automatically load-balances traffic across the dynamic pool of backend Pods.
1. How Services Work
[ Incoming Request: http://api-service:8080 ]
│
▼
┌───────────────────────────┐
│ Service: api-service │
│ ClusterIP: 10.96.0.45 │
│ Selector: app=api │
└─────────────┬─────────────┘
│ (Kube-proxy Load Balancing)
┌─────────────┼─────────────┐
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Pod 1 │ │ Pod 2 │ │ Pod 3 │
│ 10.244.1│ │ 10.244.2│ │ 10.244.3│
└─────────┘ └─────────┘ └─────────┘2. Service Types
| Type | Accessibility | Description |
|---|---|---|
ClusterIP (Default) | Internal Only | Allocates a virtual cluster-internal IP. Accessible only by other pods inside the cluster. |
NodePort | Cluster Node IPs | Opens a static high port (30000-32767) on every Worker Node's IP address. |
LoadBalancer | Public Internet | Provisions a cloud provider load balancer (AWS ALB/NLB, GCP, Azure) and forwards traffic to the Service. |
ExternalName | Internal -> External | Maps a service name to an external DNS CNAME (e.g. api.stripe.com). |
3. ClusterIP Service (Standard Internal Communication)
# service-clusterip.yaml
apiVersion: v1
kind: Service
metadata:
name: backend-api
namespace: default
spec:
type: ClusterIP
selector:
app: backend-api # Must match the label on your Pods
ports:
- protocol: TCP
port: 80 # Port exposed on the Service
targetPort: 8080 # Port your container is actually listening on4. Kubernetes Internal DNS Resolution
Kubernetes runs CoreDNS. Any Pod in the cluster can communicate with another Service using standard DNS:
# From inside the same namespace:
curl http://backend-api:80
# From a different namespace (e.g. 'staging'):
curl http://backend-api.default.svc.cluster.local:80DNS Hierarchy Breakdown
backend-api . default . svc . cluster.local
[Service] [Namespace] [Type] [Domain]5. LoadBalancer Service (Public Ingress via Cloud)
# service-loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
name: public-web-lb
spec:
type: LoadBalancer
selector:
app: web-frontend
ports:
- port: 80
targetPort: 3000Check the allocated public external IP:
kubectl get svc public-web-lbOutput:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
public-web-lb LoadBalancer 10.96.120.15 35.230.40.12 80:31245/TCPKubernetes ReplicaSets
Understand ReplicaSets — the low-level controller maintaining a stable set of replica Pods, label selectors, and self-healing mechanics.
Kubernetes ConfigMaps
Decouple non-sensitive configuration parameters, environment variables, and config files from container images using Kubernetes ConfigMaps.