> For the complete documentation index, see [llms.txt](https://docs.keeper.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.keeper.io/keeperpam/jp/secrets-manager/integrations/kubernetes-secrets-injector/examples.md).

# 例

Kubernetes Injector向けの完全な実行可能な例です。各例は、**インジェクターのインストール**と**KSM認証Secret** (`keeper-credentials`) の作成が、そのページの手順どおり完了していることを前提としています。

{% hint style="info" %}
以下のレコード名およびフィールド名 (例: `database-credentials`、`tls-cert`、`Production/Databases`) は**説明用**です。Keeperシークレットマネージャーアプリケーションと共有しているフォルダ内に一致するレコードを作成するか、注釈を自分のレコードを指すように変更してください。
{% endhint %}

### クイックチェック: Podへのシークレット注入

インジェクターが動作していることを確認する最も早い方法は、レコードをファイルに取得して読み取ることです。以下の `.yaml` 構成ファイルは、この機能の使用例です。

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: hello-secrets
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/secret: "demo-secret"
spec:
  containers:
    - name: app
      image: busybox:1.36
      command: ["sh", "-c", "cat /keeper/secrets/demo-secret.json && sleep 3600"]
```

{% code overflow="wrap" %}

```bash
kubectl logs hello-secrets         # prints the record JSON
kubectl get pod hello-secrets -o jsonpath='{.spec.containers[*].name}'   # includes keeper-secrets-sidecar
```

{% endcode %}

### ファイルとしてのデータベース認証情報

デフォルトモードでは、レコードは `/keeper/secrets/` にJSONとして書き込まれ、アプリケーションがそこから読み取ります。

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 1
  selector: { matchLabels: { app: web } }
  template:
    metadata:
      labels: { app: web }
      annotations:
        keeper.security/inject: "true"
        keeper.security/ksm-config: "keeper-credentials"
        keeper.security/secret: "database-credentials"
    spec:
      containers:
        - name: web
          image: my-app:latest   # reads /keeper/secrets/database-credentials.json
```

### `.env` ファイルのレンダリング

`config` ブロックで出力 `format` を選択します。ここでは、レコードをアプリケーションが読み込める `.env` ファイルとしてレンダリングします。

```yaml
metadata:
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/config: |
      secrets:
        - record: database-credentials
          path: /keeper/secrets/db.env
          format: env
```

```bash
kubectl exec deploy/web -c app -- cat /keeper/secrets/db.env
# LOGIN=app_user
# PASSWORD=...
```

### テンプレートによる接続文字列の生成

Goテンプレート ([Sprig](https://masterminds.github.io/sprig/) 関数付き) でレコードフィールドを任意の形式に変換できます。ここではPostgres URLの例です。

```yaml
metadata:
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/config: |
      secrets:
        - record: database-credentials
          path: /keeper/secrets/db-url.txt
          template: |
            postgresql://{{ .login }}:{{ .password }}@{{ .host }}:5432/app
```

### 環境変数として注入

環境変数しか読み取れないレガシーアプリケーション向けです。フィールド名は大文字のキーになり、プレフィックスを付けて起動時に1回取得します (環境変数は再起動なしでは変更できません)。

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: legacy-app
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/inject-env-vars: "true"
    keeper.security/env-prefix: "APP_"
    keeper.security/secret: "database-credentials"
    keeper.security/init-only: "true"
spec:
  containers:
    - name: app
      image: legacy-app:latest   # sees APP_LOGIN, APP_PASSWORD, ...
```

{% hint style="warning" %}
環境変数はPod仕様に保存されます (`kubectl get pod -o yaml` で表示され、`etcd` に永続化されます)。機密値にはファイル注入を推奨します。
{% endhint %}

### Deployment向けKubernetes Secretの作成

ワークロードがネイティブ `Secret` (`secretKeyRef`、`envFrom`、または `Secret` ボリューム) を必要とする場合、インジェクターにレコードから `Secret` を作成させます。

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: app-with-secret
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/inject-as-k8s-secret: "true"
    keeper.security/k8s-secret-name: "app-secrets"
    keeper.security/secret: "database-credentials"
spec:
  containers:
    - name: app
      image: my-app:latest
      env:
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: app-secrets
              key: password
```

{% code overflow="wrap" %}

```bash
kubectl get secret app-secrets -o jsonpath='{.data}' | jq 'keys'   # ["login","password",...]
```

{% endcode %}

### NGINX向けTLS証明書とキー

Keeperレコードに添付されたファイルを、アプリケーションがマウントするパスにダウンロードします。

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: tls-nginx
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/file-cert: "tls-cert:server.crt:/keeper/secrets/tls/tls.crt"
    keeper.security/file-key: "tls-cert:server.key:/keeper/secrets/tls/tls.key"
spec:
  containers:
    - name: nginx
      image: nginx:alpine
      volumeMounts:
        - name: keeper-secrets
          mountPath: /etc/nginx/tls
          subPath: tls
```

### フォルダ内のすべてのシークレットを注入

Keeperフォルダ内のすべてのレコードを取得し、出力パス配下にレコードごとに1ファイルとして保存します。

```yaml
metadata:
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/folder: "Production/Databases"
    keeper.security/folder-path: "/keeper/secrets/db"
```

{% hint style="info" %}
`keeper.security/folder-uid` を使用して、UIDでフォルダを参照することもできます。
{% endhint %}

### Keeper Notationによる単一フィールドの抽出

Keeper Notationを使用して、1つのフィールドだけを指定パスに書き込みます。Notationは、シークレットごとの `keeper.security/secret-<name>` 注釈で解析されます (プレーンな `keeper.security/secret` 注釈は、値を常にレコードタイトルとして扱います)。

```yaml
metadata:
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/secret-token: "keeper://API-Keys-UID/field/password:/keeper/secrets/token.txt"
```

### 自動ローテーション

サイドカーモードでは、エージェントが一定間隔で再取得し、ファイルをその場で上書きします。Podの再起動は不要です。ファイルを再読み取りするアプリケーションは、新しい値を自動的に取得します。

```yaml
metadata:
  annotations:
    keeper.security/inject: "true"
    keeper.security/ksm-config: "keeper-credentials"
    keeper.security/secret: "database-credentials"
    keeper.security/refresh-interval: "1m"
```

```bash
# rotate the value in Keeper, then watch it update in place:
kubectl exec deploy/web -c app -- sh -c 'while :; do cat /keeper/secrets/database-credentials.json; sleep 10; done'
```

### その他の例とGitHubソース

インジェクターのGitHubリポジトリには、実行可能な例の完全なカタログ (Webデモ、データベース、ローテーションダッシュボード、耐障害性、クラウド認証など) があります。

* [Github Source](https://github.com/Keeper-Security/keeper-k8s-injector/tree/main/examples) を表示


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.keeper.io/keeperpam/jp/secrets-manager/integrations/kubernetes-secrets-injector/examples.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
