# 應用二 (etcd backup、volume、security context) ###### tags: `Kubernetes` ## 1. etcd backup ## 2. Volume [參考連結](https://kubernetes.io/zh-cn/docs/concepts/storage/volumes/) 不知道為甚麼我只能用 DirectoryOrCreate,不能先創好一個再 mount 上去 --- 讓集群管理者可以創建一個 **storage pool**,使用者根據需求從 **storage pool** 擷取一部分使用,這就是 **Persistent Volumes(PV)** > Persistent Volumes(PV) 是一個 pool of Volumes > 使用者可以透過 Persistent Volumes Claim(PVC) 在PV上選擇一個區塊作為storage儲存資料,並在其中佈署自己的Applications > 可以把PV想像成一個100G的Memory,User1以PVC拿了其中的10G,User2也以PVC拿了其中的20G,這樣PV中的Memory就剩下70G。PV就是一個公共儲存空間,要使用這個公共空間,就要搭配PVC使用 總結的說,先生成 PV ,要使用多少再生成 PVC,然後 Pod 再用 volume 掛載此 PVC 到自己上。 #### 範例程式碼一 (PV): 這是一個 PersistentVolume,並且空間為 1G ,可以 read/write ,並且 Node 所在的路徑是 /tmp/data。 * ReadWriteOnce:通過單一節點以read-write方式掛載 * ReadWriteMany:通過多節點以read-write方式掛載 * ReadOnlyMany:通過多節點以read-only模式掛載 ```y= apiVersion: v1 kind: PersistentVolume metadata: name: my-pv spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce hostPath: path: /tmp/data ``` #### 範例程式碼二 (PVC): 此 PVC 名為 my-pvc,要求的硬體空間為 500MB ```y= apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 500Mi ``` #### 範例程式碼二 (PVC): 將 my-pvc 以 volume 掛載。 ```y= apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: myfrontend image: nginx volumeMounts: - mountPath: "/var/data" name: myvm volumes: - name: myvm persistentVolumeClaim: claimName: my-pvc ```