How to Create a Pod in a Kubernetes Cluster
Introduction
Kubernetes is a powerful platform for managing containerized applications. At the heart of Kubernetes is the Pod — the smallest and simplest unit you can deploy. A pod can run one or more containers, and those containers share the same resources, like network and storage. In most cases, a pod runs just a single container. Pods make it easier to manage and run applications by grouping containers that need to work closely together.
In this blog, we’ll show you how to create a basic pod in a Kubernetes cluster using a YAML file and the kubectl
command-line tool.
PreRequisites
Ensure the following before proceeding:
- A running Kubernetes cluster (via Minikube, KOPS, or a cloud provider like GKE, EKS, or AKS)
kubectl
CLI tool installed and configured- Basic knowledge of YAML and terminal commands
Steps to Create a Pod in Kubernetes
Step 1: Create a YAML File
Create a file called apache-pod.yaml
and paste the following:
apiVersion: v1 kind: Pod metadata: name: apache-pod spec: containers: - name: apache-container image: httpd:latest ports: - containerPort: 80
This configuration defines a pod name apache-pod
that runs an Apache HTTP server on port 80.
Step 2: Deploy the Pod
Run the following command to create the pod:
kubectl apply -f apache-pod.yaml
Expected output:
pod/apache-pod created
Step 3: Verify Pod Status
Check the pod’s current status:
kubectl get pods
Sample output:
NAME READY STATUS RESTARTS AGE
apache-pod 1/1 Running 0 30s
Step 4: Access the Pod
To access the container shell:
kubectl exec -it apache-pod -- bash
You can now interact with the container, check logs, or test services inside.
Step 5: Delete the Pod
Once you’re done testing, you can clean up with below commands
kubectl delete pod apache-pod
Conclusion:
Creating a Pod in Kubernetes is your first step toward mastering container orchestration. With just a YAML file and a few kubectl commands, you can deploy containers into your cluster.
While individual Pods are great for testing, for real-world production use, it’s better to use higher-level objects like Deployments and ReplicaSets that offer resilience and scaling.