【k8s】Volume-ConfigMap-file

环境

  1. kubernetes 1.20.6
  2. Spring Boot 2.5.0-RC1

目标

使用文件的方式创建 cm 然后进行挂载。

示例

创建 cm

[root@master ~]# kubectl create cm cm-file --from-file=config/
configmap/cm-file created
[root@master ~]# kubectl describe cm cm-file
Name:         cm-file
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
test.json:
----
{
  "name": "jiangbo",
  "age": 44
}

test.properties:
----
name=jiangbo
age=4444

Events:  <none>

绑定到 Pod

apiVersion: v1
kind: Pod
metadata:
  name: spring-k8s
spec:
  containers:
    - name: spring-k8s
      image: jiangbo920827/spring-k8s:liveness
      volumeMounts:
        - mountPath: /opt/cfg
          name: v1
      ports:
        - containerPort: 8080
  volumes:
    - name: v1
      configMap:
        name: cm-file

查看

[root@master ~]# kubectl exec spring-k8s -- sh -c "ls -l /opt/cfg/"
total 0
lrwxrwxrwx    1 root     root            16 May 16 05:58 test.json -> ..data/test.json
lrwxrwxrwx    1 root     root            22 May 16 05:58 test.properties -> ..data/test.properties

挂载单个

apiVersion: v1
kind: Pod
metadata:
  name: spring-k8s
spec:
  containers:
    - name: spring-k8s
      image: jiangbo920827/spring-k8s:liveness
      volumeMounts:
        - mountPath: /opt/cfg
          name: v1
      ports:
        - containerPort: 8080
  volumes:
    - name: v1
      configMap:
        name: cm-file
        items:
          - key: test.properties
            path: test/1.properties
[root@master ~]# kubectl exec spring-k8s -- sh -c "ls -l /opt/cfg/test/"
total 4
-rw-r--r--    1 root     root            22 May 16 06:00 1.properties
[root@master ~]#

总结

使用文件的方式创建了 cm 的数据,然后挂载到 Pod 中,其中可以选择只挂载 cm 数据中的一部分 key。

附录

posted @   jiangbo4444  阅读(161)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-06-30 【Java】JavaScript 加密 Java 解密
点击右上角即可分享
微信分享提示