api_access_via_pod

Home

Accessing the Kubernetes API from Within a Pod

Pods running in a Kubernetes cluster can interact with the API server using their Service Account tokens. These tokens are automatically mounted into pods, allowing them to make authenticated API calls, provided their Service Account has the necessary RBAC permissions.

1. Overview of In-Pod API Access

When a pod needs to communicate with the Kubernetes API server:

  • It typically uses the in-cluster API server address: https://kubernetes.default.svc (which resolves to https://kubernetes).

  • Authentication is handled using a Service Account token.

  • This token is automatically mounted into each pod at /var/run/secrets/kubernetes.io/serviceaccount/token.

  • The CA certificate for verifying the API server's certificate is also mounted at /var/run/secrets/kubernetes.io/serviceaccount/ca.crt.

  • The pod's namespace is mounted at /var/run/secrets/kubernetes.io/serviceaccount/namespace.

The permissions granted to the pod depend on the Role or ClusterRole bound to its Service Account.

(The original "Via no auth ..." section with an IP address is less relevant for typical in-pod access, as https://kubernetes is the standard service discovery FQDN, and it generally requires authentication.)

2. Using the Default Service Account

Every namespace has a default Service Account. Pods that do not specify a serviceAccountName use this default SA.

2.1 Inspecting the Default Service Account (Admin Task - Optional)

As an administrator, you can inspect the default Service Account and its associated secrets (if any).

Note on Service Account Token Secrets (Kubernetes 1.24+):

  • Secrets containing service account tokens are no longer automatically generated for every service account since K8s 1.24.

  • The commands below showing kubectl describe secret <token-name> are for inspecting such secrets if they exist (e.g., in older K8s versions or if manually created). Pods themselves will always get their token mounted via the ProjectedVolume mechanism, regardless of whether a separate Secret object for that token exists.

2.2 Accessing the API from a Pod (using Default SA)

  1. Run a temporary pod with shell access: We'll use a pod with common utilities (like curl) to test API access. The xxradar/hackon image is used here; you can substitute any image that includes curl.

  2. Inside the testpod shell, execute the following commands: These commands use the automatically mounted Service Account token and CA certificate.

    (The original demo pod name in the last curl was from the kubectl run command, now using $(hostname) which is more reliable from within the pod created by kubectl run).

    Note on the Token: The Service Account token is a JSON Web Token (JWT). You can copy it and decode it using online JWT decoders to see its claims, such as the service account name and namespace.

    (The original "Try the sidecar thing ..." is too vague and has been omitted. Focus is on direct SA usage.)

3. Using a Custom Service Account with Specific Roles

For better security, create custom Service Accounts with narrowly defined permissions (Roles or ClusterRoles) specific to their tasks.

  1. Create a custom Service Account:

  2. Create a Role: This example creates a Role that only allows reading pods in the default namespace.

  3. Create a RoleBinding: This binds the pod-reader Role to the tokendemo Service Account in the default namespace.

  4. Inspect the Custom SA and its Token (Admin Task - Optional):(Similar to the default SA, inspecting the token secret directly is less relevant for K8s 1.24+ from the pod's perspective, as the token is always mounted.)

  5. Run a Pod using the Custom Service Account: Create a pod manifest that specifies serviceAccountName: tokendemo.

    Apply it:

  6. Test API Access from the mydemo Pod: Exec into the mydemo pod:

    Inside the mydemo pod's shell, run:

    Exit the pod's shell when done. Don't forget to clean up the pod and SA if it was just for testing:

Home

Last updated