Kubernetes Pod YAML Explained
# YAML Configuration for a Kubernetes Pod apiVersion: v1 # Kubernetes API version kind: Pod # Type of Kubernetes resource metadata: name: myapp-pod # Name of the pod labels: app: myapp # Label for application identification type: front-end # Label for type classification spec: containers: - name: nginx-container # Name of the container image: nginx # Docker image to use ports: - containerPort: 80 # Port the container exposes name: http # Name for the port protocol: TCP # Protocol used env: - name: DBCON # Environment variable name value: connectionstring # Environment variable value command: ["/bin/sh", "-c"] # Command to run in the container args: ["echo ${DBCON}"] # Arguments for the command
YAML Components Explained
apiVersion
Specifies the Kubernetes API version. For Pods, this is typically "v1".
kind
Defines the type of resource. In this case, it's a "Pod".
metadata
Contains identifying information about the pod, including name and labels.
spec
The specification that describes the desired state of the pod.
containers
Defines the containers that will run inside the pod. A pod can contain multiple containers.
env
Sets environment variables for the container. In this example, DBCON is set to "connectionstring".
command and args
These override the default command and arguments of the container image. Similar to Docker's ENTRYPOINT and CMD.
Kubernetes Pod Commands Reference
| Command | Description | Action |
|---|---|---|
| kubectl create -f [file] | Create a pod from a YAML definition file | |
| kubectl run [podname] --image=[image] | Run a pod with a specific image | |
| kubectl get pods | List all running pods | |
| kubectl get pods -o wide | List pods with additional information | |
| kubectl describe pod [podname] | Show detailed information about a specific pod | |
| kubectl get pod [podname] -o yaml | Extract the pod definition in YAML format | |
| kubectl exec -it [podname] -- [command] | Execute a command inside a running pod | |
| kubectl delete -f [file] | Delete a pod using its definition file | |
| kubectl delete pod [podname] | Delete a pod by name |
Usage Tips
YAML First
Prefer kubectl create -f or apply -f for repeatable deployments instead of manual kubectl run.
Debug Pods
Use kubectl describe and kubectl logs to troubleshoot failing pods quickly.