[Kubernetes] QuickTip - Run a pod for debug

[Kubernetes] QuickTip - Run a pod for debug


Kubernetes Quick Tip - Running a Pod for Debugging

When working with Kubernetes, debugging a misbehaving application or troubleshooting issues in a deployment can be challenging, especially when the deployment is in an error state and prevents you from executing commands within the container. In this quick tip, we'll explore a simple and effective technique to debug such scenarios by running a pod specifically for debugging purposes. This method allows you to gain access to the container, inspect the filesystem, execute commands, and resolve issues efficiently.

1. Create a Debug Pod YAML:
Start by creating a YAML file, let's call it `debug-pod.yaml`, and define the Pod manifest using the following contents:

apiVersion: v1
kind: Pod
metadata:
  name: debug-pod
  namespace: default
spec:
  containers:
  - name: debug-container
    image: <your-image-name>
    command: ["/bin/sleep"]  # Sleep command to keep the container running
    args: ["infinity"]  # Sleep indefinitely to prevent the container from exiting

Replace `<your-image-name>` with the image name that matches the deployment you want to debug. This YAML creates a Pod with a single container that runs the `/bin/sleep infinity` command, effectively keeping the Pod alive indefinitely.

There is an advantage running the debug-pod with yaml instead of creating the same with kubectl. Using yaml you can replicate all the configuration that your deployment have, like environment from secrets and configmaps, volumeMounts and others configurations that may have.

2. Apply the YAML:
Apply the YAML file using the following command:

kubectl apply -f debug-pod.yaml

This command will create the debug Pod in your Kubernetes cluster.

3. Access the Debug Pod:
Once the Pod is running, execute the following command to access the shell of the debug container:

kubectl exec -it debug-pod -- /bin/bash

This command opens an interactive shell session within the debug container.

4. Perform Debugging Tasks:
With the shell session open, you can explore the filesystem, inspect logs, execute commands, and perform any debugging tasks necessary to troubleshoot the deployment or containerized application.

5. Clean Up:
After completing the debugging tasks, you can remove the debug Pod by running the following command:

kubectl delete pod debug-pod

This command will delete the debug Pod from your cluster.

Thank you for joining us on this adventure, and we look forward to seeing you on the next page.

- Blogpost created without using AI tools -

How do you rate this article?

5



Everything One Technology
Everything One Technology

Here in Everything One technology you will find information about Clouds, kubernetes, database (mostly postgres) and more. Save us and keep reading. Thank You, and see you in the next page.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.