How to Run a Pod in Kubernetes: Simple Steps
To run a pod in Kubernetes, create a pod definition YAML file with the container details and use
kubectl apply -f filename.yaml to start it. You can check the pod status with kubectl get pods.Syntax
A Kubernetes pod is defined using a YAML file that specifies the apiVersion, kind, metadata, and spec. The spec section includes the container image and other settings.
Use the command kubectl apply -f <filename>.yaml to create the pod from the YAML file.
yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:1.23.3
Example
This example creates a pod named my-pod running an nginx container. After applying the YAML, you can verify the pod is running.
yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: nginx-container
image: nginx:1.23.3
Output
$ kubectl apply -f pod.yaml
pod/my-pod created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-pod 1/1 Running 0 10s
Common Pitfalls
- Forgetting to specify
apiVersionorkindcauses errors. - Using an incorrect container image name or tag will prevent the pod from starting.
- Not waiting for the pod to reach
Runningstatus before accessing it. - Applying YAML with syntax errors will fail silently or show errors.
yaml
apiVersion: v1
kind: Pod
metadata:
name: bad-pod
spec:
containers:
- name: bad-container
image: nginx:wrongtag
# Corrected version:
apiVersion: v1
kind: Pod
metadata:
name: good-pod
spec:
containers:
- name: good-container
image: nginx:1.23.3
Quick Reference
kubectl apply -f pod.yaml: Create or update podkubectl get pods: List pods and statuskubectl describe pod <pod-name>: Detailed pod infokubectl delete pod <pod-name>: Remove pod
Key Takeaways
Create a pod by writing a YAML file with apiVersion, kind, metadata, and spec.
Use kubectl apply -f to run the pod from the YAML file.
Check pod status with kubectl get pods to ensure it is running.
Use correct container image names and tags to avoid startup failures.
Fix YAML syntax errors to prevent pod creation issues.