This repository contains some key concepts about Kubernetes and how to get started with it.
Kubernetes is an open-source tool for managing containers. It was initially released by Google in 2014. But what does managing containers exactly mean? Well, imagine you have 5 containers, each containing an image for your app. If you need to replicate these containers, it would be easy to do, right? Of course, it doesn't require a lot of effort. However, when you need to administer 100, 300, 500, or more containers, it becomes challenging to handle them all. This is where Kubernetes comes in. So, what does Kubernetes exactly do? These are some features of Kubernetes:
- Service discovery
- Rollouts and Rollbacks: Updates and deploys without downtime
- Resource optimization in nodes
- Self-healing: If any container fails, Kubernetes brings it back automatically
To simulate a cluster locally, you can use Minikube. To interact with the cluster, you need kubectl
, which is the Kubernetes command-line tool. Here are the documentation links for both:
-
Pod: It's the smallest unit of Kubernetes. In simple words, a pod is a container, similar to a Docker container. The difference is that you can have one or more containers in a pod, whereas in Docker, there is only one container per image. Here's an example to create a pod:
- Create a file called
pod.yaml
. - Put the following content inside the file:
apiVersion: v1 kind: Pod metadata: name: first-pod spec: containers: - name: nginx-container image: nginx ports: - containerPort: 80
- This pod has a container named
nginx-container
with thenginx
image. The image is downloaded from Dockerhub (it can be our own image as well), and it will run on port 80. To create the pod, run the following command:kubectl apply -f pod.yaml
- To check the pods, run:
kubectl get pods
- To delete a pod, use the following command:
kubectl delete pod [pod-name]
If you would like to know more about pods, check the documentation here.
Note: Typically, we won't use just a pod because if any container fails, the pod will also fail and won't be brought up again.
- Create a file called
-
ReplicaSet: A ReplicaSet is an object that manages pods. For example, if you have 3 pods, and one of them fails, the ReplicaSet detects the failure and creates a new pod. Here's an example to create a ReplicaSet:
- Create a file called
rs.yaml
. - Put the following content inside the file:
apiVersion: apps/v1 kind: ReplicaSet metadata: name: frontend labels: app: rs-test tier: frontend spec: replicas: 2 selector: matchLabels: tier: frontend template: metadata: labels: tier: frontend spec: containers: - name: container1 image: python:3.6-alpine command: ['sh', '-c', 'echo "container 1" > index.html && python -m http.server 8082'] - name: container2 image: python:3.6-alpine command: ['sh', '-c', 'echo "container 2" > index.html && python -m http.server 8083']
- This ReplicaSet will have two replicas (pods), each with two containers (
container1
,container2
) using thepython:3.6-alpine
image (they can be different images). Labels are important for identifying the pods. To create the ReplicaSet, run the following command:kubectl apply -f rs.yaml
- To check the ReplicaSet, run:
kubectl get replicaset
If you would like to know more about ReplicaSets, check the documentation here.
- Create a file called
-
Deployment: A Deployment is an object that manages ReplicaSets. It allows us to update the configuration of our YAML file. For example, if we need to change the version of a specific image or add a new image or any configuration, the Deployment creates a new ReplicaSet, keeping the previous ReplicaSet(s) in case we need to roll back. Here's an example to create a Deployment:
- Create a file called
dev.yaml
. - Put the following content inside the file:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: front spec: replicas: 3 selector: matchLabels: app: front template: metadata: labels: app: front spec: containers: - name: nginx image: nginx ports: - containerPort: 80
- The name of the deployment is
nginx-deployment
, with a labelapp
and valuefront
. Thespec
part specifies 3 replicas, a selector, and a template. The template contains a container namednginx
with thenginx
image and port 80. - To create the Deployment, run the following command:
kubectl apply -f dev.yaml
- To check the Deployment, run:
kubectl get deployment
If you would like to know more about Deployments, check the documentation here.
- Create a file called
I hope this helps! Let me know if you have any further questions.