Skip to content

Latest commit

 

History

History
145 lines (128 loc) · 12.4 KB

File metadata and controls

145 lines (128 loc) · 12.4 KB

Kubectl Cheatsheet

Check Performance

NameCommand
Get node resource usage​kubectl top node​
Get pod resource usage​kubectl top pod​
Get resource usage for a given pod​kubectl top <podname> --containers​
List resource utilization for all containers​kubectl top pod --all-namespaces --containers=true​

Resources Deletion

NameCommand
Delete pod​kubectl delete pod/<pod-name> -n <my-namespace>​
Delete pod by force​kubectl delete pod/<pod-name> --grace-period=0 --force​
Delete pods by labels​kubectl delete pod -l env=test​
Delete deployments by labels​kubectl delete deployment -l app=wordpress​
Delete all resources filtered by labels​kubectl delete pods,services -l name=myLabel​
Delete resources under a namespace​kubectl -n my-ns delete po,svc --all​
Delete persist volumes by labels​kubectl delete pvc -l app=wordpress​
Delete statefulset only (not pods)​kubectl delete sts/<stateful_set_name> --cascade=false​

Pod

NameCommand
List all pods​kubectl get pods​
List pods for all namespace​kubectl get pods -all-namespaces​
List all critical pods​kubectl get -n kube-system pods -a​
List pods with more info​kubectl get pod -o wide​, ​kubectl get pod/<pod-name> -o yaml​
Get pod info​kubectl describe pod/srv-mysql-server​
List all pods with labels​kubectl get pods --show-labels​
List running pods​kubectl get pods --field-selector=status.phase=Running​
Get Pod initContainer status​kubectl get pod --template '{{.status.initContainerStatuses}}' <pod-name>​
kubectl run command​kubectl exec -it -n "$ns" "$podname" -- sh -c "echo $msg >>/dev/err.log"​
Watch pods​kubectl get pods -n wordpress --watch​
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​kubectl get pods -o='custom-columns=PODS:.metadata.name,CONTAINERS:.spec.containers[*].name'​

Label & Annontation

NameCommand
Filter pods by label​kubectl get pods -l owner=aaa​
Manually add label to a pod​kubectl label pods dummy-input owner=aaa​
Remove label​kubectl label pods dummy-input owner-​

Deployment & Scale

NameCommand
Scale out​kubectl scale --replicas=3 deployment/nginx-app​
online rolling upgrade​kubectl rollout app-v1 app-v2 --image=img:v2​
Roll backup​kubectl rollout app-v1 app-v2 --rollback​
List rollout​kubectl get rs​
Check update status​kubectl rollout status deployment/nginx-app​
Check update history​kubectl rollout history deployment/nginx-app​
Pause/Resume​kubectl rollout pause deployment/nginx-deployment​, ​resume​

Quota & Limits & Resource

NameCommand
List Resource Quota​kubectl get resourcequota​
List Limit Range​kubectl get limitrange​
Customize resource definition​kubectl set resources deployment nginx -c=nginx --limits=cpu=200m​
Customize resource definition​kubectl set resources deployment nginx -c=nginx --limits=memory=512Mi​

Service

NameCommand
List all services​kubectl get services​
List service endpoints​kubectl get endpoints​
Get service detail​kubectl get service nginx-service -o yaml​
Get service cluster ip​kubectl get service nginx-service -o go-template='{{.spec.clusterIP}}'​
Get service cluster port​kubectl get service nginx-service -o go-template='{{(index .spec.ports 0).port}}'​
Expose deployment as lb service​kubectl expose deployment/my-app --type=LoadBalancer --name=my-service​
Expose service as lb service​kubectl expose service/wordpress-1-svc --type=LoadBalancer --name=ns1​

Secrets

NameCommand
List secrets​kubectl get secrets --all-namespaces​
Generate secret​echo -n 'mypasswd'=, then redirect to =base64 -decode​
Create secret from cfg file​kubectl create secret generic db-user-pass --from-file=./username.txt​

StatefulSet

NameCommand
List statefulset​kubectl get sts​
Delete statefulset only (not pods)​kubectl delete sts/<stateful_set_name> --cascade=false​
Scale statefulset​kubectl scale sts/<stateful_set_name> --replicas=5​

Volumes & Volume Claims

NameCommand
List storage class​kubectl get storageclass​
Check the mounted volumes​kubectl exec storage ls /data​
Check persist volume​kubectl describe pv/pv0001​
Copy local file to pod​kubectl cp /tmp/my <some-namespace>/<some-pod>:/tmp/server​
Copy pod file to local​kubectl cp <some-namespace>/<some-pod>:/tmp/server /tmp/my​

Events & Metrics

NameCommand
View all events​kubectl get events --all-namespaces​
List Events sorted by timestamp​kubectl get events --sort-by=.metadata.creationTimestamp​

Node Maintenance

NameCommand
Mark node as unschedulable​kubectl cordon $NDOE_NAME​
Mark node as schedulable​kubectl uncordon $NDOE_NAME​
Drain node in preparation for maintenance​kubectl drain $NODE_NAME​

Namespace & Security

NameCommand
List authenticated contexts​kubectl config get-contexts=, =~/.kube/config​
Set namespace preference​kubectl config set-context <context_name> --namespace=<ns_name>​
Load context from config file​kubectl get cs --kubeconfig kube_config.yml​
Switch context​kubectl config use-context <cluster-name>​
Delete the specified context​kubectl config delete-context <cluster-name>​
List all namespaces defined​kubectl get namespaces​
List certificates​kubectl get csr​

Network

NameCommand
Temporarily add a port-forwarding​kubectl port-forward redis-izl09 6379​
Add port-forwaring for deployment​kubectl port-forward deployment/redis-master 6379:6379​
Add port-forwaring for replicaset​kubectl port-forward rs/redis-master 6379:6379​
Add port-forwaring for service​kubectl port-forward svc/redis-master 6379:6379​
Get network policy​kubectl get NetworkPolicy​

Patch

NameSummary
Patch service to loadbalancer​kubectl patch svc $svc_name -p '{"spec": {"type": "LoadBalancer"}}'​

Extenstions

NameSummary
List api group​kubectl api-versions​
List all CRD​kubectl get crd​
List storageclass​kubectl get storageclass​
List all supported resources​kubectl api-resources​