6.k8s.存儲Volume.PV.PVC


#Volume

Volume 解決數據持久化和容器間共享數據
Kubernetes支持幾十種類型的后端存儲卷

hostPath掛載實例,掛載Node節點/tmp/test-volume目錄到容器/test-pd

#volume-test.yaml
apiVersion: v1
kind: Pod
metadata:
  name: volume-test
spec:
  containers:
  - image: alivv/nginx:node
    name: volume-test
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /tmp/test-volume
      type: DirectoryOrCreate  #node節點不存在則創建空目錄
#創建pod
kubectl create -f volume-test.yaml

#查看pod所在node節點
kubectl get pod volume-test -o wide
#在容器里創建文件
kubectl exec -it volume-test -- sh -c "date >/test-pd/txt"

#刪除pod
kubectl delete -f volume-test.yaml

#在node節點檢查文件存在
cat /tmp/test-volume/txt

hostPath type類型Directory、File、Socket等
參考 https://feisky.gitbooks.io/kubernetes/concepts/volume.html

#PV PVC

  • PV PersistentVolume 持久卷,作為存儲資源,包含存儲實現細節

  • PVC PersistentVolumeClaim 用戶存儲的請求,Pod消耗節點資源

#使用NFS后端存儲,創建PV、PVC、Pod實例

#創建nfs存儲

#NFS服務端 (centos7)
yum install nfs-utils rpcbind -y
mkdir -p /data/nfs/{download,bakup,www}
echo "/data/nfs *(rw,no_root_squash,sync)">>/etc/exports
exportfs -r
systemctl enable rpcbind nfs-server
systemctl restart rpcbind nfs-server
showmount -e localhost

#NFS客戶端(Node節點)
yum install nfs-utils rpcbind -y
systemctl start rpcbind
systemctl enable rpcbind

#創建PV

#pv1-demo.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name:  pv1
spec:
  capacity: #存儲能力
    storage: 10Gi
  accessModes:
  - ReadWriteMany  #讀寫權限,多節點掛載
  persistentVolumeReclaimPolicy: Retain  #回收策略 保留數據
  nfs:
    path: /data/nfs
    server: 172.16.11.141
#創建pv
kubectl create -f pv1-demo.yaml
#查看
kubectl get pv

創建PVC

#pvc-nfs-demo.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-nfs
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

kubectl create -f pvc-nfs-demo.yaml
kubectl get pvc
kubectl get pv

部署Pod使用pvc實例

#nfs-pvc-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nfs-pvc
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nfs-pvc
    spec:
      containers:
      - name: nginx
        image: alivv/nginx:node
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: web
        #使用volume
        volumeMounts:
        - name: www
          subPath: nginx-pvc  #遠程子路徑
          mountPath: /usr/share/nginx/html
      volumes:
      - name: www
        persistentVolumeClaim:
          claimName: pvc-nfs
#部署Pod使用pvc實例
kubectl apply -f nfs-pvc-deploy.yaml
#查看
kubectl get pod
#NFS節點寫入index.html文件
echo nfs pvs test $(date +"%F %T") |tee /data/nfs/nginx-pvc/index.html

#查看Pod ip
kubectl get pod -o wide

#訪問Pod ip,查看內容為NFS節點寫入內容
kubectl get pod -o wide |awk '/nfs-pvc/{print "curl "$6}'
kubectl get pod -o wide |awk '/nfs-pvc/{print "curl "$6}' |sh
#刪除demo
kubectl delete -f nfs-pvc-deploy.yaml
kubectl delete -f pvc-nfs-demo.yaml
kubectl delete -f pv1-demo.yaml

Blog地址 https://www.cnblogs.com/elvi/p/11755813.html
本文git地址 https://gitee.com/almi/k8s/tree/master/notes


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM