Pod in Kubernetes : Day 7 of 40daysofkubernetes

Pod in Kubernetes : Day 7 of 40daysofkubernetes

ยท

3 min read

Introduction

In my previous blog, we learned how to practice Kubernetes locally using Kind and created a cluster with 1 control plane and 2 worker nodes with the help of a config file. In this blog, we will discuss Pods, how to create Pods inside the cluster using two methods, and explore different commands related to Pods. Let's get started.

What is a Pod in Kubernetes?

In Kubernetes, a Pod is the basic unit of deployment. When you create a Deployment or a Job, you are actually creating Pods. Pods can contain multiple containers that share resources like network and storage. These containers within a Pod share the same network namespace (IP address) and can communicate with each other using localhost.

Pods have their own lifecycle, and Kubernetes manages the scheduling, scaling, and lifecycle of Pods. Pods can be created, replicated, and scheduled on nodes within the Kubernetes cluster.

Different ways of creating a Kubernetes object

  1. Imperative way -> Through commands or API calls

    Example: You can create an NGINX pod with the following command:

     kubectl run nginx-pod --image=nginx:latest
    

    You can verify this by running:

     kubectl get pods
    

    You can delete this pod with:

     kubectl delete pod <pod-name>
    

  2. Declarative way -> By creating manifest files. These can be YAML or JSON files, but YAML is more preferred.

    First, create a file named pod.yaml and paste the following content into it:

     # This yaml file will create a nginx pod
    
     apiVersion: v1
     kind: Pod 
     metadata:
       name: nginx-pod
       labels:
         env: test
         type: front-end
     spec:
       containers:
         - name: nginx
           image: nginx
           ports:
             - containerPort: 80
    

    Now, you can create a pod with:

     kubectl create -f pod.yaml
    

These are the two ways to create pods in a Kubernetes cluster.

Some Important commands

  1. kubectl describe pod <pod-name>: This command will show all the information about your pod.

  2. kubectl exec -it <pod-name> -- sh: With this command, you can enter the pod.

  3. kubectl get pods -o wide: This command will give a brief overview of the pod.

  4. kubectl run <image-name> --image=<image> --dry-run=client: This command will not create a pod; it only shows what would happen if you run this command.

  5. From the above command, you can get the complete YAML for this pod creation command:

     kubectl run nginx --image=nginx --dry-run=client -o yaml
    

  6. You can save the YAML content to a file and create a pod with that YAML file:

     kubectl run nginx --image=nginx --dry-run=client -o yaml > new_pod.yaml
     kubectl create -f new_pod.yaml
    

    These are some important commands. In this #40DaysOfKubernetes series, we will learn more commands as well.

Resources I used-

ย