Kubernetes Service¶
A Kubernetes Service is an API object that defines a logical set of Pods and a policy to access them.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md] It decouples the exposure of ports from the Pods themselves, allowing for stable networking endpoints even as Pods are dynamically created or destroyed.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md]
Core Concepts¶
Services utilize Labels and Selectors to manage Pods.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md] Instead of tracking individual Pod IP addresses, which change during restarts or rescheduling, the Service uses a selector to identify Pods with specific labels (e.g., app: foo or type: demo).^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md] This ensures that network traffic is consistently routed to the appropriate Pods, regardless of their underlying IP changes.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md]
Service Types¶
Kubernetes supports different types of Services depending on the exposure requirements:
ClusterIP: The default type, exposing the Service on an internal cluster IP.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md] This makes the Service reachable only from within the cluster.^[400-devops-06-kubernetes-k8s-learning-05service-service.md]NodePort: Exposes the Service on a specific port on each Node's IP.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md] If a port is not specified manually, Kubernetes will automatically select one.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md]LoadBalancer: Provisions an external load balancer (typically provided by cloud providers like AWS or GCP) to route external traffic to the Service.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md]
Configuration¶
A Service is typically defined in a YAML file.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md] Key fields include:
spec.selector: Defines the label selector used to identify the target Pods.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md]spec.ports: Configures the port mapping.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md]port: The port exposed by the Service.targetPort: The port on the Pod to which traffic is forwarded.nodePort: The port on the Node (forNodePorttype).protocol: SupportsTCP,UDP, andSCTP; defaults toTCP.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md]
Networking and Discovery¶
Load Balancing¶
A Service provides load balancing by distributing traffic across the selected Pods.^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md] Requests sent to the Service's IP and port are proxied to the backend Pods.^[400-devops-06-kubernetes-k8s-learning-05service-service.md] This is evident when repeatedly querying a Service endpoint and receiving responses from different Pod IPs.^[400-devops-06-kubernetes-k8s-learning-05service-service.md]
DNS and Internal Access¶
Within the cluster, Services can be reached by other Pods using standard DNS naming conventions, such as <service-name> or <service-name>.<namespace>.svc.^[400-devops-06-kubernetes-k8s-learning-05service-service.md] This allows for inter-service communication without hardcoding IP addresses.
Related Concepts¶
- Pod
- [[Deployment]]
- [[Labels and Selectors]]
Sources¶
^[400-devops-06-kubernetes-k8s-ithelp-day7-readme.md] ^[400-devops-06-kubernetes-k8s-learning-05service-service.md]