|
| 1 | +--- |
| 2 | +title: 将 PersistentVolume 的访问模式更改为 ReadWriteOncePod |
| 3 | +content_type: task |
| 4 | +weight: 90 |
| 5 | +min-kubernetes-server-version: v1.22 |
| 6 | +--- |
| 7 | +<!-- |
| 8 | +title: Change the Access Mode of a PersistentVolume to ReadWriteOncePod |
| 9 | +content_type: task |
| 10 | +weight: 90 |
| 11 | +min-kubernetes-server-version: v1.22 |
| 12 | +--> |
| 13 | + |
| 14 | +<!-- overview --> |
| 15 | + |
| 16 | +<!-- |
| 17 | +This page shows how to change the access mode on an existing PersistentVolume to |
| 18 | +use `ReadWriteOncePod`. |
| 19 | +--> |
| 20 | +本文演示了如何将现有 PersistentVolume 的访问模式更改为使用 `ReadWriteOncePod`。 |
| 21 | + |
| 22 | +## {{% heading "prerequisites" %}} |
| 23 | + |
| 24 | +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} |
| 25 | + |
| 26 | +{{< note >}} |
| 27 | +<!-- |
| 28 | +The `ReadWriteOncePod` access mode graduated to stable in the Kubernetes v1.29 |
| 29 | +release. If you are running a version of Kubernetes older than v1.29, you might |
| 30 | +need to enable a feature gate. Check the documentation for your version of |
| 31 | +Kubernetes. |
| 32 | +--> |
| 33 | +`ReadWriteOncePod` 访问模式在 Kubernetes v1.29 版本中已进阶至 Stable。 |
| 34 | +如果你运行的 Kubernetes 版本早于 v1.29,你可能需要启用一个特性门控。 |
| 35 | +请查阅你所用 Kubernetes 版本的文档。 |
| 36 | +{{< /note >}} |
| 37 | + |
| 38 | +{{< note >}} |
| 39 | +<!-- |
| 40 | +The `ReadWriteOncePod` access mode is only supported for |
| 41 | +{{< glossary_tooltip text="CSI" term_id="csi" >}} volumes. |
| 42 | +To use this volume access mode you will need to update the following |
| 43 | +[CSI sidecars](https://kubernetes-csi.github.io/docs/sidecar-containers.html) |
| 44 | +to these versions or greater: |
| 45 | +--> |
| 46 | +`ReadWriteOncePod` 访问模式仅支持 {{< glossary_tooltip text="CSI" term_id="csi" >}} 卷。 |
| 47 | +要使用这种卷访问模式,你需要更新以下 |
| 48 | +[CSI 边车](https://kubernetes-csi.github.io/docs/sidecar-containers.html)至下述版本或更高版本: |
| 49 | + |
| 50 | +* [csi-provisioner:v3.0.0+](https://github.com/kubernetes-csi/external-provisioner/releases/tag/v3.0.0) |
| 51 | +* [csi-attacher:v3.3.0+](https://github.com/kubernetes-csi/external-attacher/releases/tag/v3.3.0) |
| 52 | +* [csi-resizer:v1.3.0+](https://github.com/kubernetes-csi/external-resizer/releases/tag/v1.3.0) |
| 53 | +{{< /note >}} |
| 54 | + |
| 55 | +<!-- |
| 56 | +## Why should I use `ReadWriteOncePod`? |
| 57 | +
|
| 58 | +Prior to Kubernetes v1.22, the `ReadWriteOnce` access mode was commonly used to |
| 59 | +restrict PersistentVolume access for workloads that required single-writer |
| 60 | +access to storage. However, this access mode had a limitation: it restricted |
| 61 | +volume access to a single *node*, allowing multiple pods on the same node to |
| 62 | +read from and write to the same volume simultaneously. This could pose a risk |
| 63 | +for applications that demand strict single-writer access for data safety. |
| 64 | +
|
| 65 | +If ensuring single-writer access is critical for your workloads, consider |
| 66 | +migrating your volumes to `ReadWriteOncePod`. |
| 67 | +--> |
| 68 | +## 我为什么要使用 `ReadWriteOncePod`? {#why-should-i-use-readwriteoncepod} |
| 69 | + |
| 70 | +在 Kubernetes v1.22 之前,`ReadWriteOnce` |
| 71 | +访问模式通常用于限制需要单个写者存储访问模式的工作负载对 PersistentVolume 的访问。 |
| 72 | +然而,这种访问模式有一个限制:它要求只能从单个**节点**上访问卷,但允许同一节点上的多个 Pod 同时读写同一个卷。 |
| 73 | +对于需要严格遵循单个写者访问模式以确保数据安全的应用,这种模式可能形成风险。 |
| 74 | + |
| 75 | +如果确保单个写者访问模式对于你的工作负载至关重要,请考虑将你的卷迁移到 `ReadWriteOncePod`。 |
| 76 | + |
| 77 | +<!-- steps --> |
| 78 | + |
| 79 | +<!-- |
| 80 | +## Migrating existing PersistentVolumes |
| 81 | +
|
| 82 | +If you have existing PersistentVolumes, they can be migrated to use |
| 83 | +`ReadWriteOncePod`. Only migrations from `ReadWriteOnce` to `ReadWriteOncePod` |
| 84 | +are supported. |
| 85 | +
|
| 86 | +In this example, there is already a `ReadWriteOnce` "cat-pictures-pvc" |
| 87 | +PersistentVolumeClaim that is bound to a "cat-pictures-pv" PersistentVolume, |
| 88 | +and a "cat-pictures-writer" Deployment that uses this PersistentVolumeClaim. |
| 89 | +--> |
| 90 | +## 迁移现有 PersistentVolume {#migrating-existing-persistentvolumes} |
| 91 | + |
| 92 | +如果你有一些 PersistentVolume,可以将它们迁移为使用 `ReadWriteOncePod`。 |
| 93 | +系统仅支持从 `ReadWriteOnce` 迁移到 `ReadWriteOncePod`。 |
| 94 | + |
| 95 | +在此示例中,已经有一个 `ReadWriteOnce` 的 "cat-pictures-pvc" PersistentVolumeClaim |
| 96 | +被绑定到了 "cat-pictures-pv" PersistentVolume,还有一个使用此 |
| 97 | +PersistentVolumeClaim 的 "cat-pictures-writer" Deployment。 |
| 98 | + |
| 99 | +{{< note >}} |
| 100 | +<!-- |
| 101 | +If your storage plugin supports |
| 102 | +[Dynamic provisioning](/docs/concepts/storage/dynamic-provisioning/), |
| 103 | +the "cat-picutres-pv" will be created for you, but its name may differ. To get |
| 104 | +your PersistentVolume's name run: |
| 105 | +--> |
| 106 | +如果你的存储插件支持[动态制备](/zh-cn/docs/concepts/storage/dynamic-provisioning/), |
| 107 | +系统将为你创建 "cat-pictures-pv",但其名称可能不同。 |
| 108 | +要获取你的 PersistentVolume 的名称,请运行以下命令: |
| 109 | + |
| 110 | +```shell |
| 111 | +kubectl get pvc cat-pictures-pvc -o jsonpath='{.spec.volumeName}' |
| 112 | +``` |
| 113 | +{{< /note >}} |
| 114 | + |
| 115 | +<!-- |
| 116 | +And you can view the PVC before you make changes. Either view the manifest |
| 117 | +locally, or run `kubectl get pvc <name-of-pvc> -o yaml`. The output is similar |
| 118 | +to: |
| 119 | +--> |
| 120 | +你可以在进行更改之前查看 PVC。你可以在本地查看清单, |
| 121 | +或运行 `kubectl get pvc <PVC 名称> -o yaml`。这条命令的输出类似于: |
| 122 | + |
| 123 | +```yaml |
| 124 | +# cat-pictures-pvc.yaml |
| 125 | +kind: PersistentVolumeClaim |
| 126 | +apiVersion: v1 |
| 127 | +metadata: |
| 128 | + name: cat-pictures-pvc |
| 129 | +spec: |
| 130 | + accessModes: |
| 131 | + - ReadWriteOnce |
| 132 | + resources: |
| 133 | + requests: |
| 134 | + storage: 1Gi |
| 135 | +``` |
| 136 | +
|
| 137 | +<!-- |
| 138 | +Here's an example Deployment that relies on that PersistentVolumeClaim: |
| 139 | +--> |
| 140 | +以下是一个依赖于此 PersistentVolumeClaim 的 Deployment 示例: |
| 141 | +
|
| 142 | +```yaml |
| 143 | +# cat-pictures-writer-deployment.yaml |
| 144 | +apiVersion: apps/v1 |
| 145 | +kind: Deployment |
| 146 | +metadata: |
| 147 | + name: cat-pictures-writer |
| 148 | +spec: |
| 149 | + replicas: 3 |
| 150 | + selector: |
| 151 | + matchLabels: |
| 152 | + app: cat-pictures-writer |
| 153 | + template: |
| 154 | + metadata: |
| 155 | + labels: |
| 156 | + app: cat-pictures-writer |
| 157 | + spec: |
| 158 | + containers: |
| 159 | + - name: nginx |
| 160 | + image: nginx:1.14.2 |
| 161 | + ports: |
| 162 | + - containerPort: 80 |
| 163 | + volumeMounts: |
| 164 | + - name: cat-pictures |
| 165 | + mountPath: /mnt |
| 166 | + volumes: |
| 167 | + - name: cat-pictures |
| 168 | + persistentVolumeClaim: |
| 169 | + claimName: cat-pictures-pvc |
| 170 | + readOnly: false |
| 171 | +``` |
| 172 | +
|
| 173 | +<!-- |
| 174 | +As a first step, you need to edit your PersistentVolume's |
| 175 | +`spec.persistentVolumeReclaimPolicy` and set it to `Retain`. This ensures your |
| 176 | +PersistentVolume will not be deleted when you delete the corresponding |
| 177 | +PersistentVolumeClaim: |
| 178 | +--> |
| 179 | +第一步,你需要编辑 PersistentVolume 的 `spec.persistentVolumeReclaimPolicy` |
| 180 | +并将其设置为 `Retain`。此字段确保你在删除相应的 PersistentVolumeClaim 时不会删除 PersistentVolume: |
| 181 | + |
| 182 | +```shell |
| 183 | +kubectl patch pv cat-pictures-pv -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}' |
| 184 | +``` |
| 185 | + |
| 186 | +<!-- |
| 187 | +Next you need to stop any workloads that are using the PersistentVolumeClaim |
| 188 | +bound to the PersistentVolume you want to migrate, and then delete the |
| 189 | +PersistentVolumeClaim. Avoid making any other changes to the |
| 190 | +PersistentVolumeClaim, such as volume resizes, until after the migration is |
| 191 | +complete. |
| 192 | + |
| 193 | +Once that is done, you need to clear your PersistentVolume's `spec.claimRef.uid` |
| 194 | +to ensure PersistentVolumeClaims can bind to it upon recreation: |
| 195 | +--> |
| 196 | +接下来,你需要停止正在使用绑定到你要迁移的这个 PersistentVolume 上的 |
| 197 | +PersistentVolumeClaim 的所有工作负载,然后删除该 PersistentVolumeClaim。 |
| 198 | +在迁移完成之前,不要对 PersistentVolumeClaim 进行任何其他更改,例如调整卷的大小。 |
| 199 | + |
| 200 | +完成后,你需要清除 PersistentVolume 的 `spec.claimRef.uid` |
| 201 | +以确保在重新创建时 PersistentVolumeClaim 能够绑定到它: |
| 202 | + |
| 203 | +```shell |
| 204 | +kubectl scale --replicas=0 deployment cat-pictures-writer |
| 205 | +kubectl delete pvc cat-pictures-pvc |
| 206 | +kubectl patch pv cat-pictures-pv -p '{"spec":{"claimRef":{"uid":""}}}' |
| 207 | +``` |
| 208 | + |
| 209 | +<!-- |
| 210 | +After that, replace the PersistentVolume's list of valid access modes to be |
| 211 | +(only) `ReadWriteOncePod`: |
| 212 | +--> |
| 213 | +之后,将 PersistentVolume 的有效访问模式列表替换为(仅)`ReadWriteOncePod`: |
| 214 | + |
| 215 | +```shell |
| 216 | +kubectl patch pv cat-pictures-pv -p '{"spec":{"accessModes":["ReadWriteOncePod"]}}' |
| 217 | +``` |
| 218 | + |
| 219 | +{{< note >}} |
| 220 | +<!-- |
| 221 | +The `ReadWriteOncePod` access mode cannot be combined with other access modes. |
| 222 | +Make sure `ReadWriteOncePod` is the only access mode on the PersistentVolume |
| 223 | +when updating, otherwise the request will fail. |
| 224 | +--> |
| 225 | +`ReadWriteOncePod` 访问模式不能与其他访问模式结合使用。 |
| 226 | +你要确保在更新时 `ReadWriteOncePod` 是 PersistentVolume 上的唯一访问模式,否则请求将失败。 |
| 227 | +{{< /note >}} |
| 228 | + |
| 229 | +<!-- |
| 230 | +Next you need to modify your PersistentVolumeClaim to set `ReadWriteOncePod` as |
| 231 | +the only access mode. You should also set the PersistentVolumeClaim's |
| 232 | +`spec.volumeName` to the name of your PersistentVolume to ensure it binds to |
| 233 | +this specific PersistentVolume. |
| 234 | + |
| 235 | +Once this is done, you can recreate your PersistentVolumeClaim and start up your |
| 236 | +workloads: |
| 237 | +--> |
| 238 | +接下来,你需要修改 PersistentVolumeClaim,将 `ReadWriteOncePod` 设置为唯一的访问模式。 |
| 239 | +你还应将 PersistentVolumeClaim 的 `spec.volumeName` 设置为 PersistentVolume 的名称, |
| 240 | +以确保其绑定到特定的 PersistentVolume。 |
| 241 | + |
| 242 | +完成后,你可以重新创建你的 PersistentVolumeClaim 并启动你的工作负载: |
| 243 | + |
| 244 | +<!-- |
| 245 | +# IMPORTANT: Make sure to edit your PVC in cat-pictures-pvc.yaml before applying. You need to: |
| 246 | +# - Set ReadWriteOncePod as the only access mode |
| 247 | +# - Set spec.volumeName to "cat-pictures-pv" |
| 248 | +--> |
| 249 | +```shell |
| 250 | +# 重要提示:在 apply 操作之前必须编辑在 cat-pictures-pvc.yaml 中的 PVC。你需要: |
| 251 | +# - 将 ReadWriteOncePod 设置为唯一的访问模式 |
| 252 | +# - 将 spec.volumeName 设置为 "cat-pictures-pv" |
| 253 | +
|
| 254 | +kubectl apply -f cat-pictures-pvc.yaml |
| 255 | +kubectl apply -f cat-pictures-writer-deployment.yaml |
| 256 | +``` |
| 257 | + |
| 258 | +<!-- |
| 259 | +Lastly you may edit your PersistentVolume's `spec.persistentVolumeReclaimPolicy` |
| 260 | +and set to it back to `Delete` if you previously changed it. |
| 261 | +--> |
| 262 | +最后,你可以编辑 PersistentVolume 的 `spec.persistentVolumeReclaimPolicy` 并将其设置回 `Delete`, |
| 263 | +如果你之前更改了这个字段的话。 |
| 264 | + |
| 265 | +```shell |
| 266 | +kubectl patch pv cat-pictures-pv -p '{"spec":{"persistentVolumeReclaimPolicy":"Delete"}}' |
| 267 | +``` |
| 268 | + |
| 269 | +## {{% heading "whatsnext" %}} |
| 270 | + |
| 271 | +<!-- |
| 272 | +* Learn more about [PersistentVolumes](/docs/concepts/storage/persistent-volumes/). |
| 273 | +* Learn more about [PersistentVolumeClaims](/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims). |
| 274 | +* Learn more about [Configuring a Pod to Use a PersistentVolume for Storage](/docs/tasks/configure-pod-container/configure-persistent-volume-storage/) |
| 275 | +--> |
| 276 | +* 进一步了解 [PersistentVolume](/zh-cn/docs/concepts/storage/persistent-volumes/)。 |
| 277 | +* 进一步了解 [PersistentVolumeClaim](/zh-cn/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims)。 |
| 278 | +* 进一步了解[配置 Pod 以使用 PersistentVolume 作为存储](/zh-cn/docs/tasks/configure-pod-container/configure-persistent-volume-storage/)。 |
0 commit comments