Kubernetes ConfigMapマニフェスト

参考文献: Kubernetesマイクロサービス開発の実践 (早川博 著)


https://kubernetes.io/ja/docs/tasks/configure-pod-container/configure-pod-configmap/

kubectl explain configmap

kubectl api-resources


-- 1. ConfigMap作成


cat <<-'EOF' > cm01.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm01
  namespace: default
data:
  key01: val01
  key02: val02
  file1.conf: |
    key11: val11
    key12: val12
    
EOF

kubectl apply -f cm01.yaml

kubectl get cm


kubectl delete -f cm01.yaml


-- 2. Pod作成(コンテナの環境変数に設定する場合)

 

cat <<-'EOF' > po01.yaml
apiVersion: v1
kind: Pod
metadata:
  name: po01
  labels:
    app: busybox
spec:
  containers:
  - name: busybox
    image: registry.k8s.io/busybox
    command:
    - "/bin/sh"
    - "-c"
    args:
    - |
      echo KEY1=$KEY1
      echo KEY2=$(KEY2)_SUFFIX2
    env:
    - name: KEY1
      valueFrom:
        configMapKeyRef:
          name: cm01
          key: key01
    - name: KEY2
      valueFrom:
        configMapKeyRef:
          name: cm01
          key: key02

EOF

kubectl apply -f po01.yaml

kubectl logs po01

kubectl delete -f po01.yaml

-- 3. Pod作成(読み取り専用ボリュームにファイルを追加する場合)

 

cat <<-'EOF' > po02.yaml
apiVersion: v1
kind: Pod
metadata:
  name: po02
  labels:
    app: busybox
spec:
  containers:
  - name: busybox
    image: registry.k8s.io/busybox
    command:
    - "/bin/sh"
    - "-c"
    args:
    - |
      cat /etc/config/hoge/fuga/file1.conf
    volumeMounts:
    - name: cm01vol01
      mountPath: /etc/config/
  volumes:
  - name: cm01vol01
    configMap:
      name: cm01
      items:
      - key: file1.conf
        path: ./hoge/fuga/file1.conf

EOF

kubectl apply -f po02.yaml

kubectl logs po02

kubectl delete -f po02.yaml