k8s configmap 详解

这篇具有很好参考价值的文章主要介绍了k8s configmap 详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、概述

ConfigMap是k8s的一个配置管理组件,可以将配置以key-value的形式传递,通常用来保存不需要加密的配置信息,加密信息则需用到Secret,主要用来应对以下场景:

  • 生成为容器内的环境变量;

  • 设置容器启动命令的启动参数(需设置为环境变量)

  • 以Volume的形式挂载为容器内部的文件或目录。文章来源地址https://www.toymoban.com/news/detail-494011.html

二、创建configmap

  • 从普通文件创建
1: 创建configmap
[root@node1 ~]# 
[root@node1 ~]# kubectl create ns configmap
namespace/configmap created
[root@node1 ~]# 
[root@node1 ~]# kubectl create cm test-config --from-file=/root/local.repo -n configmap
configmap/test-config created

2:查看configmap内容
[root@node1 ~]# kubectl get cm -n configmap
NAME               DATA   AGE
kube-root-ca.crt   1      41s
test-config        1      39s
[root@node1 ~]# kubectl describe  cm test-config -n configmap
Name:         test-config
Namespace:    configmap
Labels:       <none>
Annotations:  <none>

Data
====
local.repo:
----
[local]
name=local repo from http
baseurl=http://192.168.10.162:7080
enabled=1
gpgcheck=0



BinaryData
====

Events:  <none>
  • 从目录创建cm
1:创建对应的cm信息
[root@node1 ~]# mkdir config
[root@node1 ~]# echo hello > config/hello.txt
[root@node1 ~]# echo world > config/world.txt
[root@node1 ~]# 
[root@node1 ~]# kubectl create cm test-config-dir --from-file=/root/config -n configmap
configmap/test-config-dir created
[root@node1 ~]# 

2:查看cm
[root@node1 ~]# kubectl get cm -n configmap
NAME               DATA   AGE
kube-root-ca.crt   1      3m58s
test-config        1      3m56s
test-config-dir    2      66s
[root@node1 ~]# 
[root@node1 ~]# kubectl describe  cm test-config-dir -n configmap
Name:         test-config-dir
Namespace:    configmap
Labels:       <none>
Annotations:  <none>

Data
====
hello.txt:
----
hello

world.txt:
----
world


BinaryData
====

Events:  <none>
[root@node1 ~]# 
  • 自定义数据源
类似和从文件创建一样,多了一个key的定义
[root@node1 ~]# kubectl create configmap cm-data --from-file=test-cm-define=/etc/yum.repos.d/local.repo  -n configmap
configmap/cm-data created
[root@node1 ~]# 
[root@node1 ~]# kubectl get cm -n configmap
NAME               DATA   AGE
cm-data            1      31s
kube-root-ca.crt   1      14m
test-config        1      14m
test-config-dir    2      11m
[root@node1 ~]# kubectl describe  cm cm-data -n configmap
Name:         cm-data
Namespace:    configmap
Labels:       <none>
Annotations:  <none>

Data
====
test-cm-define:
----
[local]
name=local repo from http
baseurl=http://192.168.10.162:7080
enabled=1
gpgcheck=0



BinaryData
====

Events:  <none>
[root@node1 ~]# 

  • 从字符集创建
[root@node1 ~]# kubectl create cm test-cm-zifu --from-literal=name=www --from-literal=edge=30 -n configmap
configmap/test-cm-zifu created
[root@node1 ~]# kubectl get cm -n configmap
NAME               DATA   AGE
cm-data            1      4m43s
kube-root-ca.crt   1      18m
test-cm-zifu       2      2s
test-config        1      18m
test-config-dir    2      16m
[root@node1 ~]# kubectl describe  cm test-cm-zifu -n configmap
Name:         test-cm-zifu
Namespace:    configmap
Labels:       <none>
Annotations:  <none>

Data
====
edge:
----
30
name:
----
www

BinaryData
====

Events:  <none>
[root@node1 ~]# 
  • 从yaml创建
1:编辑yaml文件

---
apiVersion: v1
data:
  customization.cnf: |-
    [mysqld]
    datadir = /mariadb_data
    lower_case_table_names = 1
    sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    character-set-server=utf8
    [client]
    default-character-set=utf8
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: configmap
  
