Skip to content

Commit 8bbca74

Browse files
authored
Merge pull request #42866 from wilsonwu/improve-blog-230808
[zh] Improve blog translation of Sidecar feature
2 parents 1bb9514 + 08b3ba0 commit 8bbca74

File tree

2 files changed

+328
-161
lines changed

2 files changed

+328
-161
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
layout: blog
3+
title: "Kubernetes 1.27:高效的 SELinux 卷重新标记(Beta 版)"
4+
date: 2023-04-18T10:00:00-08:00
5+
slug: kubernetes-1-27-efficient-selinux-relabeling-beta
6+
---
7+
8+
<!--
9+
layout: blog
10+
title: "Kubernetes 1.27: Efficient SELinux volume relabeling (Beta)"
11+
date: 2023-04-18T10:00:00-08:00
12+
slug: kubernetes-1-27-efficient-selinux-relabeling-beta
13+
-->
14+
15+
**作者**:Jan Šafránek (Red Hat)
16+
<!--
17+
**Author:** Jan Šafránek (Red Hat)
18+
-->
19+
20+
**译者**:Wilson Wu (DaoCloud)
21+
22+
<!--
23+
## The problem
24+
-->
25+
## 问题 {#the-problem}
26+
27+
<!--
28+
On Linux with Security-Enhanced Linux (SELinux) enabled, it's traditionally the container runtime that applies SELinux labels to a Pod and all its volumes. Kubernetes only passes the SELinux label from a Pod's `securityContext` fields to the container runtime.
29+
-->
30+
在启用了 Security-Enhancled Linux(SELinux)系统上,传统做法是让容器运行时负责为
31+
Pod 及所有卷应用 SELinux 标签。
32+
Kubernetes 仅将 SELinux 标签从 Pod 的 `securityContext` 字段传递给容器运行时。
33+
34+
<!--
35+
The container runtime then recursively changes SELinux label on all files that are visible to the Pod's containers. This can be time-consuming if there are many files on the volume, especially when the volume is on a remote filesystem.
36+
-->
37+
然后,容器运行时以递归的方式更改 Pod 容器可见的所有文件上的 SELinux 标签。
38+
如果卷上有许多文件,这一过程可能会非常耗时,尤其是当卷位于远程文件系统上时。
39+
40+
{{% alert title="Note" color="info" %}}
41+
<!--
42+
If a container uses `subPath` of a volume, only that `subPath` of the whole volume is relabeled. This allows two pods that have two different SELinux labels to use the same volume, as long as they use different subpaths of it.
43+
-->
44+
如果容器使用卷的 `subPath`,则系统仅重新标记整个卷的 `subPath`
45+
这样,使用不同 SELinux 标签的两个 Pod 可以使用同一卷,只要它们使用该卷的不同子路径即可。
46+
{{% /alert %}}
47+
48+
<!--
49+
If a Pod does not have any SELinux label assigned in Kubernetes API, the container runtime assigns a unique random one, so a process that potentially escapes the container boundary cannot access data of any other container on the host. The container runtime still recursively relabels all pod volumes with this random SELinux label.
50+
-->
51+
如果 Pod 没有从 Kubernetes API 中获得任何 SELinux 标签,则容器运行时会分配一个唯一的随机标签,
52+
因此可能逃离容器边界的进程将无法访问主机上任何其他容器的数据。
53+
容器运行时仍然使用此随机 SELinux 标签递归地重新标记所有 Pod 卷。
54+
55+
<!--
56+
## Improvement using mount options
57+
-->
58+
## 使用挂载选项进行改进 {#improvement-using-mount-options}
59+
60+
<!--
61+
If a Pod and its volume meet **all** of the following conditions, Kubernetes will _mount_ the volume directly with the right SELinux label. Such mount will happen in a constant time and the container runtime will not need to recursively relabel any files on it.
62+
-->
63+
如果 Pod 及其卷满足以下所有条件,Kubernetes 将直接使用正确的 SELinux 标签挂载该卷。
64+
这种挂载将在确定时间内完成,容器运行时不需要递归地重新标记其上的任何文件。
65+
66+
<!--
67+
1. The operating system must support SELinux.
68+
69+
Without SELinux support detected, kubelet and the container runtime do not do anything with regard to SELinux.
70+
-->
71+
1. 操作系统必须支持 SELinux。
72+
73+
如果没有检测到 SELinux 支持,kubelet 和容器运行时不会对 SELinux 执行任何操作。
74+
75+
<!--
76+
1. The [feature gates](/docs/reference/command-line-tools-reference/feature-gates/) `ReadWriteOncePod` and `SELinuxMountReadWriteOncePod` must be enabled. These feature gates are Beta in Kubernetes 1.27 and Alpha in 1.25.
77+
78+
With any of these feature gates disabled, SELinux labels will be always applied by the container runtime by a recursive walk through the volume (or its subPaths).
79+
-->
80+
2. 必须启用[特性门控](/zh-cn/docs/reference/command-line-tools-reference/feature-gates/)
81+
`ReadWriteOncePod``SELinuxMountReadWriteOncePod`。这些特性门控在 Kubernetes 1.27 中是 Beta 版,在 1.25 中是 Alpha 版。
82+
83+
禁用这些功能中任何一个后,SELinux 标签将始终由容器运行时通过递归遍历卷(或其 subPath)来应用。
84+
85+
<!--
86+
1. The Pod must have at least `seLinuxOptions.level` assigned in its [Pod Security Context](/docs/reference/kubernetes-api/workload-resources/pod-v1/#security-context) or all Pod containers must have it set in their [Security Contexts](/docs/reference/kubernetes-api/workload-resources/pod-v1/#security-context-1). Kubernetes will read the default `user`, `role` and `type` from the operating system defaults (typically `system_u`, `system_r` and `container_t`).
87+
88+
Without Kubernetes knowing at least the SELinux `level`, the container runtime will assign a random one _after_ the volumes are mounted. The container runtime will still relabel the volumes recursively in that case.
89+
-->
90+
3. Pod 必须在其 [Pod 安全上下文](/zh-cn/docs/reference/kubernetes-api/workload-resources/pod-v1/#security-context)中至少分配
91+
`seLinuxOptions.level`,或者所有 Pod 容器必须在[安全上下文](/zh-cn/docs/reference/kubernetes-api/workload-resources/pod-v1/#security-context-1)中对其进行设置。
92+
否则 Kubernetes 将从操作系统默认值(通常是 `system_u``system_r``container_t`)中读取默认的 `user``role``type`
93+
94+
如果 Kubernetes 不了解任何 SELinux `level`,容器运行时将在卷挂载****为其分配一个随机级别。
95+
在这种情况下,容器运行时仍会递归地对卷进行重新标记。
96+
97+
<!--
98+
1. The volume must be a Persistent Volume with [Access Mode](/docs/concepts/storage/persistent-volumes/#access-modes) `ReadWriteOncePod`.
99+
100+
This is a limitation of the initial implementation. As described above, two Pods can have a different SELinux label and still use the same volume, as long as they use a different `subPath` of it. This use case is not possible when the volumes are _mounted_ with the SELinux label, because the whole volume is mounted and most filesystems don't support mounting a single volume multiple times with multiple SELinux labels.
101+
102+
If running two Pods with two different SELinux contexts and using different `subPaths` of the same volume is necessary in your deployments, please comment in the [KEP](https://github.com/kubernetes/enhancements/issues/1710) issue (or upvote any existing comment - it's best not to duplicate). Such pods may not run when the feature is extended to cover all volume access modes.
103+
-->
104+
4. 该卷必须是[访问模式](/zh-cn/docs/concepts/storage/persistent-volumes/#access-modes) `ReadWriteOncePod` 的持久卷。
105+
106+
这是最初实施的限制。如上所述,两个 Pod 可以具有不同的 SELinux 标签,但仍然使用相同的卷,
107+
只要它们使用不同的 `subPath` 即可。当使用 SELinux 标签的**已挂载**卷时,此用例是不可能实现的,
108+
因为整个卷已挂载,并且大多数文件系统不支持使用多个 SELinux 标签多次挂载单个卷。
109+
110+
如果在部署中需要使用两个不同的 SELinux 上下文运行两个 Pod 并使用同一卷的不同 `subPath`
111+
请在 [KEP](https://github.com/kubernetes/enhancements/issues/1710) 问题中发表评论(或对任何现有评论进行投票 - 最好不要重复)。
112+
当功能扩展到覆盖所有卷访问模式时,此类 Pod 可能无法运行。
113+
114+
<!--
115+
1. The volume plugin or the CSI driver responsible for the volume supports mounting with SELinux mount options.
116+
117+
These in-tree volume plugins support mounting with SELinux mount options: `fc`, `iscsi`, and `rbd`.
118+
119+
CSI drivers that support mounting with SELinux mount options must announce that in their [CSIDriver](/docs/reference/kubernetes-api/config-and-storage-resources/csi-driver-v1/) instance by setting `seLinuxMount` field.
120+
121+
Volumes managed by other volume plugins or CSI drivers that don't set `seLinuxMount: true` will be recursively relabelled by the container runtime.
122+
-->
123+
5. 卷插件或负责卷的 CSI 驱动程序支持使用 SELinux 挂载选项进行挂载。
124+
125+
这些树内卷插件支持使用 SELinux 挂载选项进行挂载:`fc``iscsi``rbd`
126+
127+
支持使用 SELinux 挂载选项挂载的 CSI 驱动程序必须通过设置 `seLinuxMount`
128+
字段在其 [CSIDriver](/zh-cn/docs/reference/kubernetes-api/config-and-storage-resources/csi-driver-v1/) 实例中声明这一点。
129+
130+
由其他未设置 `seLinuxMount: true` 的卷插件或 CSI 驱动程序管理的卷将由容器运行时递归地重新标记。
131+
132+
<!--
133+
### Mounting with SELinux context
134+
-->
135+
### 使用 SELinux 上下文挂载 {#mounting-with-selinux-context}
136+
137+
<!--
138+
When all aforementioned conditions are met, kubelet will pass `-o context=<SELinux label>` mount option to the volume plugin or CSI driver. CSI driver vendors must ensure that this mount option is supported by their CSI driver and, if necessary, the CSI driver appends other mount options that are needed for `-o context` to work.
139+
-->
140+
当满足所有上述条件时,kubelet 会将 `-o context=<SELinux label>` 挂载选项传递给卷插件或 CSI 驱动程序。
141+
CSI 驱动程序供应商必须确保其 CSI 驱动程序支持此安装选项,并且如有必要,CSI 驱动程序会附加 `-o context` 工作所需的其他安装选项。
142+
143+
<!--
144+
For example, NFS may need `-o context=<SELinux label>,nosharecache`, so each volume mounted from the same NFS server can have a different SELinux label value. Similarly, CIFS may need `-o context=<SELinux label>,nosharesock`.
145+
-->
146+
例如,NFS 可能需要 `-o context=<SELinux label>,nosharecache`,因此从同一 NFS 服务器挂载的每个卷可以具有不同的 SELinux 标签值。
147+
类似地,CIFS 可能需要 `-o context=<SELinux label>,nosharesock`
148+
149+
<!--
150+
It's up to the CSI driver vendor to test their CSI driver in a SELinux enabled environment before setting `seLinuxMount: true` in the CSIDriver instance.
151+
-->
152+
在 CSIDriver 实例中设置 `seLinuxMount: true` 之前,CSI 驱动程序供应商需要在启用 SELinux 的环境中测试其 CSI 驱动程序。
153+
154+
<!--
155+
## How can I learn more?
156+
-->
157+
## 如何了解更多? {#how-can-i-learn-more}
158+
159+
<!--
160+
SELinux in containers: see excellent [visual SELinux guide](https://opensource.com/business/13/11/selinux-policy-guide) by Daniel J Walsh. Note that the guide is older than Kubernetes, it describes *Multi-Category Security* (MCS) mode using virtual machines as an example, however, a similar concept is used for containers.
161+
-->
162+
容器中的 SELinux:请参阅 Daniel J Walsh 撰写的优秀 [可视化 SELinux 指南(英文)](https://opensource.com/business/13/11/selinux-policy-guide)
163+
请注意,该指南早于 Kubernetes,它以虚拟机为例描述了**多类别安全**(MCS)模式,但是,类似的概念也适用于容器。
164+
165+
<!--
166+
See a series of blog posts for details how exactly SELinux is applied to containers by container runtimes:
167+
-->
168+
请参阅以下系列博客文章,详细了解容器运行时如何将 SELinux 应用于容器:
169+
170+
<!--
171+
* [How SELinux separates containers using Multi-Level Security](https://www.redhat.com/en/blog/how-selinux-separates-containers-using-multi-level-security)
172+
* [Why you should be using Multi-Category Security for your Linux containers](https://www.redhat.com/en/blog/why-you-should-be-using-multi-category-security-your-linux-containers)
173+
-->
174+
* [SELinux 如何使用多级安全性分离容器](https://www.redhat.com/en/blog/how-selinux-separates-containers-using-multi-level-security)
175+
* [为什么应该为 Linux 容器使用多类别安全性](https://www.redhat.com/en/blog/why-you-should-be-using-multi-category-security-your-linux-containers)
176+
177+
<!--
178+
Read the KEP: [Speed up SELinux volume relabeling using mounts](https://github.com/kubernetes/enhancements/tree/master/keps/sig-storage/1710-selinux-relabeling)
179+
-->
180+
阅读 KEP:[使用挂载加速 SELinux 卷重新标记](https://github.com/kubernetes/enhancements/tree/master/keps/sig-storage/1710-selinux-relabeling)

0 commit comments

Comments
 (0)