k8s——持久化存儲之nfs

準備

  • 所有k8s節點需要安裝nfs服務
  • 一臺nfs服務器提供存儲
  • 根據需求創建數據掛載目錄

例如我的:
在這裏插入圖片描述

創建PV

// vi pv-nfs.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name:  web-pv
  labels:
    app: web-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /k8s-data/web
    server: 192.168.50.190
kubectl apply -f pv-nfs.yaml
  • 查看pv
kubectl get pv

在這裏插入圖片描述

創建PVC

// vi pvc-nfs.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: web-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  selector:
    matchLabels:
      app: web-pv
kubectl apply -f pvc-nfs.yaml
  • 查看PVC
kubectl get pvc

在這裏插入圖片描述

使用PVC掛載

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 2
  template:
    metadata:
      labels:
        app: web
    spec:
      terminationGracePeriodSeconds: 3
      containers:
      - name: web
        image: nginx
        imagePullPolicy: Never
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: index
      volumes:
      - name: index
        persistentVolumeClaim:
          claimName: web-pvc
  • 創建service
apiVersion: v1
kind: Service
metadata:
  name: web-svc
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    nodePort: 30991
  selector:
    app: web

把主頁文件放到之前準本的nfs目錄下,訪問ip+30991即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章