容器 Comfigmap k8s comfigmap 【云原生】kubernetes中Configmap原理解析与应用实战 - subdirectories, symlinks,

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多帮助力 👁️ 233 次阅读

✨✨ 欢迎大家来到景天科技苑✨✨

 养成好习惯,先赞后看哦~

 作者简介:景天科技苑 《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi,flask等框架,云原生k8s,linux,shell脚本等实操经验,网站搭建,数据库等分享。

所属的专栏:云原生K8S,零基础到进阶实战 景天的主页:景天科技苑

文章目录

ComfigMap1.Configmap概述1.1 什么是Configmap?1.2 Configmap能解决哪些问题?1.3 Configmap应用场景1.4 局限性

2.Configmap创建方法2.1 命令行直接创建2.2 通过文件创建2.3 指定目录创建2.4 编写configmap资源清单YAML文件

3.使用Configmap3.1 通过环境变量引入:使用configMapKeyRef3.2 通过环境变量引入:使用envfrom3.3 把configmap做成volume,挂载到pod

4.Configmap热更新4.1 注意

ComfigMap

1.Configmap概述

1.1 什么是Configmap?

Configmap是k8s中的资源对象,用于保存非机密性的配置的,数据可以用key/value键值对的形式保存,也可通过文件的形式保存。

1.2 Configmap能解决哪些问题?

我们在部署服务的时候,每个服务都有自己的配置文件, 如果一台服务器上部署多个服务:nginx、tomcat、apache等,那么这些配置都存在这个节点上, 假如一台服务器不能满足线上高并发的要求,需要对服务器扩容,扩容之后的服务器还是需要部署多个服务:nginx、tomcat、apache, 新增加的服务器上还是要管理这些服务的配置,如果有一个服务出现问题,需要修改配置文件,每台物理节点上的配置都需要修改, 这种方式肯定满足不了线上大批量的配置变更要求。 所以,k8s中引入了Configmap资源对象,可以当成volume挂载到pod中,实现统一的配置管理。

1、Configmap是k8s中的资源, 相当于配置文件,可以有一个或者多个Configmap; 2、Configmap可以做成Volume,k8s pod启动之后,通过 volume 形式映射到容器内部指定目录上; 3、容器中应用程序按照原有方式读取容器特定目录上的配置文件。 4、在容器看来,配置文件就像是打包在容器内部特定目录,整个过程对应用没有任何侵入。

1.3 Configmap应用场景

1、使用k8s部署应用,当你将应用配置写进代码中,更新配置时也需要打包镜像, configmap可以将配置信息和docker镜像解耦,以便实现镜像的可移植性和可复用性, 因为一个configMap其实就是一系列配置信息的集合,可直接注入到Pod中给容器使用。 configmap注入方式有两种: 一种将configMap做为存储卷, 一种是将configMap通过env中configMapKeyRef注入到容器中。

2、使用微服务架构的话,存在多个服务共用配置的情况,如果每个服务中单独一份配置的话, 那么更新配置就很麻烦,使用configmap可以友好的进行配置共享。

1.4 局限性

ConfigMap在设计上不是用来保存大量数据的。在ConfigMap中保存的数据不可超过1 MiB。 如果你需要保存超出此尺寸限制的数据,可以考虑挂载存储卷或者使用独立的数据库或者文件服务。 一般配置文件不会超过1M,所以大胆地去用,几千行也不会超过1M

2.Configmap创建方法

2.1 命令行直接创建

直接在命令行中指定configmap参数创建,通过–from-literal指定参数

[root@master01 configmap ]#kubectl create configmap tomcat-config --from-literal=tomcat_port=8080 --from-literal=server_name=myapp.tomcat.com

configmap/tomcat-config created

命令行创建configmap可以通过帮助命令查看使用规则:

[root@master01 configmap ]#kubectl create configmap --help

Create a config map based on a file, directory, or specified literal value.

A single config map may package one or more key/value pairs.

When creating a config map based on a file, the key will default to the basename of the file, and the value will

default to the file content. If the basename is an invalid key, you may specify an alternate key.

When creating a config map based on a directory, each file whose basename is a valid key in the directory will be

packaged into the config map. Any directory entries except regular files are ignored (e.g. subdirectories, symlinks,

devices, pipes, etc).

Aliases:

configmap, cm

Examples:

