Kubernetes Basics

Tok Xin Yi
4 min readMay 5, 2021

This is a summary of what I have learnt about Docker and Kubernetes in this free course by freeCodeCamp. In this article, I will summarise what I have learnt about Kubernetes only.

What is Kubernetes?

It is a container orchestration tool. It is used when you have multiple a lot of containers at the same (at least 100+).

Kubernetes helps to:

  1. Detect errors in containers and restore failed pods automatically
  2. Helps you manage containerised applications in different deployment environments (e.g. on-premise, cloud, hybrid)

Kubernetes uses the master-worker architecture. So in one cluster of machines, there are master and worker nodes.

The master node runs the important Kubernetes processes. We need the master node to access the worker nodes.

The master node contains the following:

  1. API server — an entry point to the Kubernetes cluster
  2. Controller manager — keeps track of what is happening in the cluster
  3. Scheduler — decides which worker node to run the container based on the workload and available resources then
  4. etcd — storage that keeps all the configuration data and statuses of each node. Backup and restore of the pods and services are done using the data stored here.

Kubernetes have the following components:

  1. Pods
  2. Service
  3. Config Map
  4. Secrets
  5. Volumes
  6. Deployment
  7. StatefulSet

Pods

They are the smallest unit in Kubernetes. 1 pod usually hold 1 application container, but it can hold multiple application containers.

Each pod have their own internal IP address, which will change each time the pod is restarted/created.

Services

Service creates a permanent IP address to each pod so we don’t have to keep track of the pod’s IP address every time it changes.

The lifecycle of a pod and service is different. Hence, even if the pod dies and restarts, the service and the IP address will remain the same.

Config Map

It holds the configuration details of your application. For example, the URL of the database and environment variables that the pod might need to use during deployment.

With Config Map, administrators will not have to rebuild the image each time the configuration details changes. They just have to apply the Config Map and Kubernetes will use the latest configuration settings in the pods.

Data in Config Map is stored in plain text. Hence it is not recommended to store credential information in Config Map! Use Secret instead.

Secret

It is similar to Config Map, but it holds all the secret information. For example, the database username and password.

Data in Secret is stored in base64 encoded.

Volumes

Data in pods are not persistent. If the pod crashed and gets restarted, all the data stored in the pod is gone.

To persist the data, you have to mount a volume to the pod. The volume can be in the Kubernetes cluster or it can be remote storage outside of the Kubernetes cluster.

Kubernetes does not manage data persistence. Users will have to mount the volumes if they want the data to be persisted.

Deployment

You create the pods and services in Kubernetes by creating a deployment.

Deployment is a blueprint to tell Kubernetes how you want the pods and services to be created. For example, if you want the pods to have multiple replicas to have high availability, you have to define it in the Deployment file.

Deployment is used for stateless apps, where the pods and services can be removed and re-created at any time.

StatefulSet

It is similar to Deployment but is used for stateful apps like databases, where you have to keep track of the state of the data when you want to re-create the pods and services. <do more research to explain this better in simpler terms, if possible>

Kubernetes Cheat Sheet

To get all the pods/services/deployments/config map/secrets/replica set created in the Kubernetes cluster

kubectl get pods|services|deployment|configmap|secrets|replicaset

— -

To create a deployment using a specified image (i.e. to create the pod using a specified image)

kubectl create deployment <deployment name> --image:<image name>

To create a deployment/configmap/secrets using the parameters defined in the file

kubectl apply -f <filename>

If there are changes made to the file, just re-run the above command to apply the changes.

To remove all the pods/services/deployments/configmap/secrets/replicaset created in the cluster

kubectl delete pods|services|deployment|configmap|secrets|replicaset

To remove deployments/configmap/secrets that are created from the config file

kubectl delete -f <filename>

To debug the pods

kubectl logs <pod name>
kubectl describe pod <pod name>
kubectl exec -it <pod name> bash

Conclusion

Through this course, I have learnt the basics of Kubernetes and it helped me understand the conversation/problems my colleagues were having when debugging on the cluster. I would recommend this concise 4 hours course for people who wants to understand the basics of Docker and Kubernetes.

--

--

Tok Xin Yi

“Explore everything, keep the best.” — John Evelyn