目 录CONTENT

文章目录

【实践】k8s配置文件实战

FatFish1
2025-05-21 / 0 评论 / 0 点赞 / 21 阅读 / 0 字 / 正在检测是否收录...

在k8s容器中挂载一个卷

还记得在k8s配置文件中有以下两个key,都与volume相关:

  • spec.volumes:声明一个公共卷,让所有container都可以读取到它,在这里声明实际上并没有挂载到具体的container中,只是让container可以识别到这个卷

  • spec.containers.image.volumeMounts:声明container挂载这个卷,并且给定一个对应的目录跟它绑定

看以下声明:

kind: Deployment
... ...
spec: 
    ... ...
    template:
        ... ...
        spec:
        ... ...
            volumes:
                - name: myOwnVolume
                  persistentVolumeClaim:
                      claimN: myOwnVolume
            ... ...
            containers:
                - image:
                    ... ...
                    volumeMounts:
                        - name: myOwnVolume
                          mountPath: /home/extra/extra1

是使用yaml形式构造的,对于deployment使用了一个template构造,template里面第一层spec实际上才是上面讲的spec

spec.volumes里面声明了一个卷,而其载名为myOwnVolume,要保证这个额外卷是存在的

下面spec.spec.containers.image.volumeMounts里面声明了这个卷,挂载对于的目录是/home/extra/extra1,这个目录要存在,因此要提前创建一下目录

可以在dockerfile中使用RUN命令创建一下

RUN mkdir -p /home/extra/extra1

这样就能把卷挂载到对应目录了

0

评论区