---
title: Kubernetes
description: 使用 Deployment、ClusterIP Service、用于存放设置的 Secret 以及用于 Unico 镜像仓库的拉取 Secret,在 Kubernetes 上运行 Human Plugin。
canonical: https://developer.unico.io/zh-CN/human-plugin/self-hosting/kubernetes
locale: zh-CN
generated_by: markdown-export
---

# Kubernetes

在 ClusterIP Service 之后运行两个或更多副本,仅可从您的后端访问。

<a id="prerequisites"></a>

## 前提条件

- 一个 Kubernetes 集群,以及对其的 `kubectl` 访问权限。
- 一个专供 Human Plugin 使用、且可从集群访问的 PostgreSQL 数据库。请参阅[前提条件与访问权限](/zh-CN/human-plugin/get-started/prerequisites)。
- 由 Unico 提供的镜像仓库凭据和访问密钥。

下面的所有资源都放在一个独立的命名空间中:

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

<a id="1-registry-pull-secret"></a>

## 1. 镜像仓库拉取 Secret

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

<a id="2-settings-secret"></a>

## 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>'
```

仅当您的数据库与[配置](/zh-CN/human-plugin/self-hosting/configuration)中的默认值不同时,才添加 `POSTGRES_ADDRESS_PORT`、`POSTGRES_DATABASE` 或 `POSTGRES_SSL_MODE`。如果您使用其他工具管理 Secret(External Secrets、Sealed Secrets、密钥保管库),请用它创建相同的键。切勿提交包含值的 Secret 清单。

<a id="3-deployment-and-service"></a>

## 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`:** 各副本共享同一个数据库,因此您可以按需运行任意数量的副本。两个副本可在其中一个重启时保持 Human Plugin 可用。
- **`terminationGracePeriodSeconds: 60`:** Human Plugin 在停止时最多会等待 55 秒,以处理正在进行的请求。Kubernetes 的默认值 30 秒可能会中断一次创建操作。
- **`imagePullPolicy: Always`:** 使用 `latest` 标签时,每个新的 Pod 都会拉取最新版本。
- **探针:** 就绪探针会让流量在副本的数据库有响应之前不会到达该副本,而 `timeoutSeconds: 5` 会在数据库不再响应时使其失败。请参阅[健康检查与监控](/zh-CN/human-plugin/self-hosting/health-and-monitoring)。
- **资源:** 上述值是每个副本的最低要求。请参阅[硬件要求](/zh-CN/human-plugin/self-hosting/overview#hardware-requirements)。

<a id="4-restrict-access"></a>

## 4. 限制访问

您的后端通过 `http://human-plugin.human-plugin.svc:8080` 访问 Human Plugin。Human Plugin 自身没有身份验证,因此:

- 不要通过 Ingress、`LoadBalancer` 或 `NodePort` 暴露该 Service;
- 使用 NetworkPolicy 仅允许您后端所在的命名空间访问。

```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 连接,并需要访问您的数据库。请参阅[网络与安全](/zh-CN/human-plugin/self-hosting/networking-and-security)。

<a id="5-validate"></a>

## 5. 验证

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

在另一个终端中,检查就绪状态,并针对 `http://127.0.0.1:8080` 运行[快速入门](/zh-CN/human-plugin/get-started/quickstart#3-create-a-process)中的创建请求:

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

<a id="logs"></a>

## 日志

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

<a id="update"></a>

## 更新

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

新的 Pod 会拉取最新镜像,并在就绪后逐个替换旧的 Pod。请参阅[运维](/zh-CN/human-plugin/self-hosting/operations)。