Sub topic 8
Debugging profiles and privilege levels
This chapter assumes you can run kubectl debug against a pod, as covered in the earlier chapters. You should also know what Linux capabilities are (named slices of root's powers, such as NET_ADMIN) and have a rough idea of Pod Security Standards. You need a cluster and a kubectl client that support --profile (kubectl 1.27 or later).
Why profiles exist
A debug container is a real container, so it needs a security context: which user it runs as, which capabilities it has, and whether it is privileged. Without guidance you would either give every debug session too much power or find that a session can't do the job.
The --profile flag selects a preset security context for the debug container. Pick the weakest profile that still lets you do the task.
The available profiles
| Profile | What it gives the debug container | Typical use |
|---|---|---|
| `legacy` | The old behavior from before profiles. It is the default and may be changed in a future release. | Keeping existing scripts working |
| `general` | Reasonable defaults for most debugging, including the `SYS_PTRACE` capability so you can trace processes. | Inspecting processes and files |
| `baseline` | Settings that satisfy the Baseline Pod Security Standard. | Namespaces that enforce Baseline |
| `restricted` | Settings that satisfy the Restricted Pod Security Standard: non-root, all capabilities dropped, no privilege escalation. | Namespaces that enforce Restricted |
| `netadmin` | Everything in `general`, plus `NET_ADMIN` and `NET_RAW`. | Packet capture, routes, firewall rules |
| `sysadmin` | A privileged container running as root. | Last-resort node or kernel-level work |
Exact details can differ between kubectl versions. Run kubectl debug --help to see the profiles your client supports.
Using a profile
Pass the flag together with the usual image and target options. This session needs to look at network settings, so it uses netadmin:
kubectl debug -it web-7d4b9c-abcde --image=nicolaka/netshoot --target=app --profile=netadminInside the debug shell, tools such as ip route or tcpdump now work, because the container holds NET_ADMIN and NET_RAW. Under the default profile, the same commands would probably fail with "Operation not permitted".
A profile applies to the debug container only. The application container keeps its own security context. This matters because a restricted application does not stop you from adding a more capable debug container. That is why RBAC on ephemeral containers, covered in a later chapter, is the real control point.
Matching the profile to the cluster's policy
Many clusters enforce Pod Security Standards through namespace labels. If a namespace enforces restricted, a debug container that asks for extra capabilities can be rejected by admission. In that case choose a compliant profile:
kubectl debug -it web-7d4b9c-abcde --image=busybox:1.36 --target=app --profile=restrictedA restricted container runs as a non-root user with no added capabilities. Some actions then fail. For example, you may not be able to read files owned by root in the application's filesystem through /proc/<pid>/root. If that blocks you, move up one level only if policy allows it.
Choosing a level
A practical order is to start with restricted or general, and escalate only when a specific command fails for lack of permission. Record which profile you used in your incident notes. Privileged debugging is a sensitive action and should be traceable.
Common errors
- Forbidden by admission. The namespace enforces a Pod Security level stricter than your profile. Use
baselineorrestricted, or ask an administrator. - Operation not permitted inside the shell. The profile lacks the needed capability. Check whether
netadminorgeneralfits before reaching forsysadmin. - Different behavior between runs. You relied on the
legacydefault, which may change. Always pass--profileexplicitly in scripts and runbooks. - Profile seems ignored on a pod copy. Profiles shape the debug container that
kubectl debugcreates. When you change a container's command in a--copy-tocopy, you control its context yourself.
Exercise
During an incident you need to run tcpdump against traffic from a pod's application container, and the namespace enforces the Restricted Pod Security Standard. Which profile do you try first, and what do you do if it fails?
Show answer
Try --profile=restricted first, since it is the one that satisfies the namespace policy. tcpdump needs NETRAW and usually NETADMIN, so it will probably fail with "Operation not permitted". Then use --profile=netadmin, which adds those capabilities. The namespace's admission policy may reject it. In that case an administrator must either allow the debug session, for example through a temporary policy exception, or you must capture traffic another way, such as from the node.