kubectl command reference
kubectl is the CLI for interacting with Kubernetes clusters. This page covers the commands used most often in day-to-day operations. Commands are listed for reference and were not run here; check flags with kubectl <command> --help for your client version. For the concepts behind them, see Pods and Deployments, Services, Ingress and Gateway and Config, namespaces and Helm.
Context and Cluster
Section titled “Context and Cluster”# View current context (which cluster you're connected to)kubectl config current-context
# List all contextskubectl config get-contexts
# Switch contextkubectl config use-context my-cluster
# Set default namespace for current contextkubectl config set-context --current --namespace=my-namespaceNamespaces
Section titled “Namespaces”kubectl get namespaceskubectl create namespace stagingkubectl delete namespace staging
# All commands accept -n to target a namespacekubectl get pods -n stagingkubectl get pods --all-namespaces # or -Akubectl get podskubectl get pods -o wide # include node and IPkubectl describe pod my-podkubectl logs my-podkubectl logs my-pod -c my-container # specific container in multi-container podkubectl logs my-pod --previous # logs from last crashkubectl logs my-pod -f # follow (tail -f equivalent)kubectl exec -it my-pod -- /bin/shkubectl delete pod my-podkubectl get pods -w # watch for changesDeployments
Section titled “Deployments”kubectl get deploymentskubectl describe deployment my-appkubectl apply -f deployment.yamlkubectl delete deployment my-appkubectl scale deployment my-app --replicas=5kubectl set image deployment/my-app my-app=myrepo/my-app:2.0kubectl rollout status deployment/my-appkubectl rollout history deployment/my-appkubectl rollout undo deployment/my-appkubectl rollout undo deployment/my-app --to-revision=2kubectl rollout pause deployment/my-appkubectl rollout resume deployment/my-appServices and Ingress
Section titled “Services and Ingress”kubectl get serviceskubectl describe service my-servicekubectl get endpointslices -l kubernetes.io/service-name=my-servicekubectl get ingresskubectl describe ingress my-ingressConfigMaps and Secrets
Section titled “ConfigMaps and Secrets”kubectl get configmapskubectl describe configmap my-configkubectl get secretskubectl describe secret my-secret
# Decode a secret valuekubectl get secret my-secret -o jsonpath='{.data.password}' | base64 --decodeApplying and Deleting Resources
Section titled “Applying and Deleting Resources”# Apply a manifestkubectl apply -f manifest.yaml
# Apply everything in a directorykubectl apply -f ./k8s/
# Delete from manifestkubectl delete -f manifest.yaml
# Dry run (preview what would be applied)kubectl apply -f manifest.yaml --dry-run=client
# Generate YAML for a resourcekubectl get deployment my-app -o yamlDebugging
Section titled “Debugging”# Open a shell in a running podkubectl exec -it my-pod -- /bin/bash
# Copy files to/from a podkubectl cp my-pod:/etc/config/app.conf ./app.confkubectl cp ./local-file.txt my-pod:/tmp/
# Forward a local port to a pod or servicekubectl port-forward pod/my-pod 8080:8080kubectl port-forward service/my-service 8080:80
# Attach an ephemeral debug container to a running podkubectl debug -it my-pod --image=busybox:1.37 --target=my-container
# Get events (useful for diagnosing failures)kubectl get events --sort-by='.lastTimestamp'kubectl get events -n my-namespaceResource Usage
Section titled “Resource Usage”kubectl top nodeskubectl top podskubectl top pods --sort-by=memoryOutput Formats
Section titled “Output Formats”kubectl get pods -o jsonkubectl get pods -o yamlkubectl get pods -o widekubectl get pods -o jsonpath='{.items[*].metadata.name}'
# Custom columnskubectl get pods -o custom-columns='NAME:.metadata.name,STATUS:.status.phase'Labels and Selectors
Section titled “Labels and Selectors”# Filter by labelkubectl get pods -l app=my-appkubectl get pods -l 'env in (prod, staging)'
# Add a labelkubectl label pod my-pod version=v2
# Remove a labelkubectl label pod my-pod version-Quick Reference
Section titled “Quick Reference”| Action | Command |
|---|---|
| Apply manifest | kubectl apply -f file.yaml |
| Get all resources | kubectl get all |
| Shell into pod | kubectl exec -it pod -- sh |
| View logs | kubectl logs pod |
| Port forward | kubectl port-forward svc/svc 8080:80 |
| Rollback deployment | kubectl rollout undo deployment/app |
| Scale deployment | kubectl scale deployment app --replicas=3 |
Related pages
Section titled “Related pages”- Kubernetes introduction
- Pods and Deployments
- Services, Ingress and Gateway
- Config, namespaces and Helm