---
title: Kubernetes
description: Deployment、ClusterIP の Service、設定用の Secret、Unico のレジストリ用のプルシークレットを使って、Kubernetes 上で Human Plugin を実行します。
canonical: https://developer.unico.io/ja/human-plugin/self-hosting/kubernetes
locale: ja
generated_by: markdown-export
---

# Kubernetes

ClusterIP の Service の背後に2つ以上のレプリカを配置し、バックエンドからのみ到達できるようにします。

## 前提条件

- Kubernetes クラスタと、そのクラスタへの `kubectl` アクセス。
- Human Plugin 専用で、クラスタから到達可能な PostgreSQL データベース。[前提条件とアクセス](/ja/human-plugin/get-started/prerequisites)を参照してください。
- Unico から提供されるレジストリの認証情報とアクセスキー。

以下のリソースはすべて、専用の namespace に配置します。

```bash
kubectl create namespace human-plugin
```

## 1. レジストリのプルシークレット

```bash
kubectl -n human-plugin create secret docker-registry unico-registry \
  --docker-server=pkg.unico.run \
  --docker-username="<username>" \
  --docker-password="<token>"
```

## 2. 設定用 Secret

```bash
kubectl -n human-plugin create secret generic human-plugin \
  --from-literal=HUMAN_API_ACCESS_KEY='<your access key>' \
  --from-literal=POSTGRES_ADDRESS_HOST='<database host>' \
  --from-literal=POSTGRES_USER='human_plugin' \
  --from-literal=POSTGRES_PASSWORD='<database password>'
```

`POSTGRES_ADDRESS_PORT`、`POSTGRES_DATABASE`、`POSTGRES_SSL_MODE` は、お使いのデータベースが[設定](/ja/human-plugin/self-hosting/configuration)のデフォルト値と異なる場合にのみ追加してください。別のツール（External Secrets、Sealed Secrets、vault）でシークレットを管理している場合は、そのツールで同じキーを作成してください。値を含む Secret マニフェストは決してコミットしないでください。

## 3. Deployment と Service

```yaml title="human-plugin.yaml"
apiVersion: apps/v1
kind: Deployment
metadata:
  name: human-plugin
  namespace: human-plugin
spec:
  replicas: 2
  selector:
    matchLabels: { app: human-plugin }
  template:
    metadata:
      labels: { app: human-plugin }
    spec:
      terminationGracePeriodSeconds: 60
      imagePullSecrets:
        - name: unico-registry
      containers:
        - name: human-plugin
          image: pkg.unico.run/human-plugin-api:latest
          imagePullPolicy: Always
          ports:
            - { name: http, containerPort: 8080 }
          envFrom:
            - secretRef: { name: human-plugin }
          resources:
            requests: { cpu: "1", memory: 1Gi }
            limits: { cpu: "1", memory: 1Gi }
          readinessProbe:
            httpGet: { path: /health/ready, port: http }
            timeoutSeconds: 5
          livenessProbe:
            httpGet: { path: /health/live, port: http }
            periodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  name: human-plugin
  namespace: human-plugin
spec:
  selector: { app: human-plugin }
  ports:
    - { name: http, port: 8080, targetPort: http }
```

```bash
kubectl apply -f human-plugin.yaml
```

```bash
kubectl -n human-plugin rollout status deployment/human-plugin
```

- **`replicas: 2`:** レプリカはデータベースを共有するため、必要な数だけ実行できます。2つあれば、1つが再起動している間も Human Plugin を利用可能に保てます。
- **`terminationGracePeriodSeconds: 60`:** Human Plugin は、停止時に処理中のリクエストを最大55秒待ちます。Kubernetes のデフォルトである30秒では、作成処理が途中で打ち切られる場合があります。
- **`imagePullPolicy: Always`:** `latest` タグでは、新しい Pod が起動するたびに最新リリースを取得します。
- **プローブ:** レディネスは、データベースが応答するまでレプリカにトラフィックが届かないようにし、`timeoutSeconds: 5` によって、データベースが応答しなくなったときにプローブが失敗します。[ヘルスとモニタリング](/ja/human-plugin/self-hosting/health-and-monitoring)を参照してください。
- **リソース:** 上記の値は、各レプリカに必要な最小値です。[ハードウェア要件](/ja/human-plugin/self-hosting/overview#hardware-requirements)を参照してください。

## 4. アクセスを制限する

バックエンドは `http://human-plugin.human-plugin.svc:8080` で Human Plugin に到達します。Human Plugin には独自の認証機能がないため、次の対応が必要です。

- Service を Ingress、`LoadBalancer`、`NodePort` で公開しないでください。
- NetworkPolicy で、バックエンドの namespace のみを許可してください。

```yaml title="network-policy.yaml"
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: human-plugin-allow-backend
  namespace: human-plugin
spec:
  podSelector:
    matchLabels: { app: human-plugin }
  policyTypes: [Ingress]
  ingress:
    - from:
        - namespaceSelector:
            matchLabels: { kubernetes.io/metadata.name: <your back-end namespace> }
      ports:
        - { port: 8080 }
```

Human Plugin 自身には、Unico への送信 HTTPS 通信と、お客様のデータベースへのアクセスが必要です。[ネットワークとセキュリティ](/ja/human-plugin/self-hosting/networking-and-security)を参照してください。

## 5. 検証する

```bash
kubectl -n human-plugin port-forward svc/human-plugin 8080:8080
```

別のターミナルで、レディネスを確認し、`http://127.0.0.1:8080` に対して[クイックスタート](/ja/human-plugin/get-started/quickstart#3-create-a-process)の作成リクエストを実行します。

```bash
curl --fail http://127.0.0.1:8080/health/ready
```

## ログ

```bash
kubectl -n human-plugin logs deploy/human-plugin --since=10m
```

## 更新

```bash
kubectl -n human-plugin rollout restart deployment/human-plugin
```

新しい Pod は最新のイメージを取得し、準備ができた時点で、古い Pod を1つずつ置き換えます。[運用](/ja/human-plugin/self-hosting/operations)を参照してください。