|
| 1 | +--- |
| 2 | +title: 为 Pod 配置用户名字空间 |
| 3 | +reviewers: |
| 4 | +content_type: task |
| 5 | +weight: 160 |
| 6 | +min-kubernetes-server-version: v1.25 |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- |
| 10 | +title: Use a User Namespace With a Pod |
| 11 | +reviewers: |
| 12 | +content_type: task |
| 13 | +weight: 160 |
| 14 | +min-kubernetes-server-version: v1.25 |
| 15 | +--> |
| 16 | + |
| 17 | +<!-- overview --> |
| 18 | +{{< feature-state for_k8s_version="v1.25" state="alpha" >}} |
| 19 | + |
| 20 | +<!-- |
| 21 | +This page shows how to configure a user namespace for stateless pods. This |
| 22 | +allows to isolate the user running inside the container from the one in the |
| 23 | +host. |
| 24 | +--> |
| 25 | +本页展示如何为无状态 Pod 配置用户名字空间。可以将容器内的用户与主机上的用户隔离开来。 |
| 26 | + |
| 27 | +<!-- |
| 28 | +A process running as root in a container can run as a different (non-root) user |
| 29 | +in the host; in other words, the process has full privileges for operations |
| 30 | +inside the user namespace, but is unprivileged for operations outside the |
| 31 | +namespace. |
| 32 | +--> |
| 33 | +在容器中以 root 用户运行的进程可以以不同的(非 root)用户在宿主机上运行;换句话说, |
| 34 | +进程在用户名字空间内部拥有执行操作的全部特权,但在用户名字空间外部并没有执行操作的特权。 |
| 35 | + |
| 36 | +<!-- |
| 37 | +You can use this feature to reduce the damage a compromised container can do to |
| 38 | +the host or other pods in the same node. There are [several security |
| 39 | +vulnerabilities][KEP-vulns] rated either **HIGH** or **CRITICAL** that were not |
| 40 | +exploitable when user namespaces is active. It is expected user namespace will |
| 41 | +mitigate some future vulnerabilities too. |
| 42 | +--> |
| 43 | +你可以使用这个特性来减少有害的容器对同一宿主机上其他容器的影响。 |
| 44 | +[有些安全脆弱性问题][KEP-vulns]被评为 **HIGH** or **CRITICAL**,但当用户名字空间被启用时, |
| 45 | +它们是无法被利用的。相信用户名字空间也能减轻一些未来的漏洞的影响。 |
| 46 | + |
| 47 | +<!-- |
| 48 | +Without using a user namespace a container running as root, in the case of a |
| 49 | +container breakout, has root privileges on the node. And if some capability were |
| 50 | +granted to the container, the capabilities are valid on the host too. None of |
| 51 | +this is true when user namespaces are used. |
| 52 | +--> |
| 53 | +在不使用用户名字空间的情况下,对于以 root 用户运行的容器而言,发生容器逃逸时, |
| 54 | +容器将拥有在宿主机上的 root 特权。如果容器被赋予了某些权限,则这些权限在宿主机上同样有效。 |
| 55 | +当使用用户名字空间时这些都不可能发生。 |
| 56 | + |
| 57 | +[KEP-vulns]: https://github.com/kubernetes/enhancements/tree/217d790720c5aef09b8bd4d6ca96284a0affe6c2/keps/sig-node/127-user-namespaces#motivation |
| 58 | + |
| 59 | +## {{% heading "prerequisites" %}} |
| 60 | + |
| 61 | +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} |
| 62 | + |
| 63 | +{{% thirdparty-content single="true" %}} |
| 64 | +<!-- if adding another runtime in the future, omit the single setting --> |
| 65 | + |
| 66 | +<!-- |
| 67 | +* The node OS needs to be Linux |
| 68 | +* You need to exec commands in the host |
| 69 | +* You need to be able to exec into pods |
| 70 | +* Feature gate `UserNamespacesStatelessPodsSupport` need to be enabled. |
| 71 | +--> |
| 72 | +* 节点上的操作系统必须为 Linux |
| 73 | +* 你需要在宿主机上执行命令 |
| 74 | +* 你需要能够通过 exec 操作进入 Pod |
| 75 | +* 特性 `UserNamespacesStatelessPodsSupport` 需要被启用。 |
| 76 | + |
| 77 | +<!-- |
| 78 | +In addition, support is needed in the |
| 79 | +{{< glossary_tooltip text="container runtime" term_id="container-runtime" >}} |
| 80 | +to use this feature with Kubernetes stateless pods: |
| 81 | +--> |
| 82 | +此外, 需要{{< glossary_tooltip text="容器运行时" term_id="container-runtime" >}}提供相应的支持, |
| 83 | +才能将此特性与 Kubernetes 无状态 Pod 一起使用: |
| 84 | + |
| 85 | +<!-- |
| 86 | +* CRI-O: v1.25 has support for user namespaces. |
| 87 | +--> |
| 88 | +* CRI-O: v1.25 支持用户名字空间。 |
| 89 | + |
| 90 | +<!-- |
| 91 | +Please note that **if your container runtime doesn't support user namespaces, the |
| 92 | +new `pod.spec` field will be silently ignored and the pod will be created without |
| 93 | +user namespaces.** |
| 94 | +--> |
| 95 | +请注意 **如果你的容器运行时环境不支持用户名字空间,字段 `pod.spec` 将被忽略, |
| 96 | +并且系统会在没有用户名字空间的环境中创建 Pod。** |
| 97 | + |
| 98 | +<!-- steps --> |
| 99 | + |
| 100 | +<!-- |
| 101 | +## Run a Pod that uses a user namespace {#create-pod} |
| 102 | +--> |
| 103 | +## 运行一个使用用户名字空间的 Pod {#create-pod} |
| 104 | + |
| 105 | +<!-- |
| 106 | +A user namespace for a stateless pod is enabled setting the `hostUsers` field of |
| 107 | +`.spec` to `false`. For example: |
| 108 | +--> |
| 109 | +为一个无状态的 Pod 启用用户名字空间需要设置 `.spec` 的 `hostUsers` 字段 |
| 110 | +为 `false`. 例如: |
| 111 | + |
| 112 | +{{< codenew file="pods/user-namespaces-stateless.yaml" >}} |
| 113 | + |
| 114 | +<!-- |
| 115 | +1. Create the pod on your cluster: |
| 116 | +--> |
| 117 | +1. 在你的集群上创建 Pod: |
| 118 | + |
| 119 | + ```shell |
| 120 | + kubectl apply -f https://k8s.io/examples/pods/user-namespaces-stateless.yaml |
| 121 | + ``` |
| 122 | + |
| 123 | +<!-- |
| 124 | +1. Attach to the container and run `readlink /proc/self/ns/user`: |
| 125 | +--> |
| 126 | +2. 挂接到容器上并执行 `readlink /proc/self/ns/user`: |
| 127 | + |
| 128 | + ```shell |
| 129 | + kubectl attach -it userns bash |
| 130 | + ``` |
| 131 | + |
| 132 | +<!-- |
| 133 | +And run the command. The output is similar to this: |
| 134 | +--> |
| 135 | +执行命令的输出类似于: |
| 136 | + |
| 137 | +```none |
| 138 | +readlink /proc/self/ns/user |
| 139 | +user:[4026531837] |
| 140 | +cat /proc/self/uid_map |
| 141 | +0 0 4294967295 |
| 142 | +``` |
| 143 | + |
| 144 | +<!-- |
| 145 | +Then, open a shell in the host and run the same command. |
| 146 | +--> |
| 147 | +然后,在主机中打开一个 Shell 并运行相同的命令。 |
| 148 | + |
| 149 | +<!-- |
| 150 | +The output must be different. This means the host and the pod are using a |
| 151 | +different user namespace. When user namespaces are not enabled, the host and the |
| 152 | +pod use the same user namespace. |
| 153 | +--> |
| 154 | +输出一定是不同的。这意味着主机和 Pod 使用不同的用户名字空间。当未启用用户名字空间时, |
| 155 | +宿主机和 Pod 使用相同的用户名字空间。 |
| 156 | + |
| 157 | +<!-- |
| 158 | +If you are running the kubelet inside a user namespace, you need to compare the |
| 159 | +output from running the command in the pod to the output of running in the host: |
| 160 | +--> |
| 161 | +如果你在用户名字空间中运行 kubelet,则需要将在 Pod 中运行命令的输出与在主机中运行的输出进行比较: |
| 162 | + |
| 163 | +<!-- |
| 164 | +```none |
| 165 | +readlink /proc/$pid/ns/user |
| 166 | +user:[4026534732] |
| 167 | +
|
| 168 | +
|
| 169 | +replacing `$pid` with the kubelet PID. |
| 170 | +--> |
| 171 | +```none |
| 172 | +readlink /proc/$pid/ns/user |
| 173 | +user:[4026534732] |
| 174 | +
|
| 175 | +
|
| 176 | +使用 kubelet 的进程号代替 `$pid` |
0 commit comments