Home Kubernetes How To Create And Manage Kubernetes Pods In Linux

How To Create And Manage Kubernetes Pods In Linux

Create, View And Delete Pods In Kubernetes

By Rudhra Sivam
Published: Last Updated on 2.4K views

In this tutorial, we are going to learn how to create and manage Kubernetes Pods. First, we will start with what is a Pod in Kubernetes and how does a Pod work. Next, we shall take a brief look at the types of Kubernetes Pods. And then we will see how to create a new Pod and how to view the information of Pod from the command line. Finally, we will learn how to delete the Pod when it's no longer needed.

What is a Pod in Kubernetes?

In Kubernetes, pods are the smallest deployable computing units that you can build and control. A Pod is a collection of one or more containers with shared storage and network resources, as well as a set of rules for how the containers should be run.

The contents of a Pod are always co-located, co-scheduled, and executed in the same environment. A Pod represents an application-specific "logical host": it includes one or more closely connected application containers.

In Docker perspective, A Pod is equivalent to a set of Docker containers with common namespaces and filesystem volumes.

Types of Kubernetes Pods

In a Kubernetes cluster, there are two types of Pods.

  • Single Container POD: The most frequent Kubernetes use case is the "one-container-per-Pod" approach; in this scenario, consider a Pod as a wrapper over a single container; Kubernetes maintains Pods rather than containers directly.
  • Multiple Containers POD: A Pod can encapsulate an application that is made up of numerous tightly connected containers that need to share resources. These containers are grouped together to form a single service unit and these containers will communicate with each other.

How does POD work?

Each Pod is designed to execute a single application instance. You can utilize several Pods, one for each instance, if you are in need to extend your application horizontally. This is commonly referred to as replication in Kubernetes. A workload resource and its controller normally produce and manage replicated Pods in a group.

The ultimate aim of the Kubernetes is to deploy the application in the form of containers in a set of machines which is called as worker node in Kubernetes Cluster.

Kubernetes does not launch containers directly in the node rather the container(s) are encapsulated as Pod which is a single instance of an application.

Kubernetes Cluster
Kubernetes Cluster

In a Kubernetes Cluster, a node consists of Pods and a Pod consists of Containers. For example, you have a single instance of an application running in a single container encapsulated by a Pod.

If the number of users increase accessing the application, you need to scale up your application. In that case, you may need to spin up additional instances to share the load.

For scaling up the application, you need to spin up additional new Pod in the node with the same application. If the load increases further and the current node has no capacity, then you need to have an additional node to spin up Pods with the same application container.

Similarly, for scaling down, you need to delete existing Pod.

Hope you got the basic idea about Kubernetes Pods. Let us now see how to create and manage Pods in Kubernetes.

Before getting started, make sure you have installed Kubernetes on your system. The following links have instructions on how to setup a single node and multinode Kubernetes cluster deployment in Linux.

Once Kubernetes installed, You can begin creating Pods.

Usually, you don't need to create Pods directly, even singleton Pods. Instead, create them using workload resources such as Deployment or Job. A Deployment provides declarative updates for Pods and ReplicaSets.

Create a Deployment

Create a sample deployment using an existing image "echoserver". It is a simple HTTP server and we can expose it on port 8080 using --port option.

A container image is a file that wraps a program and all of its software dependencies in binary data. Container images are independent executable software packages that make extremely specific assumptions about their execution environment.

In this article, we will go through all the demonstrations in a single node cluster.

You can get to know the node details by running the below command as root user

# kubectl get nodes

Sample output:

NAME        STATUS   ROLES    AGE   VERSION
ostechnix   Ready    Master   25h   v1.22.3
Get Node Details
Get Node Details

Now, deploy a sample application called "hello-ostechnix" using the below command. You can name it according to your convenience.

# kubectl create deployment hello-ostechnix --image=k8s.gcr.io/echoserver:1.10

Sample output:

deployment.apps/hello-ostechnix created
[root@ostechnix ~]#

Deployment is created, verify the deployment using the below command.

# kubectl get deployments

Sample output:

NAME              READY   UP-TO-DATE   AVAILABLE   AGE
hello-ostechnix   1/1     1            1           85s

Access Deployment

