|NO.Z.00171|——————————|CloudNative|——|KuberNetes&配置管理.V02|——|configmap.v02|从文件创建configmap|

一、从文件创建configmap
### --- 可以用来kubectl create configmap从单个文件或多个文件创建ConfigMap。
~~~     也可以定义它的名字,在前面加入命名方式,若是不加的话默认是按照它的文件名来命名的:
~~~     这种方式在生产环境中是最常用的。

[root@k8s-master01 ~]# kubectl create cm game-ui-cm --from-file=configure-pod-container/configmap/ui.properties 
configmap/game-ui-cm created
[root@k8s-master01 ~]# kubectl describe cm game-ui-cm
Name:         game-ui-cm
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
ui.properties:                  // 这次创建的只有ui.properties
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
二、使用--from-file参多次传入参数
### --- 可以--from-file多次传入参数以从多个数据源创建ConfigMap。

[root@k8s-master01 ~]# kubectl create cm game-ui-cm-custom-name --from-file=ui-pro=configure-pod-container/configmap/ui.properties 
configmap/game-ui-cm-custom-name created
[root@k8s-master01 ~]# kubectl describe cm game-ui-cm-custom-name
Name:         game-ui-cm-custom-name
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
ui-pro:                             // 可以看到它的名称发生了变化,是我们定义的参数。
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
三、使用该选项--from-env-file可从环境文件创建configmap
### --- from-env-file也是一个kv形式的,它创建的不包含名字;直接读出下面的参数

[root@k8s-master01 comfigmap]# cat configure-pod-container/configmap/game.properties 
enemies=aliens
lives=3
allowed="true"
[root@k8s-master01 configmap]# wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
### --- 创建env

[root@k8s-master01 configmap]# kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties 
configmap/game-config-env-file created
四、查看game-config-env-file.yaml
### --- 查看创建的configmap.yaml配置文件
~~~     警告:--from-env-file多次传递以从多个数据源创建ConfigMap时,仅使用最后一个env文件。

[root@k8s-master01 configmap]# kubectl get configmap game-config-env-file -o yaml
apiVersion: v1
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"
kind: ConfigMap
metadata:
  creationTimestamp: "2021-04-24T10:07:54Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:allowed: {}
        f:enemies: {}
        f:lives: {}
    manager: kubectl-create
    operation: Update
    time: "2021-04-24T10:07:54Z"
  name: game-config-env-file
  namespace: default
  resourceVersion: "640922"
  uid: fad61760-e146-476d-ac6b-7db98c6cbc12
五、然后通过env文件生成环境变量
### --- 然后通过env文件生成环境变量:--from-env-file多次通过的行为通过以下方式证明:

[root@k8s-master01 ~]# wget https://kubernetes.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties
### --- 创建2个这个文件

[root@k8s-master01 configmap]# 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
六、查看创建的config-multi-env-files配置文件
### --- 查看创建的yaml配置文件

[root@k8s-master01 comfigmap]# kubectl get configmap config-multi-env-files -o yaml
apiVersion: v1
data:
  color: purple
  how: fairlyNice
  textmode: "true"
kind: ConfigMap
metadata:
  creationTimestamp: "2021-04-24T10:10:59Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:color: {}
        f:how: {}
        f:textmode: {}
    manager: kubectl-create
    operation: Update
    time: "2021-04-24T10:10:59Z"
  name: config-multi-env-files
  namespace: default
  resourceVersion: "641356"
  uid: aacb394c-23c4-48f1-af7e-e2465f204426

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(31)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示