Kubernetes Cheat Sheet
What is Kubernetes? Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.
It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon 15 years of experience of running production workloads at Google, combined with best-of-breed ideas and practices from the community.
What is kubectl? Kubectl is a command line tool used to run commands against Kubernetes clusters. It does this by authenticating with the Master Node of your cluster and making API calls to do a variety of management actions.
The format of a kubectl command looks like this:
1
kubectl [command] [type] [name] [flags]
Copied!
    [command]: specifies the action you want to perform like create, delete, get, apply
    [type]: any Kubernetes resource, whether automatically provided by Kubernetes (like a service or a pod) or created by you with a Custom Resource Definition
    [name]: the name you have given the resource — if you omit the name, kubectl will return every resource specified by the type
    [flags]: specify any additional global or command specific options such as the output format
What is kubeconfig ? kubeconfig is a configuration file which is used by kubectl In order to access your Kubernetes cluster. The default kubectl configuration file is located at ~/.kube/config and is referred to as the kubeconfig file.
kubeconfig files organize information about clusters, users, namespaces, and authentication mechanisms. The kubectl command uses these files to find the information it needs to choose a cluster and communicate with it.
The loading order follows these rules:
    1.
    If the --kubeconfig flag is set, then only the given file is loaded. The flag may only be set once and no merging takes place.
    2.
    If the $KUBECONFIG environment variable is set, then it is parsed as a list of filesystem paths according to the normal path delimiting rules for your system.
    3.
    Otherwise, the ${HOME}/.kube/config file is used and no merging takes place.
If you see a message similar to the following, kubectl is not configured correctly or is not able to connect to a Kubernetes cluster.

Cluster Management

1
#Display endpoint information about the master and services in the cluster
2
kubectl cluster-info
3
4
#Display the status of all components in the cluster
5
kubectl get componentstatuses
6
7
#Display the Kubernetes version running on the client and server
8
kubectl version
9
10
#Get the configuration of the cluster
11
kubectl config view
12
13
#List the API resources that are available
14
kubectl api-resources
15
16
#List the API versions that are available
17
kubectl api-versions
18
19
#List everything
20
kubectl get all --all-namespaces
21
22
#List all objects in current namespace plus labels
23
kubectl get all --show-labels
24
25
#List components that match label
26
kubectl get all --selector <label>
27
28
#Get a list of contexts
29
kubectl config get-contexts
30
31
#Get the current context
32
kubectl config current-context
33
34
#Switch current context
35
kubectl config use-context <context_name>
Copied!
The context consist of the clustername and namespace that the current user connects to.

ConfigMaps

Shortcode=cm
1
#Display ConfigMaps information
2
kubectl get configmaps
3
4
#create a configmap form a file
5
kubectl create configmap <my-cmname> --from-file=path/to/configmap/
6
7
#See the content of a configmap in yaml format
8
#kubectl get configmap <configmap_name> -o yaml
9
10
#Display detailed information of a configmap
11
kubectl describe configmaps <configmap_name>
12
13
#delete a configmap
14
kubectl delete configmap <configmap_name>
Copied!

Daemonsets

Shortcode = ds
1
#List one or more daemonsets
2
kubectl get daemonset
3
4
#Display the detailed state of daemonsets within a namespace
5
kubectl describe ds <daemonset_name> -n <namespace_name>
6
7
#Edit and update the definition of one or more daemonset
8
kubectl edit daemonset <daemonset_name>
9
10
#Create a new daemonset
11
kubectl create daemonset <daemonset_name>
12
13
#Delete a daemonset
14
kubectl delete daemonset <daemonset_name>
15
16
#Manage the rollout of a daemonset
17
kubectl rollout daemonset
Copied!

Deployments

