@@ -5,10 +5,8 @@ weight: 50
5
5
---
6
6
<!--
7
7
reviewers:
8
- - bprashanth
9
- - davidopp
10
- - lavalamp
11
8
- liggitt
9
+ - enj
12
10
title: Managing Service Accounts
13
11
content_type: concept
14
12
weight: 50
@@ -241,6 +239,102 @@ to obtain short-lived API access tokens is recommended instead.
241
239
获得短期的 API 访问令牌。
242
240
{{< /note >}}
243
241
242
+ <!--
243
+ # # Auto-generated legacy ServiceAccount token clean up {#auto-generated-legacy-serviceaccount-token-clean-up}
244
+
245
+ Before version 1.24, Kubernetes automatically generated Secret-based tokens for
246
+ ServiceAccounts. To distinguish between automatically generated tokens and
247
+ manually created ones, Kubernetes checks for a reference from the
248
+ ServiceAccount's secrets field. If the Secret is referenced in the `secrets`
249
+ field, it is considered an auto-generated legacy token. Otherwise, it is
250
+ considered a manually created legacy token. For example :
251
+ -->
252
+ # # 清理自动生成的传统 ServiceAccount 令牌 {#auto-generated-legacy-serviceaccount-token-clean-up}
253
+
254
+ 在 1.24 版本之前,Kubernetes 自动为 ServiceAccount 生成基于 Secret 的令牌。
255
+ 为了区分自动生成的令牌和手动创建的令牌,Kubernetes 会检查 ServiceAccount 的
256
+ Secret 字段是否有引用。如果该 Secret 被 `secrets` 字段引用,
257
+ 它被视为自动生成的传统令牌。否则,它被视为手动创建的传统令牌。例如:
258
+
259
+ <!--
260
+ ` ` ` yaml
261
+ apiVersion: v1
262
+ kind: ServiceAccount
263
+ metadata:
264
+ name: build-robot
265
+ namespace: default
266
+ secrets:
267
+ - name: build-robot-secret # usually NOT present for a manually generated token
268
+ ` ` `
269
+ -->
270
+ ` ` ` yaml
271
+ apiVersion: v1
272
+ kind: ServiceAccount
273
+ metadata:
274
+ name: build-robot
275
+ namespace: default
276
+ secrets:
277
+ - name: build-robot-secret # 对于手动生成的令牌通常不会存在此字段
278
+ ` ` `
279
+
280
+ <!--
281
+ Beginning from version 1.29, legacy ServiceAccount tokens that were generated
282
+ automatically will be marked as invalid if they remain unused for a certain
283
+ period of time (set to default at one year). Tokens that continue to be unused
284
+ for this defined period (again, by default, one year) will subsequently be
285
+ purged by the control plane.
286
+ -->
287
+ 从 1.29 版本开始,如果传统 ServiceAccount
288
+ 令牌在一定时间段(默认设置为一年)内未被使用,则会被标记为无效。
289
+ 在定义的时间段(同样默认为一年)持续未被使用的令牌将由控制平面自动清除。
290
+
291
+ <!--
292
+ If users use an invalidated auto-generated token, the token validator will
293
+
294
+ 1. add an audit annotation for the key-value pair
295
+ `authentication.k8s.io/legacy-token-invalidated : <secret name>/<namespace>`,
296
+ 1. increment the `invalid_legacy_auto_token_uses_total` metric count,
297
+ 1. update the Secret label `kubernetes.io/legacy-token-last-used` with the new
298
+ date,
299
+ 1. return an error indicating that the token has been invalidated.
300
+ -->
301
+ 如果用户使用一个无效的自动生成的令牌,令牌验证器将执行以下操作:
302
+
303
+ 1. 为键值对 `authentication.k8s.io/legacy-token-invalidated : <secret name>/<namespace>`
304
+ 添加审计注解,
305
+ 1. `invalid_legacy_auto_token_uses_total` 指标计数加一,
306
+ 1. 更新 Secret 标签 `kubernetes.io/legacy-token-last-used` 为新日期,
307
+ 1. 返回一个提示令牌已经无效的报错。
308
+
309
+ <!--
310
+ When receiving this validation error, users can update the Secret to remove the
311
+ ` kubernetes.io/legacy-token-invalid-since` label to temporarily allow use of
312
+ this token.
313
+
314
+ Here's an example of an auto-generated legacy token that has been marked with the
315
+ ` kubernetes.io/legacy-token-last-used` and `kubernetes.io/legacy-token-invalid-since`
316
+ labels :
317
+ -->
318
+ 当收到这个校验报错时,用户可以通过移除 `kubernetes.io/legacy-token-invalid-since`
319
+ 标签更新 Secret,以临时允许使用此令牌。
320
+
321
+ 以下是一个自动生成的传统令牌示例,它被标记了 `kubernetes.io/legacy-token-last-used`
322
+ 和 `kubernetes.io/legacy-token-invalid-since` 标签:
323
+
324
+ ` ` ` yaml
325
+ apiVersion: v1
326
+ kind: Secret
327
+ metadata:
328
+ name: build-robot-secret
329
+ namespace: default
330
+ labels:
331
+ kubernetes.io/legacy-token-last-used: 2022-10-24
332
+ kubernetes.io/legacy-token-invalid-since: 2023-10-25
333
+ annotations:
334
+ kubernetes.io/service-account.name: build-robot
335
+ type: kubernetes.io/service-account-token
336
+ ` ` `
337
+
244
338
<!--
245
339
# # Control plane details
246
340
@@ -346,6 +440,88 @@ it does the following when a Pod is created:
346
440
4. 如果新来 Pod 的规约不包含任何 `imagePullSecrets`,则准入控制器添加 `imagePullSecrets`,
347
441
并从 `ServiceAccount` 进行复制。
348
442
443
+ <!--
444
+ # ## Legacy ServiceAccount token tracking controller
445
+ -->
446
+ # ## 传统 ServiceAccount 令牌追踪控制器
447
+
448
+ {{< feature-state for_k8s_version="v1.28" state="stable" >}}
449
+
450
+ <!--
451
+ This controller generates a ConfigMap called
452
+ ` kube-system/kube-apiserver-legacy-service-account-token-tracking` in the
453
+ ` kube-system` namespace. The ConfigMap records the timestamp when legacy service
454
+ account tokens began to be monitored by the system.
455
+ -->
456
+ 此控制器在 `kube-system` 命名空间中生成名为
457
+ ` kube-apiserver-legacy-service-account-token-tracking` 的 ConfigMap。
458
+ 这个 ConfigMap 记录了系统开始监视传统服务账户令牌的时间戳。
459
+
460
+ <!--
461
+ # ## Legacy ServiceAccount token cleaner
462
+ -->
463
+ # ## 传统 ServiceAccount 令牌清理器
464
+
465
+ {{< feature-state for_k8s_version="v1.29" state="beta" >}}
466
+
467
+ <!--
468
+ The legacy ServiceAccount token cleaner runs as part of the
469
+ ` kube-controller-manager` and checks every 24 hours to see if any auto-generated
470
+ legacy ServiceAccount token has not been used in a *specified amount of time*.
471
+ If so, the cleaner marks those tokens as invalid.
472
+
473
+ The cleaner works by first checking the ConfigMap created by the control plane
474
+ (provided that `LegacyServiceAccountTokenTracking` is enabled). If the current
475
+ time is a *specified amount of time* after the date in the ConfigMap, the
476
+ cleaner then loops through the list of Secrets in the cluster and evaluates each
477
+ Secret that has the type `kubernetes.io/service-account-token`.
478
+ -->
479
+ 传统 ServiceAccount 令牌清理器作为 `kube-controller-manager` 的一部分运行,
480
+ 每 24 小时检查一次,查看是否有任何自动生成的传统 ServiceAccount
481
+ 令牌在**特定时间段**内未被使用。如果有的话,清理器会将这些令牌标记为无效。
482
+
483
+ 清理器的工作方式是首先检查控制平面创建的 ConfigMap(前提是启用了
484
+ ` LegacyServiceAccountTokenTracking` )。如果当前时间是 ConfigMap
485
+ 所包含日期之后的**特定时间段**,清理器会遍历集群中的 Secret 列表,
486
+ 并评估每个类型为 `kubernetes.io/service-account-token` 的 Secret。
487
+
488
+ <!--
489
+ If a Secret meets all of the following conditions, the cleaner marks it as
490
+ invalid :
491
+
492
+ - The Secret is auto-generated, meaning that it is bi-directionally referenced
493
+ by a ServiceAccount.
494
+ - The Secret is not currently mounted by any pods.
495
+ - The Secret has not been used in a *specified amount of time* since it was
496
+ created or since it was last used.
497
+ -->
498
+ 如果一个 Secret 满足以下所有条件,清理器会将其标记为无效:
499
+
500
+ - Secret 是自动生成的,意味着它被 ServiceAccount 双向引用。
501
+ - Secret 当前没有被任何 Pod 挂载。
502
+ - Secret 自从创建或上次使用以来的**特定时间段**未被使用过。
503
+
504
+ <!--
505
+ The cleaner marks a Secret invalid by adding a label called
506
+ ` kubernetes.io/legacy-token-invalid-since` to the Secret, with the current date
507
+ as the value. If an invalid Secret is not used in a *specified amount of time*,
508
+ the cleaner will delete it.
509
+ -->
510
+ 清理器通过向 Secret 添加名为 `kubernetes.io/legacy-token-invalid-since` 的标签,
511
+ 并将此值设置为当前日期,来标记 Secret 为无效。
512
+ 如果一个无效的 Secret 在**特定时间段**内未被使用,清理器将会删除它。
513
+
514
+ {{< note >}}
515
+ <!--
516
+ All the *specified amount of time* above defaults to one year. The cluster
517
+ administrator can configure this value through the
518
+ ` --legacy-service-account-token-clean-up-period` command line argument for the
519
+ ` kube-controller-manager` component.
520
+ -->
521
+ 上述所有的**特定时间段**都默认为一年。集群管理员可以通过 `kube-controller-manager`
522
+ 组件的 `--legacy-service-account-token-clean-up-period` 命令行参数来配置此值。
523
+ {{< /note >}}
524
+
349
525
# ## TokenRequest API
350
526
351
527
{{< feature-state for_k8s_version="v1.22" state="stable" >}}
@@ -514,6 +690,17 @@ service-account-token Secret that you just created.
514
690
如果你在 `examplens` 名字空间中启动一个新的 Pod,它可以使用你刚刚创建的
515
691
`myserviceaccount` service-account-token Secret。
516
692
693
+ {{< caution >}}
694
+ <!--
695
+ Do not reference manually created Secrets in the `secrets` field of a
696
+ ServiceAccount. Or the manually created Secrets will be cleaned if it is not used for a long
697
+ time. Please refer to [auto-generated legacy ServiceAccount token clean up](#auto-generated-legacy-serviceaccount-token-clean-up).
698
+ -->
699
+ 不要在 ServiceAccount 的 `secrets` 字段中引用手动创建的 Secret。
700
+ 否则,如果这些手动创建的 Secret 长时间未被使用将会被清理掉。
701
+ 请参考[清理自动生成的传统 ServiceAccount 令牌](#auto-generated-legacy-serviceaccount-token-clean-up)。
702
+ {{< /caution >}}
703
+
517
704
<!--
518
705
## Delete/invalidate a ServiceAccount token {#delete-token}
519
706
0 commit comments