首页 > 其他分享 >Kubernetes 学习整理(五)

Kubernetes 学习整理(五)

时间:2024-01-28 21:14:09浏览次数:28  
标签:configmap Kubernetes ConfigMap 学习 game file 整理 config properties

k8s-ConfigMap

Configure a Pod to Use a ConfigMap

Create a ConfigMap

包含不超过 253 个字符,只能包含小写字母、数字和 '-',必须以字母或数字开头和结尾)

kubectl create configmap <map-name> <data-source>

Create a ConfigMap from a directory

mkdir -p configure-pod-container/configmap/
wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties

wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties

#查看这两个文件内容

cat configure-pod-container/configmap/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true

cat configure-pod-container/configmap/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice


# 创建这两个文件所在的dir 的configmap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/
#configmap/game-config created


# 查看创建的configmap, 注意有2个data
kubectl describe configmaps game-config
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


BinaryData
====

Events:  <none>

读取configmap内容并以yaml格式输出

kubectl get configmaps game-config -o yaml

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: "2024-01-25T09:40:29Z"
  name: game-config
  namespace: default
  resourceVersion: "5317074"
  uid: 6c374c5c-7393-4f10-af7d-21f07272ff6d

输出的 YAML 文件包含了 ConfigMap 的详细信息,包括:

apiVersion: Kubernetes API 的版本。
kind: 资源类型,这里是 ConfigMap。
metadata: 元数据,包括创建时间、名称、命名空间、资源版本等。
data: 配置数据,包括两个键值对,分别对应 game.properties 和 ui.properties。
game.properties: 包含游戏配置的键值对。
ui.properties: 包含用户界面配置的键值对。
这样的输出对于查看和备份 ConfigMap 的配置信息非常有用。您可以通过将输出保存到文件,然后稍后使用 kubectl apply -f filename.yaml 将配置重新应用到集群中。

create configmap from single one file or more files

kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
#configmap/game-config-2 created


kubectl create configmap game-config-3 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties

create configmap from a env file: --from-env-file

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
wget https://kubernetes.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties

# The env-file `game-env-file.properties` looks like below
cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"

kubectl create configmap game-config-env-file \
>        --from-env-file=configure-pod-container/configmap/game-env-file.properties
#configmap/game-config-env-file created

kubectl get configmap game-config-env-file -o yaml
apiVersion: v1
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"
kind: ConfigMap
metadata:
  creationTimestamp: "2024-01-25T09:57:01Z"
  name: game-config-env-file
  namespace: default
  resourceVersion: "5325666"
  uid: 22718429-0066-4ca6-a647-68d673fd76a3

也支持多个env文件

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/config-multi-env-files created

kubectl get configmap config-multi-env-files -o yaml
apiVersion: v1
data:
  allowed: '"true"'
  color: purple
  enemies: aliens
  how: fairlyNice
  lives: "3"
  textmode: "true"
kind: ConfigMap
metadata:
  creationTimestamp: "2024-01-25T09:59:19Z"
  name: config-multi-env-files
  namespace: default
  resourceVersion: "5326865"
  uid: ce7bccb5-247c-4136-b89a-61c61357f6fd

创建 ConfigMap 时定义文件内容的键名

每个键值对对应一个文件,这对于将多个配置文件合并到一个 ConfigMap 中是很有用的。

# 创建一个configmap,名字是game-config-4 并指定它的键名为: game-special-key
kubectl create configmap game-config-4 --from-file=game-special-key=configure-pod-container/configmap/game.properties
# configmap/game-config-4 created

# 查看这个新创建的game-config-4, 发现文件的内容都存储在data下面的名字叫game-special-key之下。
# 这种定义键的方式允许你在 ConfigMap 中存储多个键值对,每个键值对对应一个文件。
# 这对于将多个配置文件合并到一个 ConfigMap 中是很有用的,每个文件可以使用不同的键标识。
kubectl get configmaps game-config-4 -o yaml
apiVersion: v1
data:
  game-special-key: |-
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
kind: ConfigMap
metadata:
  creationTimestamp: "2024-01-28T08:58:13Z"
  name: game-config-4
  namespace: default
  resourceVersion: "7541098"
  uid: e2d3e03b-1804-44fc-99d8-fb0dfd0b4a19

这种定义键的方式允许你在 ConfigMap 中存储多个键值对,每个键值对对应一个文件。
这对于将多个配置文件合并到一个 ConfigMap 中是很有用的,每个文件可以使用不同的键标识。

直接指定键值对来创建configmap

用于存储少量的文本配置数据,例如环境变量、参数或简单的键值对。

# 在create configmap的命令行中,直接指定 key value:special.how=very,  special.type=charm 
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
#configmap/special-config created

kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: "2024-01-28T09:05:30Z"
  name: special-config
  namespace: default
  resourceVersion: "7544876"
  uid: abb26738-ef90-4489-b1ae-7cff7ce5a46d

通过生成器generator创建 ConfigMap

用于需要基于特定规则或算法生成配置数据的场景,而不是手动指定每个键值对。

Kustomize 是一个 Kubernetes 定制工具,

用于根据特定需求对 Kubernetes YAML 配置进行定制。
它允许你在不修改原始 YAML 文件的情况下,通过一个或多个 Overlay(叠加层)来定制配置。

具体来说,Kustomize 提供了以下功能:

  1. 生成器(Generator): 允许你动态生成 Kubernetes 资源。例如,通过指定一个目录,Kustomize 可以自动识别和处理该目录中的 YAML 文件,生成相应的 Kubernetes 资源对象。

  2. 变换(Transformation): 允许你通过叠加层的方式修改生成的 Kubernetes 配置。你可以覆盖、添加或删除配置。

  3. 变量替换: 允许你在配置文件中使用变量,并在生成时进行替换。

  4. 模板功能: 提供了灵活的模板功能,可以根据需要重用部分配置。

具体使用kustomize 的 generator 功能来通过yaml文件配置来动态生成 configmap file

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4
  options:
    labels:
      game-config: config-4
  files:
  - configure-pod-container/configmap/game.properties
EOF
  1. configMapGenerator: 这是 Kustomize 中用于生成 ConfigMap 的部分。
  2. name: game-config-4: 这定义了生成的 ConfigMap 的名称为 game-config-4。
  3. options: 这里的 options 允许你指定配置的一些选项。
  4. labels: 在这里,你为生成的 ConfigMap 添加了一个标签 game-config: config-4。
  5. files: 这列出了要包含在 ConfigMap 中的文件,这里是 configure-pod-container/configmap/game.properties。
    所以,这个 kustomization.yaml 文件告诉 Kustomize 工具从 game.properties 文件生成一个名为 game-config-4 的 ConfigMap,并为其添加了一个标签。

kubectl apply -k 是 kubectl 命令的一种使用方式,

其中 -k 标志指示 kubectl 使用 Kustomize 风格的配置目录。
Kustomize 是一种 Kubernetes 配置定制工具,允许你定义和生成 Kubernetes 资源。

生成的 ConfigMap 名称有一个后缀,通过对内容进行哈希处理而附加。这可确保每次修改内容时都会生成一个新的 ConfigMap

kubectl apply -k .
#configmap/game-config-4-tbg7c4gc77 created

# check the existed configmap files
kubectl get configmap
NAME                         DATA   AGE
config-multi-env-files       6      3d2h
game-config                  2      3d2h
game-config-2                1      3d2h
game-config-3                2      3d2h
game-config-4                1      3h25m
game-config-4-tbg7c4gc77     1      36s
game-config-env-file         3      3d2h
istio-ca-root-cert           1      10d
kube-root-ca.crt             1      10d
platform-manager-configmap   2      10d
special-config               2      3h17m

# 也可以查看详情,发现它确实有个labels:game-config=config-4
kubectl describe configmaps game-config-4-tbg7c4gc77
Name:         game-config-4-tbg7c4gc77
Namespace:    default
Labels:       game-config=config-4
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

BinaryData
====

Events:  <none>

使用kustomize generator 通过yaml来生成configfile时,定义文件的键名

在yaml文件的files中, 给引用的file指定了键名:game-spacial-key

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-5
  options:
    labels:
      game-config: config-5
  files:
  - game-special-key=configure-pod-container/configmap/game.properties
EOF

# -k 指示 kubectl 使用 Kustomize 风格的配置目录
kubectl apply -k .
#configmap/game-config-5-tfhf8f4fkf created

# 生成了新的configmap game-config-5-tfhf8f4fkf
ali1-atlantic-host:~/k8scluster-automation # kubectl get configmaps
NAME                         DATA   AGE
config-multi-env-files       6      3d2h
game-config                  2      3d2h
game-config-2                1      3d2h
game-config-3                2      3d2h
game-config-4                1      3h36m
game-config-4-tbg7c4gc77     1      12m
game-config-5-tfhf8f4fkf     1      10s
game-config-env-file         3      3d2h
istio-ca-root-cert           1      10d
kube-root-ca.crt             1      10d
platform-manager-configmap   2      10d
special-config               2      3h29m

# 新的configmap 中 data 那里有被命名键名:game-special-key:
ali1-atlantic-host:~/k8scluster-automation # kubectl describe configmaps game-config-5-tfhf8f4fkf
Name:         game-config-5-tfhf8f4fkf
Namespace:    default
Labels:       game-config=config-5
Annotations:  <none>

Data
====
game-special-key:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

BinaryData
====

Events:  <none>

使用kustomize generator 指定键值对来生成configmap file



