0
0
Kubernetesdevops~5 mins

Why Services provide stable networking in Kubernetes - Why It Works

Choose your learning style9 modes available
Introduction
In Kubernetes, pods can be created and destroyed frequently, which changes their IP addresses. This makes it hard for other parts of the system to find and talk to them. Services solve this problem by giving a stable network address that stays the same even if pods change.
When you want to let other pods or external users reach your app without worrying about pod IP changes
When you run multiple copies of an app and want to balance traffic between them
When you need a fixed IP or DNS name for your app inside the cluster
When you want to expose your app to the internet with a stable address
When you want to group pods logically and access them as one unit
Commands
This command creates a Service in Kubernetes that gives a stable IP and DNS name to a group of pods matching the selector.
Terminal
kubectl apply -f service.yaml
Expected OutputExpected
service/my-service created
This command lists all Services in the current namespace, showing their stable IP addresses and ports.
Terminal
kubectl get services
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-service ClusterIP 10.96.0.1 <none> 80/TCP 10s
This command shows detailed information about the Service, including its stable IP, ports, and which pods it targets.
Terminal
kubectl describe service my-service
Expected OutputExpected
Name: my-service Namespace: default Labels: <none> Annotations: <none> Selector: app=my-app Type: ClusterIP IP: 10.96.0.1 Port: http 80/TCP Endpoints: 192.168.1.10:80,192.168.1.11:80 Session Affinity: None Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: Kubernetes Services give a fixed network address that stays the same even when pods change.

Common Mistakes
Trying to connect directly to pod IPs instead of using the Service IP
Pod IPs change when pods restart, so connections break.
Always use the Service IP or DNS name to connect to pods.
Not setting the correct selector labels in the Service
The Service won't find any pods to send traffic to, so it appears broken.
Make sure the Service selector matches the labels on the pods you want to expose.
Summary
Services provide a stable IP and DNS name to access pods in Kubernetes.
They keep network addresses fixed even when pods are recreated or changed.
Using Services avoids connection problems caused by changing pod IPs.