Run kubernetes debug container with volume mounts (ephemiral container)

By default debug containers can't access the filesystem of the pod

Since kubectl 1.30 there is a kubectl debug --custom argument that allows custom container spec additions. Here are some examples on how to use this new feature, make sure your kubectl version is 1.30 and environment variable KUBECTL_DEBUG_CUSTOM_PROFILE=true is set

kubectl custom debug profiles

It's now possible to define volumeMounts in your debugging session by redefining from the mounts of your running application container.

Define volume mounts

Create a file custom-debug-profile.json

{
  "volumeMounts": [
    {
      "mountPath": "/data",
      "name": "data",
      "readOnly": true
    }
  ]
}

Start debug container with profile

KUBECTL_DEBUG_CUSTOM_PROFILE=true
kubectl debug -it <POD_NAMe> --image=<DEBUG_CONTAINER_IMAGE> --target=<TARGET_CONTAINER> --custom="custom-debug-profile.json"

Extra environment variables

Add some extra environment variables by adding them to the custom debug profile.

{
  "env": [
    {
      "name": "FOOBAR",
      "value": "Hello world!"
    },
    {
      "name": "BARBAZ",
      "value": "Whale say!"
    }
  ]
}