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

  1. Install kubectl and configure access with kubeconfig.
  2. Use kubectl get to list resources.
  3. Use kubectl describe for detailed information.
  4. Use kubectl apply to create or update resources.
  5. Use kubectl delete to 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 -n to specify a Namespace.
  • Use --dry-run=client to preview changes.
  • Use kubectl explain to see resource field documentation.

Common issues

  • kubectl version skew can cause compatibility issues with the cluster.
  • Incorrect context or kubeconfig leads to connection errors.
  • Resource names are case-sensitive.