0% found this document useful (0 votes)
217 views

Kubectl Cheat Sheet

This document provides a Kubernetes cheat sheet with common commands for running and managing Kubernetes resources like pods, deployments, services, secrets and more. It includes over 50 commands organized into sections for pods, deployments, services, secrets, and other Kubernetes objects. The commands cover tasks like deploying applications, viewing logs, setting labels, auto-scaling, handling storage, and deleting resources.

Uploaded by

KS Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views

Kubectl Cheat Sheet

This document provides a Kubernetes cheat sheet with common commands for running and managing Kubernetes resources like pods, deployments, services, secrets and more. It includes over 50 commands organized into sections for pods, deployments, services, secrets, and other Kubernetes objects. The commands cover tasks like deploying applications, viewing logs, setting labels, auto-scaling, handling storage, and deleting resources.

Uploaded by

KS Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Mithun Technologies

+91-9980923226 , +91-8296242028 [email protected]

Kubernetes Cheat Sheet


1 Common Commands

Name Command

Run nginx
deployment kubectl run my-nginx --image=nginx --replicas=2 --port=80
with 2 replicas
Run nginx pod kubectl run my-nginx --restart=Never --image=nginx --port=80 --expose
and expose it
Run nginx
deployment kubectl run my-nginx --image=nginx --port=80 --expose
and expose it
List pods with kubectl get pod -o wide
nodes info
List everything kubectl get all --all-namespaces

Get all services kubectl get service --all-namespaces

Show nodes kubectl get nodes --show-labels


with labels
Validate yaml kubectl create --dry-run --validate -f pod-dummy.yaml
file with dry run
Start a
temporary pod kubectl run --rm -i -t --image=alpine test-$RANDOM -- sh
for testing
kubectl run kubectl exec -it mytest -- ls -l /etc/hosts
shell command
Get system
conf via kubectl -n kube-system get cm kubeadm-config -o yaml
configmap
Get
deployment kubectl get deployment mysql -o yaml
yaml
Explain
kubectl explain pods, kubectl explain svc
resource
kubectl get pods -n <namespace> --watch
Watch pods kubectl get pods --watch
Mithun Technologies
+91-9980923226 , +91-8296242028 [email protected]

Open a bash
terminal in a kubectl exec -it storage sh
pod
Check pod
environment kubectl exec <podname> env
variables
Kubectl apply
a folder of kubectl apply -R -f .
yaml files
Get services
kubectl get services –sort-by=.metadata.name
sorted by name
Get pods
sorted by kubectl get pods –sort-by=’.status.containerStatuses[0].restartCount’
restart count
List pods and kubectl get pods -o=’custom-
images columns=PODS:.metadata.name,Images:.spec.containers[*].image’

1.2 Check Performance

Name Command

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 kubectl top pod --all-namespaces --


containers containers=true
Mithun Technologies
+91-9980923226 , +91-8296242028 [email protected]

1.3 Resources Deletion

Name Command

Delete pod kubectl delete pod/<pod-name> -n <my-namespace>

kubectl delete pod/<pod-name> --grace-period=0 --


Delete pod by force 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 kubectl delete pods,services -l name=myLabel


labels
Delete resources under a kubectl -n my-ns delete po,svc --all
namespace

Delete persist volumes by labels kubectl delete pvc -l app=wordpress

kubectl delete sts/<stateful_set_name> --


Delete statefulset only (not pods) cascade=false

1.4 Log & Conf Files

Name Comment

Config folder /etc/kubernetes/

Certificate files /etc/kubernetes/pki/

Credentials to API /etc/kubernetes/kubelet.conf


server
Superuser credentials /etc/kubernetes/admin.conf

kubectl config file ~/.kube/config

Kubernets working dir /var/lib/kubelet/


Mithun Technologies
+91-9980923226 , +91-8296242028 [email protected]

Docker working dir /var/lib/docker/, /var/log/containers/

Etcd working dir /var/lib/etcd/

Network cni /etc/cni/net.d/

Log files /var/log/pods/

log in worker node /var/log/kubelet.log, /var/log/kube-proxy.log

