Kubectl Connect to Pod

Published
Length
3 min read

Luis Felipe Dias

All posts

To run a kubectl connect to pod session, you open a shell through the API. You must hold permission for the pod exec subresource in that namespace. Teams often grant broad roles so engineers can check live containers and fix bugs. Overly broad roles let users run any command inside any container across the cluster. How kubectl connect to pod works with native roles Kubernetes manages cluster permissions through role-based rules on the API server. A Role sets permissions inside one single name

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

To run a kubectl connect to pod session, you open a shell through the API. You must hold permission for the pod exec subresource in that namespace. Teams often grant broad roles so engineers can check live containers and fix bugs. Overly broad roles let users run any command inside any container across the cluster.

How kubectl connect to pod works with native roles

Kubernetes manages cluster permissions through role-based rules on the API server. A Role sets permissions inside one single namespace. A ClusterRole sets permissions across all namespaces or for cluster-wide items like nodes. Both role types hold rules that only add permissions. Kubernetes does not have deny rules in its role system.

To grant these rules to a user, you create a binding. A RoleBinding gives permissions inside one namespace by linking a user to a role. A ClusterRoleBinding gives access across the whole cluster.

When an engineer opens a container shell, the API server checks a specific subresource. Role rules name resources just like they appear in the API path. They write subresources with a slash, such as the pod log or exec path. A user needs permission on both the pod and the exec subresource to start.

Scoping namespace access and subresource rules

Platform teams usually limit container shell access to one namespace at a time. You can bind a ClusterRole with a RoleBinding inside a single namespace. This setup lets you reuse one role definition without giving cluster-wide access. The user only gets shell rights in the namespace where the RoleBinding lives.

Teams sometimes try to restrict access to one named pod in a role rule. Still, the resource names restriction for create actions only works on top-level resources. It does not apply to subresources in the same way. Because of this limit, scoping rules by namespace remains the standard native pattern.

During an incident, engineers often ask for quick shell access to check a container. If a team uses a ClusterRoleBinding by mistake, that user gains access everywhere. Keeping bindings scoped to a single namespace prevents wide access across the cluster.

K8s Pod Security Standards + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native setup solves and what it leaves open

Native roles and bindings do a good job of controlling who can enter. The API server checks the user identity and role rules before opening the stream. If the user lacks the exec subresource permission, the API server blocks the request. This stops unauthorized users from reaching containers in other namespaces.

Still, native role rules only check the initial request to open the shell. Once the stream starts, the role system does not inspect what runs inside. It cannot tell a safe read command from a destructive delete command. Any user with shell access can read local files or change running app state.

Native permissions also cannot rewrite sensitive data printed to the terminal screen. If an engineer prints a file with emails, phone numbers, or card numbers, it shows in plain text. The role system decides who logs in, not what commands do in flight.

Check Kubernetes API requests and responses on the wire

Kubernetes RBAC decides who may call the API server. RBAC does not check response payloads or split listing Secret names from reading Secret values when a role grants get access. With hoop.dev, you run an open-source HTTP proxy, a sidecar for your Kubernetes API. The kubectl client changes only the server address it talks to while RBAC stays in place.

On each call, hoop.dev guardrails read the method, normalized path, and headers to refuse bad deletes or full Secret reads with a 403 error. On the way back, hoop.dev data masking walks JSON responses by key and masks matched values in Secrets, ConfigMaps, and pod logs. Every request and verdict is logged without storing bearer tokens. View the project on GitHub.

Frequently asked questions

What permission is required to open a shell in a pod?

A user needs a Role or ClusterRole that allows the pod exec subresource. You must link that role to the user with a RoleBinding or ClusterRoleBinding.

Can I write a deny rule to block shell access in one namespace?

No, Kubernetes role permissions are purely additive and do not support deny rules. You must grant access per namespace using separate RoleBindings instead of one ClusterRoleBinding.

Does a RoleBinding grant access across the whole cluster?

No, a RoleBinding only grants permissions inside its own single namespace. Even if it links to a ClusterRole, the access stays inside that one namespace.

The query runs. The emails stay hidden.