kubernetes之使用ConfigMap管理Pod配置文件


簡介

ConfigMaps可以使容器鏡像與配置文件解耦,實現容器化應用程序的可移植性。此文提供一系列的方法示例講述如何創建ConfigMaps,使用存儲在ConfigMaps中的數據配置Pod。

備注:此文檔參考官方文檔,並加以自己的理解。如有誤導性的內容,請批評指正。

創建一個ConfigMap

我們可以使用kubectl create configmapkustomization.yaml中的ConfigMap生成器創建一個ConfigMap。從Kubernetes 1.14版本開始,kubectl開始支持使用kustomization.yaml創建ConfigMap

使用 kubectl create configmap 創建一個 ConfigMap

使用kubectl create configmap從目錄、文件或字面值創建configmaps。

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

<map-name>即配置的ConfigMap的名字,<data-source>是從目錄、文件或字面值下讀取數據的目錄。

ConfigMap中的data-source應該是一個鍵值對:

  • key=文件名稱或提供的命令行的key
  • value=提命令行中提供的文件內容或字面值

使用kubectl describekubectl get查看ConfigMap的信息。

從目錄中創建 ConfigMaps

可以使用kubectl create configmap從同一個目錄下的多個文件創建ConfigMap

# Create the local directory
mkdir -p configure-pod-container/configmap/

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties

# Create the configmap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/

configure-pod-container/configmap/目錄下包含連個文件

game.properties
ui.properties

查看配置的ConfigMap內容:

kubectl describe configmaps game-config

從結果可以看出:ConfigMap中的data塊中包含了configure-pod-container/configmap/目錄下game.propertiesui.properties兩個文件中的所有內容。

Name:         game-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game-env-file.properties:
----
enemies=aliens
lives=3
allowed="true"

# This comment and the empty line above it are ignored

game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

Events:  <none>

yaml格式輸出如下:

# kubectl get configmaps game-config -o yaml
apiVersion: v1
data:
  game-env-file.properties: |
    enemies=aliens
    lives=3
    allowed="true"

    # This comment and the empty line above it are ignored
  game.properties: |-
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-17T07:18:08Z"
  name: game-config
  namespace: default
  resourceVersion: "10100181"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: a447f523-ddf4-48f5-a0eb-6f24c732fee5

從文件中創建 ConfigMaps

使用kubectl create configmap從一個獨立的文件或多個文件中創建ConfigMap

例如:

# kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties

查看內容:

# kubectl describe configmaps game-config-2

輸出如下所示

Name:         game-config-2
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
Events:  <none>

也可以通過多次使用--from-file參數從多個數據源中創建ConfigMap

例如:

# kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties

使用--from-env-file選項從env-file中創建ConfigMap:

例如:

# Env-files contain a list of environment variables.
# These syntax rules apply:
#   Each line in an env file has to be in VAR=VAL format.
#   Lines beginning with # (i.e. comments) are ignored.
#   Blank lines are ignored.
#   There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties

# The env-file `game-env-file.properties` looks like below
cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"

# This comment and the empty line above it are ignored

創建ConfigMap

kubectl create configmap game-config-env-file \
       --from-env-file=configure-pod-container/configmap/game-env-file.properties

查看內容

# kubectl get configmap game-config-env-file -o yaml

輸出如下

apiVersion: v1
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-17T07:32:05Z"
  name: game-config-env-file
  namespace: default
  resourceVersion: "10103588"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
  uid: 0ed90509-5577-4225-a1ad-826426069da3

注意:當通過多次使用 --from-env-file 從多個數據源中創建ConfigMap時,只有最后一個 env-file生效。

演示多次使用 --from-env-file的例子
例如:

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://k8s.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties

# Create the configmap
kubectl create configmap config-multi-env-files \
        --from-env-file=configure-pod-container/configmap/game-env-file.properties \
        --from-env-file=configure-pod-container/configmap/ui-env-file.properties
# kubectl get configmap config-multi-env-files -o yaml

輸出內容如下,從結果可以看出生效的配置為最后一個--from-env-file指定的ui-env-file.properties

apiVersion: v1
data:
  color: purple
  how: fairlyNice
  textmode: "true"
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-17T07:45:01Z"
  name: config-multi-env-files
  namespace: default
  resourceVersion: "10106742"
  selfLink: /api/v1/namespaces/default/configmaps/config-multi-env-files
  uid: 583873dd-12e6-408a-9373-b9cfeb092d5a