# kustomization.yaml contents for creating a ConfigMap from literals
configMapGenerator:
- name: special-config-2
  literals:
  - special.how=very
  - special.type=charm


kubectl describe configmaps special-config-2-2b86tk8fhm
Name:         special-config-2-2b86tk8fhm
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.how:
----
very
special.type:
----
charm

BinaryData
====

Events:  <none>

cleanup

kubectl delete configmap -l 'game-config in (config-4,config-5)'
configmap "game-config-4-tbg7c4gc77" deleted
configmap "game-config-5-tfhf8f4fkf" deleted

标签:configmap,Kubernetes,ConfigMap,学习,game,file,整理,config,properties
From: https://www.cnblogs.com/vivivi/p/17993331

相关文章

  • 跟着思兼学习Klipper(26): 大卸八块 Klipper 远程控制实验汇总
    又名《给创想三维K1找个"强力外援"》前言原创文章,转载引用请务必注明链接,水平有限,如有疏漏,欢迎交流指正。文章如有更新请访问DFRobot社区及cnblogs博客园,前者内容较全,后者排版及阅读体验更佳。我们约定:主板指MCU部分,上位机指运行Klippy的MPULinux部分。玩了好......
  • Oracle数据类型的简单学习之一
    Oracle数据类型的简单学习之一背景因为信创安可替代的发展有很多项目提到了数据库切换到国产数据库的要求.一般情况是要求从Oracle/SQLServer迁移到国产的:达梦/瀚高/人大金仓/南大通用等数据库.但是因为Oracle作为数据库领域No.1的存在他对SQL的规范标准支持的并不是很......
  • IBM java的分析工具(ga和ha)学习和整理
    IBMjava的分析工具(ga和ha)学习和整理背景前几天学习了整理了jca工具今天继续学习一下ga工具ga工具主要是分析gclog相关.可以很直观的进行gclog的分析和展示.除了mat之外还有一个比较轻量级的内存dump分析工具ha.想着一起学习和分析一下.ga工具的相关学习下载:https......
  • IBM jca 工具的学习与整理
    IBMjca工具的学习与整理背景发现自己最早看到IBM这个工具的时间是2022年9月份.但是一直没有进行过仔细的学习与论证.本周出现了一个问题.虽然通过gclog明显看出来是一个oom然后内存对象里面排第一的是hashnode相关内容猜测出可能是excel导入/导出相关的内容但是自己......
  • perf_event_open 学习 —— 通过read的方式读取硬件技术器
    目录示例程序1单计数器多计数器示例程序2ConfigureasinglecounterConfiguremultiplecounters(nomultiplexing)示例程序1Linuxperf子系统的使用(一)——计数刚刚入职的时候我就研究了perf_event_open()这个巨无霸级别的系统调用,还用Python封装了一层,非常便于获取计数器......
  • 《深入浅出计算机组成原理》学习笔记1——计算机基本组成与指令执行
    一丶冯·诺依曼体系结构:计算机组成的金字塔1.从装机的角度看计算机基本组成CPU:计算机最重要的核心配件,全称中央处理器,计算机的所有“计算”都是由CPU来进行的内存撰写的程序、打开的浏览器、运行的游戏,都要加载到内存里才能运行。程序读取的数据、计算得到的结果,也都要......
  • JS原型链: 一次学习终生受用
    ProtoType原型|原型对象|显示原型prototype它是函数的一个属性prototype是一个对象。当我们创建函数的时候会默认添加prototype这个属性 __proto__隐式原型(由浏览器实现)对象的属性。__proto__属性是创建对象时自动添加的,默认值为其构造函数的prototype[[pr......
  • 【学习笔记】代数
    向量咕。线性方程组定义线性方程组指的是形如\[\begin{aligned}a_{11}&x_1+a_{12}x_2+\cdots+a_{1n}x_n=b_1\\a_{21}&x_1+a_{22}x_2+\cdots+a_{2n}x_n=b_2\\&\vdots\\\\\\\\\\\\\vdots\\\\\\\\\ddots\\\\\\\......
  • 1/28 学习进度笔记
    SQL风格语法-注册DataFrame成为表DataFrame的一个强大之处就是我们可以将它看作是一个关系型数据表,然后可以通过在程序中使用spark.sql()来执行SQL语句查询,结果返回一个DataFrame。如果想使用SQL风格的语法,需要将DataFrame注册成表,采用如下的方式:df.createTempView(""score"......
  • perf_event_open学习 —— mmap方式读取
    目录示例程序2采集单个值采集多个值示例程序2示例程序3示例程序2Linuxperf子系统的使用(二)——采样(signal方式)在上一篇《Linuxperf子系统的使用(一)——计数》已经讲解了如何使用perf_event_open、read和ioctl对perf子系统进行编程。但有时我们并不需要计数,而是要采样。比如这......