|
| 1 | +--- |
| 2 | +title: Secretsで安全にクレデンシャルを配布する |
| 3 | +content_type: task |
| 4 | +weight: 50 |
| 5 | +min-kubernetes-server-version: v1.6 |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- overview --> |
| 9 | +このページでは、パスワードや暗号化キーなどの機密データをPodに安全に注入する方法を紹介します。 |
| 10 | + |
| 11 | +## {{% heading "prerequisites" %}} |
| 12 | + |
| 13 | + |
| 14 | +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} |
| 15 | + |
| 16 | +### 機密データをbase64でエンコードする |
| 17 | + |
| 18 | +ユーザー名`my-app`とパスワード`39528$vdg7Jb`の2つの機密データが必要だとします。 |
| 19 | +まず、base64エンコーディングツールを使って、ユーザ名とパスワードをbase64表現に変換します。 |
| 20 | +ここでは、手軽に入手できるbase64プログラムを使った例を紹介します: |
| 21 | + |
| 22 | +```shell |
| 23 | +echo -n 'my-app' | base64 |
| 24 | +echo -n '39528$vdg7Jb' | base64 |
| 25 | +``` |
| 26 | + |
| 27 | +出力結果によると、ユーザ名のbase64表現は`bXktYXBw`で、パスワードのbase64表現は`Mzk1MjgkdmRnN0pi`です。 |
| 28 | + |
| 29 | +{{< caution >}} |
| 30 | +OSから信頼されているローカルツールを使用することで、外部ツールのセキュリティリスクを低減することができます。 |
| 31 | +{{< /caution >}} |
| 32 | + |
| 33 | + |
| 34 | +<!-- steps --> |
| 35 | + |
| 36 | +## Secretを作成する |
| 37 | + |
| 38 | +以下はユーザー名とパスワードを保持するSecretを作成するために使用できる設定ファイルです: |
| 39 | + |
| 40 | +{{< codenew file="pods/inject/secret.yaml" >}} |
| 41 | + |
| 42 | +1. Secret を作成する |
| 43 | + |
| 44 | + ```shell |
| 45 | + kubectl apply -f https://k8s.io/examples/pods/inject/secret.yaml |
| 46 | + ``` |
| 47 | + |
| 48 | +1. Secretの情報を取得する |
| 49 | + |
| 50 | + ```shell |
| 51 | + kubectl get secret test-secret |
| 52 | + ``` |
| 53 | + |
| 54 | + 出力: |
| 55 | + |
| 56 | + ``` |
| 57 | + NAME TYPE DATA AGE |
| 58 | + test-secret Opaque 2 1m |
| 59 | + ``` |
| 60 | + |
| 61 | +1. Secretの詳細な情報を取得する: |
| 62 | + |
| 63 | + ```shell |
| 64 | + kubectl describe secret test-secret |
| 65 | + ``` |
| 66 | + |
| 67 | + 出力: |
| 68 | + |
| 69 | + ``` |
| 70 | + Name: test-secret |
| 71 | + Namespace: default |
| 72 | + Labels: <none> |
| 73 | + Annotations: <none> |
| 74 | + |
| 75 | + Type: Opaque |
| 76 | + |
| 77 | + Data |
| 78 | + ==== |
| 79 | + password: 13 bytes |
| 80 | + username: 7 bytes |
| 81 | + ``` |
| 82 | + |
| 83 | +### kubectlでSecretを作成する |
| 84 | + |
| 85 | +base64エンコードの手順を省略したい場合は、`kubectl create secret`コマンドで同じSecretを作成することができます。 |
| 86 | + |
| 87 | +例えば: |
| 88 | + |
| 89 | +```shell |
| 90 | +kubectl create secret generic test-secret --from-literal='username=my-app' --from-literal='password=39528$vdg7Jb' |
| 91 | +``` |
| 92 | + |
| 93 | +先ほどの詳細なアプローチでは 各ステップを明示的に実行し、何が起こっているかを示していますが、`kubectl create secret`の方が便利です。 |
| 94 | + |
| 95 | + |
| 96 | +## Volumeにある機密情報をアクセスするPodを作成する |
| 97 | + |
| 98 | +これはPodの作成に使用できる設定ファイルです。 |
| 99 | + |
| 100 | +{{< codenew file="pods/inject/secret-pod.yaml" >}} |
| 101 | + |
| 102 | +1. Podを作成する: |
| 103 | + |
| 104 | + ```shell |
| 105 | + kubectl apply -f https://k8s.io/examples/pods/inject/secret-pod.yaml |
| 106 | + ``` |
| 107 | + |
| 108 | +1. Podの`STATUS`が`Running`であるのを確認する: |
| 109 | + |
| 110 | + ```shell |
| 111 | + kubectl get pod secret-test-pod |
| 112 | + ``` |
| 113 | + |
| 114 | + 出力: |
| 115 | + ``` |
| 116 | + NAME READY STATUS RESTARTS AGE |
| 117 | + secret-test-pod 1/1 Running 0 42m |
| 118 | + ``` |
| 119 | +
|
| 120 | +1. Podの中にあるコンテナにシェルを実行する |
| 121 | +
|
| 122 | + ```shell |
| 123 | + kubectl exec -i -t secret-test-pod -- /bin/bash |
| 124 | + ``` |
| 125 | + |
| 126 | +1. 機密データは `/etc/secret-volume` にマウントされたボリュームを介してコンテナに公開されます。 |
| 127 | + |
| 128 | + ディレクトリ `/etc/secret-volume` 中のファイルの一覧を確認する: |
| 129 | + ```shell |
| 130 | + # Run this in the shell inside the container |
| 131 | + ls /etc/secret-volume |
| 132 | + ``` |
| 133 | + `password`と`username` 2つのファイル名が出力される: |
| 134 | + ``` |
| 135 | + password username |
| 136 | + ``` |
| 137 | + |
| 138 | +1. `username` と `password` ファイルの中身を表示する: |
| 139 | + |
| 140 | + ```shell |
| 141 | + # Run this in the shell inside the container |
| 142 | + echo "$( cat /etc/secret-volume/username )" |
| 143 | + echo "$( cat /etc/secret-volume/password )" |
| 144 | + |
| 145 | + ``` |
| 146 | + 出力: |
| 147 | + ``` |
| 148 | + my-app |
| 149 | + 39528$vdg7Jb |
| 150 | + ``` |
| 151 | + |
| 152 | +## Secretでコンテナの環境変数を定義する |
| 153 | + |
| 154 | +### 単一のSecretでコンテナの環境変数を定義する |
| 155 | + |
| 156 | +* Secretの中でkey-valueペアで環境変数を定義する: |
| 157 | + |
| 158 | + ```shell |
| 159 | + kubectl create secret generic backend-user --from-literal=backend-username='backend-admin' |
| 160 | + ``` |
| 161 | + |
| 162 | +* Secretで定義された`backend-username`の値をPodの環境変数`SECRET_USERNAME`に割り当てます。 |
| 163 | + |
| 164 | + {{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}} |
| 165 | + |
| 166 | +* Podを作成する: |
| 167 | + |
| 168 | + ```shell |
| 169 | + kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml |
| 170 | + ``` |
| 171 | + |
| 172 | +* コンテナの環境変数`SECRET_USERNAME`の中身を表示する: |
| 173 | + |
| 174 | + ```shell |
| 175 | + kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME' |
| 176 | + ``` |
| 177 | + |
| 178 | + 出力: |
| 179 | + ``` |
| 180 | + backend-admin |
| 181 | + ``` |
| 182 | + |
| 183 | +### 複数のSecretからコンテナの環境変数を定義する |
| 184 | + |
| 185 | +* 前述の例と同様に、まずSecretを作成します: |
| 186 | + |
| 187 | + ```shell |
| 188 | + kubectl create secret generic backend-user --from-literal=backend-username='backend-admin' |
| 189 | + kubectl create secret generic db-user --from-literal=db-username='db-admin' |
| 190 | + ``` |
| 191 | + |
| 192 | +* Podの中で環境変数を定義する: |
| 193 | + |
| 194 | + {{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}} |
| 195 | + |
| 196 | +* Podを作成する: |
| 197 | + |
| 198 | + ```shell |
| 199 | + kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml |
| 200 | + ``` |
| 201 | + |
| 202 | +* コンテナの環境変数を表示する: |
| 203 | + |
| 204 | + ```shell |
| 205 | + kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c 'env | grep _USERNAME' |
| 206 | + ``` |
| 207 | + 出力: |
| 208 | + ``` |
| 209 | + DB_USERNAME=db-admin |
| 210 | + BACKEND_USERNAME=backend-admin |
| 211 | + ``` |
| 212 | + |
| 213 | + |
| 214 | +## Secretのすべてのkey-valueペアを環境変数として設定する |
| 215 | + |
| 216 | +{{< note >}} |
| 217 | +この機能は Kubernetes v1.6 以降から利用可能 |
| 218 | +{{< /note >}} |
| 219 | + |
| 220 | +* 複数のkey-valueペアを含むSecretを作成する |
| 221 | + |
| 222 | + ```shell |
| 223 | + kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb' |
| 224 | + ``` |
| 225 | + |
| 226 | +* envFromを使用してSecretのすべてのデータをコンテナの環境変数として定義します。SecretのキーがPodの環境変数名になります。 |
| 227 | + |
| 228 | + {{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}} |
| 229 | + |
| 230 | +* Podを作成する: |
| 231 | + |
| 232 | + ```shell |
| 233 | + kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml |
| 234 | + ``` |
| 235 | + |
| 236 | +* `username`と`password`コンテナの環境変数を表示する |
| 237 | + |
| 238 | + ```shell |
| 239 | + kubectl exec -i -t envfrom-secret -- /bin/sh -c 'echo "username: $username\npassword: $password\n"' |
| 240 | + ``` |
| 241 | + |
| 242 | + 出力: |
| 243 | + ``` |
| 244 | + username: my-app |
| 245 | + password: 39528$vdg7Jb |
| 246 | + ``` |
| 247 | + |
| 248 | +### 参考文献 |
| 249 | + |
| 250 | +* [Secret](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#secret-v1-core) |
| 251 | +* [Volume](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core) |
| 252 | +* [Pod](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#pod-v1-core) |
| 253 | + |
| 254 | +## {{% heading "whatsnext" %}} |
| 255 | + |
| 256 | +* [Secrets](/docs/concepts/configuration/secret/)についてもっと知る。 |
| 257 | +* [Volumes](/docs/concepts/storage/volumes/)について知る。 |
0 commit comments