This command-focused lab teaches a repeatable troubleshooting loop: locate a resource, inspect desired and current state, read events and logs, enter a container only when necessary, change declaratively, and delete by manifest.
1. Prepare and execute
Confirm the active kubeconfig context and namespace before applying changes. Save commands and manifests in version control without credentials.
kubectl create namespace kubectl-lab
kubectl -n kubectl-lab create deployment web --image=nginx:1.28-alpine --replicas=2
kubectl -n kubectl-lab expose deployment web --port=80
kubectl -n kubectl-lab get all -o wide
kubectl -n kubectl-lab get pods --show-labels
2. Declarative configuration
Save the following example in a clearly named YAML file, review it, then use server-side dry-run when the API is available.
# Prefer saved YAML and kubectl apply for lasting changes.
# Imperative create commands are useful for small labs and diagnostics.
3. Verify the result
A successful command is not enough. Inspect resource state, conditions, events, endpoints, logs, and an end-to-end request where applicable.
kubectl -n kubectl-lab describe deployment web
kubectl -n kubectl-lab logs deployment/web --tail=50
kubectl -n kubectl-lab exec deployment/web -- nginx -t
kubectl -n kubectl-lab rollout status deployment/web
kubectl -n kubectl-lab get events --sort-by=.lastTimestamp
kubectl -n kubectl-lab port-forward service/web 8080:80
4. Troubleshooting
Use –namespace explicitly, confirm the current context before mutations, and prefer describe plus events before guessing. For multi-container Pods, add -c CONTAINER to logs and exec.
5. Rollback and cleanup
Review the exact target before deleting resources, especially namespaces, claims, Secrets, and cluster-wide add-ons.
kubectl delete namespace kubectl-lab
kubectl config current-context
Production checklist
- The active context, namespace, and target version were verified.
- Manifests passed client or server-side validation.
- Resource conditions and recent events show no unresolved error.
- Access, network exposure, resource limits, persistence, and rollback were reviewed.
- Commands and expected output were recorded for the operating team.
References: topic documentation and Kubernetes documentation.