Kubernetes - kubectl cheatsheet

by

Kubernetes is a system that can at times be a bit threatening. It is truly a vast and agile service, and maintaining it can feel a bit off-putting at times. In this article, I'm going to write down some of the most common commands I use in my day to day Kubernetes maintenance.

Pods

A Pod is the smallest unit that hosts Containers in Kubernetes. It can at minimum contain one container. It can also host multiple containers together, which can talk to each other over a localhost interface (127.0.0.1).

Getting all Pods

Used to get all Pods in the system.

Can be used with the -o flag to get more information such as which Mode hosts which Container. The -n flag can be added to specify a specific namespace

kubectl get pods -A

Describe a Pod

Describing a Pod allows you to see expanded information related to a given Pod, such as the last events it experienced, its Labels, Volumes and more.

kubectl describe pod  -n 

Delete a Pod

If you want to force a pod to restart itself, simply delete it with the following command.

kubectl delete pod  -n 

Getting the logs of a Pod

You can get the logs of a pod with this simple command. Redirect it into a file to prevent overloading your terminal's buffer. Note that -c specifies the container's name inside the pod.

kubectl logs  -n  -c >container>

Accessing the shell of a container

This command will allow you to get access to the shell inside a pod. Use -c to specify the container name.

kubectl exec -it  sh -n 

Namespaces

Namespaces are an abstraction that allows you to group resources together under a specific name.

Getting all Namespaces

This one is used to get all Mamespaces the system contains, A Namespace is a logical way to associate resources with a specific name, which is useful for logically grouping components together.

kubectl get ns

Deleting a Namespace

Deleting a namespace will purge all resources associated with it, such as deployments, pods, services etc.

kubectl delete ns 

Deployments

Deployments are used to configure pods A deployment contains instructions that specifies the configuration of a Pod.

Get Deployments in a given Namespace

This command can be used to list the Deployments in a given Namespace.

kubectl get deployments -n 

Edit a Deployment

You can use this command to edit a Deployment, where you can change everything that makes a Deployment work, such as the specs of the Container, or the image it uses.

kubectl edit deployment  -n 

Delete a Deployment

If you want to delete your pods premanently, you should delete your deployment. Use the following command to do so.

kubectl delete deployment  -n 

Nodes

Nodes do the heavy lifting, they're either workers who host the worker pods themselves, or the masters who host the API.

Getting Nodes

You can see a list of Modes and their status with this command. Use the -o flag to see more information about them.

kubectl get nodes

Draining a Node

If you want to vacate your Node from all Pods you can use this command. Use it when you want to perform maintenance on a node. This will cause Kubernetes to transfer the pods to another Node that has free resources.

kubectl drain 

Deleting a Node

To permanently delete a node from your cluster, you can use this command, however you should safely drain it first!

kubectl delete node 

Showing the labels of a Node

Use this command to displays the current labels assigned to a node.

kubectl get nodes --show-labels

Labeling a Node

You can label nodes in order to specify which node will host which deployments. The selector is used in a deployment to label the pods, and you use the following command to label the node. This completes the circle and allows a pod to be assigned to a given node using the node's label.

kubectl label nodes  =

Services

Services serve as a way to abstract between pods and network services.

Getting services

This command will allow you to see all services in the cluster, along with their type, namespace, port etc. Use -n instead of -A to filter for a specific namespace.

kubectl get ns -A

Delete a service

The following command can be used to delete a service.

kubectl delete svc  -n