kubernetes services
What are kubernetes services used for?
- kubernetes pods are ephemeral (can be deleted or moved)
- kubernetes assigns an IP address on the fly, so it’s not possible to know in advance
- application’s client should not know the kubernetes pods IP addresses
- enter
kubernetes services
- each
kubernetes service
has an IP address (and an associated port) that does not change as long as the service exists
- by default, the
.spec.type
isClusterIP
- a
kubernetes service
can expose multiple ports
Service discovery
- each
kubernetes service
is available withFQDN
:<service_name>.<namespace>.svc.cluster.local
- inside the same namespace, each
kubernetes service
is available with only<service_name>
Endpoints
Example:
External service
- we can reference an external service (i.e. not hosted in k8s)
- create an external service using
spec.externalName
if the external service has a DNS - if no DNS or a port redirection is needed, we can:
- create a kubernetes service without selector
- create manually a
Endpoint
Expose a service to external clients
Hostport
spec.containers.ports.hostPort
will reserved on the cluster node where the pod will be run for the service
Nodeport
- k8s will reserved a port on ALL nodes and redirect traffic on this port to the targeted service
- ℹ️ a
ClusterIP
will also be created for internal communication
NodePort
range is defined at cluster level (by default, from30000 - 32767
)
Loadbalancer
- k8s will interact with the APIs of the Cloud provider (AWS, Azure, GCP)
- asynchronous configuration/provision
Headless services
Sometimes, a client needs the kubernetes pod ips (e.g. Netflix Ribbon). To mitigate this, k8s offers a Headless service
.
- no
ClusterIP
will be created - DNS resolution will return all the kubernetes pod IPs will be returned
Port forward
It’s also possible to access to a kubernetes pod without passing into a kubernetes service, e.g. for debugging purpose, with kubectl port-forward
: