How to deploy your first pod on a Kubernetes Cluster

In this article we will see how to create our first Pod on Kubernetes Cluster. We shall see the steps to create a pod for Nginx.

A Pod is the basic execution unit of a Kubernetes application. It is a collection of containers that are deployed together on the same host. Pods in a Kubernetes cluster can be used in two ways:

  1. Pods that run a single container: This is the most popular way of using a Pod. Kubernetes manages the pods instead of directly managing the containers. 
  2. Pods that run multiple containers that need to work together: In this model, a pod can have multiple containers that are tightly coupled to share resources. 

As stated above that a Pod can contain multiple containers, it is always recommended to have a single container when possible. Grouping multiple containers in a single Pod is a relatively advanced use case. You should use this pattern only in specific instances in which your containers are tightly coupled. 

If we deploy a single container, we can generally replace the word "pod" with "container". Pods enable data sharing and communication among their constituents. 

A Pod always runs on a Node. A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster. Each Node is managed by the Master. A Node can have multiple pods, and the Kubernetes master automatically handles scheduling the pods across the Nodes in the cluster.

The 5 stages in a pod lifecycle

  1. Pending: The Pod has been accepted by the Kubernetes system, but one or more of the Container images has not been created. This includes time before being scheduled as well as time spent downloading images over the network, which could take a while.
  2. Running: The Pod has been bound to a node, and all of the Containers have been created. At least one Container is still running, or is in the process of starting or restarting.
  3. Succeeded: All Containers in the Pod have terminated in success, and will not be restarted.
  4. Failed: All Containers in the Pod have terminated, and at least one Container has terminated in failure. That is, the Container either exited with non-zero status or was terminated by the system.
  5. Unknown: For some reason the state of the Pod could not be obtained, typically due to an error in communicating with the host of the Pod.

Pre-requisites

  1. AWS Account (Create if you don’t have one)
  2. Kubernetes Cluster (Search for "Setup a Kubernetes Cluster on AWS EC2 Instance Ubuntu 18.04 LTS using kubeadm" if you want to learn to create a Kubernetes Cluster).

Note: You can use VMs too to create a cluster if you do not want to try on AWS EC2 Instances.

What we will do

  1. Create a Kubernetes Pod for NginX and delete it

Create a Kubernetes Pod for Nginx

To create our first pod, let's just create a new directory to create our Object/Pod File. Use the following command to create a new directory in your system

mkdir my-first-pod
cd my-first-pod/

Before we proceed, verify the status of the cluster.

To check the Nodes available in the cluster and to check the  version of the "kubectl" use the following commands.

sudo kubectl get nodes
sudo kubectl version

Use the following command to list the pods in the default namespace. Since this is will be our first pod on the Cluster, you will not see any pod in the default namespace.

sudo kubectl get pods

Once you have Nodes available in the cluster you are ready to create your first pod.

Create a file "my-first-pod.yml" with the following block of code

vim my-first-pod.yml

---
apiVersion: v1
kind: Pod
metadata:
  name: myfirstpod
  labels:
    app: web
spec:
  containers:
    - name: myfirstcontainer
      image: nginx
      ports:
        - containerPort: 80

Here,

  • apiVersion: APIVersion defines the versioned schema of this representation of an object.
  • kind: Kind of object you want to create. Here it is pod since we are creating a pod.
  • name: Name must be unique within a namespace. 
  • labels: Map of string keys and values that can be used to organize and categorize objects
  • spec: Specification of the desired behavior of the pod.

Now you are ready to create your pod using the following commands.

sudo kubectl apply -f my-first-pod.yml 

You can get the details of the pods using the following command.

sudo kubectl get pods

To confirm if the Pod is actually running, run the following command. This runs a command inside our pod (Note: It is similar to running docker exec.)

sudo kubectl exec myfirstpod — service nginx status

If you no longer require the pod you can delete it using "kubectl delete command".

sudo kubectl delete pods myfirstpod

At last to verify if the pod has been deleted use the following command.

sudo kubectl get pods

Conclusion

In this article, we learned basic things about the Pods, their Lifecycle stages. We saw the steps to create our first Nginx Pod on Kubernetes. We also saw how the details regarding the Pod can be extracted. Along with this, we explored the command to delete the Pod.

Share this page:

Suggested articles

0 Comment(s)

Add comment