kube-apiserver.log, kube-scheduler.log, kube-controller-


log in master node manager.log

Env /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

Env export KUBECONFIG=/etc/kubernetes/admin.conf

1.5 Pod

Name Command

List all pods kubectl get pods

List pods for


all kubectl get pods -all-namespaces
namespace
List all kubectl get -n kube-system pods -a
critical pods
List pods
with more kubectl get pod -o wide, kubectl get pod/<pod-name> -o yaml
info
Get pod info kubectl describe pod <podname>

List all pods kubectl get pods --show-labels


with labels
List running
kubectl get pods –field-selector=status.phase=Running
pods
Get Pod
kubectl get pod --template '{{.status.initContainerStatuses}}' <pod-
initContainer name>
status
kubectl run
kubectl exec -it -n “$ns” “$podname” – sh -c “echo $msg >>/dev/err.log”
command
Watch pods kubectl get pods --watch
Mithun Technologies
+91-9980923226 , +91-8296242028 [email protected]

Get pod by kubectl get pods –selector=”app=syslog” -o


selector jsonpath=’{.items[*].metadata.name}’
List pods kubectl get pods -o=’custom-
and images columns=PODS:.metadata.name,Images:.spec.containers[*].image’
List pods
-o=’custom-
and
columns=PODS:.metadata.name,CONTAINERS:.spec.containers[*].name’
containers

1.6 Label & Annontation

Name Command

Filter pods by label kubectl get pods -l owner=denny

Manually add label to a pod kubectl label pods <podname> owner=denny

Remove label kubectl label pods <podname> owner-

1.7 Deployment & Scale

Name Command

Scale out kubectl scale --replicas=3 deployment/nginx-app

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

Rollback to previous version kubectl rollout undo deployment/nginx-deployment

1.8 Quota & Limits & Resource

Name Command
Mithun Technologies
+91-9980923226 , +91-8296242028 [email protected]

List Resource Quota kubectl get resourcequota

List Limit Range kubectl get limitrange

Customize resource kubectl set resources deployment nginx -c=nginx --


definition limits=cpu=200m

Customize resource kubectl set resources deployment nginx -c=nginx --


definition limits=memory=512Mi

1.9 Service

Name Command

List all services kubectl get services

List service endpoints kubectl get endpoints

Get service detail kubectl get service <servicename> -o yaml

kubectl get service nginx-service -o go-


Get service cluster ip
template=’{{.spec.clusterIP}}’
kubectl get service nginx-service -o go-template=’{{(index
Get service cluster port
.spec.ports 0).port}}’
Expose deployment as lb kubectl expose deployment/my-app --type=LoadBalancer --
service name=my-service

Expose service as lb kubectl expose service/wordpress-1-svc --type=LoadBalancer


service --name=ns1

1.10 Secrets

Name Command
Mithun Technologies
+91-9980923226 , +91-8296242028 [email protected]

List secrets kubectl get secrets --all-namespaces

Generate secret echo -n 'mypasswd'=, then redirect to =base64 --decode

Get secret kubectl get secret denny-cluster-kubeconfig

Get a specific field of a kubectl get secret denny-cluster-kubeconfig -o


secret jsonpath=”{.data.value}”
kubectl create secret generic db-user-pass –from-
Create secret from cfg file
file=./username.txt

1.11 StatefulSet

Name Command

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

1.12 Volumes & Volume Claims

Name Command

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

1.13 Events & Metrics

Name Command

View all events kubectl get events --all-namespaces


Mithun Technologies
+91-9980923226 , +91-8296242028 [email protected]

List Events sorted by kubectl get events –sort-


timestamp by=.metadata.creationTimestamp

1.14 Node Maintenance

Name Command

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

1.15 Namespace & Security

Name Command

List authenticated
contexts kubectl config get-contexts, ~/.kube/config

Set namespace kubectl config set-context <context_name> --


preference namespace=<ns_name>

Load context from kubectl get cs --kubeconfig kube_config.yml


config file
Switch context kubectl config use-context <cluster-name>

Delete the specified kubectl config delete-context <cluster-name>


context
List all namespaces kubectl get namespaces
defined

You might also like