k8s学习-ConfigMap(创建、使用、更新、删除等)_使用 helm 更新 configmap

	- [replace](#replace_211)
+ [删除](#_225)

概念

ConfigMap是一种 API 对象,使用时, Pods 可以将其用作环境变量命令行参数或者存储卷中的配置文件。ConfigMap将配置和Pod解耦,更易于配置文件的更改和管理。ConfigMap 并不提供保密或者加密功能。 如果你想存储的数据是机密的,请使用Secret。

模板

ConfigMap 使用 data 和 binaryData 字段。这些字段能够接收键-值对作为其取值。data 和 binaryData 字段都是可选的。

  • data 字段设计用来保存 UTF-8 字符串,指定文件、目录或者键值对。
  • binaryData 则被设计用来保存二进制数据作为 base64 编码的字串。

由于一般命令行指定目录或文件,所以没有yaml模板。
从 v1.19 开始,你可以添加一个 immutable 字段到 ConfigMap 定义中, 创建不可变更的 ConfigMap。

实战

创建

基于目录

命令

# 创建本地目录
mkdir configmap
# 将示例文件下载到 `configmap` 目录
wget https://kubernetes.io/examples/configmap/game.properties -O configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O configmap/ui.properties

# 创建 configmap
kubectl create configmap -n killer game-config --from-file=configmap/

结果
在这里插入图片描述
查看一下下载的文件的内容
在这里插入图片描述
查看下configmap
命令

kubectl get configmap -n killer

结果
在这里插入图片描述

apiVersion: v1
items:
- apiVersion: v1
  data:
    game.properties: |-
      enemies=aliens
      lives=3
      enemies.cheat=true
      enemies.cheat.level=noGoodRotten
      secret.code.passphrase=UUDDLRLRBABAS
      secret.code.allowed=true
      secret.code.lives=30
    ui.properties: |
 color.good=purple
 color.bad=yellow
 allow.textmode=true
 how.nice.to.look=fairlyNice
  kind: ConfigMap
  metadata:
    creationTimestamp: "2022-07-10T18:02:16Z"
    name: game-config
    namespace: killer
    resourceVersion: "330866"
    selfLink: /api/v1/namespaces/killer/configmaps/game-config
    uid: a9551dbc-f42b-47c0-b44d-5b80256b7ff1
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

基于文件

命令

kubectl create configmap -n killer ui-config --from-file=configmap/ui.properties

结果
在这里插入图片描述
创建环境变量
game-env-file.properties内容如下:

enemies=aliens
lives=3
allowed=“true”

命令

kubectl create configmap game-config-env-file -n killer --from-env-file=configmap/game-env-file.properties

结果
在这里插入图片描述
从 Kubernetes 1.23 版本开始,kubectl 支持多次指定 --from-env-file 参数来从多个数据源创建 ConfigMap。

kubectl create configmap config-multi-env-files \
        --from-env-file=configure-pod-container/configmap/game-env-file.properties \
        --from-env-file=configure-pod-container/configmap/ui-env-file.properties

还有通过生成器和字面值创建configmap,由于一般配置比较多,还是使用目录或文件更方便,这里就不展示了。

使用

环境变量

使用 envFrom 将所有 ConfigMap 的数据定义为容器环境变量,ConfigMap 中的键成为 Pod 中的环境变量名称。
命令
env-test.yaml文件内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: env-test-pod
spec:
  containers:
    - name: env-test-pod
      image: busybox
      imagePullPolicy: IfNotPresent
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: game-config-env-file
  restartPolicy: Never

结果
在这里插入图片描述
可以看到刚才的文件中的键值对确实成为了Pod中的环境变量。

挂载
覆盖

注意:这种挂载方式会覆盖目录
busybox的/usr下有sbin目录
命令
volume-test.yaml文件内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: volume-test-pod
spec:
  containers:
    - name: test-container
      image: busybox
      imagePullPolicy: IfNotPresent
      command: [ "/bin/sh","-c","cd /usr && ls && cat /usr/keys" ]
      volumeMounts:
      - name: config-volume
        mountPath: /usr
  volumes:
    - name: config-volume
      configMap:
        name: game-config
        items:
        - key: game.properties
          path: keys
  restartPolicy: Never

kubectl create -f volume-test.yaml -n killer

结果
在这里插入图片描述
可以看到,只有keys文件,没有sbin目录了。

subPath

写在最后

在结束之际,我想重申的是,学习并非如攀登险峻高峰,而是如滴水穿石般的持久累积。尤其当我们步入工作岗位之后,持之以恒的学习变得愈发不易,如同在茫茫大海中独自划舟,稍有松懈便可能被巨浪吞噬。然而,对于我们程序员而言,学习是生存之本,是我们在激烈市场竞争中立于不败之地的关键。一旦停止学习,我们便如同逆水行舟,不进则退,终将被时代的洪流所淘汰。因此,不断汲取新知识,不仅是对自己的提升,更是对自己的一份珍贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。

需要完整版PDF学习资源私我

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以点击这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 22
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值