api_access_via_pod
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 tohttps://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)
Run a temporary pod with shell access: We'll use a pod with common utilities (like
curl) to test API access. Thexxradar/hackonimage is used here; you can substitute any image that includescurl.Inside the
testpodshell, execute the following commands: These commands use the automatically mounted Service Account token and CA certificate.(The original
demopod name in the last curl was from thekubectl runcommand, now using$(hostname)which is more reliable from within the pod created bykubectl 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.
Create a custom Service Account:
Create a Role: This example creates a
Rolethat only allows reading pods in thedefaultnamespace.Create a RoleBinding: This binds the
pod-readerRole to thetokendemoService Account in thedefaultnamespace.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.)
Run a Pod using the Custom Service Account: Create a pod manifest that specifies
serviceAccountName: tokendemo.Apply it:
Test API Access from the
mydemoPod: Exec into themydemopod:Inside the
mydemopod'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:
Last updated