Kubernetes
在 ClusterIP Service 之后运行两个或更多副本,仅可从您的后端访问。
前提条件
- 一个 Kubernetes 集群,以及对其的
kubectl访问权限。 - 一个专供 Human Plugin 使用、且可从集群访问的 PostgreSQL 数据库。请参阅前提条件与访问权限。
- 由 Unico 提供的镜像仓库凭据和访问密钥。
下面的所有资源都放在一个独立的命名空间中:
kubectl create namespace human-plugin
1. 镜像仓库拉取 Secret
kubectl -n human-plugin create secret docker-registry unico-registry \
--docker-server=pkg.unico.run \
--docker-username="<username>" \
--docker-password="<token>"
2. 设置 Secret
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。如果您使用其他工具管理 Secret(External Secrets、Sealed Secrets、密钥保管库),请用它创建相同的键。切勿提交包含值的 Secret 清单。
3. Deployment 和 Service
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 }
kubectl apply -f human-plugin.yaml
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会在数据库不再响应时使其失败。请参阅健康检查与监控。 - 资源: 上述值是每个副本的最低要求。请参阅硬件要求。
4. 限制访问
您的后端通过 http://human-plugin.human-plugin.svc:8080 访问 Human Plugin。Human Plugin 自身没有身份验证,因此:
- 不要通过 Ingress、
LoadBalancer或NodePort暴露该 Service; - 使用 NetworkPolicy 仅允许您后端所在的命名空间访问。
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 连接,并需要访问您的数据库。请参阅网络与安全。
5. 验证
kubectl -n human-plugin port-forward svc/human-plugin 8080:8080
在另一个终端中,检查就绪状态,并针对 http://127.0.0.1:8080 运行快速入门中的创建请求:
curl --fail http://127.0.0.1:8080/health/ready
日志
kubectl -n human-plugin logs deploy/human-plugin --since=10m
更新
kubectl -n human-plugin rollout restart deployment/human-plugin
新的 Pod 会拉取最新镜像,并在就绪后逐个替换旧的 Pod。请参阅运维。