|
2 | 2 | title: 同 Pod 内的容器使用共享卷通信
|
3 | 3 | content_type: task
|
4 | 4 | ---
|
5 |
| - |
6 |
| - |
| 5 | +<!-- |
| 6 | +title: Communicate Between Containers in the Same Pod Using a Shared Volume |
| 7 | +content_type: task |
| 8 | +weight: 110 |
| 9 | +--> |
7 | 10 | <!-- overview -->
|
8 | 11 |
|
| 12 | +<!-- |
| 13 | +This page shows how to use a Volume to communicate between two Containers running |
| 14 | +in the same Pod. See also how to allow processes to communicate by |
| 15 | +[sharing process namespace](/docs/tasks/configure-pod-container/share-process-namespace/) |
| 16 | +between containers. |
| 17 | +--> |
9 | 18 | 本文旨在说明如何让一个 Pod 内的两个容器使用一个卷(Volume)进行通信。
|
10 |
| - |
11 |
| - |
| 19 | +参阅如何让两个进程跨容器通过 |
| 20 | +[共享进程名字空间](/zh/docs/tasks/configure-pod-container/share-process-namespace/)。 |
12 | 21 |
|
13 | 22 | ## {{% heading "prerequisites" %}}
|
14 | 23 |
|
15 |
| - |
16 | 24 | {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
17 | 25 |
|
18 |
| - |
19 |
| - |
20 |
| - |
21 | 26 | <!-- steps -->
|
22 | 27 |
|
| 28 | +<!-- |
| 29 | +## Creating a Pod that runs two Containers |
23 | 30 |
|
| 31 | +In this exercise, you create a Pod that runs two Containers. The two containers |
| 32 | +share a Volume that they can use to communicate. Here is the configuration file |
| 33 | +for the Pod: |
| 34 | +--> |
24 | 35 | ## 创建一个包含两个容器的 Pod
|
25 | 36 |
|
26 | 37 | 在这个练习中,你会创建一个包含两个容器的 Pod。两个容器共享一个卷用于他们之间的通信。
|
27 | 38 | Pod 的配置文件如下:
|
28 | 39 |
|
29 | 40 | {{< codenew file="pods/two-container-pod.yaml" >}}
|
30 | 41 |
|
| 42 | +<!-- |
| 43 | +In the configuration file, you can see that the Pod has a Volume named |
| 44 | +`shared-data`. |
| 45 | +
|
| 46 | +The first container listed in the configuration file runs an nginx server. The |
| 47 | +mount path for the shared Volume is `/usr/share/nginx/html`. |
| 48 | +The second container is based on the debian image, and has a mount path of |
| 49 | +`/pod-data`. The second container runs the following command and then terminates. |
| 50 | +--> |
31 | 51 | 在配置文件中,你可以看到 Pod 有一个共享卷,名为 `shared-data`。
|
32 | 52 |
|
33 | 53 | 配置文件中的第一个容器运行了一个 nginx 服务器。共享卷的挂载路径是 `/usr/share/nginx/html`。
|
34 | 54 | 第二个容器是基于 debian 镜像的,有一个 `/pod-data` 的挂载路径。第二个容器运行了下面的命令然后终止。
|
35 | 55 |
|
36 |
| - echo Hello from the debian container > /pod-data/index.html |
| 56 | +```shell |
| 57 | +echo Hello from the debian container > /pod-data/index.html |
| 58 | +``` |
37 | 59 |
|
38 |
| -注意,第二个容器在 nginx 服务器的根目录下写了 `index.html` 文件。 |
| 60 | +<!-- |
| 61 | +Notice that the second container writes the `index.html` file in the root |
| 62 | +directory of the nginx server. |
39 | 63 |
|
| 64 | +Create the Pod and the two Containers: |
| 65 | +--> |
| 66 | +注意,第二个容器在 nginx 服务器的根目录下写了 `index.html` 文件。 |
40 | 67 |
|
41 | 68 | 创建一个包含两个容器的 Pod:
|
42 | 69 |
|
43 |
| - kubectl apply -f https://k8s.io/examples/pods/two-container-pod.yaml |
| 70 | +```shell |
| 71 | +kubectl apply -f https://k8s.io/examples/pods/two-container-pod.yaml |
| 72 | +``` |
44 | 73 |
|
| 74 | +<!-- |
| 75 | +View information about the Pod and the Containers: |
| 76 | +--> |
45 | 77 | 查看 Pod 和容器的信息:
|
46 | 78 |
|
47 |
| - kubectl get pod two-containers --output=yaml |
48 |
| - |
| 79 | +```shell |
| 80 | +kubectl get pod two-containers --output=yaml |
| 81 | +``` |
49 | 82 |
|
| 83 | +<!-- |
| 84 | +Here is a portion of the output: |
| 85 | +--> |
50 | 86 | 这是输出的一部分:
|
51 | 87 |
|
52 |
| - apiVersion: v1 |
53 |
| - kind: Pod |
54 |
| - metadata: |
55 |
| - ... |
56 |
| - name: two-containers |
57 |
| - namespace: default |
58 |
| - ... |
59 |
| - spec: |
60 |
| - ... |
61 |
| - containerStatuses: |
62 |
| - |
63 |
| - - containerID: docker://c1d8abd1 ... |
64 |
| - image: debian |
65 |
| - ... |
66 |
| - lastState: |
67 |
| - terminated: |
68 |
| - ... |
69 |
| - name: debian-container |
70 |
| - ... |
71 |
| - |
72 |
| - - containerID: docker://96c1ff2c5bb ... |
73 |
| - image: nginx |
| 88 | +```yaml |
| 89 | +apiVersion: v1 |
| 90 | +kind: Pod |
| 91 | +metadata: |
| 92 | + ... |
| 93 | + name: two-containers |
| 94 | + namespace: default |
| 95 | + ... |
| 96 | +spec: |
| 97 | + ... |
| 98 | + containerStatuses: |
| 99 | + |
| 100 | + - containerID: docker://c1d8abd1 ... |
| 101 | + image: debian |
| 102 | + ... |
| 103 | + lastState: |
| 104 | + terminated: |
74 | 105 | ...
|
75 |
| - name: nginx-container |
76 |
| - ... |
77 |
| - state: |
78 |
| - running: |
79 |
| - ... |
80 |
| - |
81 |
| - |
82 |
| - |
| 106 | + name: debian-container |
| 107 | + ... |
| 108 | + |
| 109 | + - containerID: docker://96c1ff2c5bb ... |
| 110 | + image: nginx |
| 111 | + ... |
| 112 | + name: nginx-container |
| 113 | + ... |
| 114 | + state: |
| 115 | + running: |
| 116 | + ... |
| 117 | +``` |
| 118 | + |
| 119 | +<!-- |
| 120 | +You can see that the debian Container has terminated, and the nginx Container |
| 121 | +is still running. |
| 122 | +
|
| 123 | +Get a shell to nginx Container: |
| 124 | +--> |
83 | 125 | 你可以看到 debian 容器已经被终止了,而 nginx 服务器依然在运行。
|
84 | 126 |
|
85 |
| - |
86 | 127 | 进入 nginx 容器的 shell:
|
87 | 128 |
|
88 |
| - kubectl exec -it two-containers -c nginx-container -- /bin/bash |
89 |
| - |
| 129 | +```shell |
| 130 | +kubectl exec -it two-containers -c nginx-container -- /bin/bash |
| 131 | +``` |
90 | 132 |
|
| 133 | +<!-- |
| 134 | +In your shell, verify that nginx is running: |
| 135 | +--> |
91 | 136 | 在 shell 中,确认 nginx 还在运行。
|
92 | 137 |
|
93 |
| - root@two-containers:/# ps aux |
94 |
| - |
| 138 | +``` |
| 139 | +root@two-containers:/# ps aux |
| 140 | +``` |
95 | 141 |
|
| 142 | +<!-- |
| 143 | +The output is similar to this: |
| 144 | +--> |
96 | 145 | 输出类似于这样:
|
97 | 146 |
|
98 |
| - USER PID ... STAT START TIME COMMAND |
99 |
| - root 1 ... Ss 21:12 0:00 nginx: master process nginx -g daemon off; |
100 |
| - |
101 |
| - |
| 147 | +``` |
| 148 | +USER PID ... STAT START TIME COMMAND |
| 149 | +root 1 ... Ss 21:12 0:00 nginx: master process nginx -g daemon off; |
| 150 | +``` |
102 | 151 |
|
| 152 | +<!-- |
| 153 | +Recall that the debian Container created the `index.html` file in the nginx root |
| 154 | +directory. Use `curl` to send a GET request to the nginx server: |
| 155 | +--> |
103 | 156 | 回忆一下,debian 容器在 nginx 的根目录下创建了 `index.html` 文件。
|
104 | 157 | 使用 `curl` 向 nginx 服务器发送一个 GET 请求:
|
105 | 158 |
|
106 |
| - root@two-containers:/# apt-get update |
107 |
| - root@two-containers:/# apt-get install curl |
108 |
| - root@two-containers:/# curl localhost |
109 |
| - |
| 159 | +``` |
| 160 | +root@two-containers:/# apt-get update |
| 161 | +root@two-containers:/# apt-get install curl |
| 162 | +root@two-containers:/# curl localhost |
| 163 | +``` |
110 | 164 |
|
111 | 165 | 输出表示 nginx 提供了 debian 容器写的页面:
|
112 | 166 |
|
113 |
| - Hello from the debian container |
114 |
| - |
115 |
| - |
116 |
| - |
117 |
| - |
| 167 | +``` |
| 168 | +Hello from the debian container |
| 169 | +``` |
118 | 170 | <!-- discussion -->
|
119 | 171 |
|
120 |
| - |
| 172 | +<!-- |
| 173 | +## Discussion |
| 174 | +
|
| 175 | +The primary reason that Pods can have multiple containers is to support |
| 176 | +helper applications that assist a primary application. Typical examples of |
| 177 | +helper applications are data pullers, data pushers, and proxies. |
| 178 | +Helper and primary applications often need to communicate with each other. |
| 179 | +Typically this is done through a shared filesystem, as shown in this exercise, |
| 180 | +or through the loopback network interface, localhost. An example of this pattern is a |
| 181 | +web server along with a helper program that polls a Git repository for new updates. |
| 182 | +--> |
121 | 183 | ## 讨论
|
122 | 184 |
|
123 |
| - |
124 |
| - |
125 |
| - |
126 |
| - |
127 |
| - |
128 |
| - |
129 |
| - |
130 | 185 | Pod 能有多个容器的主要原因是为了支持辅助应用(helper applications),以协助主应用(primary application)。
|
131 | 186 | 辅助应用的典型例子是数据抽取,数据推送和代理。辅助应用和主应用经常需要相互通信。
|
132 | 187 | 就如这个练习所示,通信通常是通过共享文件系统完成的,或者,也通过回环网络接口 localhost 完成。
|
133 | 188 | 举个网络接口的例子,web 服务器带有一个协助程序用于拉取 Git 仓库的更新。
|
134 | 189 |
|
135 |
| - |
136 |
| - |
137 |
| - |
| 190 | +<!-- |
| 191 | +The Volume in this exercise provides a way for Containers to communicate during |
| 192 | +the life of the Pod. If the Pod is deleted and recreated, any data stored in |
| 193 | +the shared Volume is lost. |
| 194 | +--> |
138 | 195 | 在本练习中的卷为 Pod 生命周期中的容器相互通信提供了一种方法。如果 Pod 被删除或者重建了,
|
139 | 196 | 任何共享卷中的数据都会丢失。
|
140 | 197 |
|
141 |
| - |
142 |
| - |
143 |
| - |
144 | 198 | ## {{% heading "whatsnext" %}}
|
145 | 199 |
|
146 |
| - |
147 |
| - |
148 |
| - |
149 |
| -* 更多学习内容 |
150 |
| -[混合容器的方式](http://kubernetes.io/blog/2015/06/the-distributed-system-toolkit-patterns.html)。 |
151 |
| - |
152 |
| - |
153 |
| - |
154 |
| -* 学习 [模块化架构的混合容器](http://www.slideshare.net/Docker/slideshare-burns)。 |
155 |
| - |
156 |
| - |
157 |
| - |
158 |
| -* 参见 [配置一个使用存储卷的 Pod](/docs/tasks/configure-pod-container/configure-volume-storage/)。 |
159 |
| - |
160 |
| - |
161 |
| -* 参见 [卷](/docs/api-reference/{{< param "version" >}}/#volume-v1-core)。 |
162 |
| - |
163 |
| - |
164 |
| -* 参见 [Pod](/docs/api-reference/{{< param "version" >}}/#pod-v1-core). |
165 |
| - |
166 |
| - |
167 |
| - |
168 |
| - |
| 200 | +<!-- |
| 201 | +* Learn more about [patterns for composite containers](https://kubernetes.io/blog/2015/06/the-distributed-system-toolkit-patterns). |
| 202 | +* Learn about [composite containers for modular architecture](https://www.slideshare.net/Docker/slideshare-burns). |
| 203 | +* See [Configuring a Pod to Use a Volume for Storage](/docs/tasks/configure-pod-container/configure-volume-storage/). |
| 204 | +* See [Configure a Pod to share process namespace between containers in a Pod](/docs/tasks/configure-pod-container/share-process-namespace/) |
| 205 | +* See [Volume](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core). |
| 206 | +* See [Pod](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#pod-v1-core). |
| 207 | +--> |
| 208 | +* 进一步了解[复合容器的模式](https://kubernetes.io/blog/2015/06/the-distributed-system-toolkit-patterns.html) |
| 209 | +* 学习[模块化架构中的复合容器](https://www.slideshare.net/Docker/slideshare-burns) |
| 210 | +* 参见[配置 Pod 使用卷来存储数据](/zh/docs/tasks/configure-pod-container/configure-volume-storage/) |
| 211 | +* 参考 [Volume](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core) |
| 212 | +* 参考 [Pod](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#pod-v1-core) |
169 | 213 |
|
0 commit comments