博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
八. k8s--configmap学习笔记
阅读量:4322 次
发布时间:2019-06-06

本文共 6552 字,大约阅读时间需要 21 分钟。

目录

为什么使用configmap

很多情况下我们为某一应用做好镜像,当我们想修改其中的一些参数的时候,就变得比较麻烦,又要重新制作镜像,我们是不是有一种方式,让镜像根据不同的场景调用我们不同的配置文件呢,那我们就需要用到 k8s 的另外一种资源,那就是 ConfigMap。

我们知道,在几乎所有的应用开发中,都会涉及到配置文件的变更,比如说在web的程序中,需要连接数据库,缓存甚至是队列等等。而我们的一个应用程序从写第一行代码开始,要经历开发环境、测试环境、预发布环境只到最终的线上环境。而每一个环境都要定义其独立的各种配置。如果我们不能很好的管理这些配置文件,你的运维工作将顿时变的无比的繁琐。为此业内的一些大公司专门开发了自己的一套配置管理中心,如360的Qcon,百度的disconf等。kubernetes也提供了自己的一套方案,即ConfigMap。kubernetes通过ConfigMap来实现对容器中应用的配置管理。

创建configmap的四种方式

ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中。

创建ConfigMap的方式有4种:

  • 通过直接在命令行中指定configmap参数创建,即--from-literal=key=value

    kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com#查看configmap[root@master ~]# kubectl get cmNAME           DATA   AGEnginx-config   2      4s#查看configmap的具体信息[root@master ~]# kubectl describe configmaps nginx-configName:         nginx-configNamespace:    defaultLabels:       
    Annotations:
    Data====nginx_port:----80server_name:----myapp.magedu.comEvents:
  • 通过指定文件创建,即将一个配置文件创建为一个ConfigMap,--from-file=File_Path

    #文件内容cat manifests/configmap/www.confserver {        server_name myapp.magedu.com;    listen 80;    root /data/web/html}#通过文件创建configmapkubectl create configmap nginx-www --from-file=./manifests/configmap/www.conf#查看configmap[root@master configmap]# kubectl describe configmaps nginx-wwwName:         nginx-wwwNamespace:    defaultLabels:       
    Annotations:
    Data====www.conf:----server { server_name myapp.magedu.com; listen 80; root /data/web/html}Events:
  • 通过一个文件内多个键值对,--from-env-file=

    cat << EOF > env.txtdb.host=10.0.0.50db.port=3306EOFkubectl create cm env-cm --from-env-file=env.txt
    如果有多个env文件, 只有最后一个env文件会生效
    [root@master configmap_test]# cat game.propertiesenemies=alienslives=3enemies.cheat=trueenemies.cheat.level=noGoodRottensecret.code.passphrase=UUDDLRLRBABASsecret.code.allowed=truesecret.code.lives=30[root@master configmap_test]# cat ui.propertiescolor.good=purplecolor.bad=yellowallow.textmode=truehow.nice.to.look=fairlyNice
    #执行命令创建configmapkubectl create configmap configmap-env --from-env-file=./game.properties --from-env-file=./ui.properties#可以看到, 只有ui.properties生效了[root@master configmap_test]# kubectl get configmaps configmap-env -o yamlapiVersion: v1data:  allow.textmode: "true"  color.bad: yellow  color.good: purple  how.nice.to.look: fairlyNicekind: ConfigMapmetadata:  creationTimestamp: "2019-09-11T01:58:17Z"  name: configmap-env  namespace: default  resourceVersion: "186936"  selfLink: /api/v1/namespaces/default/configmaps/configmap-env  uid: 4e36009f-267c-4713-8a7a-99d8f6dd3039
  • 事先写好标准的configmap的yaml文件,然后kubectl apply -f 创建

    [root@master configmap]# cat test.yamlapiVersion: v1kind: ConfigMapmetadata:  name: cm-4data:  db.host: 10.0.0.50  db.port: "3306"[root@master configmap]# kubectl apply -f test.yaml[root@master configmap]# kubectl describe cm cm-4Name:         cm-4Namespace:    defaultLabels:       
    Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","data":{"db.host":"10.0.0.50","db.port":"3306"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-4","...Data====db.host:----10.0.0.50db.port:----3306Events:

