Kubernetes-11:ConfigMap介绍及演示
发布时间:2020-12-14 15:16:28 所属栏目:百科 来源:网络整理
导读:ConfigMap存在的意义 ConfigMap 功能在 Kubernetes1.2版本引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息,ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以用来保存单个属性,也可以用来保存整个配置文件或者J
ConfigMap存在的意义
? ConfigMap的创建 I、使用目录创建 ## 创建目录 [root@Centos8 k8sYaml]# mkdir dir [root@Centos8 k8sYaml]# cd dir/ ## 创建2个文件,文件内存放值 [root@Centos8 dir]# cat << EOF > 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 > EOF [root@Centos8 ui.properties > color.good=purple > color.bad=yellow > allow.textmode=true > how.nice.to.look=fairlyNice >ls game.properties ui.properties ## 开始创建ConfigMap [root@Centos8 dir]# kubectl create configmap game-config --from-file=/root/k8sYaml/ configmap/game-config created ### 查看ConfigMap [root@Centos8 ]# kubectl get cm NAME DATA AGE game-config 2 2m19s [root@Centos8 dir]# kubectl describe cm 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= secret.code.lives=30 ui.properties: ---- color.good=purple color.bad=yellow allow.textmode= how.fairlyNice Events: <none>
? II、使用文件名创建 ## 创建文件 [root@Centos8 cat wuzi.examplate NAME=wuzi URL=www.wuzi.com ## 创建ConfigMap [root@Centos8 dir]# kubectl create cm wuzi.com --from-wuzi.examplate configmap/wuzi.com created ## 查看cm [root@Centos8 ]# kubectl get cm NAME DATA AGE game-config 7m37s wuzi.com 1 41s [root@Centos8 ]# kubectl describe cm wuzi.com Name: wuzi.com Namespace: default Labels: <none> wuzi.examplate: ---- NAME=www.wuzi.com Events: <none>
? III、使用字面值创建(直接创建)
## 创建cm [root@Centos8 dir]# kubectl create cm spec.examplate --from-literal=spec.how=very --from-literal=spec.type=charm configmap/spec.examplate created ## 查看cm [root@Centos8 ]# kubectl describe cm spec.examplate Name: spec.examplate Namespace: default Labels: <none> spec.how: ---- very spec.type: ---- charm Events: <none> ## 也可以通过资源清单的方式来创建ConfigMap vim map.yaml ... apiVersion: v1 kind: ConfigMap metadata: name: spec-config namespace: default data: special.now: 6.4 special.time: 12:00 ... ## 查看 [root@Centos8 dir]# kubectl create -f map.yaml configmap/spec-config created [root@Centos8 dir]# kubectl describe cm spec-config Name: spec- special.now: ---- special.time: ---- Events: <none> ? 创建Pod 测试ConfigMap I、使用ConfigMap设置环境变量 vim cm-test.yaml ... apiVersion: v1 kind: Pod metadata: name: cm-pod spec: containers: - name: cm-container image: hub.vfancloud.com/test/myapp:v1 command: [ "/bin/sh",-cenv" ] env: #将名为spec.examplate的ConfigMap中名为spec.how的key的value赋值给环境变量SPEC_LEVEL_KEY - name: SPEC_LEVEL_KEY valueFrom: configMapKeyRef: name: spec.examplate key: spec.how - name: SPEC_TYPE_KEY valueFrom: configMapKeyRef: name: spec.examplate key: spec.type envFrom: #将名为wizi.com的ConfigMap中定义的所有key:value都导入环境变量 - configMapRef: name: wuzi.com restartPolicy: Never ... ## 查看环境变量 [root@Centos8 dir]# kubectl log cm-pod | grep -E SPEC_LEVEL_KEY|SPEC_TYPE_KEY|NAME|URL HOSTNAME=cm-pod SPEC_LEVEL_KEY=very wuzi.examplate=NAME=www.wuzi.com SPEC_TYPE_KEY=charm ? II、通过数据卷插件使用configMap vim volum.yaml ... apiVersion: v1 kind: Pod metadata: name: volum-cm spec: containers: - name: volum-ct image: hub.vfancloud.com/test/myapp:v1 volumeMounts: # 挂载共享数据卷 - name: config-volume # 要挂载共享数据卷的名字 mountPath: /etc/config # 挂载到指定的目录下 volumes: # 定义共享数据卷 - name: config-volume # 共享数据卷名称 configMap: name: game-config # ConfigMap的名称 # name: wuzi.com # name: spec-config restartPolicy: Never ... [root@Centos8 f volum.yaml ### 分别以文章开头的第一步创建的ConfigMap类型(目录、文件、键值)三种形式进行试验 ## ConfigMap类型为目录的,直接将此目录下的所有文件共享到了container指定目录中 /etc/config # game.properties ui.properties /etc/config # game.properties enemies=30 /etc/config # ui.properties color.good=fairlyNice ## ConfigMap类型为文件的,只将此文件共享到了container目录下 /etc/config # wuzi.examplate /etc/config # www.wuzi.com ## ConfigMap类型为键值对的,将key保存成了文件名,value保存为文件内容 /etc/config # special.now special.time /etc/config # cat special.time 00/etc/config # /etc/config # special.now 4/etc/config # ? 滚动更新 ## 先创建一个索引文件 [root@Centos8 index1.html Hello World ## 使用此文件创建ConfigMap [root@Centos8 dir]# kubectl create cm nginx-cm --from-file=./index1.html configmap/nginx-cm created ## 创建Deployment [root@Centos8 dir]# vim nginx-cm.yaml ... apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-cm namespace: default spec: replicas: template: metadata: labels: run: nginx spec: containers: - name: nginx-cm image: hub.vfancloud.com/test/myapp:v1 ports: - containerPort: 80 volumeMounts: - name: config-nginx mountPath: /usr/share/nginx/html/config volumes: - name: config-nginx configMap: name: nginx-cm ... kubectl apply -f nginx-cm.yaml ## 测试访问 [root@Centos8 dir]# curl http://10.244.3.170/config/index1.html Hello World ## 现在创建一个新文件index2.html [root@Centos8 index2.html It is a wonderful world ## 删除ConfigMap重建替换index1.html为index2.html [root@Centos8 dir]# kubectl delete cm nginx-cm configmap nginx-cm deleted [root@Centos8 ]# kubectl get cm No resources found. [root@Centos8 index2.html configmap/nginx-cm created ## 再次测试,滚动更新完成 [root@Centos8 10.244.3.170/config/index2.html It is a wonderful world
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |