> For the complete documentation index, see [llms.txt](https://utm-1.gitbook.io/utm-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://utm-1.gitbook.io/utm-docs/dokumentaciya/utm-it/resheniya/kubernetes/dlya-prorabotki/persistentvolumes.md).

# PersistentVolumes

## Ссылки

Работа с хранилищами в Kubernetes: руководство для инженеров: [habr.com](https://habr.com/ru/companies/T1Holding/articles/781368/)

Разворачиваем PostgreSQL, Redis и RabbitMQ в Kubernetes-кластер: [habr.com](https://habr.com/ru/companies/domclick/articles/649167/?ysclid=mhokjmvpnc613667604)

Работа с хранилищами в Kubernetes: [habr.com486/](https://habr.com/ru/companies/otus/articles/717486/)

deckhouse / yandex-csi-driver: [github.com](https://github.com/deckhouse/yandex-csi-driver?ysclid=mhonfcjufl687978091)

Drivers: [kubernetes-csi.github.iol](https://kubernetes-csi.github.io/docs/drivers.html)

Volumes: [kubernetes.io](https://kubernetes.io/docs/concepts/storage/volumes/#local)

## Заметки

### Подготовка - создание StorageClass и PersistentVolume

Подготовить манифест файл sc.yaml для создания ресурса `StorageClass`:

```yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
```

Создать ресурс `StorageClass`:

```bash
kubectl apply -f sc.yaml
```

Проверит состояние sc:

```bash
kubectl get sc
```

Подготовить манифест файл pv.yaml для создания ресурса `Persistent Volume`:

```yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-test-4gi
  labels:
    type: local
spec:
  capacity:
    storage: 4Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /var/pv-local-storages/pv-test-4gi
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - utm-k8s-node3-5
```

В `matchExpressions` указать название узла, на которой будет монтироваться диск.

Посмотреть имя доступных узлов можно с помощью команды:

```bash
kubectl get nodes
```

Монтирование будет выполнено в директорию /var/pv-local-storages/pv-test-4gi на узле utm-k8s-node3-5.&#x20;

Необходимо зайти на узел utm-k8s-node3-5 и создать каталог:

```bash
mkdir -p /var/pv-local-storages/pv-test-4gi
```

Создать ресурс `Persistent Volume`:

```bash
kubectl apply -f pv.yaml
```

Проверит состояние pv:

```bash
kubectl get pv
```

### Резервирование - создание PersistentVolumeClaim

Подготовить манифест файл pvc.yaml для создания ресурса `Persistent Volume Claim`:

```yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-test-4gi
spec:
  storageClassName: local-storage
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi
```

Создать ресурс `Persistent Volume Claim`:

```bash
kubectl apply -f pvc.yaml
```

Проверит состояние pv:

```bash
kubectl get pvc
```

Ресурс `PVC` в ожидании привязки: STATUS = Pending

### Использование

Создать pod, который будет монтировать volume на основе подготовленного persistentVolumeClaim

Pod будут запускать /bin/bash и делать продолжительную паузу.

Подготовить манифест файл pod.yaml для создания ресурса `Pod`:

```yaml
kind: Pod
apiVersion: v1
metadata:
  name: pod-test-pvc
spec:
  containers:
    - name: app
      image: alpine
      volumeMounts:
      - name: mystorage
        mountPath: /mnt
      command: ["/bin/sh"]
      args: ["-c", "sleep 100000"]
  volumes:
  - name: mystorage
    persistentVolumeClaim:
      claimName: pvc-test-4gi
```

Создать ресурс `Pod`:

```bash
kubectl apply -f pod.yaml 
```

Для проверки  работы Local Persistent Volume создать текстовый файл.

```bash
kubectl exec -it pod-test-pvc sh
echo "test file created" >> /mnt/local.txt
```

Подготовить манифест файл pod2.yaml для создания еще одного ресурса `Pod`:

```yaml
kind: Pod
apiVersion: v1
metadata:
  name: pod-test-pvc2
spec:
  containers:
    - name: app
      image: alpine
      volumeMounts:
      - name: mystorage
        mountPath: /mnt
      command: ["/bin/sh"]
      args: ["-c", "sleep 100000"]
  volumes:
  - name: mystorage
    persistentVolumeClaim:
      claimName: pvc-test-4gi
```

Создать ресурс `Pod`:

```bash
kubectl apply -f pod2.yaml 
```

Посмотреть список подов:

```bash
kubectl get pod
```

Оба пода должны быть в статусе STATUS=Running

Зайти на pod-test-pvc2 и посмотреть содержимое /mnt/local.txt

```bash
kubectl exec -it pod-test-pvc2 sh
cat /mnt/local.txt

# test file created
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://utm-1.gitbook.io/utm-docs/dokumentaciya/utm-it/resheniya/kubernetes/dlya-prorabotki/persistentvolumes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
