1
1
---
2
2
title : 在 Pod 中的容器之间共享进程命名空间
3
- min-kubernetes-server-version : v1.10
4
3
content_type : task
5
4
weight : 160
6
5
---
7
6
<!--
8
7
---
9
8
title: Share Process Namespace between Containers in a Pod
10
- min-kubernetes-server-version: v1.10
11
9
reviewers:
12
10
- verb
13
11
- yujuhong
@@ -19,31 +17,25 @@ weight: 160
19
17
20
18
<!-- overview -->
21
19
22
- {{< feature-state state="stable" for_k8s_version="v1.17" >}}
23
-
24
20
<!--
25
21
This page shows how to configure process namespace sharing for a pod. When
26
22
process namespace sharing is enabled, processes in a container are visible
27
- to all other containers in that pod.
23
+ to all other containers in the same pod.
28
24
-->
29
- 此页面展示如何为 pod 配置进程命名空间共享。
30
- 当启用进程命名空间共享时,容器中的进程对该 pod 中的所有其他容器都是可见的。
25
+ 此页面展示如何为 Pod 配置进程命名空间共享。
26
+ 当启用进程命名空间共享时,容器中的进程对同一 Pod 中的所有其他容器都是可见的。
31
27
32
28
<!--
33
29
You can use this feature to configure cooperating containers, such as a log
34
30
handler sidecar container, or to troubleshoot container images that don't
35
31
include debugging utilities like a shell.
36
32
-->
37
- 你可以使用此功能来配置协作容器,比如日志处理 sidecar 容器,或者对那些不包含诸如 shell 等调试实用工具的镜像进行故障排查。
38
-
39
-
33
+ 你可以使用此功能来配置协作容器,比如日志处理 sidecar 容器,
34
+ 或者对那些不包含诸如 shell 等调试实用工具的镜像进行故障排查。
40
35
41
36
## {{% heading "prerequisites" %}}
42
37
43
-
44
- {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
45
-
46
-
38
+ {{< include "task-tutorial-prereqs.md" >}}
47
39
48
40
<!-- steps -->
49
41
@@ -53,68 +45,85 @@ include debugging utilities like a shell.
53
45
## 配置 Pod
54
46
55
47
<!--
56
- Process Namespace Sharing is enabled using the `ShareProcessNamespace ` field of
57
- `v1.PodSpec` . For example:
48
+ Process namespace sharing is enabled using the `shareProcessNamespace ` field of
49
+ `.spec` for a Pod . For example:
58
50
-->
59
- 进程命名空间共享使用 ` v1.PodSpec ` 中的 ` ShareProcessNamespace ` 字段启用 。例如:
51
+ 使用 Pod ` .spec ` 中的 ` shareProcessNamespace ` 字段可以启用进程命名空间共享 。例如:
60
52
61
53
{{< codenew file="pods/share-process-namespace.yaml" >}}
62
54
63
55
<!--
64
56
1. Create the pod `nginx` on your cluster:
57
+ -->
58
+ 1 . 在集群中创建 ` nginx ` Pod:
65
59
66
- ```shell
67
- kubectl apply -f https://k8s.io/examples/pods/share-process-namespace.yaml
68
- ```
60
+ ``` shell
61
+ kubectl apply -f https://k8s.io/examples/pods/share-process-namespace.yaml
62
+ ```
69
63
64
+ <!--
70
65
1. Attach to the `shell` container and run `ps`:
66
+ -->
67
+ 2 . 获取容器 ` shell ` ,执行 ` ps ` :
68
+
69
+ ``` shell
70
+ kubectl attach -it nginx -c shell
71
+ ```
72
+
73
+ <!--
74
+ If you don't see a command prompt, try pressing enter. In the container shell:
75
+
76
+ ```shell
77
+ # run this inside the "shell" container
78
+ ps ax
79
+ ```
80
+ -->
81
+ 如果没有看到命令提示符,请按 enter 回车键。在容器 shell 中:
82
+
83
+ ``` shell
84
+ # 在 “shell” 容器中运行以下命令
85
+ ps ax
86
+ ```
87
+
88
+ <!--
89
+ The output is similar to this:
90
+ -->
91
+ 输出类似于:
92
+
93
+ ``` none
94
+ PID USER TIME COMMAND
95
+ 1 root 0:00 /pause
96
+ 8 root 0:00 nginx: master process nginx -g daemon off;
97
+ 14 101 0:00 nginx: worker process
98
+ 15 root 0:00 sh
99
+ 21 root 0:00 ps ax
100
+ ```
71
101
72
- ```shell
73
- kubectl attach -it nginx -c shell
74
- ```
75
-
76
- If you don't see a command prompt, try pressing enter.
77
-
78
- ```
79
- / # ps ax
80
- PID USER TIME COMMAND
81
- 1 root 0:00 /pause
82
- 8 root 0:00 nginx: master process nginx -g daemon off;
83
- 14 101 0:00 nginx: worker process
84
- 15 root 0:00 sh
85
- 21 root 0:00 ps ax
86
- ```
87
- -->
88
- 1 . 在集群中创建 ` nginx ` pod:
89
-
90
- ``` shell
91
- kubectl apply -f https://k8s.io/examples/pods/share-process-namespace.yaml
92
- ```
93
- 1. 获取容器 ` shell` ,执行 ` ps` :
94
-
95
- ` ` ` shell
96
- kubectl attach -it nginx -c shell
97
- ` ` `
98
- 如果没有看到命令提示符,请按 enter 回车键。
99
-
100
- ```
101
- / # ps ax
102
- PID USER TIME COMMAND
103
- 1 root 0:00 /pause
104
- 8 root 0:00 nginx: master process nginx -g daemon off;
105
- 14 101 0:00 nginx: worker process
106
- 15 root 0:00 sh
107
- 21 root 0:00 ps ax
108
- ```
109
102
<!--
110
103
You can signal processes in other containers. For example, send `SIGHUP` to
111
- nginx to restart the worker process. This requires the ` SYS_PTRACE` capability.
104
+ `nginx` to restart the worker process. This requires the `SYS_PTRACE` capability.
105
+
106
+ ```shell
107
+ # run this inside the "shell" container
108
+ kill -HUP 8 # change "8" to match the PID of the nginx leader process, if necessary
109
+ ps ax
110
+ ```
112
111
-->
113
- 你可以在其他容器中对进程发出信号。例如,发送 ` SIGHUP` 到 nginx 以重启工作进程。这需要 ` SYS_PTRACE` 功能。
112
+ 你可以在其他容器中对进程发出信号。例如,发送 ` SIGHUP ` 到 ` nginx ` 以重启工作进程。
113
+ 此操作需要 ` SYS_PTRACE ` 权能。
114
114
115
+ ``` shell
116
+ # 在 “shell” 容器中运行以下命令
117
+ kill -HUP 8 # 如有必要,更改 “8” 以匹配 nginx 领导进程的 PID
118
+ ps ax
115
119
```
116
- / # kill -HUP 8
117
- / # ps ax
120
+
121
+ <!--
122
+ The output is similar to this:
123
+ -->
124
+ 输出类似于:
125
+
126
+ ``` none
118
127
PID USER TIME COMMAND
119
128
1 root 0:00 /pause
120
129
8 root 0:00 nginx: master process nginx -g daemon off;
@@ -124,14 +133,29 @@ PID USER TIME COMMAND
124
133
```
125
134
126
135
<!--
127
- It's even possible to access another container image using the
136
+ It's even possible to access the file system of another container using the
128
137
`/proc/$pid/root` link.
138
+
139
+ ```shell
140
+ # run this inside the "shell" container
141
+ # change "8" to the PID of the Nginx process, if necessary
142
+ head /proc/8/root/etc/nginx/nginx.conf
143
+ ```
129
144
-->
130
- 甚至可以使用 `/proc/$pid/root` 链接访问另一个容器镜像 。
145
+ 甚至可以使用 ` /proc/$pid/root ` 链接访问另一个容器的文件系统 。
131
146
147
+ ``` shell
148
+ # 在 “shell” 容器中运行以下命令
149
+ # 如有必要,更改 “8” 为 Nginx 进程的 PID
150
+ head /proc/8/root/etc/nginx/nginx.conf
132
151
```
133
- / # head /proc/8/root/etc/nginx/nginx.conf
134
152
153
+ <!--
154
+ The output is similar to this:
155
+ -->
156
+ 输出类似于:
157
+
158
+ ``` none
135
159
user nginx;
136
160
worker_processes 1;
137
161
@@ -143,45 +167,45 @@ events {
143
167
worker_connections 1024;
144
168
```
145
169
146
-
147
-
148
170
<!-- discussion -->
149
171
150
172
<!--
151
- ## Understanding Process Namespace Sharing
173
+ ## Understanding process namespace sharing
152
174
-->
153
175
## 理解进程命名空间共享
154
176
155
177
<!--
156
178
Pods share many resources so it makes sense they would also share a process
157
- namespace. Some container images may expect to be isolated from other
158
- containers, though, so it's important to understand these differences:
179
+ namespace. Some containers may expect to be isolated from others, though,
180
+ so it's important to understand the differences:
159
181
-->
160
182
Pod 共享许多资源,因此它们共享进程命名空间是很有意义的。
161
- 不过,有些容器镜像可能希望与其他容器隔离 ,因此了解这些差异很重要:
183
+ 不过,有些容器可能希望与其他容器隔离 ,因此了解这些差异很重要:
162
184
163
185
<!--
164
- 1. **The container process no longer has PID 1.** Some container images refuse
186
+ 1. **The container process no longer has PID 1.** Some containers refuse
165
187
to start without PID 1 (for example, containers using `systemd`) or run
166
188
commands like `kill -HUP 1` to signal the container process. In pods with a
167
- shared process namespace, `kill -HUP 1` will signal the pod sandbox.
168
- (`/pause` in the above example.)
189
+ shared process namespace, `kill -HUP 1` will signal the pod sandbox
190
+ (`/pause` in the above example).
191
+ -->
192
+ 1 . ** 容器进程不再具有 PID 1。** 在没有 PID 1 的情况下,一些容器拒绝启动
193
+ (例如,使用 ` systemd ` 的容器),或者拒绝执行 ` kill -HUP 1 ` 之类的命令来通知容器进程。
194
+ 在具有共享进程命名空间的 Pod 中,` kill -HUP 1 ` 将通知 Pod 沙箱(在上面的例子中是 ` /pause ` )。
169
195
196
+ <!--
170
197
1. **Processes are visible to other containers in the pod.** This includes all
171
198
information visible in `/proc`, such as passwords that were passed as arguments
172
199
or environment variables. These are protected only by regular Unix permissions.
200
+ -->
201
+ 2 . ** 进程对 Pod 中的其他容器可见。** 这包括 ` /proc ` 中可见的所有信息,
202
+ 例如作为参数或环境变量传递的密码。这些仅受常规 Unix 权限的保护。
173
203
204
+ <!--
174
205
1. **Container filesystems are visible to other containers in the pod through the
175
206
`/proc/$pid/root` link.** This makes debugging easier, but it also means
176
207
that filesystem secrets are protected only by filesystem permissions.
177
208
-->
178
-
179
- 1. **容器进程不再具有 PID 1。** 在没有 PID 1 的情况下,一些容器镜像拒绝启动(例如,使用 `systemd` 的容器),或者拒绝执行 `kill -HUP 1` 之类的命令来通知容器进程。在具有共享进程命名空间的 pod 中,`kill -HUP 1` 将通知 pod 沙箱(在上面的例子中是 `/pause`)。
180
-
181
- 2. **进程对 pod 中的其他容器可见。** 这包括 `/proc` 中可见的所有信息,例如作为参数或环境变量传递的密码。这些仅受常规 Unix 权限的保护。
182
-
183
- 3. **容器文件系统通过 `/proc/$pid/root` 链接对 pod 中的其他容器可见。** 这使调试更加容易,但也意味着文件系统安全性只受文件系统权限的保护。
184
-
185
-
186
-
209
+ 3 . ** 容器文件系统通过 ` /proc/$pid/root ` 链接对 Pod 中的其他容器可见。** 这使调试更加容易,
210
+ 但也意味着文件系统安全性只受文件系统权限的保护。
187
211
0 commit comments