# Create a new config map named my-config based on folder bar

kubectl 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 disk

kubectl 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=config2

kubectl 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 file

kubectl create configmap my-config --from-file=path/to/bar

# Create a new config map named my-config from an env file

kubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env

Options:

--allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in

the template. Only applies to golang and jsonpath output formats.

--append-hash=false: Append a hash of the configmap to its name.

--dry-run='none': Must be "none", "server", or "client". If client strategy, only print the object that would be

sent, without sending it. If server strategy, submit server-side request without persisting the resource.

--field-manager='kubectl-create': Name of the manager used to track field ownership.

--from-env-file=[]: Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker

.env file).

--from-file=[]: Key file can be specified using its file path, in which case file basename will be used as

configmap key, or optionally with a key and file path, in which case the given key will be used. Specifying a directory

will iterate each named file in the directory whose basename is a valid configmap key.

--from-literal=[]: Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)

-o, --output='': Output format. One of:

json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.

--save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the

annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.

--show-managed-fields=false: If true, keep the managedFields when printing objects in JSON or YAML format.

--template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The

template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

--validate=true: If true, use a schema to validate the input before sending it

Usage:

kubectl create configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run=server|client|none]

[options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

[root@master01 configmap ]#kubectl describe configmap tomcat-config

Name: tomcat-config

Namespace: default

Labels:

Annotations:

Data

====

server_name:

----

myapp.tomcat.com

tomcat_port:

----

8080

BinaryData

====

Events:

2.2 通过文件创建

定义一个key是www,值是nginx.conf中的内容

[root@master01 configmap ]#cat nginx.conf

server {

server_name www.nginx.com;

listen 80;

root /home/nginx/www/

}

[root@master01 configmap ]#kubectl create configmap www-nginx --from-file=www=./nginx.conf

configmap/www-nginx created

此时,如果不指定文件的名称www。则创建的configmap的key是文件名,值是文件的内容

[root@master01 configmap ]#kubectl describe configmap www-nginx

Name: www-nginx

Namespace: default

Labels:

Annotations:

Data

====

www:

----

server {

server_name www.nginx.com;

listen 80;

root /home/nginx/www/

}

BinaryData

====

Events:

configmap缩写成cm

2.3 指定目录创建

[root@master01 configmap ]#mkdir test-a

[root@master01 configmap ]#cd test-a/

[root@master01 test-a ]#cat my-server.cnf

server-id=1

[root@master01 test-a ]#cat my-slave.cnf

server-id=2

指定目录创建configmap

[root@master01 test-a ]#kubectl create configmap mysql-config --from-file=/root/configmap/test-a/

configmap/mysql-config created

#查看configmap详细信息

[root@master01 test-a ]#kubectl describe cm mysql-config

Name: mysql-config

Namespace: default

Labels:

Annotations:

Data

====

my-server.cnf:

----

server-id=1

my-slave.cnf:

----

server-id=2 拼多多帮助力入口

BinaryData

====

Events:

这种类似文件名做key,文件内容做值

2.4 编写configmap资源清单YAML文件

[root@master01 configmap ]#kubectl explain cm

KIND: ConfigMap

VERSION: v1

DESCRIPTION:

ConfigMap holds configuration data for pods to consume.

FIELDS:

apiVersion

APIVersion defines the versioned schema of this representation of an

object. Servers should convert recognized schemas to the latest internal

value, and may reject unrecognized values. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

binaryData

BinaryData contains the binary data. Each key must consist of alphanumeric

characters, '-', '_' or '.'. BinaryData can contain byte sequences that are

not in the UTF-8 range. The keys stored in BinaryData must not overlap with

the ones in the Data field, this is enforced during validation process.

Using this field will require 1.10+ apiserver and kubelet.

data

Data contains the configuration data. Each key must consist of alphanumeric

characters, '-', '_' or '.'. Values with non-UTF-8 byte sequences must use

the BinaryData field. The keys stored in Data must not overlap with the

keys in the BinaryData field, this is enforced during validation process.

immutable

Immutable, if set to true, ensures that data stored in the ConfigMap cannot

be updated (only object metadata can be modified). If not set to true, the

field can be modified at any time. Defaulted to nil.

kind

Kind is a string value representing the REST resource this object

represents. Servers may infer this from the endpoint the client submits

requests to. Cannot be updated. In CamelCase. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

metadata

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