[root@node1 ~]# kubectl apply -f  configmap.yaml 
configmap/mysql-config created
[root@node1 ~]# kubectl get cm -n configmap
NAME               DATA   AGE
cm-data            1      9m24s
kube-root-ca.crt   1      23m
mysql-config       1      2s
test-cm-zifu       2      4m43s
test-config        1      23m
test-config-dir    2      20m
[root@node1 ~]# kubectl describe  cm mysql-config -n configmap
Name:         mysql-config
Namespace:    configmap
Labels:       <none>
Annotations:  <none>

Data
====
customization.cnf:
----
[mysqld]
datadir = /mariadb_data
lower_case_table_names = 1
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
character-set-server=utf8
[client]
default-character-set=utf8

BinaryData
====

Events:  <none>
[root@node1 ~]# 

三、configmap应用

  • 以volume形式挂载
1:编辑podyaml文件
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: configmap                                                                                                                                                     
spec:
  containers:
  - name: nginx
    image: docker.io/library/nginx:latest
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name:  mysql-config
      mountPath: /etc/config
  volumes:
  - name: mysql-config
    configMap:
      name: mysql-config

2:启动pod
[root@node1 ~]# kubectl apply -f  pod.yaml 
pod/nginx created
[root@node1 ~]# kubectl get po -n configmap
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          7s

3:进入pod查看挂载内容
[root@node1 ~]# kubectl exec -it nginx bash -n configmap
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx:/# cd etc/config/
root@nginx:/etc/config# ls
customization.cnf
root@nginx:/etc/config# cat customization.cnf 
[mysqld]
datadir = /mariadb_data
lower_case_table_names = 1
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
character-set-server=utf8
[client]
default-character-set=utf8root@nginx:/etc/config# 
  • 以环境变量的形式使用cm
1:创建cm
[root@node1 ~]# kubectl create cm cm-zifu --from-literal=testname=www --from-literal=testedge=30 -n configmap
configmap/cm-zifu created

2:pod使用
---        
apiVersion: v1
kind: Pod  
metadata:  
  name: nginx-env
  namespace: configmap
spec:      
  containers:
  - name: nginx
    image: docker.io/library/nginx:latest
    imagePullPolicy: IfNotPresent
    env:   
    - name: testname
      valueFrom:
        configMapKeyRef:
          name: cm-zifu                                                                                                                                                    
          key: testname
    - name: testedge
      valueFrom:
        configMapKeyRef:
          name: cm-zifu
          key: testedge
          
 3:启动pod,查看env         
[root@node1 ~]# kubectl apply -f  pod.yaml 
pod/nginx-env created
[root@node1 ~]# kubectl get po -n configmap
NAME        READY   STATUS    RESTARTS   AGE
nginx       1/1     Running   0          16m
nginx-env   1/1     Running   0          5s
[root@node1 ~]# kubectl exec -it nginx-env bash -n configmap
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-env:/# env  | grep test
testedge=30
testname=www
root@nginx-env:/# 
  • 以启动参数传入
一下以nginx 为例
1:首选查看nginx镜像的启动参数
[root@node1 ~]# crictl   inspecti docker.io/library/nginx:latest |more
        "Entrypoint": [
          "/docker-entrypoint.sh"
        ],
        "Cmd": [
          "nginx",
          "-g",
          "daemon off;"
        ],

2:先以正常的pod启动,编辑yaml文件
---  
apiVersion: v1
kind: Pod
metadata:
  name: nginx-cmd
  namespace: configmap
spec:
  containers:
  - name: nginx
    image: docker.io/library/nginx:latest
    imagePullPolicy: IfNotPresent
    command: ["/docker-entrypoint.sh"]
    args: ["nginx", "-g", "daemon off;"]
    
3:启动pod,查看启动日志
[root@node1 ~]# kubectl apply -f  pod.yaml 
pod/nginx-cmd created
[root@node1 ~]# 
[root@node1 ~]# 
[root@node1 ~]# kubectl logs nginx-cmd -n configmap
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/06/19 08:14:36 [notice] 1#1: using the "epoll" event method
2023/06/19 08:14:36 [notice] 1#1: nginx/1.21.6
2023/06/19 08:14:36 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/06/19 08:14:36 [notice] 1#1: OS: Linux 3.10.0-1160.6.1.el7.x86_64
2023/06/19 08:14:36 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/06/19 08:14:36 [notice] 1#1: start worker processes
2023/06/19 08:14:36 [notice] 1#1: start worker process 32
2023/06/19 08:14:36 [notice] 1#1: start worker process 33
2023/06/19 08:14:36 [notice] 1#1: start worker process 34
2023/06/19 08:14:36 [notice] 1#1: start worker process 35