Shortcode = deploy
1
#List one or more deployments
2
kubectl get deployments
3
4
#dumps all the deployment yaml code on the screen
5
kubectl get deployments <deployment_name> -o yaml
6
7
#Display the detailed state of one or more deployments
8
kubectl describe deployment <deployment_name>
9
10
#Edit and update the definition of one or more deployment on the server
11
kubectl edit deployment <deployment_name>
12
13
#Create one a new deployment
14
kubectl create deployment --image=<img_name> <deployment_name>
15
16
#Delete deployments
17
kubectl delete deployment <deployment_name>
18
19
#scale a deployment
20
kubectl scale deployment <deployment_name> --replicas=[X]
21
22
#Set Autoscaling config
23
kubectl autoscale deployment <deployment_name> --min=10 --max=15 --cpu-percent=80
24
25
#See the rollout status of a deployment
26
kubectl rollout status deployment <deployment_name>
27
28
#see rollout history of all deployments
29
kubectl rollout history deployment
30
31
#See the overview of recent changes
32
kubectl rollout history deployment <deployment_name>
33
kubectl rollout history deployment <deployment_name> --reversion=2
34
35
#bring down the new replicaset and bring up the old ones
36
kubectl rollout undo deployment <deployment_name>
37
kubectl rollout undo deployment <deployment_name> --to-revision=1
38
39
#Pause a rollout
40
kubectl rollout pause deployment <my_deployment>
41
42
#Resume a rollout
43
kubectl rollout resume deployment <my_deployment>
44
45
#expose a deployment as a kubernetes service (type can be NodePort/ClusterIP for on-promise cluster)
46
kubectl expose deployment <deployment_name> --type=NodePort --targetport=80 --name=<myapp-service>
47
48
#Add label to a deployment
49
kubectl lable deployments <deployment_name> state=LA
50
51
#Show deployments labels
52
kubectl get deployments --show-labels
Copied!
If you use kubectl create to create a deployment, it will automatically get a lable with the name app=<NameofDeployment> .
Label plays an essential role in the monitoring that the kubernetes deployment is doing, label is used to make sure that the suffcient amount of pods are available.

Events

Shortcode = ev
1
#List recent events for all resources in the system
2
kubectl get events
3
4
#List Warnings only
5
kubectl get events --field-selector type=Warning
6
7
#List events but exclude Pod events
8
kubectl get events --field-selector involvedObject.kind!=Pod
9
10
#Pull events for a single node with a specific name
11
kubectl get events --field-selector involvedObject.kind=Node, involvedObject.name=<node_name>
12
13
#Filter out normal events from a list of events
14
kubectl get events --field-selector type!=Normal
Copied!

Ingress

Shortcode=ing
1
#List
2
kubectl get ingress <ingress-resource-name>
3
4
# Display detailed information about an ingress resource
5
kubectl describe ingress <ingress-resource-name>
Copied!

Labels

Labels play an essential role in the kubernetes.
1
#General command for adding label to one of kubernetes resources
2
kubectl label <resource_type> <resource_name> <label>
Copied!
Usually labels are applied automatically, or we add them trough the yaml files.

Logs

Container doesn't have stout, to see what's happening within a container use logs:
1
#Print the logs for a pod
2
kubectl logs <pod_name>
3
4
#Quering pod logs using label selector
5
kubectl logs -l app=<my-app>
6
7
#Print the logs for the last hour for a pod
8
kubectl logs --since=1h <pod_name>
9
10
#Get the most recent 20 lines of logs
11
kubectl logs --tail=20 <pod_name>
12
13
#Print the logs for a pod and follow new logs
14
kubectl logs -f <pod_name>
15
16
#Print the logs for a container in a pod
17
kubectl logs -c <container_name> <pod_name>
18
19
#Output the logs for a pod into a file named ‘pod.log’
20
kubectl logs <pod_name> pod.log
21
22
#Copy files out of pod (Requires tar binary in container).
23
kubectl cp <pod_name>:/var/log .
24
25
#View the logs for a previously failed pod
26
kubectl logs --previous <pod_name>
27
28
#Watch logs in real time:
29
kubectl attach <pod_name>
Copied!
For logs we also recommend using a tool developed by Johan Haleby called Kubetail. This is a bash script that will allow you to get logs from multiple pods simultaneously. You can learn more about it at its Github repository.

Manifest Files

Another option for modifying objects is through Manifest Files. Using this method is highly recommend. It is done by using yaml files with all the necessary options for objects configured. Also it is recommended to store your yaml files in a git repository, so you can track changes and streamline changes.
1
#Create objects
2
kubectl create -f manifest_file.yaml
3
4
#Create objects in all manifest files in a directory
5
kubectl create -f ./dir
6
7
#Create objects from a URL
8
kubectl create -f ‘url’
9
10
#Delete an object
11
kubectl delete -f manifest_file.yaml
12
13
#Apply a configuration to an object by filename or stdin. Overrides the existing configuration.
14
kubectl apply -f manifest_file.yaml
15
16
#Replace a configuration to an object by filename or stdin.
17
kubectl replace -f manifest_file.yaml
Copied!
Incase of error while using apply or replace, use delete and then create combination.

