Kubernetes Cheatsheet

General InfoNamespacesServices

# get the current version

kubectl version

# get cluster info

kubectl cluster-info

# get the configurations

kubectl config view

# watch the kublet logs

watch -n 2 cat /var/log/kublet.log

# list namespace

kubectl get ns

# display yaml for namespaces

kubectl get ns -o yaml

# describe namespace

kubectl describe ns

# edit a namespac

kubectl edit ns

# delete a namespace

kubectl delete ns

# Display all services

kubectl get svc

# describe service

kubectl describe svc

# display wide output

kubectl get svc -o wide

# display yaml of a service

kubectl get svc -o yaml

# show labels of the service

kubectl get svc –show-labels

# delete a service

kubectl delete svc

# edit a service

kubectl edit svc

DeploymentsDaemon-SetsContext & Configuration

# list all deployment

kubectl get deploy

# describe a deployment

kubectl describe deploy

# display wide output of deployments

kubectl get deploy -o wide

# display yaml of deployments

kubectl get deploy -o yaml

# edit a deployment

kubectl edit deploy

# delete a deployment

kubectl delete deploy

# scale a deployment

kubectl scale deploy –replicas=5

# create a deployment

kubectl create -f file.yaml

# apply deployment

kubectl apply -f file.yaml

# display all daemon sets

kubectl get ds

# describe a daemon sets from a namespace

kubectl describe ds -n

# display yaml of a daemon set

kubectl get ds -n -o yaml

# edit a daemon sets

kubectl edit ds -n

# delete a daemon set

kubectl delete ds

# show kubeconfig settings

kubectl config view

# display the first user

kubectl config view -o jsonpath='{.users[].name}’

# get list of users

kubectl config view -o jsonpath='{.users[*].name}’

# display the context

kubectl config get-contexts

# display current context

kubectl config current-context

# set default context to cluster-name

kubectl config use-context cluster-name

# add a new cluster to your kubeconf that supports basic auth

kubectl config set-credentials kubeuser/foo.kubernetes.com –username=foo –password=bar

# set a context utilizing a specific username and namespace

kubectl config set-context gcd –user=cluster-user –namespace=foo **

kubectl config use-context gcd

# permanently save the namespace for all subsequent kubectl commands in that context

kubectl config set-context –current –namespace=gcp-s2

Service AccountReplica SetsRoles

# get a service account

kubectl get sa

# display yaml of a service account

kubectl get sa -o yaml

# edit a servie account

kubectl edit sa

# delete a service account

kubectl delete sa

# list all replica sets

kubectl get rs

# describe a replica set

kubectl describe rs

# display wide output

kubectl get rs -o wide

# display yaml of a replica set

kubectl get rs -o yaml

# edit a replica set

kubectl edit rs

# delete a replica set

kubectl delete rs

# get all roles from all namespaces

kubectl get roles –all-namespaes

# display yaml of a role

kubectl get roles -o yaml

SecretsConfigMapsIngress

# get all secrets

kubectl get secrets

# display a secrets from a namespace

kubectl get secrets -n

# display yaml of a secret

kubectl get secrets -o yaml

 

# get all config maps

kubectl get cm

# get config maps from a namespace

kubectl get cm -n

# edit a config map

kubectl edit cm

# delete a config map

kubectl delete cm

# get ingress

kubectl get ing

# get ingress from a namespace

kubectl get ing -n <NS>

Persistent VolumeAnnotateEvents

# get persistent volumes

kubectl get pv

# describe a persistent volume

kubectl describe pv

# describe a persistent volume Claim

kubectl describe pvc

# describe a Storage Class

kubectl describe sc

#Anootate Pod

kubectl annotate po

#Anootate Node

kubectl annotate no

# get all events

kubectl get events

# show events from a namespace

kubectl get events -n

NodesPods

# display all nodes

kubectl get no

# describe a node

kubectl describe no

# display yaml for a node

kubectl get no -o yaml

# display wide output

kubectl get no -o wide

# get node by selector

kubectl get no –selector=[LABEL_NAME]

# delete a node

kubectl delete no

# edit a node

kubectl edit no

# get list of pods

kubectl get po

kubectl get pods

# get pod details

kubectl describe po

kubectl describe pod

# find pod by label

kubectl get po -l app=frontend

# get pod logs

kubectl logs

# find log for specific container in pod

kubectl logs -c

kubectl logs -l name=myLabel -c

# get pod’s yaml

kubectl get po -o yaml

kubectl get pod -o yaml

# get a pod’s YAML without cluster specific information

kubectl get po -o yaml –export

kubectl get pod -o yaml –export

# list all pods in the namespace, with more details

kubectl get po -o wide

kubectl get pods -o wide

# get the pod log with label name

kubectl logs -l name=myLabel

# attach to running container

kubectl attach my-pod -i 

# listen on port 8080 on the local machine

# and forward to port 8000 on my-pod

kubectl port-forward my-pod 8080:8000

# show metrics for a given pod and its containers

kubectl top pod –containers

# delete a pod

kubectl delete po <POD NAME>

# edit a pod

kubectl edit po <POD NAME>

# create a pod on-fly

kubectl run –image= –restart=Never

# Get Pod Details

kubectl describe pod <POD NAME>

#Connect to pod/container 

kubectl exec -it <POD NAME> /bin/bash

#Connect to pod specific container inside pod

kubectl exec -it <POD NAME> /bin/bash -c <CONTAINER>