zoukankan      html  css  js  c++  java
  • configmap使用方法

    说明: 

    kubernetes统一配置管理方案configmap,实现将配置文件从容器镜像中解耦,增强应用的可移植性。数据可直接注入pod对象中,为容器所使用,注入方式有挂载为存储卷和传递为环境变量两种。

    环境

    1、适合存储明文类型数据
    2、configmap属于命名空间级别的资源,和引用它的pod必须在同一空间中

    语法:

    创建命令:
    kubernetes create configmap <map-name> <data-source>

    格式化输出查看:
    kubectl get configmap configmap_name -o yaml
    kubectl describe configmap configmap_name -o yaml

    两者都可以查看,显示的键值分隔符不同,describe像是更详细的信息!

    方法1:直接创建key value

    1、使用"--from-literal"选项可在命令行直接创建configmap对象,重复使用传递多个键值对
    
    kubectl create configmap configmap_name --from-literal=key-name-1=value-1
    
    2、分别传递两个键值对
    
    kubectl create configmap configmap_name --from-literal=key.name=tag --from-literal=value.name=master

    方法2:通过文件创建

    1、创建需要挂载使用的配置文件(index.html)

    root@rancher:/home/yangjia# cat index.html 
    yangjia test configmap create!
    
    创建configmap(注意后面填加namespace,否则默认default)
    kubectl create configmap nginx-config  --from-file=./index.html -n test
    
    查看configmap(注意后面填加namespace,否则默认default)
    kubectl get configmaps -n test

    2、验证configmap配置是否和配置文件信息一致

    root@rancher:/home/yangjia# kubectl get configmaps/nginx-config -o yaml -n test
    apiVersion: v1
    data:
      index.html: |
        yangjia test configmap create!
    kind: ConfigMap
    metadata:
      creationTimestamp: "2019-11-04T09:45:25Z"
      name: nginx-config
      namespace: test
      resourceVersion: "85333"
      selfLink: /api/v1/namespaces/test/configmaps/nginx-config
      uid: dbeb8169-212d-43b0-82f1-13d58ec9dcdd

    3、编写部署应用的yaml文件

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: my-deployment
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: my-deployment
        spec:
          containers:
          - name: my-nginx-containers
            image: nginx:latest
            imagePullPolicy: Never
            ports:
            - containerPort: 80
            volumeMounts:                              
    - mountPath: /usr/share/nginx/html #--将配置文件挂载到哪里 name: nginx-config volumes: - name: nginx-config configMap: name: configmap-nginx # --指定使用configMap中的nginx-config配置 items: - key: index.html path: index.html --- apiVersion: v1 kind: Service metadata: name: my-deployment spec: type: NodePort selector: app: my-deployment ports: - protocol: TCP port: 80 targetPort: 80 nodePort: 30189

     4、部署应用

    root@rancher:/home/yangjia# kubectl create -f nginx-sever2.yaml -n test
    service/nginx-svc-configmap created

    部署成功,pod已经成功运行,服务发现已经做好了30084端口外部访问入口。

     方法3:通过目录创建

    1、使用"--from-flie <path-to-directory>"选项可在命令行直接创建configmap对象
    kubectl create configmap tomcat-config  --from-file=/usr/local/tomcat/webapps/CONFIG/ -n test
    
    2、通过kubectl部署应用
    kubectl create -f app.yaml -n test

     方法4:环境变量传递键值数据

    1、通过valueFrom内嵌configMapKeyRef对象实现,格式如下:
    valueFrom:                              
      configMapKeyRef:                 
        key:                                ###configmap键值名
        name:                               ###configmap对象名称
          optional:                         ###当前pod资源指明此引用是否可选  
    2、创建yaml文件,包含两个资源,用“---”分隔,直接将configmap键值数据传递给pod引用 apiVersion: v1 kind: ConfigMap metadata: name: httpd_config namespace: default data: httpd_port: "8080" httpd_level: "-vv"
    ---
    apiVersion: v1 kind: Pod metadata: name: configmap_env spec: containers: - name: test-container image: alpine:3.8 command: [ "/bin/httpd" ] args: [ "-f","-p","$(HTTPD_PORT)","$(HTTPD_LOG_VERSION)" ] env: - name: HTTPD_PORT valueFrom: configMapKeyRef: name: httpd_config key: httpd_port - name: HTTPD_LOG_VERSION valueFrom: configMapKeyRef: name: httpd_config key: httpd_level restartPolicy: Never optional: true

    3、查看部署结果
    kubectl exec configmap_env ps aux
    PID  USER  TIME  COMMAND
       1 root  0:00  /bin/httpd -f -p 8080 -vv
  • 相关阅读:
    干掉:“请停用以开发者模式运行的扩展程序”
    电脑分区
    tomcat进行远程debug
    工作流审核到最后一步迟迟不能完成
    DataGrid首次进入页面时,不加载任何数据[转]
    用Excel进行个人敏捷项目看板管理
    cnblogs的编辑器BUG反馈:发布或编辑文章完成后,页面是卡死状态的
    程序员如何优雅地挣零花钱?
    SQL Server导入数据报错"无法在只读列“Id”中插入数据",几百个表怎么批量启用'启用标识插入'选项
    SQL Server 各版本发布时间、开发代号及下载地址
  • 原文地址:https://www.cnblogs.com/jarno/p/11793710.html
Copyright © 2011-2022 走看看