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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章