Pods in Kubernetes☸️ - CKA
Imperative & Declarative Approach

Enthusiastic about DevOps tools like Docker, Kubernetes, Maven, Nagios, Chef, and Ansible and currently learning and gaining experience by doing some hands-on projects on these tools. Also, started learning about AWS and GCP (Cloud Computing Platforms).
What is a Kubernetes Pod?
A Pod is the smallest and simplest unit in the Kubernetes object model. A Pod represents a single instance of a running process in your cluster. Although a Pod can run one or more containers, the most common scenario is for a Pod to run a single container.
Key characteristics of Pods:
Shared Networking: Containers inside a Pod share the same network namespace, including IP address and port space, allowing them to communicate with each other easily.
Shared Storage: Pods can also have storage resources shared between containers.
Ephemeral: Pods are designed to be temporary; they can be killed or restarted by the Kubernetes control plane when necessary (e.g., for scaling or updates).
Types of Pods
There are two main types of Pods in Kubernetes:
Single Container Pods: The most common use case where a Pod contains a single container running a specific application.
Multi-Container Pods: A less common scenario where multiple containers work together in the same Pod. These containers share the same environment and work closely to support the application. They can, for example, share files and communicate with each other using inter-process communication (IPC).
Creating a Pod (YAML Configuration)
To create a Pod in Kubernetes, we define it in a YAML file and then apply it to the cluster.
Here’s a basic example of a Pod definition with a single container.
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
env: demo
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
Explanation:
apiVersion: Specifies the API version to be used by Kubernetes.How to Check apiVersion?
You can usekubectl explain podsand there you can find which apiVersion is your client utility i.e., kubectl is using.kind: Defines the type of object (Podin this case).metadata: Contains information likenameandlabelsfor the Pod.spec: Describes the configuration of the containers inside the Pod.containers: Lists the containers within the Pod. In this case, we are using thenginximage.
Steps to Create the Pod:
Save the above configuration to a file, say
my-pod.yaml.Run the following command to create the Pod:
kubectl apply -f my-pod.yamlVerify the Pod is running:
kubectl get pods
Working with Pods (Commands & Examples)
Once a Pod is created, there are several commands you can use to interact with it.
List All Pods:
kubectl get podsGet Pod Details:
kubectl describe pod <pod-name>Delete a Pod:
kubectl delete pod <pod-name>Check Logs:
kubectl logs <pod-name>Exec into a Running Pod (Useful for debugging):
kubectl exec -it -- /bin/bash <pod-name>The approach of creating a YAML file and then apply if through
kubectlcommand is a declarative approach, let’s see how we can do with an imperative approach or can say direct command for deploying commands.
Imperative Approach
In Kubernetes, an imperative command is used to perform actions directly from the command line without needing to write a YAML manifest beforehand.
Command to Create the Pod with Nginx Image:
kubectl run nginx --image=nginx --restart=Never
Explanation:
kubectl run: Creates a new resource (in this case, a Pod).nginx: The name of the Pod.--image=nginx: Specifies the container image (nginx) to be used.--restart=Never: Ensures that the resource created is a Pod (since by default, thekubectl runcommand creates a deployment with automatic restart unless specified otherwise).
Verify the Pod is Running:
kubectl get pods
Now that we've created the nginx Pod, we'll generate the YAML configuration for it and modify the Pod's name before creating a new Pod.
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
This command saves the YAML configuration of the existing Pod to a file named pod.yaml. Here dry-run will not apply the changes just run in a demo form and -o is used to specify the language for pod configuration file such as YAML or JSON.
ls
You will find a new file pod.yaml.
Different Commands
- To edit the YAML file in object only without requirement running the apply command, see below command -
kubectl edit pod <pod-name>
- To fetch details of any component of kuberenetes
kubectl explain <component>
'
<component> can be
pod
service
rs or replicaSet
deploy
ds or daemonSet
etc.
'
kubectl create -f <file-name>--> only creates podkubectl apply -f <file-name>--> creates and updates also





