<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=521127644762074&amp;ev=PageView&amp;noscript=1">

How to Create, View, and Destroy a Pod in Kubernetes

Updated May 26, 2022

In our How-to-Kube series, we’d like to begin by covering the pod basics. Like service, volume, and namespace, a pod is a basic Kubernetes object. A pod is the smallest execution unit, consisting of a single container or a series of related containers of an application. Along with the application’s container, a pod also contains storage resources and options for container processes.

In this post, you’ll learn how to create a pod in Kubernetes using the nginx image, view the YAML which describes the pod and then delete the pod that you’ve created. We’ll be using the Minikube tool that enables you to run a single-node Kubernetes cluster on your laptop or computer. Let’s get started.

How to Create a Pod in Kubernetes

To begin, you need to launch a Kubernetes cluster. Once you’re in the Kubernetes sandbox environment, make sure you’re connected to the Kubernetes cluster by executing kubectl get nodes in the command line to see the cluster's nodes in the terminal. If that worked, you’re ready to create and run a pod.

To create a pod using the nginx image, run the commandkubectl run nginx --image=nginx --restart=Never. This will create a pod named nginx, running with the nginx image on Docker Hub. And by setting the flag --restart=Neverwe tell Kubernetes to create a single pod rather than a Deployment.

Once you hit enter, the pod will be created. You should see pod/nginx createdappear in the terminal.

How to View a Pod

You can now run the commandkubectl get podsto see the status of your pod. To view the entire configuration of the pod, just run kubectl describe pod nginxin your terminal.

The terminal will now display the YAML for the pod, starting with the name nginx, its location, the Minikube node, start time and current status. You will also see in-depth information about the nginx container, including the container ID and where the image lives.

If you scroll all the way to the bottom of the terminal, you’ll see the events that have occurred in the pod. In the case of this tutorial, you’ll see that the pod was started, created, the nginx image was pulled successfully and been assigned to this node in Minikube.

Free Download: Kubernetes Best Practices Whitepaper

Destroy Pod

The action of deleting the pod is simple. To delete the pod you have created, just runkubectl delete pod nginx.  Be sure to confirm the name of the pod you want to delete before pressing Enter. If you have completed the task of deleting the pod successfully, pod nginx deleted will appear in the terminal.

How to Implement Pod Policy

One big area of concern for Kubernetes is a lack of visibility and consistent policy enforcement across multiple clusters and dev teams. As you begin your Kubernetes journey, you should consider Kubernetes guardrails - how will you get your team to use Kubernetes safely? Doing so early will ensure you do not introduce configuration drift where there are no establish internal standards for Kube configurations. As you play, check out some Kubernetes security considerations:

  1. Check Pod SecurityContext for readOnlyRootFilesystem

  2. Kubernetes How to: Ensure imagePullPolicy set to Always

  3. How to Identify Over Permissioned Containers

  4. How to Identify Missing Readiness Probes in Kubernetes

  5. Why Fixing Kubernetes Configuration Inconsistencies is Critical for Multi-tenant and Multi-cluster Environments

Kubernetes Best Practices for Security, Reliability and Resource Requests

Just as you should consider guardrails, you should also consider Kubernetes best practices. By learning best practices as you learn Kubernetes, you’ll be well positioned to fully evaluate the platform and scale it. 

Polaris, an open source project, runs a variety of Kubernetes best practice checks to ensure that pods and controllers are configured properly. Using this open source project, you can evaluate Kubernetes and avoid problems in the future. 

Another question that often arises when starting with Kubernetes is how to size your applications. Open source project, Goldilocks, lets you rightsize your applications and get it “just-right.”  It helps you identify a starting point for resource requests and limits. 

Conclusion

Pods are a vital unit for understanding the Kubernetes object model, as they represent the processes within an application. In most cases pods serve as an indirect way to manage containers within the Kubernetes. In more complex use cases, pods may encompass multiple containers that need to share resources, serving as the central location for container management.

For more Kubernetes best practices, get this guide. It walks you through many issues you’ll face and how you can configure Kubernetes to avoid mistakes.

The Guide to Kubernetes Cost Optimization: Why it's hard and how to do it well to embrace a FinOps model