Lab 1: Get to know Kubernetes
Kubernetes was developed by Google as part of the Borg project and handed off to the open source community in 2015. Kubernetes combines more than 15 years of Google research in running a containerized infrastructure with production work loads, open source contributions, and Docker container management tools to provide an isolated and secure app platform that is portable, extensible, and self-healing in case of failovers.
Kubernetes is a solution that automates the orchestration of Container workloads. It is declarative, which means that you define the desired state and Kubernetes ensures that the state is matched at any given moment. For example you want to deploy three instances of a Container and one dies it will automatically restart a new one to match the desired state.
If you want to learn more about Kubernetes, please follow Course JTC80 Kubernetes Introduction.
In order to define the desired state, we use Resources (aka Objects). There are a lot of different types of Resources but the most important ones that we will be using are:
- Pods - Smallest deployment unit, usually runs one Container inside
- ReplicaSets - Controls the number of Pods running
- Deployment - defines the deployment of a certain Container - creates a ReplicaSet
- Service - defines how to expose the Container on the network
Deployment
A Deployment provides declarative updates for Pods (and ReplicaSets).
You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
This yaml file defines (amongst others):
Field | Description | |
---|---|---|
metadata.name | Name of the Deployment | |
spec.replicas | The number of Pods to run simultaneuosly | |
spec.template.spec.containers.image | The Container image to run | |
spec.template.spec.containers.ports | The networking ports that should be exposed |
More information on Deployments can be found here.
Service
An abstract way to expose an application running on a set of Pods as a network service.
With Kubernetes you don’t need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them.
kind: Service
metadata:
name: nginx-service
spec:
ports:
- port: 8000
targetPort: 80
protocol: TCP
selector:
app: nginx
This yaml file defines (amongst others):
Field | Description | |
---|---|---|
metadata.name | Name of the Service | |
spec.ports | Port mapping to be exposed (port 80 of the container is exposed as port 8080) | |
spec. selector.app | Defines the Deployment to map the Servcie to |
More information on Services can be found here.