Understanding Kubernetes Service Networking and Configuration
Service Definition and Purpose
A Kubernetes Service (abbreviated as 'svc') acts as an abstraction layer for exposing application pods. While pods are ephemeral, Services provide stable endpoints for accessing pod groups. Services implement load balancing across pod replicas and define access policies for application components.
Service-Pod Network Architecture
Service and Endpoints Relationship
Services maintain dynamic endpoint objects that track pod IP addresses. When pods scale up/down, endpoints automatically update to reflect current pod members.
Endpoints and Pod Connectivity
Endpoints contain live pod network information (IP:port combinations). Kube-proxy uses these endpoints to configure iptables rules for traffic routing.
Network Communicasion Flow
- Service creation generates a ClusterIP and matching endpoints object
- Kube-proxy watches endpoints changes and updates node iptables rules
- Incoming traffic gets DNAT-translated to pod IPs via iptables rules
Service Manifest Example
apiVersion: v1
kind: Service
metadata:
name: web-service
labels:
component: webserver
spec:
selector:
tier: frontend
ports:
- protocol: TCP
port: 8080
targetPort: 80
name: http-port
type: ClusterIP
Service Management Operations
Creating a Service
kubectl apply -f service-definition.yaml
Inspecting Service Details
kubectl get services
kubectl describe svc web-service
Testing Service Connectivity
kubectl exec -it test-pod -- curl http://web-service:8080