configmap结合pod使用

使用ConfigMap有二种方式:

第一种是通过环境变量的方式,直接传递给pod

apiVersion: v1kind: Podmetadata:  name: pod-cm-1   #name必须小写  namespace: default  labels:    app: myapp    tier: frontend  annotations:    create-by: tianpei.wangspec:  containers:  - name: myapp    image: ikubernetes/myapp:v1    ports:    - name: http      containerPort: 80    env:    - name: NGINX_SERVER_PORT      valueFrom:        configMapKeyRef:            name: nginx-config            key: nginx_port    - name: NGINX_SERVER_NAME      valueFrom:        configMapKeyRef:            name: nginx-config            key: server_name
#创建configmapkubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com#创建pod通过configmap注入到pod内[root@master configmap]# kubectl apply -f pod-cm-1.yamlpod/pod-cm-1 created可以看到已经成功注入到pod中了[root@master configmap]# kubectl exec -it pod-cm-1 -- /bin/sh/ # printenv |grep NGINXNGINX_SERVER_PORT=80NGINX_SERVER_NAME=myapp.magedu.com
当以环境变量的方式注入pod时, 只在pod启动时加载, 后续更改configmap不会同步到pod内

第二种是作为volume的方式挂载到pod内

apiVersion: v1kind: Podmetadata:  name: pod-cm-2   #name必须小写  namespace: default  labels:    app: myapp    tier: frontend  annotations:    create-by: tianpei.wangspec:  containers:  - name: myapp    image: ikubernetes/myapp:v1    ports:    - name: http      containerPort: 80    volumeMounts:      - name: nginxconf        mountPath: /etc/nginx/config.d        readOnly: true  volumes:    - name: nginxconf      configMap:        name: nginx-config
#通过上边的yaml文件创建pod, 并将configmap以volumes的形式挂载到pod内kubectl apply -f pod-cm-2.yaml#可以看到已经生效了[root@master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh/ # cd /etc/nginx/config.d//etc/nginx/config.d # lsnginx_port   server_name/etc/nginx/config.d # cat nginx_port80#将configmap的端口更改为8080[root@master configmap]# kubectl edit cm nginx-configconfigmap/nginx-config edited[root@master configmap]# kubectl describe cm nginx-configName:         nginx-configNamespace:    defaultLabels:       
Annotations:
Data====nginx_port:----8080server_name:----myapp.magedu.comEvents:
#等待一段时间后生效了[root@master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh/ # cd /etc/nginx/config.d//etc/nginx/config.d # lsnginx_port server_name/etc/nginx/config.d # cat nginx_port/etc/nginx/config.d # cat nginx_port/etc/nginx/config.d # cat nginx_port/etc/nginx/config.d # cat nginx_port8080​``````shellwget https://k8s.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties

configmap的item使用

创建configmap

apiVersion: v1kind: ConfigMapmetadata:  name: special-config  namespace: defaultdata:  SPECIAL_LEVEL: very  SPECIAL_TYPE: charm

将configmap中的SPECIAL_LEVEL挂载到pod/etc/config/keys

apiVersion: v1kind: Podmetadata:  name: test-podspec:  containers:    - name: test-container      image: busybox      command: [ "/bin/sh","-c","sleep 3600"]      volumeMounts:      - name: config-volume        mountPath: /etc/config  volumes:    - name: config-volume      configMap:        name: special-config        items:        - key: SPECIAL_LEVEL          path: keys

可以看到已经生效了

[root@master configmap_test]# kubectl exec -ti test-pod -- /bin/sh/ # cat /etc/config/keysvery

转载于:https://www.cnblogs.com/peitianwang/p/11498896.html

你可能感兴趣的文章
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
学习进度
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>
一款jQuery打造的具有多功能切换的幻灯片特效
查看>>