Namespaces

Shortcode = ns
1
#List one or more namespaces
2
kubectl get namespace <namespace_name>
3
4
#Display the detailed state of one or more namespace
5
kubectl describe namespace <namespace_name>
6
7
#Set default namesapce
8
kubectl config set-context $(kubectl config current-context) --namespace=<my-namespace>
9
10
#Create namespace <name>
11
kubectl create namespace <namespace_name>
12
13
#Delete a namespace
14
kubectl delete namespace <namespace_name>
15
16
#Edit and update the definition of a namespace
17
kubectl edit namespace <namespace_name>
18
19
#Display Resource (CPU/Memory/Storage) usage for a namespace
20
kubectl top namespace <namespace_name>
Copied!
The optional kubectx package can be used to make switching between namespaces easier, it contains kubectx to switch between context, and kubens to switch between Namespaces. If multiple clusters are available to a kubernetes client, switching context is relevant. If multiple namespaces exist within a cluster, switching namespaces is relevant.

Nodes

Shortcode = no.
1
#List one or more nodes
2
kubectl get nodes
3
4
#Delete a node or multiple nodes
5
kubectl delete node <node_name>
6
7
#edit a node
8
kubectl edit node <node_name>
9
10
#Display Resource usage (CPU/Memory/Storage) for node(s)
11
kubectl top node <node_name>
12
13
#Resource allocation per node
14
kubectl describe nodes | grep Allocated -A 5
15
16
#Pods running on a node
17
kubectl get pods -o wide | grep <node_name>
18
19
#Annotate a node
20
kubectl annotate node <node_name> <annotation>
21
22
#Update the taints on one or more nodes
23
kubectl taint node <node_name> <taint_name>
24
25
#Mark a node as unschedulable
26
kubectl cordon node <node_name>
27
28
#Mark node as schedulable
29
kubectl uncordon node <node_name>
30
31
#Drain a node in preparation for maintenance
32
kubectl drain node <node_name>
33
34
#Add or update the labels of one or more nodes
35
kubectl label nodes <node-name> disktype=ssd
36
37
#list nodes and their labels
38
kubectl get nodes --show-labels
Copied!

Pods

Shortcode = po
1
#List one or more pods
2
kubectl get pods
3
4
#dumps all the pod yaml code on the screen
5
kubectl get pods <pod_name> -o yaml
6
7
#Display the detailed state of a pods
8
kubectl describe pod <pod_name>
9
10
#create a pod, impremitive (depricated)
11
kubectl run <pod_name> --image=<image_name>
12
13
#Create a pod from a yaml file
14
kubectl create -f <pod.yaml>
15
16
#Delete a pod
17
kubectl delete pod <pod_name>
18
19
#edit a pod
20
kubectl edit pod <pod_name>
21
22
#Get interactive shell on a a single-container pod
23
kubectl exec -it <pod_name> /bin/sh
24
25
26
#Execute a command in a pod (pod contains a single container)
27
kubectl exec -it <pod_name> -- <command>
28
29
#Execute a command against a container in a pod (pod contains multiple containers)
30
kubectl exec -it <pod_name> -c <container_name> -- <command>
31
32
#Display Resource usage (CPU/Memory/Storage) for pods
33
kubectl top pod
34
35
#Add or update the annotations of a pod
36
kubectl annotate pod <pod_name> <annotation>
37
38
#Add or update the label of a pod
39
kubectl label pods <pod_name> env=prod
40
41
#Get pods showing labels
42
kubectl get pods --show-labels
43
44
#Remove the lable of a pod
45
kubectl label pods <pod_name> <label>-
Copied!
kubectl exec only works on pods!

Persistent Volume

Shortcode=pv
1
#List one or more persistent volumes
2
kubectl get pv
3
4
#Display the detailed information about a persistent volume
5
kubectl describe pv <pv_name>
Copied!

Persistent Volume Claim

shortcode=pvc
1
#List one or more persistent volume claims
2
kubectl get pvc
3
4
#Display the detailed information about a persistent volume claim
5
kubectl describe pvc <pvc_name>
Copied!

Replication Controllers

Shortcode = rc
1
#List the replication controllers
2
kubectl get rc
3
4
#List the replication controllers by namespace
5
kubectl get rc --namespace=<namespace_name>
Copied!

ReplicaSets

