Official Kubernetes Cheatsheet - https://kubernetes.io/docs/reference/kubectl/cheatsheet/
Kubectl is a command line interface for running commands against Kubernetes clusters.
The kubectl version has to be within one minor version difference of the Kubernetes cluster. For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master.
Kubectl can be installed on Ubuntu, Debian, CentOS, RedHat operating systems.
sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl
cat <<EOF /etc/yum.repos.d/kubernetes.repo
name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl
For further information about kubectl installation method, please refer to the Kubernetes documentation.
Installing bash completion on macOS using homebrew If running Bash 3.2 included with macOS
brew install bash-completion
or, if running Bash 4.1+
brew install bash-completion@2
If kubectl is installed via homebrew, this should start working immediately.
If you've installed via other means, you may need add the completion to your completion directory.
kubectl completion bash $(brew --prefix)/etc/bash_completion.d/kubectl
Installing bash completion on Linux:
Load the kubectl completion code for bash into the current shell
source <(kubectl completion bash)
Write bash completion code to a file and source if from .bash_profile
kubectl completion bash ~/.kube/completion.bash.inc
printf "
Kubectl shell completion
source '$HOME/.kube/completion.bash.inc'
" > $HOME/.bash_profile
source $HOME/.bash_profile
Load the kubectl completion code for zsh[1] into the current shell: source <(kubectl completion zsh) Set the kubectl completion code for zsh[1] to autoload on startup
kubectl completion zsh "${fpath[1]}/_kubectl"
Kubectl is a powerful tool to manage each object on a Kubernetes cluster.
kubectl [command] [TYPE] [NAME] [flags]
command : specifies the operation that you want to perform on one or more resources (create, get, describe, delete).
type : specifies the resource type. Resource types are case-insensitive and you can specify the singular, plural, or abbreviated forms.
name : specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed.
flags : specifies optional flags.
Get the syntax and fields details of any kind of objects. (replicaset, deployment, service, pod, etc.)
kubectl explain <object>
Create a resource from a file or from stdin.
kubectl create -f ./pod.json
cat pod.json | kubectl create -f -
Edit the data in docker-registry.yaml in JSON using the v1 API format then create the resource using the edited data.
kubectl create -f docker-registry.yaml --edit --output-version=v1 -o json
kubeclt create -f <folder_name
Delete resources by filenames, stdin, resources and names, or by resources and label selector.
kubectl delete -f ./pod.json
cat pod.json | kubectl delete -f -
kubectl delete pod,service baz foo
kubectl delete pods,services -l name=myLabel
kubectl delete pod foo --now
kubectl delete pod foo --grace-period=0 --force
kubectl delete pods --all
kubectl delete -f <folder_name
Edit a resource from the default editor.
kubectl edit svc/docker-registry
KUBE_EDITOR="nano" kubectl edit svc/docker-registry
kubectl edit job.v1.batch/<myjob> -o json
kubectl edit deployment/mydeployment -o yaml --save-config
Expose a resource as a new Kubernetes service.
Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000.
kubectl expose rc nginx --port=80 --target-port=8000
Create a service for a replication controller identified by type and name specified in "nginx-controller.yaml", which serves on port 80 and connects to the containers on port 8000.
kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000
kubectl expose pod <pod-name> --port=<port> --name=<svc-name>
Create a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https"
kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https
Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'.
kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
Create a service for a replicated nginx using replica set, which serves on port 80 and connects to the containers on port 8000.
kubectl expose rs nginx --port=80 --target-port=8000
Create a service for an nginx deployment, which serves on port 80 and connects to the containers on port 8000.
kubectl expose deployment nginx --port=80 --target-port=8000
kubectl get pod <pod_name-o yaml|grep selfLink
kubectl get --raw <selfLinkport/proxy/<filename
Display one or many resources.
kubectl get pods
kubectl get pods -o wide
kubectl get replicationcontroller web
kubectl get pod <pod-name> -o json
kubectl get -f pod.yaml -o json
kubectl get -o template pod/<pod-name--template=
kubectl get rc,services
kubectl get rc/web service/frontend pods/<pod-name
kubectl get all
kubectl config view
Configure application resources.
kubectl set env deployment/registry STORAGE_DIR=/local
kubectl set env deployment/sample-build --list
kubectl set env pods --all --list
kubectl set env deployment/sample-build STORAGE_DIR=/data -o yaml
kubectl set env rc --all ENV=prod
kubectl set env --from=secret/mysecret deployment/myapp
kubectl set env --from=configmap/myconfigmap --prefix=MYSQL_ deployment/myapp
kubectl set env deployments --all --containers="c1" ENV-
Remove the environment variable ENV from a deployment definition on disk and update the deployment config on the server
kubectl set env -f deploy.json ENV-
env | grep RAILS_ | kubectl set env -e - deployment/registry
Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'.
kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1
kubectl set image deployments,rc nginx=nginx:1.9.1 --all
kubectl set image daemonset abc *=nginx:1.9.1
Print result (in yaml format) of updating nginx container image from local file, without hitting the server
kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml
kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi
kubectl set resources deployment nginx --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi
kubectl set resources deployment nginx --limits=cpu=0,memory=0 --requests=cpu=0,memory=0
Print the result (in yaml format) of updating nginx container limits from a local, without hitting the server
kubectl set resources -f path/to/file.yaml --limits=cpu=200m,memory=512Mi --local -o yaml
kubectl set serviceaccount deployment nginx-deployment serviceaccount1
Print the result (in yaml format) of updated nginx deployment with serviceaccount from local file, without hitting apiserver
kubectl set sa -f nginx-deployment.yaml serviceaccount1 --local --dry-run -o yaml
Create and run a particular image, possibly replicated.
kubectl run nginx --image=nginx
kubectl run hazelcast --image=hazelcast --port=5701
Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container.
kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the container.
kubectl run hazelcast --image=nginx --labels="app=hazelcast,env=prod"
kubectl run nginx --image=nginx --replicas=5
kubectl run nginx --image=nginx --dry-run
Start a single instance of nginx, but overload the spec of the deployment with a partial set of values parsed from JSON.
kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
kubectl run -i -t busybox --image=busybox --restart=Never
Start the nginx container using the default command, but use custom arguments (arg1 .. argN) for that command.
kubectl run nginx --image=nginx -- <arg1<arg2... <argN
kubectl run nginx --image=nginx --command -- <cmd> <arg1>... <argN>
kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
Manage the rollout of a resource.
kubectl rollout undo deployment/abc
kubectl rollout status daemonset/foo
kubectl rollout history deployment/abc
kubectl rollout history daemonset/abc --revision=3
Mark the nginx deployment as paused. Any current state of the deployment will continue its function, new updates to the deployment will not have an effect as long as the deployment is paused.
kubectl rollout pause deployment/nginx
kubectl rollout resume deployment/nginx
kubectl rollout status deployment/nginx
kubectl rollout undo deployment/abc
kubectl rollout undo daemonset/abc --to-revision=3
kubectl rollout undo --dry-run=true deployment/abc
Creates an autoscaler that automatically chooses and sets the number of pods that run in a kubernetes cluste
Auto scale a deployment "foo", with the number of pods between 2 and 10, no target CPU utilization specified so a default autoscaling policy will be used:
kubectl autoscale deployment foo --min=2 --max=10
Auto scale a replication controller "foo", with the number of pods between 1 and 5, target CPU utilization at 80%:
kubectl autoscale rc foo --max=5 --cpu-percent=80
Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet.
kubectl scale --replicas=3 rs/foo
kubectl scale --replicas=3 -f foo.yaml
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql
kubectl scale --replicas=5 rc/foo rc/bar rc/baz
kubectl scale --replicas=3 statefulset/web
Cluster-info
Note: To further debug and diagnose cluster problems, use ‘kubectl cluster-info dump’.
kubectl cluster-info
kubectl cordon foo
kubectl uncordon foo
Drain node in preparation for maintenance.
Drain node "foo", even if there are pods not managed by a ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet on it.
kubectl drain foo --force
As above, but abort if there are pods not managed by a ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet, and use a grace period of 15 minutes.
kubectl drain foo --grace-period=90
kubectl drain <node_name--ignore-daemonsets
Update the taints on one or more nodes.
Update node 'foo' with a taint with key 'dedicated' and value 'special-user' and effect 'NoSchedule'. If a taint with that key and effect already exists, its value is replaced as specified.
kubectl taint nodes foo dedicated=special-user:NoSchedule
kubectl taint nodes foo dedicated:NoSchedule-
kubectl taint nodes foo dedicated-
kubectl taint node -l myLabel=X dedicated=foo:PreferNoSchedule
Display Resource (CPU/Memory/Storage) usage.
kubectl top node
kubectl top node NODE_NAME
kubectl top pod
kubectl top pod --namespace=NAMESPACE
kubectl top pod POD_NAME --containers
kubectl top pod -l name=myLabel
Show details of a specific resource or group of resources.
kubectl describe nodes kubernetes-node-emt8.c.myproject.internal
kubectl describe pods/<pod-name
kubectl describe -f pod.json
kubectl describe pods
kubectl describe po -l name=myLabel
Describe all pods managed by the 'frontend' replication controller (rc-created pods get the name of the rc as a prefix in the pod the name).
kubectl describe pods frontend
Execute a command in a container.
kubectl exec 123456-7890 date
kubectl exec 123456-7890 -c ruby-container date
Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-7890 and sends stdout/stderr from 'bash' back to the client
kubectl exec 123456-7890 -c ruby-container -i -t -- bash -il
List contents of /usr from the first container of pod 123456-7890 and sort by modification time. If the command you want to execute in the pod has any flags in common (e.g. -i), you must use two dashes (--) to separate your command's flags/arguments.
Also note, do not surround your command and its flags/arguments with quotes unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr").
kubectl exec 123456-7890 -i -t -- ls -t /usr
Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
kubectl logs nginx
kubectl logs -lapp=nginx
kubectl logs -p -c ruby web-1
kubectl logs -f -c ruby web-1
kubectl logs --tail=20 nginx
kubectl logs --since=1h nginx
kubectl logs job/hello
kubectl logs deployment/nginx -c nginx-1
Creates a proxy server or application-level gateway between localhost and the Kubernetes API Server. It also allows serving static content over specified HTTP path. All incoming data enters through one port and gets forwarded to the remote kubernetes API Server port, except for the path matching the static content path.
kubectl proxy --api-prefix=/
kubectl proxy --www=/my/files --www-prefix=/static/ --api-prefix=/api/
The above lets you 'curl localhost:8001/api/v1/pods'.
kubectl proxy --api-prefix=/custom/
The above lets you 'curl localhost:8001/custom/api/v1/pods'
kubectl proxy --port=8011 --www=./local/www/
Run a proxy to kubernetes apiserver on an arbitrary local port.
kubectl proxy --port=0
Apply a configuration to a resource by filename or stdin. The resource name must be specified. This resource will be created if it doesn’t exist yet. To use ‘apply’, always create the resource initially with either ‘apply’ or ‘create –save-config’.
kubectl apply -f ./pod.json
cat pod.json | kubectl apply -f -
Note: --prune is still in Alpha
Apply the configuration in manifest.yaml that matches label app=nginx and delete all the other resources that are not in the file and match label app=nginx.
kubectl apply --prune -f manifest.yaml -l app=nginx
Apply the configuration in manifest.yaml and delete all the other configmaps that are not in the file.
kubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap
Update the labels on a resource.
kubectl label pods foo unhealthy=true
kubectl label --overwrite pods foo status=unhealthy
kubectl label pods --all status=unhealthy
kubectl label -f pod.json status=unhealthy
kubectl label pods foo status=unhealthy --resource-version=1
Update pod 'foo' by removing a label named 'bar' if it exists. Does not require the --overwrite flag.
kubectl label pods foo bar-
Modify kubeconfig files using subcommands like “kubectl config set current-context my-context”.
kubectl config current-context
kubectl config delete-cluster minikube
kubectl config delete-context minikube
kubectl config get-clusters
kubectl config get-contexts
kubectl config rename-context old-name new-name
kubectl config set-cluster e2e --server=https://1.2.3.4
kubectl config set-cluster e2e --certificate-authority=~/.kube/e2e/kubernetes.ca.crt
kubectl config set-cluster e2e --insecure-skip-tls-verify=true
kubectl config set-context gce --user=cluster-admin
kubectl config use-context minikube
Print the client and server version information for the current context.
kubectl version