kubectl is the primary command-line tool to interact with your Kubernetes cluster via the API server.
It lets you create, inspect, modify, and delete Kubernetes resources.
kubectl uses a config file called **kubeconfig** to:
- Authenticate to the API server
- Select the current context (cluster + user + namespace)
- Store certificates, tokens, and endpoint info
~/.kube/configYou can override it using:
kubectl --kubeconfig /path/to/other/config ...Or set an environment variable:
export KUBECONFIG=/path/to/myconfigmkdir -p ~/.kube
sudo cp -i /etc/kubernetes/admin.conf ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/configkubectl cluster-info
kubectl get nodes
kubectl get componentstatuses # Deprecated but used in older clusterskubectl get pods
kubectl get pods -A # All namespaces
kubectl get pods -n <namespace>
kubectl describe pod <pod-name>
kubectl get deployments
kubectl describe deployment <name>
kubectl get svc
kubectl describe svc <name>kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl run busybox --image=busybox -it --rm -- /bin/shkubectl edit deployment nginx # Opens editor to live-edit
kubectl scale deployment nginx --replicas=5
kubectl rollout restart deployment nginx
kubectl rollout status deployment nginx
kubectl set image deployment/nginx nginx=nginx:1.21kubectl delete pod <name>
kubectl delete deployment <name>
kubectl delete svc <name>
kubectl delete -f <file>.yamlkubectl apply -f deployment.yaml
kubectl get -f deployment.yaml
kubectl delete -f deployment.yamlkubectl get pods -o wide
kubectl get pod <name> -o yaml
kubectl get pod <name> -o json
kubectl get pod <name> -o jsonpath='{.status.podIP}'
kubectl get pods -w # Watch for changeskubectl exec -it <pod> -- /bin/sh
kubectl exec -it <pod> -c <container> -- /bin/bash
kubectl logs <pod>
kubectl logs <pod> -c <container>
kubectl logs -f <pod> # Tail logskubectl get namespaces
kubectl get pods -n <namespace>
kubectl create namespace dev
kubectl delete namespace dev
kubectl config set-context --current --namespace=devkubectl port-forward svc/nginx 8080:80
kubectl port-forward pod/<pod-name> 9090:8080
kubectl proxykubectl config get-contexts
kubectl config use-context <name>
kubectl config view
kubectl config current-context
kubectl config set-context --current --namespace=devkubectl apply -f deployment.yaml --dry-run=client
kubectl apply -f deployment.yaml --dry-run=server
kubectl explain pod
kubectl explain deployment.spec.template.spec.containerskubectl --token=<jwt> --server=https://<api-server> ...
kubectl --kubeconfig=/path/to/kubeconfig get nodes| Task | Command |
|---|---|
| Get cluster info | kubectl cluster-info |
| List pods | kubectl get pods |
| Create deployment | kubectl create deployment |
| Apply config | kubectl apply -f file.yaml |
| Exec into pod | kubectl exec -it <pod> -- /bin/sh |
| View logs | kubectl logs <pod> |
| Port forward | kubectl port-forward <pod> 8080:80 |
| Change namespace | kubectl config set-context --current --namespace=dev |
Kubernetes allows you to merge multiple kubeconfig files into a single view by using the KUBECONFIG environment variable.
$HOME/.kube/config→ default config/tmp/dev-kubeconfig/tmp/prod-kubeconfig
KUBECONFIG=$HOME/.kube/config:/tmp/dev-kubeconfig:/tmp/prod-kubeconfig kubectl config view --flatten > /tmp/merged-configThen move or set it as your default config:
mv /tmp/merged-config ~/.kube/config| Step | Description |
|---|---|
KUBECONFIG=a:b:c |
Specifies multiple config files |
kubectl config view |
Shows merged config |
--flatten |
Combines contexts/clusters/users cleanly |
> newfile |
Outputs to a merged file |
Add to .bashrc or .zshrc:
export KUBECONFIG=$HOME/.kube/config:/tmp/dev-kubeconfig:/tmp/prod-kubeconfigkubectl config get-contexts
kubectl config use-context <context-name>Always run --flatten when merging — it inlines all references, so it becomes self-contained and safe to share or move.
✅ Pro Tip: Keep your main
~/.kube/configclean by merging only when needed, or use tools likekubectxto manage multiple contexts easily.