Services and Networking
Services provide stable network endpoints for accessing Pods. Let’s explore how to expose your applications.
Why Services?
Pods are ephemeral and their IP addresses change. Services solve this by:
- Providing stable IP addresses and DNS names
- Load balancing traffic across Pods
- Enabling service discovery
- Supporting multiple protocols
Service Types
ClusterIP (Default)
Exposes Service only within the cluster:
apiVersion: v1kind: Servicemetadata: name: my-servicespec: type: ClusterIP selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080NodePort
Exposes Service on each Node’s IP at a static port:
apiVersion: v1kind: Servicemetadata: name: my-servicespec: type: NodePort selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 nodePort: 30080 # Optional: 30000-32767Access via: <NodeIP>:30080
LoadBalancer
Creates external load balancer (cloud providers):
apiVersion: v1kind: Servicemetadata: name: my-servicespec: type: LoadBalancer selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080ExternalName
Maps Service to DNS name:
apiVersion: v1kind: Servicemetadata: name: external-dbspec: type: ExternalName externalName: database.example.comCreating Services
Deploy an application and expose it:
# Create Deploymentkubectl create deployment nginx --image=nginx
# Expose as ClusterIPkubectl expose deployment nginx --port=80 --target-port=80
# Expose as NodePortkubectl expose deployment nginx --type=NodePort --port=80
# Expose as LoadBalancerkubectl expose deployment nginx --type=LoadBalancer --port=80Service Discovery
DNS
Kubernetes DNS automatically creates records:
# Within same namespacecurl http://my-service
# Across namespacescurl http://my-service.namespace.svc.cluster.localEnvironment Variables
Each Pod gets environment variables for Services:
MY_SERVICE_SERVICE_HOST=10.96.0.10MY_SERVICE_SERVICE_PORT=80Endpoints
Services track Pod IPs through Endpoints:
# View Service endpointskubectl get endpoints my-service
# Describe Servicekubectl describe service my-serviceSession Affinity
Stick clients to same Pod:
spec: sessionAffinity: ClientIP sessionAffinityConfig: clientIP: timeoutSeconds: 10800Headless Services
For direct Pod access (StatefulSets):
apiVersion: v1kind: Servicemetadata: name: my-headless-servicespec: clusterIP: None selector: app: my-app ports: - port: 80Multi-Port Services
Support multiple ports:
spec: ports: - name: http protocol: TCP port: 80 targetPort: 8080 - name: https protocol: TCP port: 443 targetPort: 8443Testing Services
# Get Service detailskubectl get serviceskubectl describe service my-service
# Test from within clusterkubectl run test --image=busybox -it --rm -- wget -O- http://my-service
# Port forward for local accesskubectl port-forward service/my-service 8080:80
# For NodePort service on Minikubeminikube service my-service
# Get LoadBalancer external IPkubectl get service my-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}'Ingress
For HTTP/HTTPS routing with path-based rules:
apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: my-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: /spec: rules: - host: myapp.example.com http: paths: - path: / pathType: Prefix backend: service: name: my-service port: number: 80Network Policies
Control traffic between Pods:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-frontendspec: podSelector: matchLabels: app: backend ingress: - from: - podSelector: matchLabels: app: frontend ports: - protocol: TCP port: 8080Best Practices
- Use meaningful names - Make Services discoverable
- Add labels - Organize and filter Services
- Set resource limits - On backing Pods
- Use health checks - Ensure traffic goes to healthy Pods
- Choose right Service type - Based on access requirements
- Implement Network Policies - Secure Pod-to-Pod communication
- Monitor Services - Track request rates and errors
Troubleshooting
Common issues:
# Service not accessiblekubectl get endpoints my-service # Check if Pods are selectedkubectl describe service my-service
# DNS not workingkubectl run test --image=busybox -it --rm -- nslookup my-servicekubectl get pods -n kube-system -l k8s-app=kube-dns
# NodePort not accessiblekubectl get nodes -o wide # Get Node IPskubectl get service my-service # Verify NodePort
# LoadBalancer pendingkubectl describe service my-service # Check eventsCleanup
# Delete Servicekubectl delete service my-service
# Delete Service and Deploymentkubectl delete deployment,service my-appNext Steps
You’ve learned how to expose applications with Services. Next, we’ll explore persistent storage with Volumes and PersistentVolumes.