定義從文件創建ConfigMap時要使用的Key

使用--from-file參數給某個數據源的文件的數據定一個key,在ConfigMapdata塊下可以看到這個key。

kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>

這里的<my-key-name>即想要在COnfigMap中使用的key的名稱,<path-to-file>是本地數據源中key要表達的數據內容。
例如:

# kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties

查看內容

# kubectl get configmaps game-config-3 -o yaml

輸出結果如下,在data塊下有一個名稱為game-special-keykeykey的值即為--from-file=game-special-key=configure-pod-container/configmap/game.properties文件中的內容。

apiVersion: v1
data:
  game-special-key: |-
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-17T07:59:47Z"
  name: game-config-3
  namespace: default
  resourceVersion: "10110353"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-3
  uid: e1569842-1438-46c3-91da-30cd989c17da

從字面值中創建ConfigMap

使用帶有--from-literal參數的kubectl create configmap從命令行的字面量中創建一個ConfigMap。

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

可以用多個key-value的鍵值對。每一個命令行中提供的鍵值對在ConfigMapdata塊下都是獨立的。

查看內容

# kubectl get configmaps special-config -o yaml

輸出如下

apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: "2020-01-17T08:09:09Z"
  name: special-config
  namespace: default
  resourceVersion: "10112639"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: 57306de5-bd39-4d98-84fe-12357312551b

從生成器創建ConfigMap

kubelet從1.14版開始支持使用kustomization.yaml。可以從生成器創建ConfigMap,然后將其應用於在Apiserver上創建對象。

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4
  files:
  - configure-pod-container/configmap/game.properties
EOF

應用Kustomization目錄創建ConfigMap對象。

kubectl apply -k .

查看是否創建成功

# kubectl get configmap
NAME                       DATA   AGE
...
game-config-4-m9dm2f92bt   1      48s
...
# kubectl describe configmaps/game-config-4-m9dm2f92bt
Name:         game-config-4-m9dm2f92bt
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"game.properties":"enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.p...

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
Events:  <none>

注意,生成的ConfigMap名稱具有通過對內容進行哈希后而附加的后綴。 這樣可以確保每次修改內容時都會生成一個新的ConfigMap。

定義從文件生成ConfigMap時使用的Key

可以定義一個Key,而不是要在ConfigMap生成器中使用的文件名。 例如,要使用game-special-key從文件configure-pod-container/configmap/game.properties生成ConfigMap

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-5
  files:
  - game-special-key=configure-pod-container/configmap/game.properties
EOF

應用Kustomization目錄創建ConfigMap對象。

kubectl apply -k .

從字面量生成ConfigMap

為了從自定義的type=charmspecial.how=very字面量創建ConfigMap,在kustomization.yaml中定義的生成器如下:

# Create a kustomization.yaml file with ConfigMapGenerator
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: special-config-2
  literals:
  - special.how=very
  - special.type=charm
EOF

創建

kubectl apply -k .

使用 ConfigMap 中的數據定義容器的環境變量

從單一的 ConfigMap中的數據定義容器的環境變量:

1、在ConfigMap中以key-value的鍵值對定義環境變量

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

2、將ConfigMap中定義的special.how值分配給Pod的spec中的SPECIAL_LEVEL_KEY環境變量。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config
              # Specify the key associated with the value
              key: special.how
  restartPolicy: Never

創建Pod

kubectl create -f /root/k8s-example/pods/pod-single-configmap-env-variable.yaml

Pod的輸出包含環境變量SPECIAL_LEVEL_KEY=very

從多個ConfigMaps數據中定義容器變量

創建ConfigMaps:

kubectl create -f /root/k8s-example/configmap/configmaps.yaml

在Pod的spec中定義環境變量

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      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

創建Pod:

kubectl create -f /root/k8s-example/pods/pod-multiple-configmap-env-variable.yaml

Pod的輸出的環境變量包含兩個SPECIAL_LEVEL_KEY=veryLOG_LEVEL=INFO

在ConfigMap中配置所有 key-value 鍵值對作為容器環境變量

創建包含多個鍵值對的ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
kubectl create -f /root/k8s-example/configmap/configmap-multikeys.yaml

