Kubernetes (k8s) is the industry-standard container orchestration engine for deploying, scaling, and managing containerized applications across cloud clusters. Managing production Kubernetes environments requires fluency with kubectl CLI commands for inspecting cluster state, deploying manifests, streaming Pod logs, executing remote debugging shells, and managing secrets.
Key Takeaways
- Use `kubectl get pods`, `kubectl describe pod`, `kubectl logs`, and `kubectl exec` for 90% of daily debugging operations.
- Use `kubectl apply -f manifest.yaml` (instead of `create`) for declarative, idempotent resource deployments.
- Perform fast rollbacks using `kubectl rollout undo deployment/<name>` when new releases break in production.
- Inspect the `Events` section at the bottom of `kubectl describe pod` output first when troubleshooting crashing pods.
- Always configure container CPU and memory `requests` and `limits` to prevent Out-Of-Memory (OOM) node evictions.
How do you inspect cluster information and manage Kubernetes namespaces?
Kubernetes namespaces isolate cluster resources, allowing multi-tenant teams to manage staging, production, and microservice workloads independently.
Cluster Inspection Commands
# Check client and server Kubernetes versions
kubectl version --short
# Display cluster master endpoint and core service URLs
kubectl cluster-info
# List all worker nodes in the cluster
kubectl get nodes
# List nodes with internal/external IP addresses and OS details
kubectl get nodes -o wide
# Describe detailed node specs, conditions, and allocated resources
kubectl describe node node-01
# Monitor real-time node CPU and Memory consumption (requires metrics-server)
kubectl top nodesNamespace Management
# List all active namespaces
kubectl get namespaces
kubectl get ns # Short form
# Create a new namespace
kubectl create namespace staging
# Delete a namespace and all contained resources
kubectl delete namespace staging
# List pods within a specific namespace
kubectl get pods -n staging
# Change default namespace context for subsequent commands
kubectl config set-context --current --namespace=stagingHow do you inspect, log, and execute commands inside Kubernetes Pods?
Pods are the smallest deployable units in Kubernetes, encapsulating one or more co-located containers sharing network namespaces and storage volumes.
Pod Management Commands
# List all pods in the current active namespace
kubectl get pods
# List pods across ALL cluster namespaces
kubectl get pods -A
# List pods with node placement and IP addresses
kubectl get pods -o wide
# Stream real-time pod status transitions
kubectl get pods --watch
# Inspect detailed pod configuration, status, and event history
kubectl describe pod web-app-7d8b9c4f-x2z9p
# Delete a pod (triggers deployment controller to recreate it)
kubectl delete pod web-app-7d8b9c4f-x2z9p
# Force delete a stuck pod (skips graceful termination sequence)
kubectl delete pod web-app-7d8b9c4f-x2z9p --grace-period=0 --forceImperative Pod Creation & Debugging
# Run a temporary standalone Nginx pod
kubectl run nginx --image=nginx:alpine
# Stream pod log output
kubectl logs web-app-7d8b9c4f-x2z9p
# Stream logs from a specific container in a multi-container pod
kubectl logs web-app-7d8b9c4f-x2z9p -c app-container
# Inspect logs from a previously crashed container instance
kubectl logs web-app-7d8b9c4f-x2z9p --previous
# Follow / tail live pod logs
kubectl logs -f web-app-7d8b9c4f-x2z9p
# Execute an interactive shell inside a running pod
kubectl exec -it web-app-7d8b9c4f-x2z9p -- /bin/bash
kubectl exec -it web-app-7d8b9c4f-x2z9p -c app-container -- /bin/sh
# Copy files between local machine and container
kubectl cp web-app-7d8b9c4f-x2z9p:/var/log/app.log ./local-app.log
kubectl cp ./local-file.txt web-app-7d8b9c4f-x2z9p:/tmp/remote-file.txtHow do you deploy, scale, and manage application Deployments and Services?
Deployments provide declarative updates for Pods and ReplicaSets, enabling zero-downtime rolling updates and instant rollbacks.
Deployment Management Commands
# List all deployments in the current namespace
kubectl get deployments
kubectl get deploy # Short form
# Inspect deployment details and rollout strategy
kubectl describe deploy web-app
# Create an imperative deployment
kubectl create deployment nginx --image=nginx:latest --replicas=3
# Apply declarative manifest files (preferred production approach)
kubectl apply -f deployment.yaml
# Scale deployment replica count up or down
kubectl scale deploy web-app --replicas=5
# Update container image with rolling update
kubectl set image deploy/web-app app-container=nginx:1.26-alpine
# Monitor live rollout status
kubectl rollout status deploy/web-app
# View rollout revision history
kubectl rollout history deploy/web-app
# Roll back to the previous deployment revision
kubectl rollout undo deploy/web-app
# Roll back to a specific revision number
kubectl rollout undo deploy/web-app --to-revision=2
# Trigger a zero-downtime restart of all deployment pods
kubectl rollout restart deploy/web-appService Networking & Exposure
# List all active services
kubectl get services
kubectl get svc # Short form
# Expose a deployment as an internal ClusterIP service
kubectl expose deploy web-app --port=80 --target-port=8080 --type=ClusterIP
# Expose a deployment externally via NodePort
kubectl expose deploy web-app --port=80 --type=NodePort
# Expose a deployment externally via cloud LoadBalancer
kubectl expose deploy web-app --port=80 --type=LoadBalancer
# Forward local port 8080 directly to a cluster service or pod
kubectl port-forward svc/web-app-service 8080:80
kubectl port-forward pod/web-app-7d8b9c4f-x2z9p 8080:8080How do you manage ConfigMaps, Secrets, Node Resources, and Troubleshooting?
ConfigMaps store non-confidential environment key-value pairs, while Secrets hold base64-encoded credentials, keys, and tokens.
ConfigMaps & Secrets Commands
# Create ConfigMap from literal values
kubectl create configmap app-config --from-literal=ENV=production --from-literal=LOG_LEVEL=info
# Create ConfigMap from environment file
kubectl create configmap app-config --from-file=config.env
# Inspect ConfigMap details
kubectl get configmaps
kubectl describe configmap app-config
# Create Secret from literal credentials
kubectl create secret generic db-credentials --from-literal=username=admin --from-literal=password=secret123
# Inspect Secret details (keys are base64 encoded)
kubectl get secrets
kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 --decodeResource Usage & Node Troubleshooting
# Inspect Pod CPU and Memory consumption
kubectl top pods
# Inspect Pod CPU and Memory across all namespaces sorted by memory
kubectl top pods -A --sort-by=memory
# View node capacity and allocatable resource limits
kubectl describe nodes | grep -A 8 Allocatable
# Attach ephemeral debug container to a running pod (Kubectl 1.30+ feature)
kubectl debug -it pod/web-app-7d8b9c4f-x2z9p --image=busybox --target=app-containerFrequently Asked Questions
What is the difference between kubectl apply and kubectl create?
kubectl create is imperative and fails if the resource already exists. kubectl apply is declarative and idempotent; it creates the resource if missing or updates the existing resource to match the desired state in your YAML manifest.
How do I troubleshoot a Pod stuck in CrashLoopBackOff status?
First run kubectl describe pod <pod-name> and scroll to the bottom Events section to check for OOMKilled errors or volume mount failures. Next, view the crashed container logs using kubectl logs <pod-name> --previous.
How do I switch between different Kubernetes clusters and contexts using kubectl?
List available contexts with kubectl config get-contexts and switch active clusters using kubectl config use-context <context-name>.
What to Read Next
- Docker Cheat Sheet: Container Management — Container images and Dockerfile optimization by VD.
- Nginx Cheat Sheet: Reverse Proxy & SSL — Ingress controller proxy configurations.
- Linux Bash Cheat Sheet: Commands & Scripting — Shell automation and terminal tools.



