|
| 1 | +--- |
| 2 | +title: kubectlを使用してSecretを管理する |
| 3 | +content_type: task |
| 4 | +weight: 10 |
| 5 | +description: kubectlコマンドラインを使用してSecretを作成する |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- overview --> |
| 9 | + |
| 10 | +## {{% heading "prerequisites" %}} |
| 11 | + |
| 12 | +{{< include "task-tutorial-prereqs.md" >}} |
| 13 | + |
| 14 | +<!-- steps --> |
| 15 | + |
| 16 | +## Secretを作成する |
| 17 | + |
| 18 | +`Secret`はデータベースにアクセスするためにPodが必要とするユーザー資格情報を含めることができます。 |
| 19 | +たとえば、データベース接続文字列はユーザー名とパスワードで構成されます。 |
| 20 | +ユーザー名はローカルマシンの`./username.txt`に、パスワードは`./password.txt`に保存します。 |
| 21 | + |
| 22 | +```shell |
| 23 | +echo -n 'admin' > ./username.txt |
| 24 | +echo -n '1f2d1e2e67df' > ./password.txt |
| 25 | +``` |
| 26 | + |
| 27 | +上記の2つのコマンドの`-n`フラグは、生成されたファイルにテキスト末尾の余分な改行文字が含まれないようにします。 |
| 28 | +`kubectl`がファイルを読み取り、内容をbase64文字列にエンコードすると、余分な改行文字もエンコードされるため、これは重要です。 |
| 29 | + |
| 30 | +`kubectl create secret`コマンドはこれらのファイルをSecretにパッケージ化し、APIサーバー上にオブジェクトを作成します。 |
| 31 | + |
| 32 | +```shell |
| 33 | +kubectl create secret generic db-user-pass \ |
| 34 | + --from-file=./username.txt \ |
| 35 | + --from-file=./password.txt |
| 36 | +``` |
| 37 | + |
| 38 | +出力は次のようになります: |
| 39 | + |
| 40 | +``` |
| 41 | +secret/db-user-pass created |
| 42 | +``` |
| 43 | + |
| 44 | +ファイル名がデフォルトのキー名になります。オプションで`--from-file=[key=]source`を使用してキー名を設定できます。たとえば: |
| 45 | + |
| 46 | +```shell |
| 47 | +kubectl create secret generic db-user-pass \ |
| 48 | + --from-file=username=./username.txt \ |
| 49 | + --from-file=password=./password.txt |
| 50 | +``` |
| 51 | + |
| 52 | +`--from-file`に指定したファイルに含まれるパスワードの特殊文字をエスケープする必要はありません。 |
| 53 | + |
| 54 | +また、`--from-literal=<key>=<value>`タグを使用してSecretデータを提供することもできます。 |
| 55 | +このタグは、複数のキーと値のペアを提供するために複数回指定することができます。 |
| 56 | +`$`、`\`、`*`、`=`、`!`などの特殊文字は[シェル](https://en.wikipedia.org/wiki/Shell_(computing))によって解釈されるため、エスケープを必要とすることに注意してください。 |
| 57 | +ほとんどのシェルでは、パスワードをエスケープする最も簡単な方法は、シングルクォート(`'`)で囲むことです。 |
| 58 | +たとえば、実際のパスワードが`S!B\*d$zDsb=`の場合、次のようにコマンドを実行します: |
| 59 | + |
| 60 | +```shell |
| 61 | +kubectl create secret generic dev-db-secret \ |
| 62 | + --from-literal=username=devuser \ |
| 63 | + --from-literal=password='S!B\*d$zDsb=' |
| 64 | +``` |
| 65 | + |
| 66 | +## Secretを検証する |
| 67 | + |
| 68 | +Secretが作成されたことを確認できます: |
| 69 | + |
| 70 | +```shell |
| 71 | +kubectl get secrets |
| 72 | +``` |
| 73 | + |
| 74 | +出力は次のようになります: |
| 75 | + |
| 76 | +``` |
| 77 | +NAME TYPE DATA AGE |
| 78 | +db-user-pass Opaque 2 51s |
| 79 | +``` |
| 80 | + |
| 81 | +`Secret`の説明を参照できます: |
| 82 | + |
| 83 | +```shell |
| 84 | +kubectl describe secrets/db-user-pass |
| 85 | +``` |
| 86 | + |
| 87 | +出力は次のようになります: |
| 88 | + |
| 89 | +``` |
| 90 | +Name: db-user-pass |
| 91 | +Namespace: default |
| 92 | +Labels: <none> |
| 93 | +Annotations: <none> |
| 94 | +
|
| 95 | +Type: Opaque |
| 96 | +
|
| 97 | +Data |
| 98 | +==== |
| 99 | +password: 12 bytes |
| 100 | +username: 5 bytes |
| 101 | +``` |
| 102 | + |
| 103 | +`kubectl get`と`kubectl describe`コマンドはデフォルトでは`Secret`の内容を表示しません。 |
| 104 | +これは、`Secret`が不用意に他人にさらされたり、ターミナルログに保存されたりしないようにするためです。 |
| 105 | + |
| 106 | +## Secretをデコードする {#decoding-secret} |
| 107 | + |
| 108 | +先ほど作成したSecretの内容を見るには、以下のコマンドを実行します: |
| 109 | + |
| 110 | +```shell |
| 111 | +kubectl get secret db-user-pass -o jsonpath='{.data}' |
| 112 | +``` |
| 113 | + |
| 114 | +出力は次のようになります: |
| 115 | + |
| 116 | +```json |
| 117 | +{"password.txt":"MWYyZDFlMmU2N2Rm","username.txt":"YWRtaW4="} |
| 118 | +``` |
| 119 | + |
| 120 | +`password.txt`のデータをデコードします: |
| 121 | + |
| 122 | +```shell |
| 123 | +echo 'MWYyZDFlMmU2N2Rm' | base64 --decode |
| 124 | +``` |
| 125 | + |
| 126 | +出力は次のようになります: |
| 127 | + |
| 128 | +``` |
| 129 | +1f2d1e2e67df |
| 130 | +``` |
| 131 | + |
| 132 | +## クリーンアップ |
| 133 | + |
| 134 | +作成したSecretを削除するには次のコマンドを実行します: |
| 135 | + |
| 136 | +```shell |
| 137 | +kubectl delete secret db-user-pass |
| 138 | +``` |
| 139 | + |
| 140 | +<!-- discussion --> |
| 141 | + |
| 142 | +## {{% heading "whatsnext" %}} |
| 143 | + |
| 144 | +- [Secretのコンセプト](/ja/docs/concepts/configuration/secret/)を読む |
| 145 | +- [設定ファイルを使用してSecretを管理する](/docs/tasks/configmap-secret/managing-secret-using-config-file/)方法を知る |
| 146 | +- [kustomizeを使用してSecretを管理する](/docs/tasks/configmap-secret/managing-secret-using-kustomize/)方法を知る |
0 commit comments