kubectl Cheat Sheet

Display List of Contexts

kubectl config get-contexts

Display the current-context

kubectl config current-context 

Set the default context to cluster-1

kubectl config use-context cluster-1 

Get all namespaces on the cluster

kubectl get namespaces
or
kubectl get ns

Create a namespace(below command creates nginx namespace)

kubectl create namespace nginx
or
kubectl create ns nginx

Create resource(s) from yaml files(app.yaml file has resource definition)

kubectl apply -f ./app.yaml

List Kubernetes pods on a cluster

kubectl get pods --all-namespaces    #to list all pods in all namespaces
kubectl get pods -n webapp           #to list all pods in webapp namespace
kubectl get pods -o wide             #to list all pods in the current/default namespace, with more details

Get a pod’s YAML

kubectl get pod app-pod -o yaml

List Deployments on a cluster

kubectl get deployments -n web-app        #to list deployments in web-app namespace
kubectl get deployments --all-namespaces  #to list deployments in all namespaces
kubectl get deployment web-deployment     #to list a particular deployment in current/default namespace

List Services on a cluster

kubectl get services                      #to list services in current/default namespace
kubectl get services --all-namespaces     #to list services in all namespaces
kubectl get service app-service           #to list a particular service in current/default namespace

Leave a Reply

%d