visit
Kubernetes is the
de facto
standard for running containerized applications. Kubernetes (K8s) is an open-source system for automating deployment
, scaling
, and management
of containerized applications.Kubernetes makes it easy to deploy and run containerized applications. Kubernetes is simple to use. Kubernetes is complex to understand because it provides a huge set of options to make your deployment easier.Aptly named, Kubernetes is a pilot (or) helmsman that helps you to sail the container world. Kubernetes is a portable and extensible system built by the community for the community. As Kelsey, correctly quotesKubernetes does the things that the very best system administrator would do automation, failover, centralized logging, monitoring. It takes what we’ve learned in the DevOps community and makes it default, out of the box.In order to work with Kubernetes, it is very important to understand
The Kubernetes run in a highly available cluster mode. Each Kubernetes cluster consists of one or more
main
node and a few other
nodes.Main Node
The main node consists of an API server, Scheduler, Controllers, etcd. This node is called the control plane of Kubernetes. This control plane is the brain of Kubernetes.API server
, we instruct or get information from the Kubernetes.Scheduler
is responsible for scheduling the pods. controllers
are responsible for running the resource controllers.etcd
is a storage for the Kubernetes. It is key-value storage.Node
The worker nodes have a
Kubelet
and proxy
.The Kubelets are the actual workhorse and the Kube-proxy handles the networking.Working
We provide the yaml file to the Kubernetes cluster through
kubectl apply
command.The
apply
command calls the API server, which will send the information to the controller and simultaneously stores the information to the etcd.The
etcd
then replicate this information across multiple nodes to survive any node failure.The
controller
will check whether the given state matches the desired state. If it is not it initiates the pod deployment, by sending the information to the schedulerThe checks are called as the reconciliation loop that runs inside the Kubernetes. The job of this loop is to validate whether the state requested is maintained correctly. If the expected state and actual states mismatch this loop will do the necessary actions to convert the actual state into the expected state.
The
scheduler
has a queue inside. Once the message is received in the queue.The scheduler will then invoke the
kubelet
to do the intended action such as deploying the container.This is a 10000 feet bird view of how Kubernetes does the deployment.There are various components inside the Kubernetes. Let us take a look at what are they and how are they useful.apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
The below file is the sample deployment file that tells the Kubernetes to create a deployment of
nginx
using the nginx:1.7.9
container.apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 3 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "my-storage-class"
resources:
requests:
storage: 1Gi
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: MyApp
ports:
- protocol: TCP
port: 80
targetPort: 9376
You can consider the services as the load balancer, proxy or traffic router in the world of Kubernetes.
1. Load Balancer
2. Node Port
3. Ingress
Secrets
Often for the applications, we need to provide passwords, tokens, etc., Kubernetes provides secrets object to store and manage the sensitive information. We can create a secret like below:apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
stringData:
config.yaml: |-
apiUrl: "//my.api.com/api/v1"
username: {{username}}
password: {{password}}
While Kubernetes is an ocean and whatever we have seen is just a drop in it. Since Kubernetes supports a wide range of applications and options, there are various different options and features available.Few best practices to follow while working with Kubernetes are:
Split your yaml files into smaller files.
The
single responsibility principle
applies here.Always remember to have smaller image sizes. Use builder pattern to create the images from Alpine images.
Each container in the pod can request and limit their resources.
resources:
requests:
memory: "100Mi"
cpu: "100m"
limits:
memory: "200Mi"
cpu: "500m"
Previously published at