Contents

Kubernetes Services

What is the Services in the Kubernetes?

The Services are ways to communicate users to frontend, frontend to backend, and backend to extra datasource.
The Services enable loose coupling in between microservices in application.
The Services are in the labels/selector matched Nodes and so you can access to the Nodes with configured Port.
The Service that is in the Node can access labels/selector matched PODs with the configured Port in the Node.

Services Types

NodePort
The Service makes POD access to other PODs on the Node.
ClusterIP
The Service create virtual IP inside cluster to enable communication between difference services such as frontend server to backend server.
The cluster is a group of PODs that are created by same configuration(YAML or commands, replicas).
LoadBalancer
Support Load Balance in Application.

NodePort

TargetPort
The Target is the POD.
The TargetPort is a port of the POD.
The POD is in the Node.
The Service Target Port.
Port
The Service is in the Node.
The Service’s port to go to the target port.
NodePort
The NodePort is a port of the Node.
The ranges are 30000-32767.

YAML

pod-definition.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
spec:
  containers:
    - name: nginx-container
      image: nginx

service-definition.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: NodePort
  ports:
    - targetPort: 80
      port: 80
      nodePort: 30008
  selector: # -> This is labels from pod-definition.yml
    app: myapp
    type: front-end

Commands

Create a Service

1
kubectl create -f service-definition.yml

Show Service List

1
kubectl get services

ClusterIP

YAML

pod-definition.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
spec:
  containers:
    - name: nginx-container
      image: nginx

service-definition.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apiVersion: v1
kind: Service
metadata:
  name: back-end
spec:
  type: ClusterIP
  ports:
    - targetPort: 80
      port: 80
  selector: # -> This is labels from pod-definition.yml
    app: myapp
    type: front-end

Commands

Create a Service

1
kubectl create -f service-definition.yml

Show Service List

1
kubectl get services

LoadBalancer

YAML

pod-definition.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
spec:
  containers:
    - name: nginx-container
      image: nginx

service-definition.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: LoadBalancer # -> When you use Cloud Services as Google, AWS and Azure, that's it!
  ports:
    - targetPort: 80
      port: 80
      nodePort: 30008
  selector: # -> This is labels from pod-definition.yml
    app: myapp
    type: front-end

Commands

Create a Service

1
kubectl create -f service-definition.yml

Show Service List

1
kubectl get services

Commands

Show Service List

1
kubectl get services

Show Service Detail

1
kubectl describe service myapp-service