Shortcode = rs
1
#List ReplicaSets
2
kubectl get replicasets
3
4
#Display the detailed state of one or more ReplicaSets
5
kubectl describe replicasets <replicaset_name>
6
7
#Scale a ReplicaSet
8
kubectl scale --replicas=[x] replicaset <replicaset_name>
9
10
#autoscale replicaset
11
kubectl autoscale rs <replicaset_name> --max=10 --min=3 --cpu-percent=50
12
13
#Delete a ReplicaSet
14
kubectl delete replicaset <replicaset-name>
Copied!

Secrets

1
#List secrets
2
kubectl get secrets
3
4
#List details about secrets
5
kubectl describe secrets
6
7
#Create a generic/docker registry/tls secret
8
kubectl create secret generic <secret_name> --from-literal=user=user1 --from-literal=password=password
9
10
#See the content of a secret in yaml format
11
kubectl get secret <secret_name> -o yaml
12
13
#Delete a secret
14
kubectl delete secret <secret_name>
Copied!

Services

Shortcode = svc
1
#List one or more services
2
kubectl get services
3
4
#List endpoint pods of a service
5
kubectl get endpoints <service_name>
6
7
#Display the detailed state of a service
8
kubectl describe services <service_name>
9
10
#Expose a deployment as a new Kubernetes service
11
kubectl expose deployment <deployment_name> --port=[X] --target-port=[Y]
12
13
#Edit and update the definition of one or more services
14
kubectl edit services <service_name>
15
16
#delete a service
17
kubectl delete services <service_name>
Copied!
Select service type using --type . it could be ClusterIP, NodePort, LoadBalancer or ExternalName. Default is 'ClusterIP'.
Services are using Labels, So it is very important for a Service that a Label is present, if you try to expose something that doesn't have a lablel, you can use a Service on top of that.

Service Accounts

Shortcode = sa
1
#List service accounts
2
kubectl get serviceaccounts
3
4
#Display the detailed state of one or more service accounts
5
kubectl describe serviceaccounts
6
7
#Replace a service account
8
kubectl replace serviceaccount
9
10
#Delete a service account
11
kubectl delete serviceaccount <service_account_name>
Copied!

StatefulSet

Shortcode = sts
1
#List StatefulSet
2
kubectl get statefulset
3
4
#Delete StatefulSet only (not pods)
5
kubectl delete statefulset/[stateful_set_name] --cascade=false
Copied!

Common Options

In Kubectl you can specify optional flags with commands. Here are some of the most common and useful ones.
-o Output format. For example if you wanted to list all of the pods in ps output format with more information:
1
kubectl get pods -o wide
Copied!
--dry-run you can generate the yaml file using kubectl command, with out creating that object:
1
kubectl create pod <pod_name> --image=nginx --dry-run -o yaml > my-pod.yaml
Copied!
-n Shorthand for --namespace. For example, if you’d like to list all the Pods in a specific Namespace you would do this command:
1
kubectl get pods --namespace=[namespace_name]
2
kubectl get pods -n=[namespace_name]
Copied!
-f Filename, directory, or URL to files to use to create a resource. For example when creating a pod using data in a file named newpod.json.
1
kubectl create -f ./newpod.json
Copied!
Create vs Apply :kubectl create can be used to create new resources while kubectl apply inserts or updates resources while maintaining any manual changes made like scaling pods.
--field-selectorlet you select Kubernetes resources based on the value of one or more resource fields. This kubectl command selects all Pods for which the value of the status.phase field is Running:
1
kubectl get pods --field-selector status.phase=Running
Copied!
You can use the =, ==, and != operators with field selectors (= and == mean the same thing).
1
kubectl get pods -l environment=production,tier!=frontend
2
kubectl get pods -l 'environment in (production,test),tier notin (frontend,backend)'
Copied!
--watch or -w - watch for changes:
1
kubectl get pods -n kube-system -w
Copied!
--record Add the current command as an annotation to the resource. The recorded change is useful for future introspection. For example, to see the commands executed in each Deployment revision:
1
#it would add CHANGE-CAUSE
2
kubectl create -f deployment-definition.yml --record
3
4
#Now it shows CHANGE-CAUSE
5
kubectl rollout history deployment/myapp-deployment
Copied!
-h for getting help:
1
kubectl -h
2
kubectl create -h
3
kubectl run -h
Copied!
explain command is always handy:
1
kubectl explain deployment
2
kubectl explain deployment.spec
3
kubectl explain deployment.spec.strategy
Copied!
good luck!
.
.
.
Collected by Payam Borosan
Last modified 9mo ago