
Introduction #
An overview of Kubernetes services and their role.
What is a Service? #
A Service in Kubernetes is a network abstraction that connects a set of Pods to other Pods or to external applications. It provides a stable IP address and a service discovery mechanism to simplify communication in a cluster.
Good to know: This lesson details Service types. For full HTTP exposure (Ingress, Gateway API, TLS, canary deployment), see the « Kubernetes: exposing your applications » lesson of the course.
Why use Services? #
Pods in Kubernetes are ephemeral and their IP addresses can change. Services provide persistent, reliable connectivity, even when Pods are recreated or moved.
Service types #
The different service types available in Kubernetes:
ClusterIP #
The ClusterIP type is the default mode. It makes the service reachable only inside the Kubernetes cluster through an internal IP address.
apiVersion: v1
kind: Service
metadata:
name: clusterip-service
spec:
type: ClusterIP
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080 Since it is the default type, you can omit type: ClusterIP in your configuration.NodePort #
The NodePort type exposes the service on a specific port of every node in the cluster, allowing direct external access.
apiVersion: v1
kind: Service
metadata:
name: nodeport-service
spec:
type: NodePort
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30007 Kubernetes opens the same port (between 30000 and 32767 by default) on all nodes of the cluster. A NodePort service also includes a ClusterIP address: traffic received on <node IP>:30007 is forwarded to the selected pods, whichever node hosts them. You therefore reach it from outside at http://<node-IP>:30007.
LoadBalancer #
The LoadBalancer type configures an external load balancer to expose the service publicly. This type is often used in cloud environments.
apiVersion: v1
kind: Service
metadata:
name: loadbalancer-service
spec:
type: LoadBalancer
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080Kubernetes asks the cloud provider (or MetalLB on premises) for an external load balancer with its own IP address. It sends traffic to the nodes; for this, a LoadBalancer service also automatically creates a NodePort and a ClusterIP. The address obtained appears in the EXTERNAL-IP column of kubectl get svc.
ExternalName #
The ExternalName type maps a Kubernetes service to an external DNS name. It is used to redirect traffic to non-Kubernetes services.
apiVersion: v1
kind: Service
metadata:
name: externalname-service
spec:
type: ExternalName
externalName: my.external.service.com Maps the service to the content of the externalName field (for example foo.bar.example.com) by returning a CNAME record with its value. No proxying of any kind is set up.Key components #
The essential elements of a service configuration.
Selectors and target Pods #
Services use selectors to associate target Pods with a service. Pod labels are used for this matching.
selector:
app: my-appPorts #
Services define two kinds of ports:
- Port: the port the service listens on.
- TargetPort: the port the target Pods listen on.
Endpoints #
Endpoints are the IP addresses and ports of the Pods that back the service. They are updated automatically when Pods change.
Best practices #
Tips for using services optimally.
Use clear labels #
Make sure the labels used in selectors are descriptive and consistent to avoid incorrect matches.
Secure your services #
Use NetworkPolicies to control access to sensitive services and limit exposure.
Monitor your endpoints #
Regularly monitor service endpoints to detect anomalies or non-functional Pods.
Conclusion #
Why Kubernetes services matter.
Services are an essential Kubernetes component, enabling reliable, stable network communication between Pods and with the outside world. By using the different service types correctly, you can ensure efficient connectivity and optimal management of your applications in the cluster.