You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
58
164
59
165
The modification of pods is implemented via a plugin
60
166
called an [Admission Controller](/docs/reference/access-authn-authz/admission-controllers/).
@@ -81,10 +187,18 @@ it does the following when a Pod is created:
81
187
1. If the spec of the incoming Pod does already contain any `imagePullSecrets`, then the
82
188
admission controller adds `imagePullSecrets`, copying them from the `ServiceAccount`.
83
189
84
-
### Bound service account token volume mechanism {#bound-service-account-token-volume}
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
+
88
202
The Kubernetes control plane (specifically, the ServiceAccount admission controller)
89
203
adds a projected volume to Pods, and the kubelet ensures that this volume contains a token
90
204
that lets containers authenticate as the right ServiceAccount.
@@ -101,7 +215,7 @@ Here's an example of how that looks for a launched Pod:
101
215
defaultMode: 420 # decimal equivalent of octal 0644
102
216
sources:
103
217
- serviceAccountToken:
104
-
expirationSeconds: 3597
218
+
expirationSeconds: 3607
105
219
path: token
106
220
- configMap:
107
221
items:
@@ -132,11 +246,16 @@ Any container within the Pod that mounts this volume can access the above inform
132
246
133
247
## Create additional API tokens {#create-token}
134
248
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
137
256
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.
140
259
141
260
Here is a sample manifest for such a Secret:
142
261
@@ -232,9 +351,6 @@ secrets:
232
351
- name: example-automated-thing-token-4rdrh
233
352
```
234
353
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
-
238
354
## Clean up
239
355
240
356
If you created a namespace `examplens` to experiment with, you can remove it:
0 commit comments