Kubernetes 学习整理(五)

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

posted @ 2024-01-28 20:55  vivi~  阅读(5)  评论(0编辑  收藏  举报