Check Pod Ports with Kubectl

Published
Length
3 min read

Luis Felipe Dias

All posts

You can check pod ports with kubectl by reading the pod spec from the API server. You can also run a network check inside the container to see open listening ports. Most teams grant engineers broad cluster roles to speed up daily network checks. When roles are too broad, users gain excess rights across namespaces and workloads. How to check pod ports with kubectl To check pod ports with kubectl, you take one of two main paths. First, you can query the Kubernetes API server for the pod object

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.

You can check pod ports with kubectl by reading the pod spec from the API server. You can also run a network check inside the container to see open listening ports. Most teams grant engineers broad cluster roles to speed up daily network checks. When roles are too broad, users gain excess rights across namespaces and workloads.

How to check pod ports with kubectl

To check pod ports with kubectl, you take one of two main paths. First, you can query the Kubernetes API server for the pod object details. The API server returns the container port list stored in the pod manifest. This list shows the ports that the author wrote into the pod spec. It does not prove that the app inside the container is listening right now.

Second, you can open an interactive shell or run a command inside the pod. This path uses the pod exec subresource on the Kubernetes API server. From inside the container, you can inspect the active network table of the pod. That check shows which ports the app binds to at that exact moment. Both paths depend on Kubernetes role-based access control, also called RBAC.

In Kubernetes RBAC, a Role sets rules within one single namespace. A ClusterRole sets rules across the entire cluster and all namespaces. A RoleBinding links a Role or ClusterRole to a user within one namespace. A ClusterRoleBinding grants those permissions across the whole cluster at once. RBAC rules are additive only, which means there are no deny rules.

What native RBAC solves and what it leaves open

Native Kubernetes RBAC controls who can read pod specs or use the exec subresource. You can grant a user read access to pods in a single namespace. That user can view declared container ports without changing any running workload. If a user needs to check active sockets, they need the pod exec subresource.

This is where native setup leaves a major operational gap for security teams. Kubernetes RBAC decides who can start an exec call against a pod. It does not inspect or filter the commands that run inside that session. Once a user has exec rights to check ports, they can run any command. They can read local files, inspect memory, or query internal network services.

K8s Pod Security Standards: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Also, reading pod specs or logs can return sensitive values stored in the cluster. Native RBAC does not rewrite emails, phone numbers, card numbers, or national IDs in results. Teams often over-grant ClusterRoleBindings so engineers can debug port issues across namespaces fast. Because RBAC has no deny rules, you cannot block risky commands inside an allowed session.

Scoping permissions for safer port checks

To reduce risk with native tools, start with a Role in one namespace. Grant read verbs on the pod resource so users can view declared ports. Avoid ClusterRoleBindings for routine port checks unless a user manages the whole cluster. Keep pod exec permissions separate from basic pod read permissions in your roles.

When engineers only need to check declared ports, pod read access is enough. Reserve the pod exec subresource for cases where declared ports do not match traffic. Even with tight roles, remember that RBAC stops at the API endpoint level. Controlling individual commands and sensitive output in flight requires controls beyond native RBAC.

Govern kubectl and API calls in flight

Identity providers and Kubernetes RBAC decide who logs into a cluster. They cannot redact keys inside returned JSON objects or hold a risky delete call for review. By design, hoop.dev sits inline as an open-source proxy, a sidecar for your Kubernetes API, without replacing RBAC. On every production call from an engineer or AI agent, hoop.dev decides to pass, mask, block, or hold it for human approval.

First, hoop.dev guardrails refuse banned methods, paths, or headers before they run or hold risky deletes for review. Next, hoop.dev data masking rewrites sensitive keys in returned Secret, ConfigMap, and log responses in memory, and writes each decision to the audit trail. Explore the repo on GitHub.

Frequently asked questions

Does reading a pod spec show all active listening ports?

No, reading a pod spec only shows the ports listed in the manifest. A container process can listen on an unlisted port or fail to bind at all.

Which Kubernetes permission lets a user check ports inside a container?

The user needs permission on the pod exec subresource in that namespace. You grant this through a RoleBinding or a ClusterRoleBinding in Kubernetes RBAC.

Can Kubernetes RBAC allow port checks while blocking other shell commands?

No, Kubernetes RBAC only checks if a user can access the exec subresource. It does not filter or block specific commands run inside the container session.

The query runs. The emails stay hidden.