|
| 1 | +--- |
| 2 | +title: Configurer un Pod pour utiliser un stockage de type PersistentVolume |
| 3 | +content_type: task |
| 4 | +weight: 60 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- overview --> |
| 8 | + |
| 9 | +Cette page montre comment configurer un Pod afin qu'il utilise un {{< glossary_tooltip text="PersistentVolumeClaim" term_id="persistent-volume-claim" >}} comme stockage. |
| 10 | + |
| 11 | +Voici un resume des etapes: |
| 12 | + |
| 13 | +1. En tant qu'administrateur d'un cluster, vous creez un PersistentVolume qui pointe vers un systeme de stockage physique. Vous n'associez le volume avec aucun pod pour le moment. |
| 14 | + |
| 15 | +1. En tant que developer / utilisateur du cluster, vous creez un PersistentVolumeClaim qui sera automatiquement lie a un PersistentVolume adapte. |
| 16 | + |
| 17 | +1. Vous creez un Pod qui utilise le PersistentVolumeClaim cree precedemment comme stockage. |
| 18 | + |
| 19 | + |
| 20 | +## {{% heading "prerequisites" %}} |
| 21 | + |
| 22 | + |
| 23 | +* Vous devez avoir a disposition un cluster qui n'a qu'un seul noeud, et l'utilitaire en ligne de commande |
| 24 | +{{< glossary_tooltip text="kubectl" term_id="kubectl" >}} doit etre configure pour communiquer avec votre cluster. Si vous n'avez pas deja de cluster a disposition, vous pouvez en creer un en utilisant [Minikube](https://minikube.sigs.k8s.io/docs/). |
| 25 | + |
| 26 | +* Vous pouvez vous familiariser avec la documentation des |
| 27 | +[Persistent Volumes](/docs/concepts/storage/persistent-volumes/). |
| 28 | + |
| 29 | +<!-- steps --> |
| 30 | + |
| 31 | +## Creer un fichier index.html sur votre noeud |
| 32 | + |
| 33 | +Open a shell to the single Node in your cluster. How you open a shell depends |
| 34 | +on how you set up your cluster. For example, if you are using Minikube, you |
| 35 | +can open a shell to your Node by entering `minikube ssh`. |
| 36 | + |
| 37 | +In your shell on that Node, create a `/mnt/data` directory: |
| 38 | + |
| 39 | +```shell |
| 40 | +# This assumes that your Node uses "sudo" to run commands |
| 41 | +# as the superuser |
| 42 | +sudo mkdir /mnt/data |
| 43 | +``` |
| 44 | + |
| 45 | +Dans le dossier `/mnt/data`, creez un fichier `index.html`: |
| 46 | +```shell |
| 47 | +# This again assumes that your Node uses "sudo" to run commands |
| 48 | +# as the superuser |
| 49 | +sudo sh -c "echo 'Hello from Kubernetes storage' > /mnt/data/index.html" |
| 50 | +``` |
| 51 | + |
| 52 | +{{< note >}} |
| 53 | +Si votre noeud utilise un utilitaire d'acces privilegie autre que `sudo`, les commandes notees ici fonctionneront en remplacant `sudo` par le nom de l'utilitaire. |
| 54 | +{{< /note >}} |
| 55 | + |
| 56 | +Testez que le fichier `index.html` existe: |
| 57 | +```shell |
| 58 | +cat /mnt/data/index.html |
| 59 | +``` |
| 60 | + |
| 61 | +Le resultat de la commande doit etre: |
| 62 | +``` |
| 63 | +Hello from Kubernetes storage |
| 64 | +``` |
| 65 | + |
| 66 | +Vous pouvez maintenant fermer l'acces shell a votre Noeud. |
| 67 | + |
| 68 | +## Creer un PersistentVolume |
| 69 | + |
| 70 | +In this exercise, you create a *hostPath* PersistentVolume. Kubernetes supports |
| 71 | +hostPath for development and testing on a single-node cluster. A hostPath |
| 72 | +PersistentVolume uses a file or directory on the Node to emulate network-attached storage. |
| 73 | + |
| 74 | +In a production cluster, you would not use hostPath. Instead a cluster administrator |
| 75 | +would provision a network resource like a Google Compute Engine persistent disk, |
| 76 | +an NFS share, or an Amazon Elastic Block Store volume. Cluster administrators can also |
| 77 | +use [StorageClasses](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#storageclass-v1-storage-k8s-io) |
| 78 | +to set up |
| 79 | +[dynamic provisioning](/docs/concepts/storage/dynamic-provisioning/). |
| 80 | + |
| 81 | +Here is the configuration file for the hostPath PersistentVolume: |
| 82 | + |
| 83 | +{{< codenew file="pods/storage/pv-volume.yaml" >}} |
| 84 | + |
| 85 | +The configuration file specifies that the volume is at `/mnt/data` on the |
| 86 | +cluster's Node. The configuration also specifies a size of 10 gibibytes and |
| 87 | +an access mode of `ReadWriteOnce`, which means the volume can be mounted as |
| 88 | +read-write by a single Node. It defines the [StorageClass name](/docs/concepts/storage/persistent-volumes/#class) |
| 89 | +`manual` for the PersistentVolume, which will be used to bind |
| 90 | +PersistentVolumeClaim requests to this PersistentVolume. |
| 91 | + |
| 92 | +Create the PersistentVolume: |
| 93 | + |
| 94 | +```shell |
| 95 | +kubectl apply -f https://k8s.io/examples/pods/storage/pv-volume.yaml |
| 96 | +``` |
| 97 | + |
| 98 | +View information about the PersistentVolume: |
| 99 | + |
| 100 | +```shell |
| 101 | +kubectl get pv task-pv-volume |
| 102 | +``` |
| 103 | + |
| 104 | +The output shows that the PersistentVolume has a `STATUS` of `Available`. This |
| 105 | +means it has not yet been bound to a PersistentVolumeClaim. |
| 106 | + |
| 107 | + NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE |
| 108 | + task-pv-volume 10Gi RWO Retain Available manual 4s |
| 109 | + |
| 110 | +## Create a PersistentVolumeClaim |
| 111 | + |
| 112 | +The next step is to create a PersistentVolumeClaim. Pods use PersistentVolumeClaims |
| 113 | +to request physical storage. In this exercise, you create a PersistentVolumeClaim |
| 114 | +that requests a volume of at least three gibibytes that can provide read-write |
| 115 | +access for at least one Node. |
| 116 | + |
| 117 | +Here is the configuration file for the PersistentVolumeClaim: |
| 118 | + |
| 119 | +{{< codenew file="pods/storage/pv-claim.yaml" >}} |
| 120 | + |
| 121 | +Create the PersistentVolumeClaim: |
| 122 | + |
| 123 | + kubectl apply -f https://k8s.io/examples/pods/storage/pv-claim.yaml |
| 124 | + |
| 125 | +After you create the PersistentVolumeClaim, the Kubernetes control plane looks |
| 126 | +for a PersistentVolume that satisfies the claim's requirements. If the control |
| 127 | +plane finds a suitable PersistentVolume with the same StorageClass, it binds the |
| 128 | +claim to the volume. |
| 129 | + |
| 130 | +Look again at the PersistentVolume: |
| 131 | + |
| 132 | +```shell |
| 133 | +kubectl get pv task-pv-volume |
| 134 | +``` |
| 135 | + |
| 136 | +Now the output shows a `STATUS` of `Bound`. |
| 137 | + |
| 138 | + NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE |
| 139 | + task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim manual 2m |
| 140 | + |
| 141 | +Look at the PersistentVolumeClaim: |
| 142 | + |
| 143 | +```shell |
| 144 | +kubectl get pvc task-pv-claim |
| 145 | +``` |
| 146 | + |
| 147 | +The output shows that the PersistentVolumeClaim is bound to your PersistentVolume, |
| 148 | +`task-pv-volume`. |
| 149 | + |
| 150 | + NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS AGE |
| 151 | + task-pv-claim Bound task-pv-volume 10Gi RWO manual 30s |
| 152 | + |
| 153 | +## Create a Pod |
| 154 | + |
| 155 | +The next step is to create a Pod that uses your PersistentVolumeClaim as a volume. |
| 156 | + |
| 157 | +Here is the configuration file for the Pod: |
| 158 | + |
| 159 | +{{< codenew file="pods/storage/pv-pod.yaml" >}} |
| 160 | + |
| 161 | +Notice that the Pod's configuration file specifies a PersistentVolumeClaim, but |
| 162 | +it does not specify a PersistentVolume. From the Pod's point of view, the claim |
| 163 | +is a volume. |
| 164 | + |
| 165 | +Create the Pod: |
| 166 | + |
| 167 | +```shell |
| 168 | +kubectl apply -f https://k8s.io/examples/pods/storage/pv-pod.yaml |
| 169 | +``` |
| 170 | + |
| 171 | +Verify that the container in the Pod is running; |
| 172 | + |
| 173 | +```shell |
| 174 | +kubectl get pod task-pv-pod |
| 175 | +``` |
| 176 | + |
| 177 | +Get a shell to the container running in your Pod: |
| 178 | + |
| 179 | +```shell |
| 180 | +kubectl exec -it task-pv-pod -- /bin/bash |
| 181 | +``` |
| 182 | + |
| 183 | +In your shell, verify that nginx is serving the `index.html` file from the |
| 184 | +hostPath volume: |
| 185 | + |
| 186 | +```shell |
| 187 | +# Be sure to run these 3 commands inside the root shell that comes from |
| 188 | +# running "kubectl exec" in the previous step |
| 189 | +apt update |
| 190 | +apt install curl |
| 191 | +curl http://localhost/ |
| 192 | +``` |
| 193 | + |
| 194 | +The output shows the text that you wrote to the `index.html` file on the |
| 195 | +hostPath volume: |
| 196 | + |
| 197 | + Hello from Kubernetes storage |
| 198 | + |
| 199 | + |
| 200 | +If you see that message, you have successfully configured a Pod to |
| 201 | +use storage from a PersistentVolumeClaim. |
| 202 | + |
| 203 | +## Clean up |
| 204 | + |
| 205 | +Delete the Pod, the PersistentVolumeClaim and the PersistentVolume: |
| 206 | + |
| 207 | +```shell |
| 208 | +kubectl delete pod task-pv-pod |
| 209 | +kubectl delete pvc task-pv-claim |
| 210 | +kubectl delete pv task-pv-volume |
| 211 | +``` |
| 212 | + |
| 213 | +If you don't already have a shell open to the Node in your cluster, |
| 214 | +open a new shell the same way that you did earlier. |
| 215 | + |
| 216 | +In the shell on your Node, remove the file and directory that you created: |
| 217 | + |
| 218 | +```shell |
| 219 | +# This assumes that your Node uses "sudo" to run commands |
| 220 | +# as the superuser |
| 221 | +sudo rm /mnt/data/index.html |
| 222 | +sudo rmdir /mnt/data |
| 223 | +``` |
| 224 | + |
| 225 | +You can now close the shell to your Node. |
| 226 | + |
| 227 | +## Mounting the same persistentVolume in two places |
| 228 | + |
| 229 | +{{< codenew file="pods/storage/pv-duplicate.yaml" >}} |
| 230 | + |
| 231 | +You can perform 2 volume mounts on your nginx container: |
| 232 | + |
| 233 | +`/usr/share/nginx/html` for the static website |
| 234 | +`/etc/nginx/nginx.conf` for the default config |
| 235 | + |
| 236 | +<!-- discussion --> |
| 237 | + |
| 238 | +## Access control |
| 239 | + |
| 240 | +Storage configured with a group ID (GID) allows writing only by Pods using the same |
| 241 | +GID. Mismatched or missing GIDs cause permission denied errors. To reduce the |
| 242 | +need for coordination with users, an administrator can annotate a PersistentVolume |
| 243 | +with a GID. Then the GID is automatically added to any Pod that uses the |
| 244 | +PersistentVolume. |
| 245 | + |
| 246 | +Use the `pv.beta.kubernetes.io/gid` annotation as follows: |
| 247 | +```yaml |
| 248 | +apiVersion: v1 |
| 249 | +kind: PersistentVolume |
| 250 | +metadata: |
| 251 | + name: pv1 |
| 252 | + annotations: |
| 253 | + pv.beta.kubernetes.io/gid: "1234" |
| 254 | +``` |
| 255 | +When a Pod consumes a PersistentVolume that has a GID annotation, the annotated GID |
| 256 | +is applied to all containers in the Pod in the same way that GIDs specified in the |
| 257 | +Pod's security context are. Every GID, whether it originates from a PersistentVolume |
| 258 | +annotation or the Pod's specification, is applied to the first process run in |
| 259 | +each container. |
| 260 | +
|
| 261 | +{{< note >}} |
| 262 | +When a Pod consumes a PersistentVolume, the GIDs associated with the |
| 263 | +PersistentVolume are not present on the Pod resource itself. |
| 264 | +{{< /note >}} |
| 265 | +
|
| 266 | +
|
| 267 | +
|
| 268 | +
|
| 269 | +## {{% heading "whatsnext" %}} |
| 270 | +
|
| 271 | +
|
| 272 | +* Pour en savoir plus sur les [PersistentVolumes](/docs/concepts/storage/persistent-volumes/). |
| 273 | +* Lire la [documentation de conception sur le stockage persistant](https://git.k8s.io/design-proposals-archive/storage/persistent-storage.md). |
| 274 | +
|
| 275 | +### Reference |
| 276 | +
|
| 277 | +* [PersistentVolume](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolume-v1-core) |
| 278 | +* [PersistentVolumeSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumespec-v1-core) |
| 279 | +* [PersistentVolumeClaim](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumeclaim-v1-core) |
| 280 | +* [PersistentVolumeClaimSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#persistentvolumeclaimspec-v1-core) |
| 281 | +
|
| 282 | +
|
| 283 | +
|
| 284 | +
|
0 commit comments