nfs 卷能将 NFS (网络文件系统) 挂载到你的 Pod 中。 不像 emptyDir 那样会在删除 Pod 的同时也会被删除,nfs 卷的内容在删除 Pod 时会被保存,卷只是被卸载。 这意味着 nfs 卷可以被预先填充数据,并且这些数据可以在 Pod 之间共享。

在使用 NFS 卷之前,你必须运行自己的 NFS 服务器并将目标 share 导出备用

个人搭建的NFS Linux服务器Exporter配置

/data/nfs/ 10.10.20.0/24(rw,sync,no_subtree_check,no_root_squash)

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kubernetes.io/change-cause: kubectl set image deploy my-nginx nginx=nginx:1.15.2 --record=true
  creationTimestamp: null
  labels:
    app: my-nginx
  name: my-nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: "2022-04-14T10:57:29+08:00"
      creationTimestamp: null
      labels:
        app: my-nginx
    spec:
      affinity: {}
      containers:
      - image: nginx:1.15.2
        imagePullPolicy: IfNotPresent
        volumeMouth:
          - name: nfs-test
            mountPath: /mnt
        lifecycle: {}
        name: nginx
        resources:
          requests:
            cpu: 10m
            memory: 10Mi
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext: {}
      volumes:
      - name: nfs-test
        nfs:
          path: /data/nfs
          readOnly: true
          server: 10.10.20.5
status: {}

创建基于NFS的PV和PVC,以及引用PV和PVC的deployment

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs
spec:
  capacity:
    storage: 1Mi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 10.10.20.5
    path: "/data/nfs"
  mountOptions:
    - nfsvers=4.2

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: ""
  resources:
    requests:
      storage: 1Mi
  volumeName: nfs


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-web
spec:
  replicas: 2
  selector:
    matchLabels:
      role: web-frontend
  template:
    metadata:
      labels:
        role: web-frontend
    spec:
      containers:
      - name: web
        image: nginx
        ports:
          - name: web
            containerPort: 80
        volumeMounts:
            # name must match the volume name below
            - name: nfs
              mountPath: "/usr/share/nginx/html"
      volumes:
      - name: nfs
        persistentVolumeClaim:
          claimName: nfs

Logo

开源、云原生的融合云平台

更多推荐