阿里雲 K8S 通過 pv 和 pvc 靜態掛載已有的 NAS 目錄


一、創建和獲取阿里雲的掛載點,不了解的可以看阿里雲幫助文檔

https://help.aliyun.com/document_detail/134923.html?spm=a2c4g.11186623.6.793.6264714bLwZMoQ

例如,我這里獲得的掛載點為:88888888-ney35.cn-shenzhen.nas.aliyuncs.com

二、創建目錄,把文件上傳到上面去

找台機器把目錄掛載上去,上傳需要容器需要使用的文件

yum install -y nfs-utils

mount -t nfs -o vers=3,nolock,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport 88888888-ney35.cn-shenzhen.nas.aliyuncs.com:/ /mnt

# 創建對應的目錄, 注意創建的目錄要與 PV 的路徑一樣
cd /mnt
mkdir gc-wms2
cd gc-wms2
mkdir dir1 dir2
touch dir1/hello.txt
touch dir2/world.txt

 tree ./
./
├── dir1
│   └── hello.txt
└── dir2
    └── world.txt

# umount 
cd ~ && umount /mnt

三、創建 PV 和 PVC

cat gc_wms_nas2.yaml 

apiVersion: v1
kind: PersistentVolume
metadata:
  name: gc-wms-pv-nas
  namespace: devops
  labels:
    alicloud-pvname: gc-wms-pv-nas
spec:
  capacity:
    storage: 10Mi
  accessModes:
    - ReadWriteMany
  csi:
    driver: nasplugin.csi.alibabacloud.com
    volumeHandle: gc-wms-pv-nas
    volumeAttributes:
      server: "88888888-ney35.cn-shenzhen.nas.aliyuncs.com"  # 指定掛載點
      path: "/gc-wms2"           # 與第二部分在 nas 創建的目錄一致
  mountOptions:
  - nolock,tcp,noresvport
  - vers=3

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: gc-wms-pvc-nas
  namespace: devops
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Mi
  selector:
    matchLabels:
      alicloud-pvname: gc-wms-pv-nas

# 創建
kubectl apply -f gc_wms_nas2.yaml 

四、創建應用

cat test.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-nas
  namespace: devops
  labels:
    app: klvchen-nginx
spec:
  selector:
    matchLabels:
      app: klvchen-nginx
  template:
    metadata:
      labels:
        app: klvchen-nginx
    spec:
      containers:
      - name: klvchen-nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        volumeMounts:
          - name: pv-nas
            mountPath: "/data1"
            subPath: "dir1"             # 指定掛載 nas 中 /gc-wms2/dir1 文件夾 
          - name: pv-nas
            mountPath: "/data2"
            subPath: "dir2"             # 指定掛載 nas 中 /gc-wms2/dir2 文件夾
      volumes:
        - name: pv-nas
          persistentVolumeClaim:
            claimName: gc-wms-pvc-nas   # 指定上面 PVC 的名字

# 啟動
kubectl apply -f test.yaml

五、測試

kubectl get pods -n devops
NAME                              READY   STATUS    RESTARTS   AGE
nginx-nas-69f5fd8f78-phfs4        1/1     Running   0          7s

kubectl -n devops exec -it nginx-nas-69f5fd8f78-phfs4 /bin/bash

# 測試成功
root@nginx-nas-69f5fd8f78-phfs4:/# cd data1/
root@nginx-nas-69f5fd8f78-phfs4:/data1# ls
hello.txt
root@nginx-nas-69f5fd8f78-phfs4:/data1# cd ..
root@nginx-nas-69f5fd8f78-phfs4:/# cd data2/
root@nginx-nas-69f5fd8f78-phfs4:/data2# ls  
world.txt


免責聲明!

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



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