手動(dòng)創(chuàng)建k8s local-pv

k8s 使用local-pv

基本概念

kubernetes從1.10版本開始支持local volume(本地卷),workload(不僅是statefulsets類型)可以充分利用本地快速SSD,從而獲取比remote volume(如cephfs、RBD)更好的性能.

下面兩種類型應(yīng)用適合使用local volume。

數(shù)據(jù)緩存,應(yīng)用可以就近訪問數(shù)據(jù),快速處理。
分布式存儲(chǔ)系統(tǒng),如分布式數(shù)據(jù)庫(kù)Cassandra ,分布式文件系統(tǒng)ceph/gluster
下面會(huì)先以手動(dòng)方式創(chuàng)建PV、PVC、Pod的方式,介紹如何使用local volume,然后再介紹external storage提供的半自動(dòng)方式,最后介紹社區(qū)的一些發(fā)展。

手動(dòng)創(chuàng)建local-pv

手動(dòng)創(chuàng)建local-pv 需要?jiǎng)?chuàng)建storageclass, pv. 如果你的資源申請(qǐng)pv使用使用的persistvolumetemplate, pvc 會(huì)自動(dòng)創(chuàng)建.

storageclass

首先需要有一個(gè)名為local-volume的sc。

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

sc的provisioner是 kubernetes.io/no-provisioner。

WaitForFirstConsumer表示PV不要立即綁定PVC,而是直到有Pod需要用PVC的時(shí)候才綁定。調(diào)度器會(huì)在調(diào)度時(shí)綜合考慮選擇合適的local PV,這樣就不會(huì)導(dǎo)致跟Pod資源設(shè)置,selectors,affinity and anti-affinity策略等產(chǎn)生沖突。很明顯:如果PVC先跟local PV綁定了,由于local PV是跟node綁定的,這樣selectors,affinity等等就基本沒用了,所以更好的做法是先根據(jù)調(diào)度策略選擇node,然后再綁定local PV.

手動(dòng)創(chuàng)建pv

apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-local-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-volume
  local:
    path: /data/local/vol1
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - 192.168.100.1

這里使用實(shí)際存儲(chǔ)是192.168.100.1 上的/data/local/vol1 目錄.
所以你要手動(dòng)建立這些目錄給pv掛載使用,可以使用下面的方式使用loop掛載的方式掛載到目錄.

cat lpfs.sh
#!/bin/bash
# Usage: sudo loopm ount file size m ount-point
touch $1
truncate -s $2 $1
mke2fs -t ext4 -F $1 1> /dev/null 2> /dev/null
if [[ ! -d $3 ]]; then
    echo $3 " not exist, creating..."
     mkdir $3
fi
mount $1 $3
df -h |grep $3
lpfs.sh  /data0/k8spv/data0 5G /data/local/vol1

綁定pv,使用pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: local-volume
---
kind: Pod
apiVersion: v1
metadata:
  name: mypod
spec:
  containers:
    - name: myfrontend
      image: nginx
      volumeMounts:
      - mountPath: "/usr/share/nginx/html"
        name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:
        claimName: myclaim

自動(dòng)創(chuàng)建

安裝網(wǎng)絡(luò)pv的使用慣例,我們使用storageclass指定了provisioner后,按理說可以使用自動(dòng)化的pv創(chuàng)建的,這種手動(dòng)創(chuàng)建pv的方式太low太麻煩了,還不nodeselector好了.官方也提供了自動(dòng)化的創(chuàng)建pv的方式.下篇文章介紹.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容