Kubectl
Table of Contents
:ID: BC7E2058-A3F6-4149-9CA3-902B6BDE5D1F
See also https://kubernetes.io/docs/reference/kubectl/cheatsheet/
For some of these commands, use the -n flag to specify the namespace if the
namespace wasn’t already set with set-context
| Description | Command |
|---|---|
| Get namespaces | kubectl get ns |
| Set namespace | kubectl config set-context –current –namespace=foo |
| Show config | kubectl config view |
| Show current namespaces | kubectl config view (then grep namespace) |
| Show current context | kubectl config current-context |
| Set current context | kubectl config use-context foo |
| Remove context | kubectl config unset contexts.foo |
| Get all pods | kubectl get pods |
| Exec | kubectl exec -it mypod – bash |
| Get deployment info | kubectl get deployments foo -o yaml |
| Nuke everything in a namespace | kubectl delete all –all -n foospace |
| Pod Resource usage | kubectl top pod -n foospace |
| Cluster Resource usage | kubectl top node |
# Recipes
# forward a port
kubectl port-forward --namespace foospace $(kubectl get pods --namespace foospace --selector "app=mssql" --output=name) 1433:1433
This executes an inner command to get the pod name from a selector.
# Get container ID
Example: get the container ID for the web container
kubectl get po --template '{{range.items}}{{.metadata.name}}{{end}}' -n foospace --selector=component=web
# Copy remote file to local
kubectl cp deploymentname/$(kubectl get po --template '{{range.items}}{{.metadata.name}}{{end}}' -n foospace --selector=component=web):/path/to/remote/file /path/to/local