Understanding Pods — Kubernetes
Kubernetes can be viewed as a modern Platform as a Service(PaaS). Kubernetes helps you manage your containers and ensure there is no downtime. In addition, Kubernetes will help you in automated deployments, scaling, load balancing, monitoring and also dealing with failover for your application.
Pods are the most important concept in Kubernetes. Everything else either manages, exposes or is used by pods. By the time you are using pods in Kubernetes especially in a real-life application, most likely you have already dealt with Docker containers. If Docker is a new concept to you, I would highly suggest that you take some time to learn at least its basics. From there it would be very easy to understand how everything fits in. Let’s, however, get a glimpse of how containers work.
Refresher on Containers.
Let’s look at containers from this angle. You have written your code in your local machine and now you are ready to move that code to production. Many are the times when the code in your local machine will not work in production. This is because of things such as different Operating Systems, dependencies, and libraries. This is where containers come in handy. Containers will help you separate your code from the underlying infrastructure. To do this you will need to use Docker. Docker is not a language or a framework but rather a tool that helps you containerize your application. Once you containerize your application, you will run your container in Kubernetes as pods.
What are PODS?
To help you understand what pods are, let me take you to another world. The oceanic world.
In the oceanic world, a group of whales is called a pod. A pod usually includes whales that are either related to each other or have formed friendships with each other. Pods can be anything from two to thirty whales or more. Whales in a pod socialize together, travel together and protect each other from predators.
In Kubernetes, a pod is a group of one or more containers. Pods run in Nodes. Nodes are worker machines in Kubernetes which might be a virtual machine or an actual machine. Simply put, at a high level, you will run containers in your pods, and your pods run in Kubernetes. You will have many containers in a pod in the event that the containers need to be scheduled together, run on the same node, communicate locally and at times also share storage. In many cases, though you would want to have one container in your Pod. Your Kubernetes pods, just like the whale pods will work together, communicate with each other and protect your application from preventable downtimes.
How we create Pods.
Like many other Kubernetes resources that you will encounter you can create a pod in two ways.
- By posting a JSON or YAML manifest to the Kubernetes REST API endpoint.
- The other way is by running the kubectl command that will allow you to quickly configure the pod's properties. Not all properties, however, can be set using this command.
Let’s create a simple YAML descriptor for a pod.
apiVersion : v1
- image: njerry/my-app
- containerPort: 8080
protocol : TCP
apiVersion: specifies the version of the Kubernetes API you are using
kind: We are describing a pod
metadata: name- The name of your pod
container: image — the container image we are creating the container from as it is in the docker registry.
name: the name of the container
port: the port the app is listening on
Challenge: Create a pod called my-pod with the image nginx and expose it on port 80.
To do this you will follow the steps below.
- Go to your Kubernetes cluster. It could be in your local machine or from a Kubernetes cloud provider.
- Create a .yaml file by switching to a Linux terminal text editor. In this case, I am using vim.
3. Insert the pod’s contents as we had done previously. Save and exit the vim/nano mode.
4. Writing and saving this information does not do anything. You will need to apply these changes.
5. View the pod you have created.
kubectl get pods
The running state indicates that our pod has been created successfully and it’s working. A pending state would mean that the pod has not yet been scheduled.
6. Get the IP address of the Pod.
7. View more details about the pod.
8. If need be, delete your pod.
You can delete your pod in two ways
# deleting using the pod's namekubectl delete pod my-pod
Option b: Deleting using the file you used to create the pod
kubectl delete -f mypod.yaml
That was the yaml file way of creating your pod. To create your pod using the kubectl command you would do it this way.
kubectl run podname --image=imagename restartPolicy --port=port number expose
Viola! Just like that our pod has been created.
It is important to have a strong understanding of pods because everything else in Kubernetes revolves around pods. Once you understand pods you can now have a look at deployments in Kubernetes. There is a lot more on pods but grasping the concept above will help understand how everything else around pods fits in together. Find the link below for an article I have written to help you get started with Kubernetes deployments.