In today's session, I delved into one of the most fundamental concepts in Kubernetes: Pods. Kubernetes Pods are the smallest and simplest unit in the Kubernetes object model. They represent a single instance of a running process in your cluster, and understanding them is crucial for mastering container orchestration.
Here’s a detailed breakdown of what I learned on Day 33:
✅1. What is a Pod?
A Pod is a collection of containers that run together on a host. Each pod has its own IP address and can share resources like storage and network. Pods are the basic execution units of Kubernetes and can contain one or more containers.
Components of a Pod:
Container(s): The actual application or process running inside the pod.
Pod IP: Each pod is assigned a unique IP address within the cluster, allowing easy communication between different pods.
Shared Storage: Pods can share storage volumes, allowing containers to share data within a pod.
✅2. Pod Lifecycle
The lifecycle of a pod is simple and can be broken down into three stages:
Create: A pod is created, and the container inside starts.
Run: The pod continues to run as long as its containers are active.
Terminate: When the pod’s task is complete, or if there is an error, the pod terminates.
✅3. Pods Usage Scenarios
Pods can be used in two key ways:
Microservices: Running lightweight, independent services.
Stateful Applications: Running applications that need to store and persist data.
✅4. Kubernetes Pod Commands
Basic Commands:
Check the status of the nodes:
kubectl get nodes
Creating a Pod Using YAML:
I created a simple YAML file (pod1.yaml
) that defines a pod with a single container.
pod1.yaml:
apiVersion: v1 kind: Pod metadata: name: pod1 labels: app: nginx-app spec: containers: - name: nginx-container image: nginx:1.23
Commands to manage the pod:
Apply the YAML file:
kubectl apply -f pod1.yaml
Check the pod status:
kubectl get pods
Describe the pod:
kubectl describe pods pod1
Get more details of the pod:
kubectl get pods -o wide
View pods with labels:
kubectl get pods -o wide --show-labels
Access the pod via curl:
curl <pod_ip>
Delete the pod:
kubectl delete pods pod1 kubectl delete -f pod1.yaml
✅5. Adding Metadata and Environment Variables
I created a new pod (pod2.yaml
) that includes metadata and environment variables.
pod2.yaml:
apiVersion: v1 kind: Pod metadata: name: pod2 spec: containers: - name: ubuntu-container image: ubuntu:latest env: - name: MYNAME value: "Kedar" - name: CITY value: "Pune" command: [ "sleep", "3600" ]
Commands:
Create the pod:
kubectl apply -f pod2.yaml
Access the pod's shell:
kubectl exec -it pod2 -- bash
Print the environment variables:
printenv | egrep -i 'city|myname'
Exit the pod:
exit
✅6. Creating a Pod with Two Containers
For the third task, I created a pod that runs two containers. One container holds the application logic, while the other container manages a separate function.
Example YAML for Pod with Two Containers:
apiVersion: v1 kind: Pod metadata: name: pod3 spec: containers: - name: app-container image: nginx:1.23 - name: sidecar-container image: busybox command: ["/bin/sh", "-c", "echo Hello from the sidecar container > /etc/etc.txt"]
Commands:
Apply the pod definition:
kubectl apply -f pod3.yaml
Verify the file created in the sidecar container:
kubectl exec -it pod3 -- cat /etc/etc.txt
✅7. Cleanup
After completing all tasks, I cleaned up the pods and resources to avoid any conflicts or unnecessary usage.
Delete all pods:
kubectl delete -f pod2.yaml kubectl delete -f pod3.yaml
🚀Thanks for joining me on Day 33! Let’s keep learning and growing together!
Happy Learning! 😊
#90DaysOfDevOps
#Kubernetes #K8s #DevOps #Containerization #Microservices #CloudNative #Pods #ContainerManagement #Docker #InfrastructureAsCode #KubernetesTutorial #CloudComputing #LearningK8s #TechCommunity #Programming