---
# System prepended metadata

title: k8s 設定 local volume
tags: [Elasticsearch]

---

---
title: k8s 設定 local volume
tags: Elasticsearch
description: View the slide with "Slide Mode".
---

# k8s 設定 local volume

<!-- Put the link to this slide here so people can follow -->

## [建置 Local Persistent Volumes](https://kubernetes.io/blog/2018/04/13/local-persistent-volumes-beta/)
## 1. 建立 `StorageClass`
```yaml=
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
```
## 2. [Prepare and set up local volumes in discovery directory](https://github.com/kubernetes-sigs/sig-storage-local-static-provisioner/blob/master/docs/operations.md#prepare-and-set-up-local-volumes-in-discovery-directory)
- `Use a whole disk as a filesystem PV`
```bash=
# 1. Format and mount
sudo mkfs.ext4 /dev/sdb
DISK_UUID=$(blkid -s UUID -o value /dev/sdb) 
sudo mkdir -p /mnt/disks/es-vol
sudo mount -t ext4 /dev/sdb /mnt/disks/es-vol

# 2. Persistent mount entry into /etc/fstab
echo UUID=`sudo blkid -s UUID -o value /dev/sdb` /mnt/disks/$DISK_UUID ext4 defaults 0 2 | sudo tee -a /etc/fstab

# 3. set the permissions
chmod -R 777 /mnt/disks/es-vol
```



## 3. 手動建置 `pv`
```yaml=
apiVersion: v1
kind: PersistentVolume
metadata:
  name: es-pv-1
spec:
  capacity:
    storage: 500Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /mnt/disks/es-vol
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node1
```
- 幾個 `pod`，就要手動產生幾個對應的 `pv`，調整 `name` 與 `nodeAffinity`的 `values`
- 當看到pv與pvc狀態為 `pending` 且訊息為 `volumeBindingMode:WaitForFirstConsumer`，代表目前沒有 `pod` 使用，
 
## 4. 在 `values.yaml` 指定 `StorageClass`
```yaml=
volumeClaimTemplate:
  accessModes: [ "ReadWriteOnce" ]
  resources:
    requests:
      storage: 30Gi
  # new
  StorageClass: local-storage
```

