ConfigMap--kubernetes的“配置中心”

一、简介

ConfigMap 是一种 API 对象,用来将非机密性的数据保存到健值对中。使用时可以用作环境变量、命令行参数或者存储卷中的配置文件。
ConfigMap 允许您将配置文件与镜像文件分离,以使容器化的应用程序具有可移植性。

二、创建

2.1 使用 kubectl 创建ConfigMap

格式:

kubectl create configmap <map-name> <data-source>

map-name 是要分配给 ConfigMap 的名称
data-source 是要从中提取数据的目录,文件或者文字值

数据源对应于 ConfigMap 中的 key-value (键值对)

  • key = 您在命令行上提供的文件名或者密钥
  • value = 您在命令行上提供的文件内容或者文字值

2.1.1 根据目录创建

从同一目录中的多个文件创建 ConfigMap

[root@harbor configmap]# ll
总用量 16
-rw-r-----. 1 root root 2737 6月  29 14:51 base-ms.yml
-rw-r-----. 1 root root 2452 6月  29 16:10 business-ms.yml
-rw-r-----. 1 root root  540 6月  29 14:50 eureka.yml
-rw-r-----. 1 root root 1119 6月  29 14:50 oauth2.yml
[root@harbor configmap]# kubectl create configmap test-configmap --from-file=/opt/configmap/ -n xzzyy-test 
configmap/test-configmap created

查看configmap内容

kubectl describe configmaps -n xzzyy-test test-configmap

输出类似以下内容:

Name:         test-configmap
Namespace:    xzzyy-test
Labels:       <none>
Annotations:  <none>

Data
====
base-ms.yml:
----
cn:
    enableCodeGenerator: true
    enableDruidDBConfig: true
    enableGenerateCode: true
    enableGlobalException: true
    enableTxAdviceInterceptor: true
    ......
business-ms.yml:
----
cn:
    enableCodeGenerator: true
    enableDruidDBConfig: true
    enableGenerateCode: true
    enableGlobalException: true
    enableRequestLogHandler: true
    enableTxAdviceInterceptor: true
    enableAopLog: true
    ......
eureka.yml:
----
server:
    port: 40000
	......
oauth2.yml:
----
eureka:
    client:
        serviceUrl:
            defaultZone: http://root:root@test-eureka-svc:40000/eureka
			.....
Events:  <none>

修改

kubectl edit configmaps -n xzzyy-test test-configmap

删除

kubectl delete configmaps -n xzzyy-test test-configmap

2.1.2 根据文件创建

从单个文件或多个文件创建 ConfigMap

kubectl create configmap test-configmap2 \
	--from-file=/opt/configmap/base-ms.yml \
  	--from-file=/opt/configmap/business-ms.yml \
  	--from-file=/opt/configmap/eureka.yml \
 	--from-file=/opt/configmap/oauth2.yml \
  	-n xzzyy-test 

内容与从目录创建类似,此处不展示了。

2.1.3 根据文字值创建

kubectl create configmap special-config \
  	--from-literal=special.how=very \
  	--from-literal=special.type=charm \

查看内容

[root@harbor opt]# kubectl describe configmaps special-config 
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.how:
----
very
special.type:
----
charm
Events:  <none>

2.2 根据生成器创建

自 1.14 开始, kubectl 开始支持 kustomization.yaml。
个人认为这种创建方式不如kubectl简单,此处就不研究了,想继续了解可以参考官网:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-pod-configmap/#%E6%A0%B9%E6%8D%AE%E7%94%9F%E6%88%90%E5%99%A8%E5%88%9B%E5%BB%BA-configmap

三、使用

3.1 定义容器的环境变量

3.1.1 使用单个 ConfigMap 中的数据定义容器环境变量

1、在 ConfigMap 中将环境变量定义为键值对:

kubectl create configmap special-config --from-literal=special.how=very

2、将 ConfigMap 中定义的 special.how 值分配给 Pod 的 SPECIAL_LEVEL_KEY 环境变量。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox:latest
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY    # 容器环境变量名称
          valueFrom:
            configMapKeyRef:
              name: special-config   # configmap的名字
              key: special.how       # configmap的key
  restartPolicy: Never

3.1.2 使用来自多个 ConfigMap 的数据定义容器环境变量

1、在 ConfigMap 中将环境变量定义为键值对:

kubectl create configmap special-config --from-literal=special.how=very
kubectl create configmap env-config --from-literal=log_level=INFO

2、将 ConfigMap 中定义的 special.howlog_level值分配给 Pod 中的 SPECIAL_LEVEL_KEYLOG_LEVEL环境变量。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox:latest
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: env-config
              key: log_level
  restartPolicy: Never

3.1.3 将 ConfigMap 中的所有键值对配置为容器环境变量

1、创建一个包含多个键值对的 ConfigMap。

kubectl create configmap special-config-1 \
	--from-literal=SPECIAL_LEVEL=very \
	--from-literal=SPECIAL_TYPE=INFO \
	--from-literal=AUTH=liuli

