Skip to content

Commit a4629cd

Browse files
author
Tim Bannister
committed
Update ServiceAccount tasks in light of TokenRequest
Now that TokenRequest is the default way to get a service account token for a Pod, update the task pages that relate to this.
1 parent f9db6ae commit a4629cd

File tree

2 files changed

+172
-23
lines changed

2 files changed

+172
-23
lines changed

content/en/docs/reference/access-authn-authz/service-accounts-admin.md

Lines changed: 138 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ authenticate to the cluster's API server.
1919
For an introduction to service accounts, read [configure service accounts](/docs/tasks/configure-pod-container/configure-service-account/).
2020

2121
This task guide explains some of the concepts behind ServiceAccounts. The
22-
guide also explains how to add and remove tokens from ServiceAccounts.
22+
guide also explains how to obtain or revoke tokens that represent
23+
ServiceAccounts.
2324

2425
<!-- body -->
2526

@@ -40,21 +41,126 @@ kubectl create namespace examplens
4041
Kubernetes distinguishes between the concept of a user account and a service account
4142
for a number of reasons:
4243

43-
- User accounts are for humans. Service accounts are for processes, which run
44-
in pods.
45-
- User accounts are intended to be global. Names must be unique across all
46-
namespaces of a cluster. In Kubernetes, service accounts are namespaced.
47-
- Typically, a cluster's user accounts might be synced from a corporate
44+
- User accounts are for humans. Service accounts are for application processes,
45+
which (for Kubernetes) run in containers that are part of pods.
46+
- User accounts are intended to be global: names must be unique across all
47+
namespaces of a cluster. No matter what namespace you look at, a particular
48+
username that represents a user represents the same user.
49+
In Kubernetes, service accounts are namespaced: two different namespaces can
50+
contain ServiceAccounts that have identical names.
51+
- Typically, a cluster's user accounts might be synchronised from a corporate
4852
database, where new user account creation requires special privileges and is
49-
tied to complex business processes. Service account creation is intended to be
50-
more lightweight, allowing cluster users to create service accounts for
51-
specific tasks by following the principle of least privilege.
52-
- Auditing considerations for humans and service accounts may differ.
53-
- A config bundle for a complex system may include definition of various service
53+
tied to complex business processes. By contrast, service account creation is
54+
intended to be more lightweight, allowing cluster users to create service accounts
55+
for specific tasks on demand. Separating ServiceAccount creation from the steps to
56+
onboard human users makes it easier for workloads to following the principle of
57+
least privilege.
58+
- Auditing considerations for humans and service accounts may differ; the separation
59+
makes that easier to achieve.
60+
- A configuration bundle for a complex system may include definition of various service
5461
accounts for components of that system. Because service accounts can be created
55-
without many constraints and have namespaced names, such config is portable.
62+
without many constraints and have namespaced names, such configuration is
63+
usually portable.
5664

