KubernetesリソースオブジェクトConfigmap実践

6098 ワード

コンフィギュレーションデータを保存するためのConfigMapのキー値ペアは、個々のプロパティを保存するためにも、コンフィギュレーションファイルを保存するためにも使用できます.ConfigMapはsecretと似ていますが、機密情報を含まない文字列をより容易に処理できます.
Confgimap作成kubectl create configmapを使用して、ファイル、ディレクトリ、key-value文字列の作成などからConfigMapを作成できます.kubectl create -f fileで作成することもできます.
1、key-valueで作成
kubectl create configmap config-test --from-literal=user=andriy --from-literal=pwd=password

2、環境変数ファイルを使って作成する
echo -e "user=andriy
pwd=password" | tee cm.env user=andriy pwd=password kubectl create configmap cm-env-test --from-env-file=cm.env

3、ディレクトリを使用して作成(ディレクトリの下にあるすべてのファイルを含む)
mkdir config
echo "andriy" > config/user
echo "password" > config/pwd
kubectl create configmap cm-dir-test --from-file=config/

4、yaml/jsonで作成
cat << EOF > cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-yaml-test
data:
  user: andriy
  pwd: password
EOF
kubectl create -f cm.yaml
kubectl get cm cm-env-test -o go-template='{{.data}}'コマンドを使用してconfigmapのkey-value値を表示します.
Confgimap使用
ConfigMapは、環境変数の設定、コンテナコマンドラインパラメータの設定、およびVolumeに直接ファイルまたはディレクトリをマウントする3つの方法でPodで使用できます.
[warning]注意
  • ConfigMapは、Podが参照する前に
  • を作成する必要があります.
  • envFromを使用すると、無効なキー
  • が自動的に無視されます.
  • Podは同じネーミングスペース内のConfigMap
  • しか使用できません.
    1、環境変数として使用
  • 再定義key
  • apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
        - name: test-container
          image: busybox
          command: ["/bin/sh", "-c", "env"]
          env:
            - name: USERNAME
              valueFrom:
                configMapKeyRef:
                  name: cm-env-test
                  key: user
            - name: PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: cm-env-test
                  key: pwd
      restartPolicy: Never
    
  • configmapのkey
  • を直接参照
    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
        - name: test-container
          image: busybox
          command: ["/bin/sh", "-c", "env"]
          envFrom:
            - configMapRef:
                name: cm-env-test
      restartPolicy: Never
    

    上記podの作成後、出力情報をkubectl logs test-podで表示できます.
    2、コマンドラインパラメータとして使用
    apiVersion: v1
    kind: Pod
    metadata:
      name: configmap-volue-test
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: ["/bin/sh", "-c", "echo $(USERNAME) $(PASSWORD)" ]
          env:
            - name: USERNAME
              valueFrom:
                configMapKeyRef:
                  name: cm-env-test
                  key: user
            - name: PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: cm-env-test
                  key: pwd
      restartPolicy: Never
    

    Podログ出力
    kubectl logs configmap-volue-test 
    	andriy password
    

    3、volumeを使用してConfigMapをファイルまたはディレクトリとして直接マウントする
  • 作成したConfigMapはPodの/etc/configディレクトリに直接マウントされ、各key-valueキー値ペアはファイル名、valueはコンテンツ
  • のファイルを生成します.
    apiVersion: v1
    kind: Pod
    metadata:
      name: configmap-vol-test
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: ["/bin/sh", "-c", "cat /etc/config/*"]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: cm-env-test
      restartPolicy: Never
    

    Podログ出力
    kubectl logs configmap-vol-test 
    	passwordandriy
    
  • は、作成されたConfigMapのuserというキーを/etc/configディレクトリの下にある相対パス/keys/usernameにマウントします.同じ名前のファイルがある場合は、直接上書きします.他のkeyは
  • をマウントしません
    apiVersion: v1
    kind: Pod
    metadata:
      name: file-cm-test
    spec:
      containers:
        - name: test-file-con
          image: gcr.io/google_containers/busybox
          command: ["/bin/sh","-c","cat /etc/config/keys/username"]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: cm-env-test
            items:
            - key: user
              path: keys/username
      restartPolicy: Never
    

    Podログ出力
    kubectl logs file-cm-test 
    	andriy
    
  • ConfigMapは、同じディレクトリの下に複数のkeyと複数のディレクトリをマウントすることをサポートします.たとえば、userおよびpwdを/etc/configにマウントします.また、userは/etc/config 2に同時にマウントされます.
  • apiVersion: v1
    kind: Pod
    metadata:
      name: file-test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: ["/bin/sh","-c","sleep 36000"]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
          - name: config-volume2
            mountPath: /etc/config2
      volumes:
        - name: config-volume
          configMap:
            name: cm-env-test
            items:
            - key: user
              path: keys/username
            - key: pwd
              path: keys/password
        - name: config-volume2
          configMap:
            name: cm-env-test
            items:
            - key: user
              path: keys/username
      restartPolicy: Never
    

    4、subpathを使用してConfigMapを個別のファイルとしてディレクトリにマウントする
    一般的にconfigmapでファイルをマウントする場合は、マウントディレクトリを上書きしてからcongfigmapの内容をファイルとしてマウントします.元のフォルダの下のファイルを上書きしないようにするには、configmapの各keyをファイル形式でディレクトリにマウントするだけで、subpathパラメータを使用します.
    apiVersion: v1
    kind: Pod
    metadata:
      name: cm-test-pod
    spec:
      containers:
        - name: test-container
          image: nginx
          command: ["/bin/sh","-c","sleep 36000"]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/nginx/username
            subPath: username
      volumes:
        - name: config-volume
          configMap:
            name: cm-env-test
            items:
            - key: user
              path: username
      restartPolicy: Never
    

    Pod検証
    kubectl exec -it cm-test-pod -- cat /etc/nginx/username
    	andriy