Services

Applications running in a Kubernetes cluster can communicate with each other. But outside world, only through the Service.

A Kubernetes Service is an abstraction which defines a logical group of Pods and a policy by which to access the application.

The group of Pods targeted by a Service is determined by a Label Selector (Discussed in Label & Selectors topic).

You can have many services within the cluster. Kubernetes services can efficiently power a micro service architecture.

Services provide features that are standardized across the cluster:

  • load-balancing
  • service discovery between applications
  • features to support zero-downtime application deployments

Services can be exposed in 3 different ways by specifying a “type” in the ServiceSpec:

  1. ClusterIP
  2. NodePort
  3. LoadBalancer

We will see in-detail about the service types in individual sections.

Defining a service

For example, suppose you have a deployment (or group of pods) that expose port 9000 and has a label app = php-webapp.

kind: Service
apiVersion: v1
metadata:
  name: php-service
spec:
  selector:
    app: php-webapp
  ports:
  - port: 9000
    name: php-fpm
    protocol: TCP
    targetPort: 9000

The above specification will create a new Service object named “php-service” which targets TCP port 9000 on any Pod with the “app=php-webapp” label.

This Service will also be assigned an IP address (cluster IP), which is used by the service proxies.

If you an another application that expose ports 80 and 443, you can also create a service with multiple ports.

 

kind: Service
apiVersion: v1
metadata:
  name: frontend-service
spec:
  selector:
    app: webserver
  ports:
  - port: 80
    name: http
    protocol: TCP
    targetPort: 80
  - port: 443
    name: https
    protocol: TCP
    targetPort: 443
Advertisements