kubectl Cheatsheet
[TOC]
References
cheatsheet-kubernetes-A4 (Internet Archive link). This page is taken from it.
1.1 Common Commands
Name | Command |
Run curl test temporarily |
|
Run wget test temporarily |
|
Run nginx deployment with 2 replicas |
|
Run nginx pod and expose it |
|
Run nginx deployment and expose it |
|
Set namespace preference |
|
List pods with nodes info |
|
List everything |
|
Get all services |
|
Get all deployments |
|
Show nodes with labels |
|
Get resources with json output |
|
Validate yaml file with dry run |
|
Start a temporary pod for testing |
|
kubectl run shell command |
|
Get system conf via configmap |
|
Get deployment yaml |
|
Explain resource |
|
Watch pods |
|
Query healthcheck endpoint |
|
Open a bash terminal in a pod |
|
Check pod environment variables |
|
Enable kubectl shell autocompletion |
|
Use minikube dockerd in your laptop |
|
Kubectl apply a folder of yaml files |
|
Get services sorted by name | kubectl get services –sort-by=.metadata.name |
Get pods sorted by restart count | kubectl get pods –sort-by=’.status.containerStatuses[0].restartCount’ |
List pods and images | kubectl get pods -o=’custom-columns=PODS:.metadata.name,Images:.spec.containers[*].image’ |
List all container images | |
kubeconfig skip tls verification | |
Reference | |
Reference |
1.2 Check Performance
Name | Command |
Get node resource usage |
|
Get pod resource usage |
|
Get resource usage for a given pod |
|
List resource utilization for all containers |
|
1.3 Resources Deletion
Name | Command |
Delete pod |
|
Delete pod by force |
|
Delete pods by labels |
|
Delete deployments by labels |
|
Delete all resources filtered by labels |
|
Delete resources under a namespace |
|
Delete persist volumes by labels |
|
Delete state fulset only (not pods) |
|
1.4 Log & Conf Files
Name | Comment |
Config folder |
|
Certificate files |
|
Credentials to API server |
|
Superuser credentials |
|
kubectl config file |
|
Kubernets working dir |
|
Docker working dir |
|
Etcd working dir |
|
Network cni |
|
Log files |
|
log in worker node |
|
log in master node |
|
Env |
|
Env | export KUBECONFIG=/etc/kubernetes/admin.conf |
1.5 Pod
Name | Command |
List all pods |
|
List pods for all namespace |
|
List all critical pods |
|
List pods with more info |
|
Get pod info |
|
List all pods with labels |
|
kubectl get pods –field-selector=status.phase!=Running –all-namespaces | |
List running pods | kubectl get pods –field-selector=status.phase=Running |
Get Pod initContainer status |
|
kubectl run command | kubectl exec -it -n “$ns” “$podname” – sh -c “echo $msg >>/dev/err.log” |
Watch pods |
|
Get pod by selector | kubectl get pods –selector=”app=syslog” -o jsonpath=’{.items[*].metadata.name}’ |
List pods and images | kubectl get pods -o=’custom-columns=PODS:.metadata.name,Images:.spec.containers[*].image’ |
List pods and containers | -o=’custom-columns=PODS:.metadata.name,CONTAINERS:.spec.containers[*].name’ |
Reference |
1.6 Label & Annontation
Name | Command |
Filter pods by label |
|
Manually add label to a pod |
|
Remove label |
|
Manually add annonation to a pod |
|
1.7 Deployment & Scale
Name | Command |
Scale out |
|
online rolling upgrade |
|
Roll backup |
|
List rollout |
|
Check update status |
|
Check update history |
|
Pause/Resume |
|
Rollback to previous version |
|
Reference |
1.8 Quota & Limits & Resource
Name | Command |
List Resource Quota |
|
List Limit Range |
|
Customize resource definition |
|
Customize resource definition |
|
Reference |
1.9 Service
Name | Command |
List all services |
|
List service endpoints |
|
Get service detail |
|
Get service cluster ip | kubectl get service nginx-service -o go-template=’’ |
Get service cluster port | kubectl get service nginx-service -o go-template=’’ |
Expose deployment as lb service |
|
Expose service as lb service |
|
Reference |
1.10 Secrets
Name | Command |
List secrets |
|
Generate secret |
|
Get secret |
|
Get a specific field of a secret | kubectl get secret denny-cluster-kubeconfig -o jsonpath=”{.data.value}” |
Create secret from cfg file | kubectl create secret generic db-user-pass –from-file=./username.txt |
Reference |
1.11 StatefulSet
Name | Command |
List statefulset |
|
Delete statefulset only (not pods) |
|
Scale statefulset |
|
Reference |
1.12 Volumes & Volume Claims
Name | Command |
List storage class |
|
Check the mounted volumes |
|
Check persist volume |
|
Copy local file to pod |
|
Copy pod file to local |
|
Reference |
1.13 Events & Metrics
Name | Command |
View all events |
|
List Events sorted by timestamp | kubectl get events –sort-by=.metadata.creationTimestamp |
1.14 Node Maintenance
Name | Command |
Mark node as unschedulable |
|
Mark node as schedulable |
|
Drain node in preparation for maintenance |
|
1.15 Namespace & Security
Name | Command |
List authenticated contexts |
|
Set namespace preference |
|
Load context from config file |
|
Switch context |
|
Delete the specified context |
|
List all namespaces defined |
|
List certificates |
|
kubectl –as=system:serviceaccount:ns-denny:test-privileged-sa -n ns-denny auth can-i use pods/list | |
| |
Reference |
1.16 Network
Name | Command |
Temporarily add a port-forwarding |
|
Add port-forwaring for deployment |
|
Add port-forwaring for replicaset |
|
Add port-forwaring for service |
|
Get network policy |
|
1.17 Patch
Name | Summary |
Patch service to loadbalancer | =kubectl patch svc $svc_name -p ‘{“spec”: {“type”: “LoadBalancer”}}’= |
1.18 Extenstions
Name | Summary |
Enumerates the resource types available |
|
List api group |
|
List all CRD |
|
List storageclass |
|
1.19 Components & Services
1.19.1 Services on Master Nodes
Name | Summary |
exposes the Kubernetes API from master nodes | |
reliable data store for all k8s cluster data | |
schedule pods to run on selected nodes | |
node controller, replication controller, endpoints controller, and service account & token controllers |
1.19.2 Services on Worker Nodes
Name | Summary |
makes sure that containers are running in a pod | |
perform connection forwarding | |
1.19.3 Addons: pods and services that implement cluster features
Name | Summary |
DNS | serves DNS records for Kubernetes services |
Web UI | a general purpose, web-based UI for Kubernetes clusters |
Container Resource Monitoring | collect, store and serve container metrics |
Cluster-level Logging | save container logs to a central log store with search/browsing interface |
1.19.4 Tools
Name | Summary |
the command line util to talk to k8s cluster | |
the command to bootstrap the cluster | |
the command line to control a Kubernetes Cluster Federation | |
Kubernetes Components |
Last updated