Kubectl cheat sheet

Kubectl cheat sheet

Once the Kubernetes cluster is ready, We can describe our application specs in YAML or JSON format.

Here is a list of kubectl cheat sheet that will be very useful while working with kubernetes.

Cluster introspection

To check the kubectl version

kubectl version
kubeadm version

To get the cluster information

kubectl cluster-info
To get the configuration information

kubectl config view
To check the health status of the Kubernetes components

kubectl get cs
If anything goes wrong in the existing kubeadm cluster, We must reset it and reconfigure it from the scratch.

Read Also: Install the latest python on Mac OS X

Note: Please turn off the swap before you reset the cluster.

kubeadm reset
Once you reset the cluster, Please follow this link to reconfigure again.

How to install kubernetes with kubeadm on ubuntu

Creating Objects

To create resources like pods, deployments, and services in a YAML file.

kubectl create -f <file.yml>
If you make any changes to the existing application in YAML file, you can apply these changes by

kubectl apply -f <file.yml>
Pods & container introspection

kubectl get all
To check a particular resource like pods, deployments

kubectl get po && kubectl get deploy && kubectl get svc
To describe the resources(pods, deployments, services)

kubectl describe po && kubectl describe deploy && kubectl describe svc

To delete the resources

kubectl delete  <resource_type> <resource_name>
Debugging

To check the logs of application and save them in a file in the server

kubectl logs <pod_name> > <file_name.log>
To ssh into the pod

kubectl exec -it <pod_name> sh
To check the docker image version of pods which are running in the server.

kubectl get pods -o jsonpath="{..image}" | tr -s '[[:space:]]' '\n' | sort | uniq -c
To display Resource (CPU/Memory/Storage) usage of pods in the default namespace.

Note: Install heapster (metrics collector) before you run below command.

kubectl top pods -n default
To see the how much of its resource quota each node is using, we can use this command

kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo {}; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- ; echo'
To list and delete “evicted” pods due to insufficient resources on the server.

kubectl get pods --all-namespaces| grep Evicted | $(awk '{print "kubectl -n " $1 " delete pod "$2}')

To Copy the files and directories from a Kubernetes Container [POD] to the local host and vice versa.

kubectl cp <namespace>/<pod>:/tmp/foo <host-machine path>

Rolling-back

To check the rollout status

kubectl rollout status deployment <deployment_name>
To check the rollback history

kubectl rollout history deployment <deployment_name> --revision <revision_number>

To rollback to the previous version.

kubectl rollout undo deployment <deployment_name>

Scaling a Deployment

To scale a deployment to the desired number of pods

kubectl scale deployement <deployment_name> --replicas=<desired_number_of_pods>
To enable autoscaling of pods based on the CPU utilization

kubectl autoscale deployment <deployment_name> --min=<number> --max=<number> --cpu-percent=80

 

Leave a Reply

Your email address will not be published.


*