使用envFrom定義ConfigMap中的所有數據作為容器的環境變量。ConfigMap中的Key變成了Pod中的環境變量。

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

創建Pod

kubectl create -f https://kubernetes.io/examples/pods/pod-configmap-envFrom.yaml

Pod輸出包含環境變量SPECIAL_LEVEL=verySPECIAL_TYPE=charm

在Pod命令中使用ConfigMap中定義的變量

在Pod的spec中的command塊可以使用$(VAR_NAME)的方式,導入ConfigMap中定義的環境變量

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_LEVEL
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_TYPE
  restartPolicy: Never

test-container容器輸出如下:

very charm

Volumes 中添加 ConfigMap 數據

創建ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
# kubectl create -f /root/k8s-example/configmap/configmap-multikeys.yaml

在Pod的spec規格的volume塊下添加ConfigMap的名稱。此配置會添加ConfigMap中的數據到指定的volumeMounts.mountPath的目錄中,即如下容器的/etc/config目錄。command塊列出volumeMounts.mountPath目錄下的所有文件。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never

創建Pod:

# kubectl apply -f pod-configmap-volume.yaml

查看容器日志輸出

# kubectl logs dapi-test-pod

輸出內容如下:

SPECIAL_LEVEL
SPECIAL_TYPE

注意:如果 /etc/config 目錄下有文件,將會被刪除掉。

添加 ConfigMap數據到指定 Volume 路徑下

使用 path 字段配置ConfigMap中的特定項到指定的路徑下。例如SPECIAL_LEVEL項的內容將會掛載在config-volume指定的/etc/config/keys卷上。

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

創建Pod

# kubectl apply -f /root/k8s-example/pods/pod-configmap-volume-specific-key.yaml

查看日志輸出

# kubectl logs dapi-test-pod
very

注意:所有以前在 /etc/config/ 目錄下的文件將會被刪除

刪除資源

# kubectl delete configmaps game-config
# kubectl delete configmaps game-config-2
# kubectl delete configmaps config-multi-env-files
# kubectl delete configmaps game-config-3
...

理解 ConfigMap 和 Pod

ConfigMap 的 API 資源以鍵值對的形式存儲配置數據。數據可以在Pod中使用,也可以提供系統組件的配置。例如 controllers。ConfigMap與Secrets類似,但是提供了一種處理不包含敏感信息的字符串的方法。 用戶和系統組件都可以將配置數據存儲在ConfigMap中。

注意:ConfigMap應該引用屬性文件,而不是替換它們。 可以將ConfigMap表示為類似於Linux / etc目錄及其內容的東西。 例如,如果從ConfigMap創建Kubernetes卷,則ConfigMap中的每個數據項都由該卷中的單個文件表示。

ConfigMap的data字段包含配置數據。 如下例所示,它可以很簡單,例如使用--from-literal定義的單個屬性例,也可以很復雜,如使用--from-file定義的配置文件

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: example-config
  namespace: default
data:
  # example of a simple property defined using --from-literal
  example.property.1: hello
  example.property.2: world
  # example of a complex property defined using --from-file
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3

ConfigMap的限制條件

  • 在Pod的被創建之前必須先創建 ConfigMap(除非設置了 ConfigMap為可選項。如果引用的ConfigMap不存在,則Pod不會啟動。 同樣,對ConfigMap中不存在的key的引用也會阻止Pod啟動。

  • 如果使用envFrom從ConfigMap中定義環境變量,則將忽略被認為無效的鍵。 可以啟動Pod,但無效名稱將記錄在事件日志(InvalidVariableNames)中。 日志消息列出了每個跳過的鍵,可使用如下命令查看

kubectl get events

類似輸出如下:

LASTSEEN FIRSTSEEN COUNT NAME          KIND  SUBOBJECT  TYPE      REASON                            SOURCE                MESSAGE
   0s       0s        1     dapi-test-pod Pod              Warning   InvalidEnvironmentVariableNames   {kubelet, 127.0.0.1}  Keys [1badkey, 2alsobad] from the EnvFrom configMap default/myconfig were skipped since they are considered invalid environment variable names.
  • ConfigMap駐留在特定的命名空間中。 ConfigMap只能由位於相同名稱空間中的Pod引用
  • 不能給靜態Pod配置ConfigMaps,Kubelet不支持。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM