ConfigMapの使用kubernetesはポッドプロファイルを管理します

簡単な紹介

容器とConfigMaps画像は、構成ファイル、アプリケーションコンテナの移植から切り離すことができます。模範的な記事のシリーズを提供するためにどのように作成する方法を説明しConfigMaps保存して、ConfigMapsポッド内の構成データ。

注:このドキュメントでは、彼らの公式文書を参照して理解されるべきである、と。誤解を招くような場合は、批判をしてください。

ConfigMapを作成します。

私たちは、使用することができますkubectl create configmapkustomization.yamlConfigMapビルダーを作成しますConfigMap開始からKubernetes 1.14バージョンは、kubectlはの使用をサポートするために始めkustomization.yaml作成をConfigMap

configmapを作成kubectl ConfigMapの使用を作成します。

使用するとkubectl create configmap、ディレクトリ、ファイル、またはリテラルからconfigmapsを作成します。

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

<map-name>その設定ConfigMap名、<data-source>ディレクトリデータは、ディレクトリ、ファイル、またはリテラルから読み取ります。

ConfigMapをされdata-source、キーと値のペアになるはず。

  • 行に設けられたキー=キーコマンドまたはファイル名
  • リテラルまたは提供されるコマンドライン上のファイルの内容を値=提供

使用kubectl describeまたはkubectl 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

その結果から見ることができます:ブロックが含まれているディレクトリすべての2つのファイルの内容を。ConfigMapdataconfigure-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を作成するために使用するファイルからキー定義

使用する--from-fileには、与えられたキーのデータソースパラメータにデータファイルをキーブロックを見ることができます。ConfigMapdata

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

ここでは<my-key-name>、キーの名前は、あなたがCOnfigMapで使用することを、<path-to-file>ローカル・データ・ソースキーのデータ内容を表現することにします。
例えば:

# 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から生成されたときにキー定義を使用しました

あなたはというよりも、キーを定義することができ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、ConfigMapkey-valueキーと値のペアは、環境変数を定義します

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

2、ConfigMap定義されたspecial.how値は、ポッドに割り当てられている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

ポッドを作成します。

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

ポッドの出力は、環境変数を含めますSPECIAL_LEVEL_KEY=very

複数のConfigMaps容器は、データ変数を定義します

ConfigMapsの作成:

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

ポッド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

ポッドの作成:

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

ポッド環境変数の出力は、2つの構成SPECIAL_LEVEL_KEY=veryとしますLOG_LEVEL=INFO

コンテナ環境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

使用するenvFromデータ定義ConfigMapにおけるコンテナとして全ての環境変数を。ポッドの環境変数となりました。ConfigMapKey

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

ポッドを作成します。

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

ポッド出力は、環境変数を含めるSPECIAL_LEVEL=veryと、SPECIAL_TYPE=charm

コマンドポッドで定義された変数ConfigMap

ポッド内speccommandブロックを使用することができる$(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

ボリュームは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

ポッド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

ポッドの作成:

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

ビューコンテナのログ出力

# kubectl logs dapi-test-pod

出力は次のよう:

SPECIAL_LEVEL
SPECIAL_TYPE

注:あなたは、/ etc / configディレクトリ内のファイルを持っている場合は削除されます。

ボリュームConfigMapは、指定されたデータ・パスに追加しました

使用し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

ポッドを作成します。

# 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

APIリソースはキーと値のペアでConfigMapは、構成データを保存します。ポッドで使用することができるデータは、システム構成要素を提供するように構成されてもよいです。たとえばcontrollers秘密ConfigMapと似ていますが、機密情報の処理が含まれていない文字列のための方法が提供されます。コンフィギュレーションデータにユーザとシステムコンポーネントはConfigMapに格納することができます。

注:ConfigMapではなく、それらを交換するよりも、プロパティファイルを参照する必要があります。ConfigMapは、Linux / etcディレクトリとその内容物と同様に表現することができます。ボリュームがKubernetes ConfigMapを作成した場合、例えば、ボリューム内の単一のファイルによって各データ項目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の制限

  • ConfigMapがオプション設定されていない限り、ポッドが(作成される前に。参照ConfigMapが存在しない場合は、ポッドが起動しません。また、ConfigMapのために存在していないあなたはConfigMapを作成しなければならないkeyの言及もポッドを開始防ぎます。

  • あなたが使用している場合はenvFromConfigMapから定義された環境変数を、キーは無視無効とみなされます。あなたはポッドを開始することができますが、無効な名前は、イベントログ(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は、同じスペースポッドに名前で参照すること
  • あなたは、静的ポッド、Kubeletへのconfigure ConfigMapsはサポートされていないことはできません。

おすすめ

転載: www.cnblogs.com/mcsiberiawolf/p/12227855.html