You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

K8s部署的SpringBoot通过PVC读写NFS:如何修改Deployment.yaml

修改Deployment.yaml实现NFS挂载到SpringBoot容器

要让你的SpringBoot容器能访问NFS存储,需要在Deployment的Pod模板里添加存储卷挂载配置,具体修改如下:

修改后的完整Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: authentication
  name: authentication
  namespace: auth
spec:
  selector:
    matchLabels:
      app: authentication
  strategy:
    rollingUpdate:
      maxSurge: 15%
      maxUnavailable: 0
    type: RollingUpdate
  replicas: 1
  template:
    metadata:
      labels:
        app: authentication
    spec:
      volumes:
        - name: nfs-storage
          persistentVolumeClaim:
            claimName: fileserver-nfs-pvc
      containers:
        - name: authentication
          image: ip:8080/authentication:0.0.0
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          # 添加存储卷挂载
          volumeMounts:
            - name: nfs-storage
              mountPath: /app/nfs-storage
          resources:
            limits:
              cpu: "0.9"
              memory: 1Gi
            requests:
              cpu: "0.6"
              memory: 512Mi

关键修改说明

  • volumes 配置:在template.spec下新增volumes字段,通过persistentVolumeClaim.claimName关联你已创建的fileserver-nfs-pvc,给存储卷命名为nfs-storage(名字可自定义,只要和下方volumeMounts的名称对应即可)。
  • volumeMounts 配置:在容器配置里新增volumeMounts,指定关联的存储卷名,并设置mountPath为容器内的路径(示例用/app/nfs-storage,你可根据项目需求调整)。

SpringBoot代码操作示例

你的SpringBoot项目使用java.nio操作文件时,直接调用容器内的挂载路径即可,比如:

import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

// 写入文件到NFS存储
Files.write(Paths.get("/app/nfs-storage", "demo.txt"), "测试NFS写入".getBytes(), StandardOpenOption.CREATE);

// 从NFS存储读取文件
String fileContent = Files.readString(Paths.get("/app/nfs-storage", "demo.txt"));

内容的提问来源于stack exchange,提问作者kernel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 18:47:03