k8s概念-ConfigMap

news/2025/3/12 12:33:06/

回到目录

一般用于去存储 Pod 中应用所需的一些配置信息,或者环境变量,将配置于 Pod 分开,避免应为修改配置导致还需要重新构建 镜像与容器。

1 创建ConfigMap

#使用 kubectl create configmap -h 查看示例,构建 configmap 对象
Examples:# Create a new config map named my-config based on folder barkubectl create configmap my-config --from-file=path/to/bar# Create a new config map named my-config with specified keys instead of file basenames on diskkubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt# Create a new config map named my-config with key1=config1 and key2=config2kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2# Create a new config map named my-config from the key=value pairs in the filekubectl create configmap my-config --from-file=path/to/bar# Create a new config map named my-config from an env filekubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env

1.1 将指定目录下所有文件加载到配置文件中

# Create a new config map named my-config based on folder bar
# kubectl create configmap my-config --from-file=path/to/bar
#example:
#在test目录下有两个配置文件db.properties redis.properties#1.创建configmap,名字为test-cm,从test目录下加载
[root@k8s-master1 configmap]# kubectl create cm test-cm --from-file=test/
configmap/test-cm created#2.查看configmap
[root@k8s-master1 configmap]# kubectl get cm
NAME               DATA   AGE
kube-root-ca.crt   1      5d9h #k8s默认的认证
test-cm            2      7s	 #我们创建的#3.查看test-cm的详细信息
[root@k8s-master1 configmap]# kubectl describe cm test-cm
#内容如下:
Name:         test-cm
Namespace:    default
Labels:       <none>
Annotations:  <none>Data
====
db.properties:   #key
----
username: root
password: 1234redis.properties:
----
port: 6379BinaryData
====Events:  <none>

1.2 指定一个或多个文件和key

# Create a new config map named my-config with specified keys instead of file basenames on disk
#  kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt#指定一个或多个文件,不加key时以文件名作为key#example:#创建configmap,名称为testfile-cm,从指定的test目录下的db.properties加载
[root@k8s-master1 configmap]# kubectl create cm testfile-cm --from-file=test/db.properties
configmap/testfile-cm created#查看configmap
[root@k8s-master1 configmap]# kubectl get cm
NAME               DATA   AGE
kube-root-ca.crt   1      5d10h
testfile-cm        1      5s#查看详情
[root@k8s-master1 configmap]# kubectl describe cm testfile-cm
Name:         testfile-cm
Namespace:    default
Labels:       <none>
Annotations:  <none>Data
====
db.properties: #不指定key,以文件名为key,如果指定则以具体指定作为key
----
username: root
password: 1234BinaryData
====Events:  <none>

1.3 命令上手动添加key-value

# Create a new config map named my-config with key1=config1 and key2=config2
#kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2#直接写入key和value#example:#创建configmap,名称为test-cm,手动添加key:user,value:root;key:password,value:1234
[root@k8s-master1 configmap]# kubectl create cm test-cm --from-literal=user=root --from-literal=password=1234
configmap/test-cm created
[root@k8s-master1 configmap]# kubectl get cm
NAME               DATA   AGE
kube-root-ca.crt   1      5d10h
test-cm            2      7s
[root@k8s-master1 configmap]# kubectl describe cm test-cm
Name:         test-cm
Namespace:    default
Labels:       <none>
Annotations:  <none>Data
====
password: #key
----
1234   #value
user: #key
----
root  #valueBinaryData
====Events:  <none>

2 configmap的使用

在pod中

  • 将configmap中的数据设置为容器的环境变量

  • 将ConfigMap中的数据设置为命令行参数

  • 使用Volume将ConfigMap作为文件或目录挂载

  • 编写代码在 Pod 中运行,使用 Kubernetes API 来读取 ConfigMap

将configmap挂载到容器中,configmap变化,容器自动更新

而 写入环境变量不会自动更新

2.1 配置到容器的环境变量中

# test-pod-configmap.yaml
apiVersion: v1
kind: Pod
metadata:name: test-pod-configmap
spec:containers:- name: test-busyboximage: busyboximagePullPolicy: IfNotPresentargs:- sleep- "86400"env:                  # env,配置容器环境变量- name: KEY1		  # 容器环境变量key为key1valueFrom:		  # value内容configMapKeyRef:	  # 通过configmap的key映射name: my-config	  # configmap的名称key: key1		  # configmap中的key- name: KEY2valueFrom:configMapKeyRef:name: my-configkey: key2

