kubectl: The Kubernetes Command-Line Tool
What you'll learn: How to use kubectl commands to inspect, create, update, and debug resources in your Kubernetes clusters.
What is kubectl?
kubectl is your primary interface to Kubernetes—think of it as the command center from which you pilot your containerized infrastructure. It communicates with the Kubernetes API server to execute operations on cluster resources like Pods, Services, ConfigMaps, and everything else you've learned so far.
Essential kubectl Commands
Inspecting Resources
Use kubectl get to list resources and kubectl describe for detailed information:
kubectl get pods
kubectl get services --namespace=production
kubectl describe pod my-app-pod
The kubectl logs command retrieves container output, essential for debugging:
kubectl logs my-app-pod
kubectl logs my-app-pod --previous # logs from crashed container
Creating and Updating Resources
Apply YAML manifests declaratively with kubectl apply:
kubectl apply -f deployment.yaml
kubectl apply -f ./configs/ # entire directory
For quick imperative creation:
kubectl create configmap app-config --from-file=config.json
kubectl expose deployment my-app --port=80 --type=LoadBalancer
Debugging and Troubleshooting
Execute commands inside running containers:
kubectl exec -it my-app-pod -- /bin/bash
View real-time resource usage:
kubectl top pods
kubectl top nodes
Delete resources when needed:
kubectl delete pod my-app-pod
kubectl delete -f deployment.yaml
Context Matters
Switch between clusters and namespaces efficiently:
kubectl config get-contexts
kubectl config use-context production
kubectl config set-context --current --namespace=staging
Key Takeaway: kubectl is your Swiss Army knife for Kubernetes—master get, describe, logs, apply, exec, and delete to effectively manage, inspect, and debug your containerized applications in production.