江西建设职业技术学院最新官方网站,铜山网站开发,学校网站首页设计图片,学校培训网站开发1 ConfigMap介绍
1.1 概述
在部署应用程序时#xff0c;我们都会涉及到应用的配置#xff0c;在容器中#xff0c;如Docker容器中#xff0c;如果将配置文件打入容器镜像#xff0c;这种行为等同于写死配置#xff0c;每次修改完配置#xff0c;镜像就得重新构建。当然…1 ConfigMap介绍
1.1 概述
在部署应用程序时我们都会涉及到应用的配置在容器中如Docker容器中如果将配置文件打入容器镜像这种行为等同于写死配置每次修改完配置镜像就得重新构建。当然我们也可以通过挂载包含该文件的卷进行配置管理和修改。而在k8s中我们要讲一种更好的方式即ConfigMap这种资源对象的出现更是极大的方便了应用程序的配置管理。 ConfigMap是一个或多个key/value的形式保存在k8s中内部可以管理变量也可以管理完整的配置文件内容。
1.2 用法
生成容器内的环境变量在pod中可以通过spec.env或者spec.envFrom进行引用。设置容器启动命令的启动参数前提是设置为环境变量。以卷volume的方式挂载到容器内部的文件或目录通过spec.volumes引用。 注:在使用命令的时候注意单词 configmap等价于cmcm算是简写类似于deployment可以使用命令时写成deployservice可以写成svcnamespace可以写成nspod可以写成po。 1.3 应用场景
configmap配置信息和镜像解耦实现方式是把配置信息放到configmap对象中然后在pod中作为volume挂载到pod中从而实现导入配置的目的
适用场景
通过configmap给pod定义全局环境变量通过configmap给pod传递命令行参数如mysql -u -p中的账户名密码都可以通过它传递通过configmap给pod中的容器服务提供配置文件配置文件以挂载到容器的形式适用
注意事项
configmap需要在pod使用它之前创建pod和configmap必须在同一个namespace中才能使用主要用于非安全加密的配置场景configmap通常用于小于1MB的配置常用于配置文件
1.4 创建
1.4.1 yaml文件方式创建
示例
apiVersion: v1
kind: ConfigMap
metadata:name: cm-test01
data:appconf01: value01appconf02: value02
命令
[rootk8s-master k8s]# kubectl create -f cm-test01.yaml
configmap/cm-test01 created
1.4.2 命令行方式创建
读取文件方式也可以是目录通过--from-file参数从文件中读取。可以指定key的名称若不指定则默认使用文件名为key。 案例如当前目录有一个配置文件为test.properties
[rootk8s-master k8s]# cat test.properties
key01:value01
key02:value02
conf01: value03
命令
[rootk8s-master k8s]# kubectl create cm cm-test-file --from-filetest.properties
configmap/cm-test-file created
//指定参数方式通过--from-literal指定keyxxvaluexx创建confimap中的data内配置属性。
[rootk8s-master k8s]# kubectl create configmap cm-test-literal --from-literalkey01value01 --from-literalkey02value02
configmap/cm-test-literal created
1.5 查询
1.5.1 查询configmap列表
[rootk8s-master k8s]# kubectl get cm
NAME DATA AGE
cm-test-file 1 3m51s
cm-test-literal 2 3m30s
cm-test01 2 9m2s
kube-root-ca.crt 1 4d16h
1.5.2 查看configmap详细信息
[rootk8s-master k8s]# kubectl describe cm cm-test01
Name: cm-test01
Namespace: default
Labels: none
Annotations: noneDataappconf01:
----
value01
appconf02:
----
value02
Events: none
[rootk8s-master k8s]# kubectl describe cm cm-test-file
Name: cm-test-file
Namespace: default
Labels: none
Annotations: noneDatatest.properties:
----
key01:value01
key02:value02
conf01: value03Events: none
[rootk8s-master k8s]# kubectl describe cm cm-test-literal
Name: cm-test-literal
Namespace: default
Labels: none
Annotations: noneDatakey01:
----
value01
key02:
----
value02
Events: none
1.5.3 查看yaml输出
[rootk8s-master k8s]# kubectl get cm cm-test01 -o yaml
apiVersion: v1
data:appconf01: value01appconf02: value02
kind: ConfigMap
metadata:creationTimestamp: 2022-08-07T00:18:47Zname: cm-test01namespace: defaultresourceVersion: 44876uid: 7492a928-b163-4b86-a3dd-54a7fbe59a10
[rootk8s-master k8s]# kubectl get configmap cm-test-file -o yaml
apiVersion: v1
data:test.properties: |key01:value01key02:value02conf01: value03kind: ConfigMap
metadata:creationTimestamp: 2022-08-07T00:23:58Zname: cm-test-filenamespace: defaultresourceVersion: 45341uid: 818a39b4-cbaf-4643-9541-1119b4d61982
[rootk8s-master k8s]# kubectl get cm cm-test-literal -o yaml
apiVersion: v1
data:key01: value01key02: value02
kind: ConfigMap
metadata:creationTimestamp: 2022-08-07T00:24:19Zname: cm-test-literalnamespace: defaultresourceVersion: 45372uid: 051849e0-bf0a-4926-a549-2405dc4963cc
1.6 更新
1.6.1 edit
[rootk8s-master k8s]# kubectl edit cm cm-test01
configmap/cm-test01 edited
通过kubectl describe cm cm-test01查看更新是否生效。
[rootk8s-master k8s]# kubectl describe cm cm-test01
Name: cm-test01
Namespace: default
Labels: none
Annotations: noneDataappconf02:
----
value02
appconf01:
----
value001
Events: none
1.6.2 apply
直接更新yaml文件里的值通过 kubectl apply -f configmap-test01.yaml重新发布一遍进行更新。
1.7 删除
1.7.1 通过yaml文件方式删除
$ kubectl delete -f configmap-test01.yaml
1.7.2 直接删除资源
kubectl delet cm cm-test01
2 ConfigMap和Pod的使用
容器应用对ConfigMap的使用主要是两种 1通过环境变量获取ConfigMap的内容spec.env和spec.envFrom 2通过卷volume挂载的方式将ConfigMap的内容挂载到容器内部的文件或目录spec.volumes。
2.1 环境变量方式
2.1.1 sepc.env方式
2.1.1.1 创建pod
[rootk8s-master k8s]# vim pod-test01.yamlapiVersion: v1
kind: Pod
metadata:name: cm-pod-test001
spec:containers:- name: cm-testimage: tomcat:8command: [ /bin/sh, -c, env | grep APP]env:- name: APPCONF01 # 定义环境变量的名称valueFrom: # key “appconf01”的值获取configMapKeyRef:name: cm-test01 # 环境变量的值来自于configmap cm-test01key: appconf01 # configmap中的配置key为appconf01- name: APPCONF02 # 定义环境变量的名称valueFrom: # key “appconf02”的值获取configMapKeyRef:name: cm-test01 # 环境变量的值来自于configmap cm-test01key: appconf02 # configmap中的配置key为appconf02restartPolicy: Never # 重启策略从不。
执行创建pod
[rootk8s-master k8s]# kubectl create -f pod-test01.yaml
pod/cm-test001 created
2.1.1.2 查看pod
[rootk8s-master k8s]# kubectl get pods
[rootk8s /cm/test]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cm-pod-test001 0/1 Completed 0 1h
2.1.1.3 查看pod日志
[rootk8s-master k8s]# kubectl logs cm-pod-test001
APPCONF01value01
APPCONF02value02
说明容器内部的环境变量使用ConfigMap中进行读取的
2.1.2 spec.envFrom方式
2.1.2.1 创建pod
[rootk8s-master k8s]# vim pod-test02.yamlpiVersion: v1
kind: Pod
metadata:name: cm-pod-test002
spec:containers:- name: cm-test2image: tomcat:8command: [ /bin/sh, -c, env]envFrom:- configMapRef:name: cm-test01 # 根据ConfigMap cm-test01资源自动生成环境变量restartPolicy: Never
执行创建pod
[rootk8s-master k8s]# kubectl create -f pod-test02.yaml
2.1.2.2 查看pod
[rootk8s-master k8s]# kubectl get po
NAME READY STATUS RESTARTS AGE
cm-pod-test001 0/1 Completed 0 2h
cm-pod-test002 0/1 Completed 0 1h
注意
环境变量的名称受限制[a-zA-Z][a-zA-Z0-9_]*不能以数字或非法字符开头。
2.2 卷挂载方式
2.2.1 指定items
[rootk8s-master k8s]# vim pod-test03.yaml
apiVersion: v1
kind: Pod
metadata:name: cm-pod-test003
spec:containers:- name: cm-test3image: tomcat:8volumeMounts:- name: vm-01-1mountPath: /confvolumes:- name: vm-01-1configMap:name: cm-test-fileitems:- key: key-testpropertiespath: test.propertiesrestartPolicy: Never
2.2.2 不指定items
[rootk8s-master k8s]# vim pod-test04.yaml
apiVersion: v1
kind: Pod
metadata:name: cm-pod-test004
spec:containers:- name: cm-test4image: tomcat:8volumeMounts:- name: vm-02-2mountPath: /confvolumes:- name: vm-02-2configMap:name: cm-test-filerestartPolicy: Never
进入容器中查看
[rootk8s-master k8s]# kubectl exec -it cm-pod-test004 -c cm-test4 -- bash
进入容器后ls /conf查看是否有test.properties文件。
[rootk8s-master k8s]# kubectl exec -it cm-pod-test004 -c cm-test4 -- bash
rootcm-pod-test004:/usr/local/tomcat# ls /conf
test.properties
2.3 补充
关于--from-file的方式的创建指定key和不指定key的区别 1不指定key名
创建
[rootk8s-master k8s]# kubectl create cm cm-test-file --from-filetest.properties
输出
[rootk8s-master k8s]# kubectl get cm cm-test-file -o yaml
2指定key 创建
[rootk8s-master k8s]# kubectl create cm cm-test-file02 --from-filetptest.properties
输出
[rootk8s-master k8s]# kubectl get cm cm-test-file -o yaml
若指定key的名称configmap中将会使用指定名称若不指定则默认使用文件名为key。