2.2 设置为命令行参数

# test-pod-configmap-cmd
apiVersion: v1
kind: Pod
metadata:name: test-pod-configmap-cmd
spec:containers:- name: test-busyboximage: busyboximagePullPolicy: IfNotPresentcommand: [ "/bin/sh","-c","echo $(KEY1) $(KEY2)"]env:- name: KEY1valueFrom:configMapKeyRef:name: my-configkey: key1- name: KEY2valueFrom:configMapKeyRef:name: my-configkey: key2restartPolicy: Never

2.3 将configmap挂载到容器中

# test-pod-projected-configmap-volume.yaml
apiVersion: v1
kind: Pod
metadata:name: test-pod-projected-configmap-volume
spec:containers:- name: test-pod-busyboximage: busyboximagePullPolicy: IfNotPresentargs:- sleep- "86400"volumeMounts:                        # 挂载- name: config-volume				# 挂载的数据卷名mountPath: "/projected-volume"		# 挂载到哪的路径readOnly: true						# 只读权限volumes:								# 数据卷- name: config-volume					# 数据卷名称projected:sources:- configMap:						# 数据卷为configmapname: my-config					# 数据卷名

http://www.ppmy.cn/news/1005317.html

相关文章

从web漏洞到linux权限提升

从web漏洞到linux权限提升 一、Linux系统介绍与使用二、Linux权限说明2.1、文件权限2.2、linux文件、目录权限说明 三、权限提升 一、Linux系统介绍与使用 linux-全称GNU/Linux&#xff0c;是一种免费使用和自由传播的类UNIX操作系统&#xff0c;是基于POSIXI的多用户、多任务…

C++设计模式行为型之观察者模式

一、概述 观察者模式定义对象间的一种一对多的依赖关系&#xff0c;当一个对象的状态发生改变时&#xff0c;所有依赖于它的对象都得到通知并被自动更新。 二、示例代码 #include <list> class Subject; class Observer { public: virtual ~Observer(); virt…

学了一个礼拜 JavaScript 为什么还是学不会?

前言 首先从你的描述里面我先以我的主观臆断来猜测一下你是没有任何编程的基础的&#xff0c;Js按理来说在各语言中并不是非常难学&#xff0c;当然如果你是纯新手入门&#xff0c;那么确实前期需要时间来沉淀一下语法&#xff0c;一个礼拜的话&#xff0c;按理来说应该是在沉…

新手Vite打包工具的使用并解决yarn create vite报错

一、手动创建 1.创建vite-Demo文件夹 2.初始化 yarn init -y 3.安装vite yarn add -D vite 4.打包准备 说明&#xff1a;不需要在src下面创建&#xff0c;在vite-Demo文件夹创建 4.1index.js文件 document.body.insertAdjacentHTML("beforeend","<h1>…

2500、删除每行中最大值在IDEA中调试Java

leetcode:2500、删除每行中最大值在IDEA中调试&#xff0c;使用Java实现 题目描述&#xff1a; 给你一个 m x n 大小的矩阵 grid &#xff0c;由若干正整数组成。 执行下述操作&#xff0c;直到 grid 变为空矩阵&#xff1a; 从每一行删除值最大的元素。如果存在多个这样的…

七、Spring 面向切面编程(AOP)学习总结

文章目录 一、初识面向切面编程&#xff08;AOP&#xff09;1.1 什么是 AOP1.2 AOP的应用场景1.3 Aop 在 Spring 中的作用1.3.1 Aop 的核心概念 1.4 使用 Spring 实现 AOP1.4.1 方式一&#xff1a;使用 Spring API 接口实现 AOP 【主要是SpringAPI接口实现】1.4.2 方式二&#…

Vue3文本省略(Ellipsis)

APIs 参数说明类型默认值必传maxWidth文本最大宽度number | string‘100%’falseline最大行数numberundefinedfalseexpand是否启用点击文本展开全部booleanfalsefalsetooltip是否启用文本提示框booleantruefalsetooltipMaxWidth提示框内容最大宽度&#xff0c;单位px&#xff…

新手指南:流程图中各种图形的含义及用法解析

我们经常在技术设计、沟通、业务演示等一些领域看到流程图&#xff0c;它也可以称为输入输出图。顾名思义&#xff0c;它是指一种简单的工作流程的具体步骤&#xff0c;比如包括一次会议的流程&#xff0c;以及一次生产制造的顺序和过程等。本文将为大家介绍流程图的含义和具体…