|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes 1.31: Custom Profiling in Kubectl Debug Graduates to Beta" |
| 4 | +date: 2024-08-22 |
| 5 | +slug: kubernetes-1-31-custom-profiling-kubectl-debug |
| 6 | +author: > |
| 7 | + Arda Güçlü (Red Hat) |
| 8 | +--- |
| 9 | + |
| 10 | +There are many ways of troubleshooting the pods and nodes in the cluster. However, `kubectl debug` is one of the easiest, highly used and most prominent ones. It |
| 11 | +provides a set of static profiles and each profile serves for a different kind of role. For instance, from the network administrator's point of view, |
| 12 | +debugging the node should be as easy as this: |
| 13 | + |
| 14 | +```shell |
| 15 | +$ kubectl debug node/mynode -it --image=busybox --profile=netadmin |
| 16 | +``` |
| 17 | + |
| 18 | +On the other hand, static profiles also bring about inherent rigidity, which has some implications for some pods contrary to their ease of use. |
| 19 | +Because there are various kinds of pods (or nodes) that all have their specific |
| 20 | +necessities, and unfortunately, some can't be debugged by only using the static profiles. |
| 21 | + |
| 22 | +Take an instance of a simple pod consisting of a container whose healthiness relies on an environment variable: |
| 23 | + |
| 24 | +```yaml |
| 25 | +apiVersion: v1 |
| 26 | +kind: Pod |
| 27 | +metadata: |
| 28 | + name: example-pod |
| 29 | +spec: |
| 30 | + containers: |
| 31 | + - name: example-container |
| 32 | + image: customapp:latest |
| 33 | + env: |
| 34 | + - name: REQUIRED_ENV_VAR |
| 35 | + value: "value1" |
| 36 | +``` |
| 37 | +
|
| 38 | +Currently, copying the pod is the sole mechanism that supports debugging this pod in kubectl debug. Furthermore, what if user needs to modify the `REQUIRED_ENV_VAR` to something different |
| 39 | +for advanced troubleshooting?. There is no mechanism to achieve this. |
| 40 | + |
| 41 | +## Custom Profiling |
| 42 | + |
| 43 | +Custom profiling is a new functionality available under `--custom` flag, introduced in kubectl debug to provide extensibility. It expects partial `Container` spec in either YAML or JSON format. |
| 44 | +In order to debug the example-container above by creating an ephemeral container, we simply have to define this YAML: |
| 45 | + |
| 46 | +```yaml |
| 47 | +# partial_container.yaml |
| 48 | +env: |
| 49 | + - name: REQUIRED_ENV_VAR |
| 50 | + value: value2 |
| 51 | +``` |
| 52 | + |
| 53 | +and execute: |
| 54 | + |
| 55 | +```shell |
| 56 | +kubectl debug example-pod -it --image=customapp --custom=partial_container.yaml |
| 57 | +``` |
| 58 | + |
| 59 | +Here is another example that modifies multiple fields at once (change port number, add resource limits, modify environment variable) in JSON: |
| 60 | + |
| 61 | +```json |
| 62 | +{ |
| 63 | + "ports": [ |
| 64 | + { |
| 65 | + "containerPort": 80 |
| 66 | + } |
| 67 | + ], |
| 68 | + "resources": { |
| 69 | + "limits": { |
| 70 | + "cpu": "0.5", |
| 71 | + "memory": "512Mi" |
| 72 | + }, |
| 73 | + "requests": { |
| 74 | + "cpu": "0.2", |
| 75 | + "memory": "256Mi" |
| 76 | + } |
| 77 | + }, |
| 78 | + "env": [ |
| 79 | + { |
| 80 | + "name": "REQUIRED_ENV_VAR", |
| 81 | + "value": "value2" |
| 82 | + } |
| 83 | + ] |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Constraints |
| 88 | + |
| 89 | +Uncontrolled extensibility hurts the usability. So that, custom profiling is not allowed for certain fields such as command, image, lifecycle, volume devices and container name. |
| 90 | +In the future, more fields can be added to the disallowed list if required. |
| 91 | + |
| 92 | +## Limitations |
| 93 | + |
| 94 | +The `kubectl debug` command has 3 aspects: Debugging with ephemeral containers, pod copying, and node debugging. The largest intersection set of these aspects is the container spec within a Pod |
| 95 | +That's why, custom profiling only supports the modification of the fields that are defined with `containers`. This leads to a limitation that if user needs to modify the other fields in the Pod spec, it is not supported. |
| 96 | + |
| 97 | +## Acknowledgments |
| 98 | + |
| 99 | +Special thanks to all the contributors who reviewed and commented on this feature, from the initial conception to its actual implementation (alphabetical order): |
| 100 | + |
| 101 | +- [Eddie Zaneski](https://github.com/eddiezane) |
| 102 | +- [Maciej Szulik](https://github.com/soltysh) |
| 103 | +- [Lee Verberne](https://github.com/verb) |
0 commit comments