[root@node1 ~]# kubectl get po -n configmap  -o wide 
NAME        READY   STATUS    RESTARTS   AGE   IP             NODE    NOMINATED NODE   READINESS GATES
nginx       1/1     Running   0          87m   10.233.96.28   node2   <none>           <none>
nginx-cmd   1/1     Running   0          15s   10.233.96.36   node2   <none>           <none>
nginx-env   1/1     Running   0          70m   10.233.92.33   node3   <none>           <none>
[root@node1 ~]# curl 10.233.96.36
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

4:**创建cm,指定参数**
[root@node1 ~]# kubectl create configmap nginx-cmd --from-literal=daemon="daemon off"  -n configmap
configmap/nginx-cmd created
[root@node1 ~]# kubectl get cm/nginx-cmd  -n configmap 
NAME        DATA   AGE
nginx-cmd   1      18s

5:编辑pod使用cm
---  
apiVersion: v1
kind: Pod
metadata:
  name: nginx-cmd
  namespace: configmap
spec:
  containers:
  - name: nginx
    image: docker.io/library/nginx:latest
    imagePullPolicy: IfNotPresent
    command: ["/docker-entrypoint.sh"]
    args: ["nginx", "-g", "$(daemon-cmd);"]                                                                                                                                
    env:
    - name: daemon-cmd
      valueFrom:
        configMapKeyRef:
          name:  nginx-cmd
          key: daemon
          
6:启动pod测试
[root@node1 ~]# kubectl apply -f  pod.yaml 
pod/nginx-cmd created
[root@node1 ~]# kubectl get po -n configmap -owide 
NAME        READY   STATUS    RESTARTS   AGE    IP             NODE    NOMINATED NODE   READINESS GATES
nginx-cmd   1/1     Running   0          2m5s   10.233.90.20   node1   <none>           <none>

[root@node1 ~]# kubectl logs nginx-cmd -n configmap
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/06/19 08:24:04 [notice] 1#1: using the "epoll" event method
2023/06/19 08:24:04 [notice] 1#1: nginx/1.21.6
2023/06/19 08:24:04 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2023/06/19 08:24:04 [notice] 1#1: OS: Linux 3.10.0-1160.6.1.el7.x86_64
2023/06/19 08:24:04 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/06/19 08:24:04 [notice] 1#1: start worker processes
2023/06/19 08:24:04 [notice] 1#1: start worker process 31
2023/06/19 08:24:04 [notice] 1#1: start worker process 32
2023/06/19 08:24:04 [notice] 1#1: start worker process 33
2023/06/19 08:24:04 [notice] 1#1: start worker process 34

7:测试业务正常
[root@node1 ~]# curl 10.233.90.20
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

  • 使用某一个cm的key作为volume挂载
1:创建一个多个from-file的cm
kubectl create configmap multi-file --from-file=test-cm-define=/etc/yum.repos.d/local.repo --from-file=/root/cert.pem --from-file=/root/dashboard.yaml  -n configmap

2:编辑yaml 使用此cm的某一个key
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx-key
  namespace: configmap
spec:
  containers:
  - name: nginx
    image: docker.io/library/nginx:latest
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: localrepo
      mountPath: /etc/yum.repos.d
  volumes:
  - name: localrepo
    configMap:
      name: multi-file
      items:
      - key: test-cm-define     ###指定单独的key
        path: loca.repo         ###挂载路径
        
3:启动pod,查看
[root@node1 ~]# kubectl apply -f   pod-key.yaml 
pod/nginx-key created
[root@node1 ~]# kubectl exec -it  nginx-key  bash -n configmap
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-key:/# cd etc/yum.repos.d/
root@nginx-key:/etc/yum.repos.d# 
root@nginx-key:/etc/yum.repos.d# ls
loca.repo
root@nginx-key:/etc/yum.repos.d#

