The Kubectl Cheat Sheet (Kube-Control)

Table of Contents

What is kubectl?

`Kubectl` is a command-line tool used to deploy and manage applications on a Kubernetes cluster. It allows you to interact with the Kubernetes API to create, update, and delete resources such as pods, services, and deployments. kubectl can be used to manage both local and remote Kubernetes clusters and is an essential tool for anyone working with Kubernetes.

How to install kubectl

If you want to use kubectl, the command-line tool for Kubernetes, you need to install it on your machine. Depending on your operating system, there are different ways to do that. Some operating systems, such as Windows, Linux, and Mac, have local specific options that let you download and install kubectl directly. Other solutions, such as Minikube, allow you to install the tool set into your Kubernetes cluster without needing a local installation.

Either way, installation is quick and easy, and you can find detailed steps for each option on the [official Kubernetes documentation](

Tip #1 - How to check kubectl version?

To check the version of `kubectl`, run the following command:

`kubectl version`

This will display the version information for both the client and server components of `kubectl`.

Tip #2 - What are kubectl logs?

`kubectl logs` is a command used to print the logs of a container in a pod. This is useful for debugging purposes and for getting information about what a container is doing. By default, it will print the logs from the first container in the specified pod. You can also specify a container name to print logs from a specific container within the pod.

Tip # 3 - Deleting with kubectl delete pod

`kubectl delete pod` is a command used to delete a pod. When you delete a pod, Kubernetes will create a new one to replace it. This is useful for updating pods with new configurations or images. If you want to delete the pod and prevent Kubernetes from creating a new one, you can use the --force flag.

Tip #4 - Running kubectl -k to Deploy Kustomization File

`kubectl -k` is a command used to deploy Kubernetes resources using a kustomization file. A kustomization file is a YAML file that defines a set of Kubernetes resources and how they should be deployed. It allows you to customize your deployments without modifying the original YAML files.

Tip #5 - Copying Files with kubectl cp

`kubectl cp` is a command used to copy files and directories between a container and the user's local file system. This is useful for transferring files to and from a container for debugging purposes or to extract data from a container.

To copy a file from a container to the local file system, use the following command:

`kubectl cp <namespace>/<pod>:<container-path> <local-path>`

To copy a file from the local file system to a container, use the following command:

`kubectl cp <local-path> <namespace>/<pod>:<container-path>`

Tip #6 - Run Container Commands with kubectl exec

`kubectl exec` is a command used to run a command inside a container in a pod. This is useful for debugging purposes or for running commands that are not included in the container image. By default, it will run the command in the first container of the specified pod. You can also specify a container name to run the command in a specific container within the pod.

Tip #7 - Port Forwarding with kubectl Port Forward

`kubectl port` forward is a command used to forward a local port to a port on a pod. This is useful for accessing services running inside a pod directly from your local machine. For example, if a pod is running a web server on `port 80`, you can use `kubectl port forward` to forward `port 80` on the pod to `port 8080` on your local machine. Then, you can access the web server by navigating to `http://localhost:8080` in your web browser.

To forward a port, use the following command:

`kubectl port-forward <pod-name> <local-port>:<pod-port>`

`<pod-name>` is the name of the pod you want to forward the port from. `<local-port>` is the port on your machine that you want to forward to. `<pod-port>` is the port on the pod that you want to forward from.

Tip #8 - Submitting a File using the kubectl Apply Command

`kubectl apply` is a command used to apply a configuration file to a Kubernetes cluster. It is the recommended way to manage Kubernetes resources and can be used to create, update, or delete resources. When you apply a configuration file, Kubernetes will compare the desired state of the resources in the file to the current state of the resources in the cluster and make any necessary changes to bring the resources to the desired state.

Tip # 9 - What does kubectl create Namespace do?

`kubectl create namespace` is a command used to create a new namespace in a Kubernetes cluster. Namespaces are a way to divide a Kubernetes cluster into multiple virtual clusters. They provide a scope for names, so resources with the same name can coexist in different namespaces. Namespaces can be used to separate different environments, such as production and development, or different teams working on the same cluster. To create a new namespace, use the following command:

`kubectl create namespace <namespace-name>`

Replace `<namespace-name>` with the desired name of the new namespace.

Tip #10 - Troubleshooting with kubectl Debug Command

`kubectl debug` is a command used to enter a debug container that is co-located with a target container in the same pod. This is useful for debugging purposes, as it allows you to examine the environment of a container and debug issues that may be occurring. When you enter the debug container, you will have access to the same file system and network namespace as the target container, allowing you to run diagnostic commands and examine logs.

To enter the debug container, use the following command:

`kubectl debug <pod-name> -c <container-name>`

`<pod-name>` is the name of the pod that contains the target container. `<container-name>` is the name of the target container that you want to debug.

Botkube makes Kubectl Easier

Botkube simplifies the use of `kubectl` in many areas, but two stand out as particularly beneficial. Firstly, Botkube enables users to run `kubectl` commands directly from Slack or Teams, eliminating the need to open a separate command line interface. This feature enables developers, site reliability managers, and other employees in the group chat to run necessary `kubectl` commands with ease.

Secondly, Botkube introduces the ability to create command aliases, allowing users to assign quick phrases or letters to commonly used kubectl commands. This feature eliminates the need to remember long and complex four-word commands, streamlining the Kubectl process for all users. These features make Botkube an essential tool for any team looking to simplify their Kubectl workflow and increase productivity. Read more about [Kubectl aliases on our blog](