To access this deployment as a service, you need to expose it as a service. Use the below command to expose the deployment on the port 8080.

# kubectl expose deployment hello-ostechnix --type=NodePort --port=8080

Sample output:

service/hello-ostechnix exposed

Use the below command to get the URL of the exposed service.

# minikube service hello-ostechnix --url

Sample output:

http://192.168.181.131:30525
Service URL
Service URL

Copy the URL and paste it in your browser, it will list the details of the deployed application.

View Details Of The Deployed Application In Browser
View Details Of The Deployed Application In Browser

Make sure the port 30525 is allowed in your router or firewall. If this port is blocked, you may not view the Pod's information in the browser.

Delete Deployment

First, delete the service "hello-ostechnix" using command:

# kubectl delete services hello-ostechnix

Sample output:

service "hello-ostechnix" deleted

Next, delete the deployment "hello-ostechnix":

# kubectl delete deployment hello-ostechnix

Sample output:

deployment.apps "hello-ostechnix" deleted

Once you deleted the deployment, the Pod associated with the application will be terminated. It will take a few seconds to complete the termination.

# kubectl get pods

Sample output:

NAME                               READY   STATUS        RESTARTS   AGE
hello-ostechnix-5d4cf4df75-jlwff   1/1     Terminating   0          57m

After terminating the deployment, the application will no longer be accessible.

# kubectl get pods

Sample Output:

No resources found in default namespace.

Run Pods With Images

We can use the 'kubectl run' command to create and run an application in a Pod.

$ kubectl run <POD name> --image=<image name>

Here, the Pod name can be anything and the image name must be specific which would be available in Docker Hub or any local repository.

Let us create a Pod with Nginx image.

# kubectl run ostechnix-nginx --image=nginx

Sample output:

pod/ostechnix-nginx created

A Pod named 'ostechnix-nginx' has been created. You can verify the status of the Pod by the below command.

# kubectl get pods

Sample output:

NAME              READY   STATUS    RESTARTS   AGE
ostechnix-nginx   1/1     Running   0          4m20s

You can get additional information like where the Pod is running and IP of the Pod by using the 'wide' option.

# kubectl get pods -o wide

Sample output:

NAME              READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
ostechnix-nginx   1/1     Running   0          38m   172.17.0.6   ostechnix   <none>           <none>

You can get complete information about the POD by using the 'kubectl describe' command.

# kubectl describe pod ostechnix-nginx

This command will get you the complete details of POD like container details and its states, Events details which occurred since the Pod was created.

Name:         ostechnix-nginx
Namespace:    default
Priority:     0
Node:         ostechnix/192.168.181.131
Start Time:   Thu, 03 Feb 2022 01:40:48 -0800
Labels:       run=ostechnix-nginx
Annotations:  <none>
Status:       Running
IP:           172.17.0.6
IPs:
  IP:  172.17.0.6
Containers:
  ostechnix-nginx:
    Container ID:   docker://29eeb0392247aef193d6dff0138f8ef132dfb6359d8e67c3a5e4a21d7e259989
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Thu, 03 Feb 2022 01:40:54 -0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-ggvk6 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-ggvk6:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  7m26s  default-scheduler  Successfully assigned default/ostechnix-nginx to ostechnix
  Normal  Pulling    7m24s  kubelet            Pulling image "nginx"
  Normal  Pulled     7m20s  kubelet            Successfully pulled image "nginx" in 4.114836826s
  Normal  Created    7m20s  kubelet            Created container ostechnix-nginx
  Normal  Started    7m20s  kubelet            Started container ostechnix-nginx
[root@ostechnix ~]#
Display Complete Information Of A Pod
Display Complete Information Of A Pod

Once you done, you can delete the Pod by using the 'kubectl delete' command.

# kubectl delete pod ostechnix-nginx

Sample output:

pod "ostechnix-nginx" deleted

Verify if the Pod is deleted by listing the available Pods:

# kubectl get pods
No resources found in default namespace.
Delete A Pod
Delete A Pod

Conclusion

In this tutorial, we have gone through the concept of Pod in Kubernetes, a simple application deployment and how to run a Pod. We will have more details about creating Pod with YAML definition file in our upcoming details.

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More