2、使用 envFrom 将所有 ConfigMap 的数据定义为容器环境变量,ConfigMap 中的键成为 Pod 中的环境变量名称。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox:latest
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: special-config-1
  restartPolicy: Never

3.2 容器 entrypoint 的命令行参数

kubectl create configmap special-config-2 \
	--from-literal=SPECIAL_LEVEL=very \
	--from-literal=SPECIAL_TYPE=INFO \
	--from-literal=AUTH=liuli
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox:latest
      command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY) $(AUTH_KEY)" ]
      args: ["$(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY) $(AUTH_KEY)"]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config-2
              key: SPECIAL_LEVEL
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config-2
              key: SPECIAL_TYPE
        - name: AUTH_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config-2
              key: AUTH
  restartPolicy: Never

3.3 容器数据卷中生成文件

3.3.1 将 ConfigMap 中键值在容器中生成文件

注意:该操作会清空mountPath/etc/config目录下的内容

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox:latest
      command: [ "/bin/sh", "-c", "ls -l /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config-1
  restartPolicy: Never

3.3.2 将 ConfigMap 数据添加到容器中的特定路径

注意:该操作会清空mountPath/etc/config目录下的内容

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox:latest
      command: [ "/bin/sh","-c","cat /etc/config/keys" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config-1
        items:
        - key: SPECIAL_LEVEL
          path: keys
  restartPolicy: Never

四、实例

结合公司项目,我选择了将ConfigMap中的键值通过volumeMounts的方式生成配置文件,供Jar包读取启动。

4.1 创建ConfigMap

配置文件目录

[root@harbor configmap]# ll
总用量 72
-rw-r-----. 1 root root 2731 6月  30 09:50 dev-base-ms.yml
-rw-r-----. 1 root root 2448 6月  30 09:51 dev-business-ms.yml
-rw-r-----. 1 root root 1466 6月  30 09:51 dev-common-ws.yml
-rw-r-----. 1 root root 2972 6月  30 09:52 dev-data-rep-ws.yml
-rw-r-----. 1 root root 2926 6月  30 09:53 dev-doctor-ws.yml
-rw-r-----. 1 root root 2534 6月  30 09:54 dev-esb-ws.yml
-rw-r-----. 1 root root  401 6月  30 09:54 dev-eureka.yml
-rw-r-----. 1 root root 4474 6月  30 09:55 dev-job-web.yml
-rw-r-----. 1 root root 3126 6月  30 09:55 dev-message-ms.yml
-rw-r-----. 1 root root 2276 6月  30 09:56 dev-netty.yml
-rw-r-----. 1 root root 1116 6月  30 09:56 dev-oauth2.yml
-rw-r-----. 1 root root 1421 6月  30 09:57 dev-offline-registration-ms.yml
-rw-r-----. 1 root root 3529 6月  30 09:58 dev-online-registration-ms.yml
-rw-r-----. 1 root root 3250 6月  30 10:00 dev-recommender-ms.yml
-rw-r-----. 1 root root 3965 6月  30 10:00 dev-saas-ws.yml
-rw-r-----. 1 root root 2718 6月  30 10:01 dev-vasc-ms.yml
-rw-r-----. 1 root root 2192 6月  30 10:01 dev-zuul.yml

通过目录生成ConfigMap

[root@harbor configmap]# kubectl create configmap dev-config -n xzzyy-dev --from-file=/opt/config/java/dev/xzzyy/configmap/
configmap/dev-config created

查看

[root@harbor configmap]# kubectl get configmaps -n xzzyy-dev 
NAME         DATA   AGE
dev-config   17     56s

4.2 编写dockerfile文件

以eureka为例

FROM openjdk:8u252-slim-buster
EXPOSE 30000
WORKDIR /my-java
ADD eureka-sever-0.0.1.jar /my-java
CMD ["java","-Xms2048m","-Xmx4096m","-XX:PermSize=256m","-XX:MaxPermSize=512m","-XX:MaxNewSize=512m","-Dfile.encoding=UTF8","-Duser.timezone=GMT+08","-jar","eureka-sever-0.0.1.jar","--spring.config.location=/config/bootstrap.yml"]

4.3 编写deployment文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: dev-eureka
  namespace: xzzyy-dev
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: dev-eureka-pod
  template:
    metadata:
      labels:
        app: dev-eureka-pod
    spec:
      restartPolicy: Always
      imagePullSecrets:
      - name: xzzyy-dev-secret
      containers:
      - name: dev-eureka-con
        image: dev-eureka:v1
        imagePullPolicy: Always
        ports:
        - name:
          containerPort: 30000
          protocol: TCP
        volumeMounts:
        - name: config-volume
          mountPath: /config
      volumes:
      - name: config-volume
        configMap:
          name: dev-config
          items:
          - key: dev-eureka.yml
            path: bootstrap.yml
---
apiVersion: v1
kind: Service
metadata:
  name: dev-eureka-svc
  namespace: xzzyy-dev
  labels:
    name: dev-eureka-svc
spec:
  type: NodePort
  selector:
    app: dev-eureka-pod
  ports:
    - protocol: TCP
      port: 30000
      targetPort: 30000
      nodePort: 30000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值