When working with Kubernetes, kubectl is your essential command-line tool. It helps you deploy applications, inspect cluster resources, debug issues, and more.
What is kubectl?
kubectl is the CLI tool for interacting with Kubernetes clusters. It allows you to deploy applications, manage resources, and view logs efficiently.
Basic Structure
kubectl <command> <type> <name> [flags]
-
command: The operation to perform (get, describe, create, delete, apply, etc.) -
type: Resource type (pods, deployments, services, etc.) -
name: Resource name (optional)
Common Resource Types
- pods
- deployments
- services
- nodes
- configmaps
- secrets
- namespaces
- ingress
Essential kubectl Commands & Use Cases
1. Check Cluster Info
kubectl cluster-info
Use Case: Verify cluster connection and endpoints.
2. List All Resources
kubectl get all
Use Case: Get a quick overview of all resources in the current namespace.
3. Get Specific Resources
kubectl get pods
kubectl get deployments
kubectl get services
Use Case: Monitor resource status.
4. View Detailed Resource Info
kubectl describe pod <pod-name>
Use Case: Debug failed pods and view event logs.
5. Apply Configuration (YAML)
kubectl apply -f <file>.yaml
Use Case: Deploy apps or update configurations using manifest files.
6. Delete Resources
kubectl delete pod <pod-name>
kubectl delete -f <file>.yaml
Use Case: Remove unwanted or failed resources.
7. Logs and Debugging
kubectl logs <pod-name>
kubectl logs <pod-name> -c <container-name>
Use Case: Debug container issues.
8. Execute Commands in a Pod
kubectl exec -it <pod-name> -- /bin/bash
Use Case: Access a container shell for troubleshooting.
9. Port Forwarding
kubectl port-forward svc/my-service 8080:80
Use Case: Access internal services locally.
10. Scaling Deployments
kubectl scale deployment <deployment-name> --replicas=3
Use Case: Adjust the number of replicas based on load.
11. View Events
kubectl get events
Use Case: Monitor recent cluster activities.
12. Switch Context / Namespace
kubectl config use-context <context-name>
kubectl config set-context --current --namespace=<namespace>
Use Case: Manage multiple clusters or focus on a specific namespace.
Bonus Tips
Shortcuts:
-
po= pods -
svc= services -
dep= deployments -
ns= namespaces
Example:
kubectl get po
kubectl get svc
Watch Mode:
kubectl get pods -w
Use Case: Continuously monitor status changes in real-time.
kubectl is a powerful tool that gives you control over your Kubernetes cluster. Mastering it will make managing containerized applications much more efficient.
HappyReading!
Top comments (0)