Kubernetes K8S之存储ConfigMap详解

本文详细介绍了Kubernetes中的ConfigMap,包括其作为非机密数据存储的作用、创建方法(目录、文件、命令行、yaml)、在Pod中的多种使用方式(环境变量、命令行参数、数据卷)以及ConfigMap的热更新过程,帮助理解ConfigMap在K8S集群中的配置管理。
摘要由CSDN通过智能技术生成

K8S之存储ConfigMap概述与说明,并详解常用ConfigMap示例

主机配置规划

服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟)
k8s-master CentOS7.7 2C/4G/20G 172.16.1.110 10.0.0.110
k8s-node01 CentOS7.7 2C/4G/20G 172.16.1.111 10.0.0.111
k8s-node02 CentOS7.7 2C/4G/20G 172.16.1.112 10.0.0.112

ConfigMap概述

ConfigMap 是一种 API 对象,用来将非机密性的数据保存到健值对中。使用时可以用作环境变量命令行参数或者存储卷中的配置文件。

ConfigMap 将环境配置信息和容器镜像解耦,便于应用配置的修改。当你需要储存机密信息时可以使用 Secret 对象。

备注:ConfigMap 并不提供保密或者加密功能。如果你想存储的数据是机密的,请使用 Secret;或者使用其他第三方工具来保证数据的私密性,而不是用 ConfigMap。

ConfigMap创建方式

通过目录创建

配置文件目录

[root@k8s-master storage]# pwd
/root/k8s_practice/storage
[root@k8s-master storage]# ll /root/k8s_practice/storage/configmap    # 配置文件存在哪个目录下
total 8
-rw-r--r-- 1 root root 159 Jun  7 14:52 game.properties
-rw-r--r-- 1 root root  83 Jun  7 14:53 ui.properties
[root@k8s-master storage]# 
[root@k8s-master storage]# cat configmap/game.properties    # 涉及文件1
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAs
secret.code.allowed=true
secret.code.lives=30

[root@k8s-master storage]# 
[root@k8s-master storage]# cat configmap/ui.properties   # 涉及文件2
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

创建ConfigMap并查看状态

[root@k8s-master storage]# kubectl create configmap game-config --from-file=/root/k8s_practice/storage/configmap
configmap/game-config created
[root@k8s-master storage]# 
[root@k8s-master storage]# kubectl get configmap 
NAME          DATA   AGE
game-config   2      14s

查看ConfigMap有哪些数据

[root@k8s-master storage]# kubectl get configmap -o yaml   ##### 查看方式1
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: "2020-06-07T06:57:28Z"
    name: game-config
    namespace: default
    resourceVersion: "889177"
    selfLink: /api/v1/namespaces/default/configmaps/game-config
    uid: 6952ac85-ded0-4c5e-89fd-b0c6f0546ecf
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
[root@k8s-master storage]# 
[root@k8s-master storage]# kubectl describe configmap game-config   ##### 查看方式2
Name:         game-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

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

Events:  <none>

通过文件创建

配置文件位置

[root@k8s-master storage]# pwd
/root/k8s_practice/storage
[root@k8s-master storage]# cat /root/k8s_practice/storage/configmap/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

创建ConfigMap并查看状态

[root@k8s-master storage]# kubectl create configmap game-config-2 --from-file=/root/k8s_practice
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值