Kubernetes pods are ephemeral, meaning they can be destroyed at any time or moved to another location in the cluster. This means the IP of the pod changes without notice creating problems on how other applications and external traffic can reach the pod.

This is where services come into the picture. A service has a stable IP that clients can reliably use to contact a set of pods. There may be one or more pods in the set and the service provides load balancing over this set, taking care of which replica pod gets a particular request.

How Services Work

Services use labels to target pods. Each pod you want to target with the service should have a label on it. There is a corresponding label on the Service. In the example below the label on the service is app: blog which is also on the pod.

Discovering services can be done through environment variables or through DNS. DNS is the recommended method where a DNS server is added to the cluster and all pods should be able to resolve services through their DNS name.

Types of Services

There are three main types of services we can use.

Cluster IP: this is used to expose services within the cluster

Node Port: this allows services to be exposed externally to the cluster

Load Balancer: this is used when an external load balancer should be used, for example in GKE in the cloud

Example Service

Below is a sample yaml file for the config of a service of the default Cluster IP type:

apiVersion: v1
kind: Service
metadata:
  name: my-blog
  labels:
    app: blog
spec:
  selector:
    app: blog
    tier: backend
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
  type: ClusterIP