一、基于字符值创建¶
1.创建ConfigMap
[root@k8s-master01 conf]# kubectl create cm envfromliteral --from-literal=level=INFO --from-literal=PASSWORD=redis123
2.验证
[root@k8s-master01 conf]# kubectl get cm envfromliteral -oyaml
apiVersion: v1
data:
PASSWORD: redis123
level: INFO
kind: ConfigMap
metadata:
creationTimestamp: "2022-12-03T08:43:38Z"
name: envfromliteral
namespace: default
resourceVersion: "36640"
uid: 7bd86383-d671-49be-b174-18be41ee5a7e
[root@k8s-master01 ~]# kubectl describe cm envfromliteral
Name: envfromliteral
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
level:
----
INFO
PASSWORD:
----
redis123
BinaryData
====
Events: <none>
3.5 基于yaml文件¶
1.编写yaml文件
[root@k8s-master01 ~]# mkdir -p configmap/conf
[root@k8s-master01 ~]# vim configmap/conf/cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: envfromliteral2
namespace: default
data:
PASSWORD: redis123
level: INFO
2.创建cm
[root@k8s-master01 ~]# kubectl create -f configmap/conf/cm.yaml
3.进行验证
[root@k8s-master01 ~]# kubectl get cm
NAME DATA AGE
cmfromdir 2 16m
cmfromfile 1 12m
envfromliteral 2 5m46s
envfromliteral2 2 27s
gameenvcm 2 7m25s
kube-root-ca.crt 1 51d
[root@k8s-master01 ~]# kubectl get cm envfromliteral2 -oyaml
apiVersion: v1
data:
PASSWORD: redis123
level: INFO
kind: ConfigMap
metadata:
creationTimestamp: "2023-05-02T09:29:17Z"
name: envfromliteral2
namespace: default
resourceVersion: "724100"
uid: fd63ca4d-e950-4ac1-a462-e133d03dd8d8
[root@k8s-master01 ~]# kubectl describe cm envfromliteral2
Name: envfromliteral2
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
PASSWORD:
----
redis123
level:
----
INFO
BinaryData
====
Events: <none>
3.6 自定义conf文件¶
3.6.1 自定义单个conf文件¶
1.创建工作目录
[root@k8s-master01 ~]# mkdir -p configmap/conf
2.编写配置文件
[root@k8s-master01 ~]# vim configmap/conf/game.conf
lives=3
secret.code=true
3.创建ConfigMap,指定key名为game-conf
[root@k8s-master01 ~]# cd configmap/conf/
[root@k8s-master01 conf]# kubectl create cm cmspecialname --from-file=game-conf=game.conf
configmap/cmspecialname created
4.验证
[root@k8s-master01 conf]# kubectl get cm
NAME DATA AGE
cmfromdir 2 15m
cmfromfile 1 10m
cmspecialname 1 41s
kube-root-ca.crt 1 26h
[root@k8s-master01 conf]# kubectl get cm cmspecialname -oyaml
apiVersion: v1
data:
game-conf: |
lives=3
secret.code=true
kind: ConfigMap
metadata:
creationTimestamp: "2022-12-03T08:29:15Z"
name: cmspecialname
namespace: default
resourceVersion: "34779"
uid: 8a5ec907-81bf-4467-a38e-4bf36c2b731f
3.6.2 自定义多个conf文件¶
1.创建工作目录
[root@k8s-master01 ~]# mkdir -p configmap/conf
2.编写配置文件
[root@k8s-master01 ~]# vim configmap/conf/game.conf
lives=3
secret.code=true
[root@k8s-master01 ~]# vim configmap/conf/redis.conf
password 123
3.创建ConfigMap,指定多个key名,有game-conf和redis-conf
[root@k8s-master01 ~]# cd configmap/conf/
[root@k8s-master01 conf]# kubectl create cm cmspecialname2 --from-file=game-conf=game.conf --from-file=redis-conf=redis.conf
4.验证
[root@k8s-master01 conf]# kubectl get cm cmspecialname2 -oyaml
apiVersion: v1
data:
game-conf: |
lives=3
secret.code=true
redis-conf: |
password 123
kind: ConfigMap
metadata:
creationTimestamp: "2022-12-03T08:31:50Z"
name: cmspecialname2
namespace: default
resourceVersion: "35112"
uid: 48514c82-e671-4816-8abf-33f9fe84cbe6