@@ -13,7 +13,7 @@ weight: 60
13
13
<!-- overview -->
14
14
15
15
<!--
16
- This page shows how to configure a Pod to use a
16
+ This page shows you how to configure a Pod to use a
17
17
{{< glossary_tooltip text="PersistentVolumeClaim" term_id="persistent-volume-claim" >}}
18
18
for storage.
19
19
Here is a summary of the process:
@@ -27,7 +27,7 @@ PersistentVolume.
27
27
28
28
1. You create a Pod that uses the above PersistentVolumeClaim for storage.
29
29
-->
30
- 本文介绍如何配置 Pod 使用
30
+ 本文将向你介绍如何配置 Pod 使用
31
31
{{< glossary_tooltip text="PersistentVolumeClaim" term_id="persistent-volume-claim" >}}
32
32
作为存储。
33
33
以下是该过程的总结:
@@ -42,7 +42,8 @@ PersistentVolume.
42
42
## {{% heading "prerequisites" %}}
43
43
44
44
<!--
45
- * You need to have a Kubernetes cluster that has only one Node, and the kubectl
45
+ * You need to have a Kubernetes cluster that has only one Node, and the
46
+ {{< glossary_tooltip text="kubectl" term_id="kubectl" >}}
46
47
command-line tool must be configured to communicate with your cluster. If you
47
48
do not already have a single-node cluster, you can create one by using
48
49
[Minikube](https://minikube.sigs.k8s.io/docs/).
@@ -51,7 +52,8 @@ do not already have a single-node cluster, you can create one by using
51
52
[Persistent Volumes](/docs/concepts/storage/persistent-volumes/).
52
53
-->
53
54
54
- * 你需要一个包含单个节点的 Kubernetes 集群,并且必须配置 kubectl 命令行工具以便与集群交互。
55
+ * 你需要一个包含单个节点的 Kubernetes 集群,并且必须配置
56
+ {{< glossary_tooltip text="kubectl" term_id="kubectl" >}} 命令行工具以便与集群交互。
55
57
如果还没有单节点集群,可以使用
56
58
[ Minikube] ( https://minikube.sigs.k8s.io/docs/ ) 创建一个。
57
59
.
@@ -62,19 +64,19 @@ do not already have a single-node cluster, you can create one by using
62
64
<!--
63
65
## Create an index.html file on your Node
64
66
65
- Open a shell to the Node in your cluster. How you open a shell depends on how
66
- you set up your cluster. For example, if you are using Minikube, you can open a
67
- shell to your Node by entering `minikube ssh`.
67
+ Open a shell to the single Node in your cluster. How you open a shell depends
68
+ on how you set up your cluster. For example, if you are using Minikube, you
69
+ can open a shell to your Node by entering `minikube ssh`.
68
70
69
- In your shell, create a `/mnt/data` directory:
71
+ In your shell on that Node , create a `/mnt/data` directory:
70
72
-->
71
73
## 在你的节点上创建一个 index.html 文件
72
74
73
- 打开集群中节点的一个 Shell。
75
+ 打开集群中的某个节点的 Shell。
74
76
如何打开 Shell 取决于集群的设置。
75
77
例如,如果你正在使用 Minikube,那么可以通过输入 ` minikube ssh ` 来打开节点的 Shell。
76
78
77
- 在 Shell 中,创建一个 ` /mnt/data ` 目录:
79
+ 在该节点的 Shell 中,创建一个 ` /mnt/data ` 目录:
78
80
79
81
<!--
80
82
# This assumes that your Node uses "sudo" to run commands
@@ -94,7 +96,7 @@ In the `/mnt/data` directory, create an `index.html` file:
94
96
# This again assumes that your Node uses "sudo" to run commands
95
97
# as the superuser
96
98
-->
97
- ```
99
+ ``` shell
98
100
# 这里再次假定你的节点使用 "sudo" 来以超级用户角色执行命令
99
101
sudo sh -c " echo 'Hello from Kubernetes storage' > /mnt/data/index.html"
100
102
```
@@ -139,7 +141,7 @@ PersistentVolume uses a file or directory on the Node to emulate network-attache
139
141
-->
140
142
## 创建 PersistentVolume
141
143
142
- 在本练习中,你将创建一个 * hostPath* 类型的 PersistentVolume。
144
+ 在本练习中,你将创建一个 ** hostPath* * 类型的 PersistentVolume。
143
145
Kubernetes 支持用于在单节点集群上开发和测试的 hostPath 类型的 PersistentVolume。
144
146
hostPath 类型的 PersistentVolume 使用节点上的文件或目录来模拟网络附加存储。
145
147
@@ -149,18 +151,36 @@ would provision a network resource like a Google Compute Engine persistent disk,
149
151
an NFS share, or an Amazon Elastic Block Store volume. Cluster administrators can also
150
152
use [StorageClasses](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#storageclass-v1-storage)
151
153
to set up
152
- [dynamic provisioning](https://kubernetes.io /blog/2016/10/dynamic-provisioning-and-storage-in-kubernetes).
154
+ [dynamic provisioning](/blog/2016/10/dynamic-provisioning-and-storage-in-kubernetes).
153
155
154
156
Here is the configuration file for the hostPath PersistentVolume:
155
157
-->
156
158
在生产集群中,你不会使用 hostPath。
157
159
集群管理员会提供网络存储资源,比如 Google Compute Engine 持久盘卷、NFS 共享卷或 Amazon Elastic Block Store 卷。
158
- 集群管理员还可以使用 [ StorageClasses] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#storageclass-v1-storage) 来设置[ 动态提供存储] ( https://kubernetes.io/blog/2016/10/dynamic-provisioning-and-storage-in-kubernetes ) 。
160
+ 集群管理员还可以使用
161
+ [ StorageClasses] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#storageclass-v1-storage)
162
+ 来设置[ 动态提供存储] ( /blog/2016/10/dynamic-provisioning-and-storage-in-kubernetes ) 。
159
163
160
164
下面是 hostPath PersistentVolume 的配置文件:
161
165
162
166
{{< codenew file="pods/storage/pv-volume.yaml" >}}
163
167
168
+ <!--
169
+ The configuration file specifies that the volume is at `/mnt/data` on the
170
+ cluster's Node. The configuration also specifies a size of 10 gibibytes and
171
+ an access mode of `ReadWriteOnce`, which means the volume can be mounted as
172
+ read-write by a single Node. It defines the [StorageClass name](/docs/concepts/storage/persistent-volumes/#class)
173
+ `manual` for the PersistentVolume, which will be used to bind
174
+ PersistentVolumeClaim requests to this PersistentVolume.
175
+ -->
176
+ 配置文件指定卷位于集群节点上的 ` /mnt/data ` 路径。
177
+ 配置还指定了卷的容量大小为 10 GB,
178
+ 访问模式为 ` ReadWriteOnce ` ,
179
+ 这意味着该卷可以被单个节点以读写方式安装。
180
+ 配置文件还在 PersistentVolume 中定义了
181
+ [ StorageClass 的名称] ( /zh-cn/docs/concepts/storage/persistent-volumes/#class )
182
+ 为 ` manual ` 。它将用于将 PersistentVolumeClaim 的请求绑定到此 PersistentVolume。
183
+
164
184
<!--
165
185
Create the PersistentVolume:
166
186
-->
@@ -216,7 +236,7 @@ Create the PersistentVolumeClaim:
216
236
创建 PersistentVolumeClaim:
217
237
218
238
``` shell
219
- kubectl create -f https://k8s.io/examples/pods/storage/pv-claim.yaml
239
+ kubectl apply -f https://k8s.io/examples/pods/storage/pv-claim.yaml
220
240
```
221
241
222
242
<!--
@@ -252,7 +272,7 @@ Look at the PersistentVolumeClaim:
252
272
-->
253
273
查看 PersistentVolumeClaim:
254
274
255
- ```
275
+ ``` shell
256
276
kubectl get pvc task-pv-claim
257
277
```
258
278
@@ -299,7 +319,7 @@ kubectl apply -f https://k8s.io/examples/pods/storage/pv-pod.yaml
299
319
```
300
320
301
321
<!--
302
- Verify that the Container in the Pod is running;
322
+ Verify that the container in the Pod is running;
303
323
-->
304
324
检查 Pod 中的容器是否运行正常:
305
325
@@ -308,7 +328,7 @@ kubectl get pod task-pv-pod
308
328
```
309
329
310
330
<!--
311
- Get a shell to the Container running in your Pod:
331
+ Get a shell to the container running in your Pod:
312
332
-->
313
333
打开一个 Shell 访问 Pod 中的容器:
314
334
@@ -326,11 +346,11 @@ hostPath volume:
326
346
# Be sure to run these 3 commands inside the root shell that comes from
327
347
# running "kubectl exec" in the previous step
328
348
-->
329
- ```
349
+ ``` shell
330
350
# 一定要在上一步 "kubectl exec" 所返回的 Shell 中执行下面三个命令
331
- root@task-pv-pod:/# apt-get update
332
- root@task-pv-pod:/# apt-get install curl
333
- root@task-pv-pod:/# curl localhost
351
+ apt update
352
+ apt install curl
353
+ curl http:// localhost/
334
354
```
335
355
336
356
<!--
@@ -374,7 +394,10 @@ In the shell on your Node, remove the file and directory that you created:
374
394
如果你还没有连接到集群中节点的 Shell,可以按之前所做操作,打开一个新的 Shell。
375
395
376
396
在节点的 Shell 上,删除你所创建的目录和文件:
377
-
397
+ <!--
398
+ # This assumes that your Node uses "sudo" to run commands
399
+ # as the superuser
400
+ -->
378
401
379
402
``` shell
380
403
# 这里假定你使用 "sudo" 来以超级用户的角色执行命令
@@ -390,7 +413,6 @@ You can now close the shell to your Node.
390
413
<!--
391
414
## Mounting the same persistentVolume in two places
392
415
-->
393
-
394
416
## 在两个地方挂载相同的 persistentVolume
395
417
396
418
{{< codenew file="pods/storage/pv-duplicate.yaml" >}}
@@ -427,8 +449,8 @@ GID 不匹配或缺失将会导致无权访问错误。
427
449
这样 GID 就能自动添加到使用 PersistentVolume 的任何 Pod 中。
428
450
429
451
使用 ` pv.beta.kubernetes.io/gid ` 注解的方法如下所示:
430
-
431
452
``` yaml
453
+ apiVersion : v1
432
454
kind : PersistentVolume
433
455
apiVersion : v1
434
456
metadata :
@@ -439,10 +461,10 @@ metadata:
439
461
440
462
<!--
441
463
When a Pod consumes a PersistentVolume that has a GID annotation, the annotated GID
442
- is applied to all Containers in the Pod in the same way that GIDs specified in the
464
+ is applied to all containers in the Pod in the same way that GIDs specified in the
443
465
Pod's security context are. Every GID, whether it originates from a PersistentVolume
444
466
annotation or the Pod's specification, is applied to the first process run in
445
- each Container .
467
+ each container .
446
468
-->
447
469
当 Pod 使用带有 GID 注解的 PersistentVolume 时,注解的 GID 会被应用于 Pod 中的所有容器,
448
470
应用的方法与 Pod 的安全上下文中指定的 GID 相同。
@@ -476,5 +498,3 @@ PersistentVolume are not present on the Pod resource itself.
476
498
* [PersistentVolumeSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumespec-v1-core)
477
499
* [PersistentVolumeClaim](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumeclaim-v1-core)
478
500
* [PersistentVolumeClaimSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumeclaimspec-v1-core)
479
-
480
-
0 commit comments