How to use kubectl?
· Category: Kubernetes
Short answer
kubectl is the official CLI for Kubernetes. It communicates with the cluster API server to create, inspect, update, and delete resources.
Steps
- Install
kubectland configure access withkubeconfig. - Use
kubectl getto list resources. - Use
kubectl describefor detailed information. - Use
kubectl applyto create or update resources. - Use
kubectl deleteto remove resources.
Example
kubectl get pods
kubectl get pods -o wide
kubectl describe pod mypod
kubectl logs mypod
kubectl exec -it mypod -- /bin/sh
kubectl apply -f deployment.yaml
kubectl delete -f deployment.yaml
Tips
- Use
-nto specify a Namespace. - Use
--dry-run=clientto preview changes. - Use
kubectl explainto see resource field documentation.
Common issues
kubectlversion skew can cause compatibility issues with the cluster.- Incorrect context or kubeconfig leads to connection errors.
- Resource names are case-sensitive.