# AI Agent API 的 Deployment:滚动更新、三种探针、资源限制、非 root 运行、优雅退出 apiVersion: apps/v1 kind: Deployment metadata: name: agent-api labels: app.kubernetes.io/name: agent-api spec: replicas: 3 revisionHistoryLimit: 5 selector: matchLabels: app.kubernetes.io/name: agent-api strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 # 更新时最多多出 1 个 Pod maxUnavailable: 0 # 更新过程中可用 Pod 数不减少 template: metadata: labels: app.kubernetes.io/name: agent-api spec: terminationGracePeriodSeconds: 90 # 留时间让正在进行的流式回答结束 securityContext: runAsNonRoot: true runAsUser: 10001 containers: - name: api image: registry.example.com/agent-api:1.4.2 # 使用固定版本,不用 latest ports: - name: http containerPort: 8000 envFrom: - configMapRef: name: agent-api-config env: - name: LLM_API_KEY valueFrom: secretKeyRef: name: agent-api-secrets # 由运维或密钥管理系统创建,不在仓库中 key: llm-api-key resources: requests: cpu: "250m" memory: "256Mi" limits: memory: "512Mi" # 内存设上限防止拖垮节点;CPU 不设上限避免被限流(按团队规范) startupProbe: # 启动慢(加载模型配置、预热连接)时,避免被存活探针误杀 httpGet: path: /healthz port: http periodSeconds: 5 failureThreshold: 24 readinessProbe: # 未就绪时不接收流量(例如依赖的 Redis 不可用) httpGet: path: /readyz port: http periodSeconds: 5 failureThreshold: 3 livenessProbe: # 进程卡死时重启;不要检查外部依赖,否则依赖故障会引发全体重启 httpGet: path: /healthz port: http periodSeconds: 10 failureThreshold: 3 lifecycle: preStop: # 先等负载均衡摘除,再收到 SIGTERM。sleep 动作需要较新的 Kubernetes(1.30+,以官方文档为准),旧版本用 exec sleep sleep: seconds: 5 securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: ["ALL"]