Skip to content

Commit c3607e8

Browse files
authored
Merge pull request #32141 from tengqm/zh-securitycontext
[zh] Resync security context page
2 parents 82b7401 + 1d0febb commit c3607e8

File tree

1 file changed

+88
-66
lines changed

1 file changed

+88
-66
lines changed

content/zh/docs/tasks/configure-pod-container/security-context.md

Lines changed: 88 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 为 Pod 或容器配置安全性上下文
2+
title: 为 Pod 或容器配置安全上下文
33
content_type: task
44
weight: 80
55
---
@@ -21,38 +21,49 @@ a Pod or Container. Security context settings include, but are not limited to:
2121
2222
* Discretionary Access Control: Permission to access an object, like a file, is based on
2323
[user ID (UID) and group ID (GID)](https://wiki.archlinux.org/index.php/users_and_groups).
24-
* [Security Enhanced Linux (SELinux)](https://en.wikipedia.org/wiki/Security-Enhanced_Linux): Objects are assigned security labels.
24+
* [Security Enhanced Linux (SELinux)](https://en.wikipedia.org/wiki/Security-Enhanced_Linux):
25+
Objects are assigned security labels.
2526
* Running as privileged or unprivileged.
2627
* [Linux Capabilities](https://linux-audit.com/linux-capabilities-hardening-linux-binaries-by-removing-setuid/):
2728
Give a process some privileges, but not all the privileges of the root user.
2829
-->
2930
安全上下文(Security Context)定义 Pod 或 Container 的特权与访问控制设置。
3031
安全上下文包括但不限于:
3132

32-
* 自主访问控制(Discretionary Access Control):基于
33-
[用户 ID(UID)和组 ID(GID)](https://wiki.archlinux.org/index.php/users_and_groups).
33+
* 自主访问控制(Discretionary Access Control):
34+
基于[用户 ID(UID)和组 ID(GID)](https://wiki.archlinux.org/index.php/users_and_groups)
3435
来判定对对象(例如文件)的访问权限。
3536
* [安全性增强的 Linux(SELinux)](https://zh.wikipedia.org/wiki/%E5%AE%89%E5%85%A8%E5%A2%9E%E5%BC%BA%E5%BC%8FLinux)
3637
为对象赋予安全性标签。
3738
* 以特权模式或者非特权模式运行。
3839
* [Linux 权能](https://linux-audit.com/linux-capabilities-hardening-linux-binaries-by-removing-setuid/):
3940
为进程赋予 root 用户的部分特权而非全部特权。
4041
<!--
41-
* [AppArmor](/docs/tutorials/clusters/apparmor/): Use program profiles to restrict the capabilities of individual programs.
42-
* [Seccomp](/docs/tutorials/clusters/seccomp/): Filter a process's system calls.
43-
* AllowPrivilegeEscalation: Controls whether a process can gain more
44-
privileges than its parent process. This bool directly controls whether the
42+
* [AppArmor](/docs/tutorials/security/apparmor/):
43+
Use program profiles to restrict the capabilities of individual programs.
44+
* [Seccomp](/docs/tutorials/security/seccomp/): Filter a process's system calls.
45+
* `allowPrivilegeEscalation`: Controls whether a process can gain more privileges than
46+
its parent process. This bool directly controls whether the
4547
[`no_new_privs`](https://www.kernel.org/doc/Documentation/prctl/no_new_privs.txt)
46-
flag gets set on the container process. AllowPrivilegeEscalation is true
47-
always when the container is: 1) run as Privileged OR 2) has `CAP_SYS_ADMIN`.
48+
flag gets set on the container process.
49+
`allowPrivilegeEscalation` is always true
50+
when the container:
51+
52+
- is run as privileged, or
53+
- has `CAP_SYS_ADMIN`
54+
4855
* readOnlyRootFilesystem: Mounts the container's root filesystem as read-only.
4956
-->
50-
* [AppArmor](/zh/docs/tutorials/clusters/apparmor/)使用程序框架来限制个别程序的权能
51-
* [Seccomp](/zh/docs/tutorials/clusters/seccomp/):过滤进程的系统调用。
52-
* AllowPrivilegeEscalation:控制进程是否可以获得超出其父进程的特权。
57+
* [AppArmor](/zh/docs/tutorials/security/apparmor/)使用程序配置来限制个别程序的权能
58+
* [Seccomp](/zh/docs/tutorials/security/seccomp/):过滤进程的系统调用。
59+
* `allowPrivilegeEscalation`:控制进程是否可以获得超出其父进程的特权。
5360
此布尔值直接控制是否为容器进程设置
5461
[`no_new_privs`](https://www.kernel.org/doc/Documentation/prctl/no_new_privs.txt)标志。
55-
当容器以特权模式运行或者具有 `CAP_SYS_ADMIN` 权能时,AllowPrivilegeEscalation 总是为 true。
62+
当容器满足一下条件之一时,`allowPrivilegeEscalation` 总是为 true:
63+
64+
- 以特权模式运行,或者
65+
- 具有 `CAP_SYS_ADMIN` 权能
66+
5667
* readOnlyRootFilesystem:以只读方式加载容器的根文件系统。
5768

5869
<!--
@@ -213,19 +224,24 @@ Run the following command:
213224
id
214225
```
215226

216-
输出为:
217-
```
227+
<!--
228+
The output is similar to this:
229+
-->
230+
输出类似于:
231+
232+
```none
218233
uid=1000 gid=3000 groups=2000
219234
```
220235

221236
<!--
222-
You will see that gid is 3000 which is same as `runAsGroup` field. If the `runAsGroup` was omitted the gid would
223-
remain as 0(root) and the process will be able to interact with files that are owned by root(0) group and that have
224-
the required group permissions for root(0) group.
237+
From the output, you can see that `gid` is 3000 which is same as the `runAsGroup` field.
238+
If the `runAsGroup` was omitted, the `gid` would remain as 0 (root) and the process will
239+
be able to interact with files that are owned by the root(0) group and groups that have
240+
the required group permissions for the root (0) group.
225241
226242
Exit your shell:
227243
-->
228-
你会看到 `gid` 值为 3000,也就是 `runAsGroup` 字段的值。
244+
从输出中你会看到 `gid` 值为 3000,也就是 `runAsGroup` 字段的值。
229245
如果 `runAsGroup` 被忽略,则 `gid` 会取值 0(root),而进程就能够与 root
230246
用户组所拥有以及要求 root 用户组访问权限的文件交互。
231247

@@ -251,18 +267,21 @@ slowing Pod startup. You can use the `fsGroupChangePolicy` field inside a `secur
251267
to control the way that Kubernetes checks and manages ownership and permissions
252268
for a volume.
253269
-->
254-
默认情况下,Kubernetes 在挂载一个卷时,会递归地更改每个卷中的内容的属主和访问权限,使之与 Pod
255-
`securityContext` 中指定的 `fsGroup` 匹配。
270+
默认情况下,Kubernetes 在挂载一个卷时,会递归地更改每个卷中的内容的属主和访问权限,
271+
使之与 Pod `securityContext` 中指定的 `fsGroup` 匹配。
256272
对于较大的数据卷,检查和变更属主与访问权限可能会花费很长时间,降低 Pod 启动速度。
257273
你可以在 `securityContext` 中使用 `fsGroupChangePolicy` 字段来控制 Kubernetes
258274
检查和管理卷属主和访问权限的方式。
259275

260276
<!--
261-
**fsGroupChangePolicy** - `fsGroupChangePolicy` defines behavior for changing ownership and permission of the volume
262-
before being exposed inside a Pod. This field only applies to volume types that support
263-
`fsGroup` controlled ownership and permissions. This field has two possible values:
277+
**fsGroupChangePolicy** - `fsGroupChangePolicy` defines behavior for changing ownership
278+
and permission of the volume before being exposed inside a Pod.
279+
This field only applies to volume types that support `fsGroup` controlled ownership and permissions.
280+
This field has two possible values:
264281
265-
* _OnRootMismatch_: Only change permissions and ownership if permission and ownership of root directory does not match with expected permissions of the volume. This could help shorten the time it takes to change ownership and permission of a volume.
282+
* _OnRootMismatch_: Only change permissions and ownership if permission and ownership of
283+
root directory does not match with expected permissions of the volume.
284+
This could help shorten the time it takes to change ownership and permission of a volume.
266285
* _Always_: Always change permission and ownership of the volume when volume is mounted.
267286
268287
For example:
@@ -293,7 +312,7 @@ This field has no effect on ephemeral volume types such as
293312
and [`emptydir`](/docs/concepts/storage/volumes/#emptydir).
294313
-->
295314
{{< note >}}
296-
此字段对于[`secret`](/zh/docs/concepts/storage/volumes/#secret)、
315+
此字段对于 [`secret`](/zh/docs/concepts/storage/volumes/#secret)、
297316
[`configMap`](/zh/docs/concepts/storage/volumes/#configmap)
298317
和 [`emptydir`](/zh/docs/concepts/storage/volumes/#emptydir)
299318
这类临时性存储无效。
@@ -316,23 +335,24 @@ ownership and permission change, `fsGroupChangePolicy` does not take effect, and
316335
as specified by CSI, the driver is expected to mount the volume with the
317336
provided `fsGroup`, resulting in a volume that is readable/writable by the
318337
`fsGroup`.
338+
-->
339+
如果你部署了一个[容器存储接口 (CSI)](https://github.com/container-storage-interface/spec/blob/master/spec.md)
340+
驱动,而该驱动支持 `VOLUME_MOUNT_GROUP` `NodeServiceCapability`,
341+
在 `securityContext` 中指定 `fsGroup` 来设置文件所有权和权限的过程将由 CSI
342+
驱动而不是 Kubernetes 来执行,前提是 Kubernetes 的 `DelegateFSGroupToCSIDriver`
343+
特性门控已启用。在这种情况下,由于 Kubernetes 不执行任何所有权和权限更改,
344+
`fsGroupChangePolicy` 不会生效,并且按照 CSI 的规定,CSI 驱动应该使用所指定的
345+
`fsGroup` 来挂载卷,从而生成了一个对 `fsGroup` 可读/可写的卷.
319346

347+
<!--
320348
Please refer to the [KEP](https://github.com/gnufied/enhancements/blob/master/keps/sig-storage/2317-fsgroup-on-mount/README.md)
321349
and the description of the `VolumeCapability.MountVolume.volume_mount_group`
322350
field in the [CSI spec](https://github.com/container-storage-interface/spec/blob/master/spec.md#createvolume)
323351
for more information.
324352
-->
325-
如果你部署了一个[容器存储接口 (CSI)](https://github.com/container-storage-interface/spec/blob/master/spec.md)
326-
驱动支持 `VOLUME_MOUNT_GROUP` `NodeServiceCapability`,
327-
在 `securityContext` 中指定 `fsGroup` 来设置文件所有权和权限的过程将由 CSI 驱动
328-
而不是 Kubernetes 来执行,前提是 Kubernetes 的 `DelegateFSGroupToCSIDriver`
329-
特性门控已启用。在这种情况下,由于 Kubernetes 不执行任何
330-
所有权和权限更改,`fsGroupChangePolicy` 不会生效,并且
331-
按照 CSI 的规定,CSI 驱动应该使用所指定的 `fsGroup` 来挂载卷,从而生成了一个对 `fsGroup` 可读/可写的卷.
332-
333353
更多的信息请参考 [KEP](https://github.com/gnufied/enhancements/blob/master/keps/sig-storage/2317-fsgroup-on-mount/README.md)
334-
和 [CSI 规范](https://github.com/container-storage-interface/spec/blob/master/spec.md#createvolume) 中的字
335-
`VolumeCapability.MountVolume.volume_mount_group` 的描述
354+
和 [CSI 规范](https://github.com/container-storage-interface/spec/blob/master/spec.md#createvolume)
355+
中的字段 `VolumeCapability.MountVolume.volume_mount_group` 的描述。
336356

337357
<!--
338358
## Set the security context for a Container
@@ -352,9 +372,8 @@ and the Container have a `securityContext` field:
352372
若要为 Container 设置安全性配置,可以在 Container 清单中包含 `securityContext`
353373
字段。`securityContext` 字段的取值是一个
354374
[SecurityContext](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#securitycontext-v1-core)
355-
对象。你为 Container 设置的安全性配置仅适用于该容器本身,并且所指定的设置
356-
在与 Pod 层面设置的内容发生重叠时,会重载后者。Container 层面的设置不会影响
357-
到 Pod 的卷。
375+
对象。你为 Container 设置的安全性配置仅适用于该容器本身,并且所指定的设置在与
376+
Pod 层面设置的内容发生重叠时,会重载后者。Container 层面的设置不会影响到 Pod 的卷。
358377

359378
下面是一个 Pod 的配置文件,其中包含一个 Container。Pod 和 Container 都有
360379
`securityContext` 字段:
@@ -402,7 +421,7 @@ The output shows that the processes are running as user 2000. This is the value
402421
of `runAsUser` specified for the Container. It overrides the value 1000 that is
403422
specified for the Pod.
404423
-->
405-
输出显示进程以用户 2000 账号运行。该值是在 Container 的 `runAsUser` 中设置的。
424+
输出显示进程以用户 2000 运行。该值是在 Container 的 `runAsUser` 中设置的。
406425
该设置值重载了 Pod 层面所设置的值 1000。
407426

408427
```
@@ -434,12 +453,12 @@ Here is configuration file that does not add or remove any Container capabilitie
434453
-->
435454
## 为 Container 设置权能 {#set-capabilities-for-a-container}
436455

437-
使用 [Linux 权能](https://man7.org/linux/man-pages/man7/capabilities.7.html)你可以
438-
赋予进程 root 用户所拥有的某些特权,但不必赋予其全部特权。
439-
要为 Container 添加或移除 Linux 权能,可以在 Container 清单的 `securityContext`
440-
包含 `capabilities` 字段。
456+
使用 [Linux 权能](https://man7.org/linux/man-pages/man7/capabilities.7.html)
457+
你可以赋予进程 root 用户所拥有的某些特权,但不必赋予其全部特权。
458+
要为 Container 添加或移除 Linux 权能,可以在 Container 清单的 `securityContext`
459+
节包含 `capabilities` 字段。
441460

442-
首先,查看不包含 `capabilities` 字段时候会发生什么。
461+
首先,看一下不包含 `capabilities` 字段时候会发生什么。
443462
下面是一个配置文件,其中没有添加或移除容器的权能:
444463

445464
{{< codenew file="pods/security/security-context-3.yaml" >}}
@@ -598,12 +617,15 @@ for definitions of the capability constants.
598617
了解权能常数的定义。
599618

600619
<!--
601-
Linux capability constants have the form `CAP_XXX`. But when you list capabilities in your Container manifest, you must omit the `CAP_` portion of the constant. For example, to add `CAP_SYS_TIME`, include `SYS_TIME` in your list of capabilities.
620+
Linux capability constants have the form `CAP_XXX`.
621+
But when you list capabilities in your Container manifest, you must
622+
omit the `CAP_` portion of the constant.
623+
For example, to add `CAP_SYS_TIME`, include `SYS_TIME` in your list of capabilities.
602624
-->
603625
{{< note >}}
604626
Linux 权能常数定义的形式为 `CAP_XXX`。但是你在 Container 清单中列举权能时,
605-
要将权能名称中的 `CAP_` 部分去掉。例如,要添加 `CAP_SYS_TIME`可在权能
606-
列表中添加 `SYS_TIME`
627+
要将权能名称中的 `CAP_` 部分去掉。例如,要添加 `CAP_SYS_TIME`
628+
可在权能列表中添加 `SYS_TIME`
607629
{{< /note >}}
608630

609631
<!--
@@ -623,18 +645,18 @@ flag).
623645
Here is an example that sets the Seccomp profile to the node's container runtime
624646
default profile:
625647
-->
626-
## 为容器设置 Seccomp 样板
648+
## 为容器设置 Seccomp 配置
627649

628-
若要为容器设置 Seccomp 样板(Profile),可在你的 Pod 或 Container 清单的
650+
若要为容器设置 Seccomp 配置(Profile),可在你的 Pod 或 Container 清单的
629651
`securityContext` 节中包含 `seccompProfile` 字段。该字段是一个
630652
[SeccompProfile](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#seccompprofile-v1-core)
631653
对象,包含 `type``localhostProfile` 属性。
632654
`type` 的合法选项包括 `RuntimeDefault``Unconfined``Localhost`
633-
`localhostProfile` 只能在 `type: Localhost` 配置下才需要设置
634-
该字段标明节点上预先配置的样板的路径,路径是相对于 kubelet 所配置的
635-
Seccomp 样板路径(使用 `--root-dir` 配置)而言的。
655+
`localhostProfile` 只能在 `type: Localhost` 配置下才可以设置
656+
该字段标明节点上预先设定的配置的路径,路径是相对于 kubelet 所配置的
657+
Seccomp 配置路径(使用 `--root-dir` 设置)而言的。
636658

637-
下面是一个例子,设置容器使用节点上容器运行时的默认样板作为 Seccomp 样板
659+
下面是一个例子,设置容器使用节点上容器运行时的默认配置作为 Seccomp 配置
638660

639661
```yaml
640662
...
@@ -704,15 +726,15 @@ Pod 的安全上下文适用于 Pod 中的容器,也适用于 Pod 所挂载的
704726

705727
<!--
706728
* `fsGroup`: Volumes that support ownership management are modified to be owned
707-
and writable by the GID specified in `fsGroup`. See the
708-
[Ownership Management design document](https://git.k8s.io/community/contributors/design-proposals/storage/volume-ownership-management.md)
709-
for more details.
729+
and writable by the GID specified in `fsGroup`. See the
730+
[Ownership Management design document](https://git.k8s.io/community/contributors/design-proposals/storage/volume-ownership-management.md)
731+
for more details.
710732

711733
* `seLinuxOptions`: Volumes that support SELinux labeling are relabeled to be accessible
712-
by the label specified under `seLinuxOptions`. Usually you only
713-
need to set the `level` section. This sets the
714-
[Multi-Category Security (MCS)](https://selinuxproject.org/page/NB_MLS)
715-
label given to all Containers in the Pod as well as the Volumes.
734+
by the label specified under `seLinuxOptions`. Usually you only
735+
need to set the `level` section. This sets the
736+
[Multi-Category Security (MCS)](https://selinuxproject.org/page/NB_MLS)
737+
label given to all Containers in the Pod as well as the Volumes.
716738
-->
717739
* `fsGroup`:支持属主管理的卷会被修改,将其属主变更为 `fsGroup` 所指定的 GID,
718740
并且对该 GID 可写。进一步的细节可参阅
@@ -763,9 +785,9 @@ kubectl delete pod security-context-demo-4
763785
-->
764786
* [PodSecurityContext](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podsecuritycontext-v1-core) API 定义
765787
* [SecurityContext](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#securitycontext-v1-core) API 定义
766-
* [使用最新的安全性增强来调优 Docker](https://github.com/containerd/containerd/blob/main/docs/cri/config.md)
767-
* [安全性上下文的设计文档](https://git.k8s.io/community/contributors/design-proposals/auth/security_context.md)
768-
* [属主管理的设计文档](https://git.k8s.io/community/contributors/design-proposals/storage/volume-ownership-management.md)
788+
* [使用最新的安全性增强来调优 Docker(英文)](https://github.com/containerd/containerd/blob/main/docs/cri/config.md)
789+
* [安全上下文的设计文档(英文)](https://git.k8s.io/community/contributors/design-proposals/auth/security_context.md)
790+
* [属主管理的设计文档(英文)](https://git.k8s.io/community/contributors/design-proposals/storage/volume-ownership-management.md)
769791
* [Pod 安全策略](/zh/docs/concepts/policy/pod-security-policy/)
770-
* [AllowPrivilegeEscalation 的设计文档](https://git.k8s.io/community/contributors/design-proposals/auth/no-new-privs.md)
792+
* [AllowPrivilegeEscalation 的设计文档(英文)](https://git.k8s.io/community/contributors/design-proposals/auth/no-new-privs.md)
771793

0 commit comments

Comments
 (0)