zoukankan      html  css  js  c++  java
  • k8s之configmap和secret

    secret存储卷 加密存放配置文件

    kubectl explain pods.spec.volumes.secret

    configmap存储卷 放置配置文件的 配置中心

     kubectl explain pods.spec.volumes.configMap

    配置容器化运用的方式:

    1. 自定义命令行参数;

     command

    args: [ ]

    1. 把配置文件直接焙进镜像;

     

    1. 环境变量

          (1)cloud native的运用程序一般可以直接通过环境变量加载配置

           (2) 通过entrypoint 脚本来预处理变量为配置文件中的配置信息

     

    1. 存储卷 配置文件挂载到容器运用的配置文件目录

     

    env

    pod资源环境变量的获取方式

    kubectl explain pods.spec.containers.env

     kubectl explain pods.spec.containers.env.valueFrom

    configMapKeyRef

     kubectl explain pods.spec.containers.env.valueFrom.configMapKeyRef

    secretKeyRef

    kubectl explain pods.spec.containers.env.valueFrom.secretKeyRef

    创建configMap

    作用:为了将配置文件从镜像中解耦,,从而增强了运用的可移植性,运用的可复用性

    一个configMap就是一系列配置信息的集合,而这些数据可以注入到pod 中容器中所使用

    两种方式: 存储卷 或通过env注入

    kubectl explain configMap

    kubectl explain cm

    实例: 创建configmap有两种方式

    方式一

    kubectl create configmap --help

    kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.baidu.com

                            configmap名  通过字符  key   value

    kubectl get cm

    kubectl describe cm nginx-config

    configmap可以直接让启动的pod直接调用

    方式二

    mkdir configmap  &&  cd configmap/

    vim www.conf

    server{

           server_name  myapp.qq.com;

           listen  80;

           root  /data/web/html;

    }

    kubectl create configmap nginx-www --from-file=./www.conf

                           cm名   通过文件 默认文件名为key  文件内容为value

    kubectl get cm

    kubectl get cm nginx-www -o yaml

    kubectl describe configmaps nginx-www

    configmap注入到pod

     方式一 通过env注入

    cp ../manifests/pod-demo.yaml ./

    vim pod-demo.yaml

    mv pod-demo.yaml pod-configmap.yaml

    vim pod-configmap.yaml

    apiVersion: v1

    kind: Pod

    metadata:

      name: pod-cm-1

      namespace: default

      labels:

        app: myapp

        tier: frontend

    spec:

      containers:

      - name: myapp

        image: ikubernetes/myapp:v1

        ports:

        - name: http

          containerPort: 80

        env:

        - name: NGINX_SERVER_PORT 环境变量名

          valueFrom: 值来自

            configMapKeyRef: 来自 configmap

              name: nginx-config  configmap

              key: nginx_port   configmapkey   对应的value会传给变量名NGINX_SERVER_PORT

              

        - name: NGINX_SERVER_NAME

          valueFrom:

            configMapKeyRef:

              name: nginx-config

              key: server_name

    kubectl apply -f pod-configmap.yaml 创建pod 并把环境变量注入到容器

    验证

    kubectl get pods

    kubectl exec -it pod-cm-1 -- /bin/sh

    printenv  

    结果:

    NGINX_SERVER_PORT=80

    NGINX_SERVER_NAME=myapp.baidu.com

    表示环境变量注入成功

    修改configmap

    kubectl edit cm nginx-config

    nginx_port: "8080"

    kubectl describe cm nginx-config 修改成功 pod不会改

    方式二

    通过存储卷的方式来获取环境变量 实时更新

     cp pod-configmap.yaml pod-configmap-2.yaml

     vim pod-configmap-2.yaml

    apiVersion: v1

    kind: Pod

    metadata:

      name: pod-cm-2

      namespace: default

      labels:

        app: myapp

        tier: frontend

    spec:

      containers:

      - name: myapp

        image: ikubernetes/myapp:v1

        ports:

        - name: http

          containerPort: 80

        volumeMounts:  容器挂载pod存储卷

        - name: nginxconf  挂载的pod存储卷名

          mountPath: /etc/nginx/config.d/ 挂载到容器哪里

          readOnly: true 配置信息不让容器修改

      volumes:  pod创建存储卷

      - name: nginxconf  存储卷名

        configMap: 存储卷类型 configMap  

          name: nginx-config 指定挂载的configMap的名字,ngixn-config这个cmkey为文件名,value为文件内容

    kubectl apply -f pod-configmap-2.yaml

    验证

    kubectl get pods

    kubectl exec -it pod-cm-2 -- /bin/sh

    /etc/nginx/config.d

    nginx_port   server_name

    kubectl edit cm nginx-config

    nginx_port: "8088"

    回容器 cat nginx_port

    结果 8088

    运用:

     kubectl delete -f pod-configmap-2.yaml

    cp pod-configmap-2.yaml pod-configmap-3.yaml

    apiVersion: v1

    kind: Pod

    metadata:

      name: pod-cm-3

      namespace: default

      labels:

        app: myapp

        tier: frontend

    spec:

      containers:

      - name: myapp

        image: ikubernetes/myapp:v1

        ports:

        - name: http

          containerPort: 80

        volumeMounts:

        - name: nginxconf

          mountPath: /etc/nginx/conf.d/   cm定义的内容放到配置文件目录里面,配置直接有效

          readOnly: true

      volumes:

      - name: nginxconf

        configMap:

          name: nginx-www  类型configmapnginx-wwwkey为文件名,value为文件内容

    kubectl apply -f pod-configmap-3.yaml

    验证

    kubectl get pods

    kubectl exec -it pod-cm-3 -- /bin/sh

    cd /etc/nginx/conf.d/

    cat www.conf

    nginx -T 可看到configmapnginx-wwwvalue已经加载到配置了

    测试

     mkdir -p /data/web/html

    echo "configmap configration map result" >> /data/web/html/index.html

    任一节点  curl  10.244.1.160

    测试修改配置文件

     kubectl edit cm nginx-www

    listen  8080;

    回容器看看

    nginx -T 结果也改了

    nginx -s reload 需要重载配置才会生效

    挂载部分键值

    kubectl explain pods.spec.volumes.configMap.items

    key

    path 对应的key value文件内容 文件名是key 即文件保存的路径

    secret 保存敏感数据

    存放私钥和证书

    kubectl create secret --help

    kubectl explain pods.spec.imagePullSecrets

    kubectl create secret generic --help

    创建一个generic

    kubectl create secret generic mysqp-root-password --from-literal=password=123456

                 secret  generic        generic=key=values

    kubectl get secrets

    kubectl describe secrets mysqp-root-password 查看创建的 secret描述信息

    kubectl get secrets mysqp-root-password -o yaml

    echo MTIzNDU2 |base64 -d 解码hash

    实例:

    cp pod-configmap.yaml  pod-secret-1.yaml

    vim pod-secret-1.yaml

    apiVersion: v1

    kind: Pod

    metadata:

      name: pod-secret-1

      namespace: default

      labels:

        app: myapp

        tier: frontend

    spec:

      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

        - name: MYSQL_ROOT_PASSWORD 传的环境变量名 即环境变量

          valueFrom: 环境变量来自

            secretKeyRef: 来自secret的环境变量

              name: mysqp-root-password  secret的名

              key: password  secret存储的mysqp-root-password的键值key, password即为键值名,对应的values传给环境变量MYSQL_ROOT_PASSWORD

                                     

     

    secret通过env注入到pod

    kubectl apply -f pod-secret-1.yaml

    验证

    kubectl exec -it pod-secret-1 -- /bin/sh

    printenv

  • 相关阅读:
    解决另一种方法解决Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-thtq7d55/frida/
    linux ubuntu pip3 list 出现警告
    Ubuntu安装curl报错:软件包有未满足的依赖关系: curl : 依赖: libcurl4 (= 7.58.0-2ubuntu3.10)
    安卓逆向手机环境搭建
    Proj THUDBFuzz Paper Reading: DIANE: Identifying Fuzzing Triggers in Apps to Generate Under-constrained Inputs for IoT Devices
    Proj THUDBFuzz Paper Reading: Automated Conformance Testing for JavaScript Engines via Deep Compiler Fuzzing
    Proj THUDBFuzz Paper Reading: Adaptive LL(*) Parsing: The Power of Dynamic Analysis
    Proj THUDBFuzz Paper Reading: Baloo: Measuring and Modeling the Performance Configurations of Distributed DBMS
    Proj IoTDBFuzz Paper Reading: Testing Java Exceptions: An Instrumentation Technique
    Proj IoTDBFuzz Paper Reading: VisFuzz: Understanding and Intervening Fuzzing with Interactive Visualization
  • 原文地址:https://www.cnblogs.com/leiwenbin627/p/11315347.html
Copyright © 2011-2022 走看看