Kubernetes组件Configmap

简介

是K8s专门用来存储Pod内服务配置文件的一个类型,通过定义一个Configmap后用pod中的Volume进行挂载,之后Pod对应的目录下会生成Configmap中定义key的一个文件,文件内容即为Value值,从而达成服务调用configmap中的配置,同时也支持对Pod中的环境变量进行定义,每次apply configmap即为热更新Configmap,服务会重读配置。

创建方式

1.通过文件进行创建

    # ls /kubernetes/configmap-file

 test1.conf           number1.conf

    # cat test1.conf

123123144123123
 12313212312

    # cat number1.conf

- name: ohdhafha
  haha: haoihdaa
       test:{difgafga}

    创建Configmap

    # kubectl create configmap file-config --from-file=/kubernetes/configmap-file

    # kubectl get configmaps

NAME          DATA   AGE
file-config   2      41s

 2.通过yaml文件创建

   # vim test-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
  namespace: default
data:
  test.conf:  thisistest
  number.config: 12i39104174013

使用

1.将Configmap中的配置调到Pod目录中

    # 用上面的yaml文件作基础

    # vim test-deployment.config

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-configmap
  namespace: default
spec:
  selector:
    matchLabels:
      app: test
  replicas: 2
  template:
    metadata:
      name: test
      namespace: default
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: ntp.weijiayu.club/myapp/nginx:v1
        ports:
          - containerPort: 80
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      volumes:
      - name: config-volume
        configMap:
          name: test

    # kubectl get pods

NAME                              READY   STATUS    RESTARTS   AGE
test-configmap-54d6c6997f-5zztq   1/1     Running   0          37s
test-configmap-54d6c6997f-lwh24   1/1     Running   0          37s

    # 进入Pod到/etc/config/查看文件

    # kubectl exec -it test-configmap-54d6c6997f-5zztq -- /bin/bash

    # cd /etc/config/ && ls

    # cat test.conf

 thisistest

 2.将Configmap中的配置调到Pod环境变量中

    还是使用上面的configmap作为基础

    # vim test-env-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-env
  namespace: default
spec:
  selector:
    matchLabels:
      app: test
  replicas: 2
  template:
    metadata:
      name: test
      namespace: default
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: ntp.weijiayu.club/myapp/nginx:v2
        env:
          - name: TEST_VALUE
            valueFrom:
              configMapKeyRef:
                name: test
                key: test.conf
          - name: NUMBER_VALUE
            valueFrom:
              configMapKeyRef:
                name: test
                key: number.config
        command:
          - "/bin/bash"
        args:
          - "-c"
          - "env"
        envFrom:
          - configMapRef:
              name: test
      restartPolicy: Always

    # kubectl -f test-env-deployment.yaml

    # kubectl get pods

NAME                        READY   STATUS             RESTARTS   AGE
test-env-7454f8f899-22l5f   0/1     CrashLoopBackOff   2          39s
test-env-7454f8f899-wsbtn   0/1     CrashLoopBackOff   2          39s

    # kubectl logs test-env-7454f8f899-22l5f

number.config=12i39104174013
test.conf=thisistest                                                   
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
TEST_VALUE=thisistest                                            <--------------这是我们添加的变量
HOSTNAME=test-env-7454f8f899-22l5f
PWD=/
PKG_RELEASE=1~buster
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.254.0.1:443
NJS_VERSION=0.3.7
SHLVL=0
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.254.0.1
NUMBER_VALUE=12i39104174013                         <--------------这是我们添加的变量
KUBERNETES_SERVICE_HOST=10.254.0.1
KUBERNETES_PORT=tcp://10.254.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NGINX_VERSION=1.17.6
_=/usr/bin/env

发布了62 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41674452/article/details/103868470