到了这里,关于k8s configmap 详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 学习笔记二十九:K8S配置管理中心Configmap实现微服务配置管理

    Configmap是k8s中的资源对象,用于保存非机密性的配置的,数据可以用key/value键值对的形式保存,也可通过文件的形式保存。 Configmap能解决哪些问题? 我们在部署服务的时候,每个服务都有自己的配置文件,如果一台服务器上部署多个服务:nginx、tomcat、apache等,那么这些配置

    2024年02月06日
    浏览(36)
  • k8s概念-ConfigMap

    回到目录 一般用于去存储 Pod 中应用所需的一些配置信息,或者环境变量,将配置于 Pod 分开,避免应为修改配置导致还需要重新构建 镜像与容器。 1 创建ConfigMap 1.1 将指定目录下所有文件加载到配置文件中 1.2 指定一个或多个文件和key 1.3 命令上手动添加key-value 2 configma

    2024年02月14日
    浏览(27)
  • 轻松掌握K8S使用kubectl操作配置文件挂载ConfigMap和密钥Secret知识点05

    1、挂载应用配置文件配置集ConfigMap 当有许多应用如redis、mysql,希望将它的配置文件挂载出去,以便随时修改,可以用ConfigMap配置集 具体用法查看使用命令行操作里的 3、ConfigMap配置集实战 2、挂载应用配置文件的敏感信息Secret Secret 对象类型用来保存敏感信息,例如使用ya

    2024年02月16日
    浏览(83)
  • 开源项目ChatGPT-Next-Web的容器化部署(四)-- k8s容器部署使用configmap配置

    本文的内容是在k8s容器中,如何使用configmap对.env文件进行挂载,实现环境的差异化配置。 项目ChatGPT-Next-Web使用了.env文件来配置不同环境下的值: 所以,我们同理新增两个配置文件,见下: 生产环境的.env文件对应生产环境的数据库连接等信息,不可能写在上面的源码中。

    2024年04月12日
    浏览(41)
  • K8s Deployment挂载ConfigMap权限设置

    目录 样例 其中“defaultMode: 420”是设置权限的 在K8s(Kubernetes)中, defaultMode 是用来设置Configmap挂载后的文件权限,它采用Unix文件权限标准。 420 是8进制数字,转换成二进制是 100100000 ,转换成文件权限码就是 0644 。 文件权限码 0644 代表所有者(owner)有读/写权限(6=4+2),

    2024年02月20日
    浏览(37)
  • k8s里pv pvc configmap

    通过storageClassName 将PV 和PVC 关联起来。

    2024年02月11日
    浏览(20)
  • Kubernetes技术--k8s核心技术 configMap

    1.概述    configMap最主要的作用是 存储一些 不加密 的数据 到 /etcd ,让pod以变量或者数据卷(volume)挂载到容器。    应用场景:配置文件、存储信息等 2.使用 -1. 创建配置文件。 这里我们需要先编写一个配置文件。使用redis,如下所示:

    2024年02月10日
    浏览(39)
  • K8S初级入门系列之四-Namespace/ConfigMap/Secret

         本章节我们继续学习Namespace、ConfigMap、Secret基础概念,了解他们基本用法和操作。NameSpace为命名空间,在同一集群中试下资源隔离。ConfigMap通过key-value的方式实现明文配置数据的保存,Secret与ConfigMap类似,不过是采用密文方式保存。      K8S集群可以通过Namespace创建多

    2024年02月15日
    浏览(39)
  • 云原生-k8s核心概念(pod,deploy,service,ingress,configmap,volume)

    Gitee-k8s学习 云原生实战-kubernetes核心实战 Namespace是kubernetes系统中的一种非常重要资源,它的主要作用是用来实现多套环境的资源隔离或者多租户的资源隔离 Pod可以认为是容器的封装,一个Pod中可以存在一个或者多个容器。 kubernetes很少直接控制Pod,一般都是通过Pod控制器来

    2024年02月03日
    浏览(47)
  • 飞天使-k8s知识点12-kubernetes资源对象5-Volume与ConfigMap等

    为什么需要volume ConfigMap Volume nfs挂载volume 持久卷的痛点 参考文档: 作者:又拍云 链接:https://juejin.cn/post/7186925237592653884 来源:稀土掘金

    2024年01月18日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包