57-
## ServiceAccount admission controller
65+
## Bound service account token volume mechanism {#bound-service-account-token-volume}
66+
67+
{{< feature-state for_k8s_version="v1.22" state="stable" >}}
68+
69+
By default, the Kubernetes control plane (specifically, the
70+
[ServiceAccount admission controller](#service-account-admission-controller))
71+
adds a [projected volume](/docs/concepts/storage/projected-volumes/) to Pods,
72+
and this volume includes a token for Kubernetes API access.
73+
74+
Here's an example of how that looks for a launched Pod:
75+
76+
```yaml
77+
...
78+
- name: kube-api-access-<random-suffix>
79+
projected:
80+
sources:
81+
- serviceAccountToken:
82+
path: token # must match the path the app expects
83+
- configMap:
84+
items:
85+
- key: ca.crt
86+
path: ca.crt
87+
name: kube-root-ca.crt
88+
- downwardAPI:
89+
items:
90+
- fieldRef:
91+
apiVersion: v1
92+
fieldPath: metadata.namespace
93+
path: namespace
94+
```
95+
96+
That manifest snippet defines a projected volume that consists of three sources. In this case,
97+
each source also represents a single path within that volume. The three sources are:
98+
99+
1. A `serviceAccountToken` source, that contains a token that the kubelet acquires from kube-apiserver
100+
The kubelet fetches time-bound tokens using the TokenRequest API. A token served for a TokenRequest expires
101+
either when the pod is deleted or after a defined lifespan (by default, that is 1 hour).
102+
The token is bound to the specific Pod and has the kube-apiserver as its audience.
103+
This mechanism superseded an earlier mechanism that added a volume based on a Secret,
104+
where the Secret represented the ServiceAccount for the Pod, but did not expire.
105+
1. A `configMap` source. The ConfigMap contains a bundle of certificate authority data. Pods can use these
106+
certificates to make sure that they are connecting to your cluster's kube-apiserver (and not to middlebox
107+
or an accidentally misconfigured peer).
108+
1. A `downwardAPI` source that looks up the name of thhe namespace containing the Pod, and makes
109+
that name information available to application code running inside the Pod.
110+
111+
Any container within the Pod that mounts this particular volume can access the above information.
112+
113+
{{< note >}}
114+
There is no specific mechanism to invalidate a token issued via TokenRequest. If you no longer
115+
trust a bound service account token for a Pod, you can delete that Pod. Deleting a Pod expires
116+
its bound service account tokens.
117+
{{< /note >}}
118+
119+
## Manual Secret management for ServiceAccounts
120+
121+
Versions of Kubernetes before v1.22 automatically created credentials for accessing
122+
the Kubernetes API. This older mechanism was based on creating token Secrets that
123+
could then be mounted into running Pods.
124+
125+
In more recent versions, including Kubernetes v{{< skew currentVersion >}}, API credentials
126+
are [obtained directly](#bound-service-account-token-volume) using the
127+
[TokenRequest](/docs/reference/kubernetes-api/authentication-resources/token-request-v1/) API,
128+
and are mounted into Pods using a projected volume.
129+
The tokens obtained using this method have bounded lifetimes, and are automatically
130+
invalidated when the Pod they are mounted into is deleted.
131+
132+
You can still [manually create](/docs/tasks/configure-pod-container/configure-service-account/#manually-create-an-api-token-for-a-serviceaccount) a Secret to hold a service account token; for example, if you need a token that never expires.
133+
134+
Once you manually create a Secret and link it to a ServiceAccount, the Kubernetes control plane automatically populates the token into that Secret.
135+
136+
{{< note >}}
137+
Although the manual mechanism for creating a long-lived ServiceAccount token exists,
138+
using [TokenRequest](/docs/reference/kubernetes-api/authentication-resources/token-request-v1/)
139+
to obtain short-lived API access tokens is recommended instead.
140+
{{< /note >}}
141+
142+
## Control plane details
143+
144+
### Token controller
145+
146+
The service account token controller runs as part of `kube-controller-manager`.
147+
This controller acts asynchronously. It:
148+
149+
- watches for ServiceAccount deletion and deletes all corresponding ServiceAccount
150+
token Secrets.
151+
- watches for ServiceAccount token Secret addition, and ensures the referenced
152+
ServiceAccount exists, and adds a token to the Secret if needed.
153+
- watches for Secret deletion and removes a reference from the corresponding
154+
ServiceAccount if needed.
155+
156+
You must pass a service account private key file to the token controller in
157+
the `kube-controller-manager` using the `--service-account-private-key-file`
158+
flag. The private key is used to sign generated service account tokens.
159+
Similarly, you must pass the corresponding public key to the `kube-apiserver`
160+
using the `--service-account-key-file` flag. The public key will be used to
161+
verify the tokens during authentication.
162+
163+
### ServiceAccount admission controller
58164

59165
The modification of pods is implemented via a plugin
60166
called an [Admission Controller](/docs/reference/access-authn-authz/admission-controllers/).
@@ -81,10 +187,18 @@ it does the following when a Pod is created:
81187
1. If the spec of the incoming Pod does already contain any `imagePullSecrets`, then the
82188
admission controller adds `imagePullSecrets`, copying them from the `ServiceAccount`.
83189

84-
### Bound service account token volume mechanism {#bound-service-account-token-volume}
190+
### TokenRequest API
85191

86192
{{< feature-state for_k8s_version="v1.22" state="stable" >}}
87193

194+
You use the [TokenRequest](/docs/reference/kubernetes-api/authentication-resources/token-request-v1/)
195+
subresource of a ServiceAccount to obtain a time-bound token for that ServiceAccount.
196+
You don't need to call this to obtain an API token for use within a container, since
197+
the kubelet sets this up for you using a _projected volume_.
198+
199+
If you want to use the TokenRequest API from `kubectl`, see
200+
[Manually create an API token for a ServiceAccount](/docs/tasks/configure-pod-container/configure-service-account/#manually-create-an-api-token-for-a-serviceaccount).
201+
88202
The Kubernetes control plane (specifically, the ServiceAccount admission controller)
89203
adds a projected volume to Pods, and the kubelet ensures that this volume contains a token
90204
that lets containers authenticate as the right ServiceAccount.
@@ -101,7 +215,7 @@ Here's an example of how that looks for a launched Pod:
101215
defaultMode: 420 # decimal equivalent of octal 0644
102216
sources:
103217
- serviceAccountToken:
104-
expirationSeconds: 3597
218+
expirationSeconds: 3607
105219
path: token
106220
- configMap:
107221
items:
@@ -132,11 +246,16 @@ Any container within the Pod that mounts this volume can access the above inform
132246

133247
## Create additional API tokens {#create-token}
134248

135-
The control plane ensures that a Secret with an API token exists for each
136-
ServiceAccount. To create additional API tokens for a ServiceAccount, create a
249+
{{< caution >}}
250+
Only create long-lived API tokens if the [token request](#tokenrequest-api) mechanism
251+
is not suitable. The token request mechanism provides time-limited tokens; because these
252+
expire, they represent a lower risk to information security.
253+
{{< /caution >}}
254+
255+
To create a non-expiring, persisted API token for a ServiceAccount, create a
137256
Secret of type `kubernetes.io/service-account-token` with an annotation
138-
referencing the ServiceAccount, and the control plane will update that Secret with a
139-
generated token.
257+
referencing the ServiceAccount. The control plane then generates a long-lived token and
258+
updates that Secret with that generated token data.
140259

141260
Here is a sample manifest for such a Secret:
142261

@@ -232,9 +351,6 @@ secrets:
232351
- name: example-automated-thing-token-4rdrh
233352
```
234353

235-
You can see that there is now a new associated Secret with a different name. The
236-
old Secret is no longer valid.
237-
238354
## Clean up
239355

240356
If you created a namespace `examplens` to experiment with, you can remove it:

content/en/docs/tasks/configure-pod-container/configure-service-account.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,38 @@ kubectl delete serviceaccount/build-robot
169169

170170
## Manually create an API token for a ServiceAccount
171171

172+
Suppose you have an existing service account named "build-robot" as mentioned earlier.
173+
174+
You can get a time-limited API token for that ServiceAccount using `kubectl`:
175+
176+
```shell
177+
kubectl create token admin-user
178+
```
179+
180+
The output from that command is a token that you can use to authenticate as that
181+
ServiceAccount. You can request a specific token duration using the `--duration`
182+
command line argument to `kubectl create token` (the actual duration of the issued
183+
token might be shorter, or could even be longer).
184+
185+
{{< note >}}
186+
Versions of Kubernetes before v1.22 automatically created long term credentials for
187+
accessing the Kubernetes API. This older mechanism was based on creating token Secrets
188+
that could then be mounted into running Pods.
189+
In more recent versions, including Kubernetes v{{< skew currentVersion >}}, API credentials
190+
are obtained directly by using the [TokenRequest](/docs/reference/kubernetes-api/authentication-resources/token-request-v1/) API,
191+
and are mounted into Pods using a [projected volume](/docs/reference/access-authn-authz/service-accounts-admin/#bound-service-account-token-volume).
192+
The tokens obtained using this method have bounded lifetimes, and are automatically
193+
invalidated when the Pod they are mounted into is deleted.
194+
195+
You can still manually create a service account token Secret; for example, if you need a token that never expires.
196+
However, using the [TokenRequest](/docs/reference/kubernetes-api/authentication-resources/token-request-v1/)
197+
subresource to obtain a token to access the API is recommended instead.
198+
{{< /note >}}
199+
200+
### Manually create a long-lived API token for a ServiceAccount
201+
172202
If you want to obtain an API token for a ServiceAccount, you create a new Secret
173-
with a special annotation, `kubernetes.io/service-account.name`
203+
with a special annotation, `kubernetes.io/service-account.name`.
174204

175205
```shell
176206
kubectl apply -f - <<EOF
@@ -225,6 +255,9 @@ Secret somewhere that your terminal / computer screen could be seen by an
225255
onlooker.
226256
{{< /note >}}
227257

258+
When you delete a ServiceAccount that has an associated Secret, the Kubernetes
259+
control plane automatically cleans up the long-lived token from that Secret.
260+
228261
## Add ImagePullSecrets to a service account
229262

230263
First, [create an imagePullSecret](/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod).

0 commit comments

Comments
 (0)