THE Definitive Cheat Sheet: Kubectl
THE Definitive Cheat Sheet: Kubectl
DEFINITIVE
CHEAT
SHEET
KUBECTL
WWW.THECHIEF.IO
⭐Kubectl Sheetcheat
Note: This cheatsheet is part of Learn Kubernetes by Building 10 projects ebook.
Pods
Create a service
Delete service(s)
Describe a service
Nodes
Get node
Get a specific node
Show node metrics
Get external IPs of cluster nodes
Describe commands with verbose output
Check which nodes are ready
Mark a node as unschedulable
Drain a node for maintenance
Mark a node as schedulable
Namespaces
List namespaces
List or describe a namespace
Create namespace
Delete namespace
Service accounts
List service accounts
Get a service account
Create a service account
Delete a service account
Describe a service account
Events
List events
List sorted events
List formatted events
Documentation
Get the documentation for pod manifests
Get the documentation for service manifests
Describing resources
Editing resources
Edit a service
Edit a service with your favorite text editor
Deleting Resources
Cluster
Display addresses of the master and services
Dump cluster state to STDOUT
Dump cluster state to a file
Compares the current cluster state against the state that the cluster would be in if the
manifest was applied
List all images running in a cluster
Kubectl context
Alias
Create an alias on *nix
Create an alias on Windows
Kubectl imperative (create) vs declarative (apply)
Create
Apply
Pods
or
or
kubectl get po
Examples:
JSON output
or
or
Wide output:
Custom columns:
or
or
-o=custom-columns=
Print a table using a comma separated list of custom columns
<spec>
or
or
or
Example:
Example:
Patch a pod
Example:
Another example:
Create in a namespace
or
If you create the pod from a file, you can also use:
To force deletion:
or
Sometimes a pod contains more than 1 container. You need to filter the output to get logs for a
specific container(s)
If you need to output the logs for all pods with a label
Example:
You can also view logs in a multi container case with labels:
Deployments
Create a deployment
or
Example:
Get a deployment
kubectl get deploy <deployment>
Watch a deployment
or
Scale a deployment
Edit a deployment
Rollback deployment
After editing your deployment, you had an error, a solution can be rolling back to the old
deployment status:
Example:
gives you:
REVISION CHANGE-CAUSE
2 kubectl set image deployment/nginx nginx=nginx:1.9.1 --record=true
3 <none>
Example:
Port Forwarding
Example:
Example: Listen on ports 8000 and 9000 on localhost, forwarded from the same ports in the pod
(8000 and 9000)
Example:
Example:
Create a service
Examples:
Delete service(s)
or
Describe a service
Nodes
Get node
Namespaces
List namespaces
or
kubectl get ns
Create namespace
or
or
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Namespace
metadata:
name: mynamespace
EOF
Delete namespace
or
Service accounts
or
kubectl get sa
or
or
or
or
Events
List events
Example:
Documentation
Describing resources
or
apiservices.apiregistration.k8s.io
certificatesigningrequests.certificates.k8s.io
clusterrolebindings.rbac.authorization.k8s.io
clusterroles.rbac.authorization.k8s.io
componentstatuses
configmaps
controllerrevisions.apps
cronjobs.batch
csidrivers.storage.k8s.io
csinodes.storage.k8s.io
customresourcedefinitions.apiextensions.k8s.io
daemonsets.apps
daemonsets.extensions
deployments.apps
deployments.extensions
endpoints
events
events.events.k8s.io
horizontalpodautoscalers.autoscaling
ingresses.extensions
ingresses.networking.k8s.io
jobs.batch
leases.coordination.k8s.io
limitranges
mutatingwebhookconfigurations.admissionregistration.k8s.io
namespaces
networkpolicies.extensions
networkpolicies.networking.k8s.io
nodes
persistentvolumeclaims
persistentvolumes
poddisruptionbudgets.policy
pods
podsecuritypolicies.extensions
podsecuritypolicies.policy
podtemplates
priorityclasses.scheduling.k8s.io
replicasets.apps
replicasets.extensions
replicationcontrollers
resourcequotas
rolebindings.rbac.authorization.k8s.io
roles.rbac.authorization.k8s.io
runtimeclasses.node.k8s.io
secrets
serviceaccounts
services
statefulsets.apps
storageclasses.storage.k8s.io
validatingwebhookconfigurations.admissionregistration.k8s.io
volumeattachments.storage.k8s.io
Editing resources
Edit a service
Deleting Resources
componentstatuses cs
configmaps cm
daemonsets ds
deployments deploy
endpoints ep
event ev
horizontalpodautoscalers hpa
ingresses ing
limitranges limits
namespaces ns
nodes no
persistentvolumeclaims pvc
persistentvolumes pv
pods po
podsecuritypolicies psp
replicasets rs
replicationcontrollers rc
resourcequotas quota
serviceaccount sa
services svc
Verbose Kubectl
Useful steady state information about the service and important log
--v=2 messages that may correlate to significant changes in the system. This is the
recommended default log level for most systems.
Cluster
kubectl cluster-info
Kubectl context
KUBECONFIG=~/.kube/config1:~/.kube/config2:~/.kube/config3
Alias
alias k=kubectl
Create
You tell your cluster what you want to create, replace or delete, not how you want you it to look
like.
Apply
You tell your cluster how you want it to look like.
The creation, deletion and modification of objects is done via a single command. The declarative
approach is a statement of the desired end result.
If the deployment is deleted in <deployment-filename> , it will also be deleted from the cluster.