0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[k8s]ConfigMap について確認したときのメモ

Posted at

以下あたりを見つつ、確認したときのメモ。

ConfigMaps

GKE ConfigMap

ConfigMap って何?

GKE ConfigMap

ConfigMap は、構成ファイル、コマンドライン引数、環境変数、ポート番号、構成に関するその他のアーティファクトを、実行時に Pod のコンテナやシステム コンポーネントにバインドします。ConfigMap を使用すると、Pod やコンポーネントから構成を分離できるため、ワークロードの移植性が維持されます。これにより、構成の変更や管理が容易になり、構成データが Pod 仕様に制約されなくなります。
ConfigMap は、機密性が低く、暗号化されていない構成情報を保存して共有するのに便利です。クラスタで機密情報を使用するには、Secret を使用する必要があります

ConfigMap の作り方

3つほど考えられる

  • kubectl でファイルから値を参照して作る(--from-file)
  • kubectl で直接値を渡す(--from-literal)
  • マニュフェストファイルから作る

ファイルから作る場合以下のようにする

# 設定ファイルサンプル
$wget https://kubernetes.io/examples/configmap/game.properties

$cat 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%

# 作成
$kubectl create configmap game-config-2 --from-file=game.properties

#  確認
$ k get configmaps
NAME            DATA   AGE
game-config-2   1      9s

# データの確認
$ k get configmaps -o yaml
apiVersion: v1
items:
- apiVersion: v1
  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
  kind: ConfigMap
  metadata:
    creationTimestamp: "2020-06-02T07:28:28Z"
    name: game-config-2
    namespace: default
    resourceVersion: "2319007"
    selfLink: /api/v1/namespaces/default/configmaps/game-config-2
    uid: a757690a-a4a2-11ea-80d8-0aafc37fdac4
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

--from-literal オプションを使う場合、以下

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

$ k get configmaps
NAME             DATA   AGE
game-config-2    1      110s
special-config   2      5s


$k get configmaps special-config -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: "2020-06-02T07:30:13Z"
  name: special-config
  namespace: default
  resourceVersion: "2319180"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: e5ebf59f-a4a2-11ea-80d8-0aafc37fdac4

マニュフェストから作る場合。

# テンプレート作成
$k create configmap test --dry-run -o yaml > config-map.yaml

# data を追加
$vi config-map.yaml
$cat config-map.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: test
data:
  special.how: very
  special.type: charm

$k apply -f config-map.yaml
configmap/test created

# 確認
$k get configmaps test -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"special.how":"very","special.type":"charm"},"kind":"ConfigMap","metadata":{"annotations":{},"creationTimestamp":null,"name":"test","namespace":"default"}}
  creationTimestamp: "2020-06-02T07:36:09Z"
  name: test
  namespace: default
  resourceVersion: "2319762"
  selfLink: /api/v1/namespaces/default/configmaps/test
  uid: ba377737-a4a3-11ea-80d8-0aafc37fdac4

Pod での参照方法

Define container environment variables using ConfigMap data

valueFrom.configMapKeyRef で 環境変数としてコンテナでは参照できる。

pod-single-configmap-env-variable.yaml
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
$k apply -f pod-single-configmap-env-variable.yaml
pod/dapi-test-pod created

$k get pods
NAME            READY   STATUS      RESTARTS   AGE
dapi-test-pod   0/1     Completed   0          3s

$ k logs dapi-test-pod |grep SPECIAL
SPECIAL_LEVEL_KEY=very
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?