Kubernetes 02 - Creating Pods

It’s essential to understand the various ways we can create and manage resources, particularly pods in Kubernetes. In this post, we’ll explore different methods to create pods using kubectl.

What is a Pod?

Before we dive into creation methods, let’s clarify what a Pod is:

  • A Pod is the smallest deployable unit in Kubernetes.
  • It’s like a wrapper around one or more containers.
  • Containers in a Pod share the same network namespace and can easily communicate with each other.

Three Ways to Create Pods

Kubernetes offers three primary methods to create Pods using the kubectl CLI:

  • kubectl run: Used for creating a single Pod per command
  • kubectl create: Allows creation of various resources via CLI or YAML
  • kubectl apply: Enables creation or update of resources using YAML

Let’s focus on the kubectl run command for this guide.

Creating a Pod with kubectl

Before we begin, let’s ensure our Kubernetes environment is ready:

kubectl version

This command should return version information for both the client and server if your setup is correct.

Now, let’s deploy a Pod running the nginx web server:

kubectl run my-nginx --image nginx

Let’s break down this command:

  • kubectl run: This is the command to create a Pod
  • my-nginx: This is the name we’re giving to our Pod
  • --image nginx: This specifies the container image to use (in this case, the official nginx image)

To verify the Pod’s creation, use:

kubectl get pods

This will show the status of the Pod, including its name, status, and age. You should see output similar to this:

NAME       READY   STATUS    RESTARTS   AGE
my-nginx   1/1     Running   0          41s

For a broader view of all Kubernetes objects:

kubectl get all

This command will display all resources in the default namespace:

NAME           READY   STATUS    RESTARTS   AGE
pod/my-nginx   1/1     Running   0          2m37s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   31h

The Role of Pods in Kubernetes

Unlike Docker, where you create containers directly, Kubernetes introduces an abstraction layer called Pods. Here are some key points about Pods:

  • Kubernetes creates Pods, not containers directly.
  • You create Pods (via CLI, YAML, or API), and Kubernetes manages the containers within them.
  • The kubelet component communicates with the container runtime to create containers inside Pods.
  • Every resource type that runs containers in Kubernetes uses Pods as its foundation.

Deployments: A More Robust Way to Manage Applications

While Pods are essential, Deployments offer a more robust way to manage applications. Let’s create a Deployment for our nginx web server:

kubectl create deployment my-nginx --image nginx

Here my-nginx is the name of the Deployment. This command sets up a hierarchical structure:

Let’s run kubectl get pods to see the pods created by the Deployment:

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx                    1/1     Running   0          3h20m
my-nginx-7fbf685c4d-lgvfc   1/1     Running   0          2m46s

Run kubectl get all to see all resources created by the Deployment:

NAME                            READY   STATUS    RESTARTS   AGE
pod/my-nginx                    1/1     Running   0          3h21m
pod/my-nginx-7fbf685c4d-lgvfc   1/1     Running   0          3m48s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   34h

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/my-nginx   1/1     1            1           3m48s

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/my-nginx-7fbf685c4d   1         1         1       3m48s

The deployment is a type of resource that manages Pods. The Deployment creates a ReplicaSet, which in turn creates Pods.

Cleaning Up

To delete the Deployment and associated resources, use:

kubectl delete pod my-nginx
kubectl delete deployment my-nginx

These commands will remove the Pod and Deployment we created earlier.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • Dependency Injection
  • CPU Cache
  • Understanding Linear Blended Skinning in 3D Animation
  • Starvation in Operating Systems
  • Virtual Memory