Kubernetes Service resource types¶
A Service is a core Kubernetes resource that defines a logical set of Pods and a policy by which to access them.^[400-devops-06-kubernetes-k8s-paas-03k8s.md] Services enable the abstraction of a stable network endpoint (IP or DNS name) to a dynamic group of Pod replicas, which may have changing IP addresses.^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
Core Concepts¶
In Kubernetes, Pods are ephemeral; their IP addresses change when they are restarted or rescheduled.^[400-devops-06-kubernetes-k8s-paas-03k8s.md] To address this, Services provide a stable abstraction point.^[400-devops-06-kubernetes-k8s-paas-03k8s.md] This abstraction is achieved by using a Label Selector to group Pods, which allows the Service to route traffic to all Pods matching specific labels.^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
Service Resource Management¶
Managing Services typically involves using kubectl commands to create, expose, and inspect resources.^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
- Imperative Creation: You can create a Service directly from the command line using the
kubectl exposecommand, which takes an existing resource (like a Deployment) and maps a port to it^[400-devops-06-kubernetes-k8s-paas-03k8s.md].- Example:
kubectl expose deployment nginx-dp --port=80 -n kube-public^[400-devops-06-kubernetes-k8s-paas-03k8s.md]
- Example:
- Declarative Creation: Alternatively, you can define a Service in a YAML manifest (declarative) and apply it^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
Service Types and Spec¶
The behavior of a Service is defined by its spec section within the YAML configuration^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
- ClusterIP (Default): Assigns a virtual IP address from the cluster's dedicated range (e.g.,
192.168.0.0/16)^[400-devops-06-kubernetes-k8s-paas-03k8s.md]. This IP is only reachable from within the cluster^[400-devops-06-kubernetes-k8s-paas-03k8s.md]. - Type: Specifies the type of Service, such as
ClusterIP,NodePort, orLoadBalancer^[400-devops-06-kubernetes-k8s-paas-03k8s.md]. - Session Affinity: Defines the sticky session policy (e.g.,
NoneorClientIP)^[400-devops-06-kubernetes-k8s-paas-03k8s.md].
Related Concepts¶
Sources¶
400-devops-06-kubernetes-k8s-paas-03k8s.md