Services

As we mentioned before Pods are ephemeral and they can be born and die during the day. In order to be able to group together a set of Pods and make them discoverable without having to keep in mind that they may die and be regenerated (e.g. because a node was faulty) we need something more abstract. This is where Services come in.

A Service is basically a group of Pods that consistitute a Service (e.g. a group of Nginx instances). When someone want to access this group of Pods it does it through the service, which will redirect the request to one of those Pods.

For more information about Services checkout the Kubernetes User Guideopen in new window.

In this Tutorial you'll learn how to run a Set of Nginx instances and expose them under a Service.

Nginx Example

In this section we'll deploy an Nginx Chart that will provide 3 nginx instances and one service that connects them.

The Nginx instances are generated by a Deployment and the Service connects those instances. This is the most common way to deploy an stateless application.

Go to the tutorial chart repo and run the nginx chart.

Here is the Deployment and Service definition:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.10-alpine
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  type: LoadBalancer
  ports:
    - name: web
      port: 80
  selector:
    app: nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

Now go to the Services section. You'll see the nginx Service with the 3 nginx instances linked to it.

Nginx Service with 3 Ready Pods