Last updated Nov 18, 2020
There are multiple resources for the Kubernetes kubectl command line reference. It is very good practice to learn the usage of all those commands. But, here I would like to share the most day to day commands that we use on the terminal to take care of our daily development and debugging activities with respect to Kubernetes.

To Get the Context


# To get the current context
$ kubectl config current-context

# To Display list of contexts
$ kubectl config get-contexts

# Now, create the namespace in the cluster
$ kubectl create namespace <your namespace name>

# Use the namespace for the subsequent kubectl operations
$ kubectl config set-context --namespace=<your namespace name> --current

Namespace Commands


# Describe the namespace
$ kubectl describe namespace <your namespace name>

# Get the namespace details either in yaml or json
$ kubectl get namespace blog-ns -o json
$ kubectl get namespace blog-ns -o yaml

# To list all the resources under a namespace
$ kubectl get all -n <your namespace name> 

ConfigMap and Secretes Commands


# Create a config map
$ kubectl create configmap <name of the config map> --from-literal=username=user --from-literal=hostname=host

# Create a secret
# Remember Secretes are namespace specific, they can not be accessed across the namespaces
$ kubectl create secret generic <secret name> --from-literal=<key>=<value>

# Whenever you store a secret, you should store them in a base64 format
$ echo -n <your password> | base64

# To decode the value
$ echo -n base64 value | base64 -decode

Deployment and Pod Commands


# List all the pods in all namespaces
$ kubectl get pods --all-namespaces 

# Create a POD quickly
$ kubectl run <pod name> --image=busybox --restart=Never -n <your namespace>

# In some cases, you just need the pod yaml
$ kubectl run <pod name> --image=nginx --dry-run=client --restart=Never -n <your namespace> -o yaml

# Rolling Update of the deployment image
$ kubectl set image deployment/<deployment name> <containername>=<new image name>

# Restart the deployment
$ kubectl rollout restart deployment/<deployment name>

# To undo the rollout
$ kubectl rollout undo deployment/<deployment name>

# To know the rollout history of a deployment
$ kubectl rollout history deployment <deployment name>

# To view the logs
$ kubectl logs <podname> --namespace=<namespace name>

# To open the interactive shell to the pod
$ kubectl exec -it <pod name> --namespace=<namespace name> -- /bin/sh

# To check the evens on the pod
$ kubectl describe pod <pod name> | grep -C 10 Events:

# Extract a pod definition yaml file to local file
$ kubectl get pod <podname> -o yaml > <local pod filename>.yaml

Searching or Listing Commands


# List the resources in a sorted order by any of the metadata
$ kubectl get services --sort-by=.metadata.name

# Get the particular fields displayed from the resources
# If it is a list then, use names[*].abc
$ kubectl get services -A -o custom-columns=NAME:metadata.name;

# List the resources with specific label
$ kubectl get pod -l <label name>=<label value>
$ kubectl get pods --selector <label name>=<label value>

# To list all of the resources matching with the label
$ kubectl get all --selector <lable name>=<label value>

All the above command reference is the typical actions we do as a Cloud Developer/Engineer in our day to day activities. But for more references the ocean like documentation available at here