Docker 部署
Docker 部署
Section titled “Docker 部署”本指南展示如何将 Plumego 服务容器化、通过环境变量配置服务,以及将健康端点连接到 Kubernetes 式就绪探针。该模式适用于 Kubernetes、Docker Compose 或任何 OCI 兼容运行时。
健康端点的边界说明见 Health and Readiness。容器运行时依赖的优雅关闭模式见 Graceful Shutdown。
本指南涵盖的内容
Section titled “本指南涵盖的内容”- Plumego 服务的最小多阶段 Dockerfile
- 来自
env.example的环境变量命名约定 - 将
/healthz和/readyz接入存活和就绪探针 - 用于本地验证的 Docker Compose 配置
第 1 步 — 编写最小多阶段 Dockerfile
Section titled “第 1 步 — 编写最小多阶段 Dockerfile”在 builder 阶段构建 Go 二进制,然后只把二进制复制到最小运行时镜像中。
# syntax=docker/dockerfile:1FROM golang:1.26-alpine AS builderWORKDIR /srcCOPY go.mod go.sum ./RUN go mod downloadCOPY . .RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /out/service ./reference/standard-service
FROM gcr.io/distroless/static-debian12:nonrootCOPY --from=builder /out/service /serviceEXPOSE 8080ENTRYPOINT ["/service"]CGO_ENABLED=0 加 distroless/static 组合产生一个没有 shell、没有 libc、没有包管理器的镜像,二进制是唯一的可写表面。如果你的服务使用带 SQLite 或任何 cgo 依赖驱动的 x/data,请改用 gcr.io/distroless/base-debian12 并去掉 CGO_ENABLED=0。
第 2 步 — 通过环境变量配置
Section titled “第 2 步 — 通过环境变量配置”Plumego 服务从环境变量读取配置。规范变量名来自仓库根目录的 env.example。生产容器的最小配置集:
# 网络APP_ADDR=:8080
# 超时(毫秒)APP_READ_TIMEOUT_MS=30000APP_WRITE_TIMEOUT_MS=30000APP_IDLE_TIMEOUT_MS=60000APP_SHUTDOWN_TIMEOUT_MS=5000
# 并发APP_MAX_CONCURRENCY=256APP_QUEUE_DEPTH=512
# 安全APP_DEBUG=falseAUTH_TOKEN=<从 secrets manager 设置>通过 Docker --env-file、Kubernetes ConfigMap / Secret 或你的平台 secret 管理系统传递这些变量。永远不要把 secret 烘焙进镜像。
第 3 步 — 接入健康探针
Section titled “第 3 步 — 接入健康探针”参考服务已经暴露了 /healthz(存活)和 /readyz(就绪)。配置编排器之前先验证它们是否响应:
docker run --rm -p 8080:8080 myapp:latest &curl http://localhost:8080/healthz # → {"status":"ok", ...}curl http://localhost:8080/readyz # → {"ready":true, ...}两者在成功时都返回 JSON 和 200;当任何注册的 ComponentChecker 失败时,就绪端点返回 503。
第 4 步 — 配置 Kubernetes 探针
Section titled “第 4 步 — 配置 Kubernetes 探针”将 /healthz 映射到存活探针,将 /readyz 映射到就绪探针。存活探针的周期保持宽松——存活失败会触发 Pod 重启:
livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 5 periodSeconds: 30 failureThreshold: 3
readinessProbe: httpGet: path: /readyz port: 8080 initialDelaySeconds: 3 periodSeconds: 10 failureThreshold: 3在滚动部署期间,Kubernetes 在 Pod 的就绪探针失败时立即将其从负载均衡器轮询中移除。优雅关闭序列——信号 → drain → Shutdown——确保进行中的请求在 Pod 退出前完成。
第 5 步 — 用 Docker Compose 本地验证
Section titled “第 5 步 — 用 Docker Compose 本地验证”在推送镜像到注册中心之前,用 Docker Compose 本地测试完整容器配置:
services: api: build: . ports: - "8080:8080" environment: APP_ADDR: ":8080" APP_DEBUG: "false" APP_SHUTDOWN_TIMEOUT_MS: "5000" healthcheck: test: ["CMD", "wget", "-qO-", "http://localhost:8080/readyz"] interval: 10s timeout: 5s retries: 3 start_period: 5sdocker compose up --builddocker compose ps # 确认 health: healthydocker compose down这个模式带来什么
Section titled “这个模式带来什么”- 多阶段构建使典型 Plumego 服务的最终镜像体积在 10 MB 以内。
- 环境变量配置意味着同一个镜像可以在所有环境运行,无需重新构建。
- 独立的存活和就绪探针匹配 Plumego 健康模型:依赖失败时引流流量,而不是重启进程。
APP_SHUTDOWN_TIMEOUT_MS控制进程收到SIGTERM后等待进行中请求完成的时间,与 Kubernetes 的terminationGracePeriodSeconds对齐。
参考应用中的完整示例
Section titled “参考应用中的完整示例”参考服务提供了可直接使用的 Dockerfile 和 docker-compose.yml:
reference/standard-service/Dockerfile— 多阶段构建、distroless 最终镜像、非 root 用户reference/standard-service/docker-compose.yml— 本地 compose 配置,通过环境变量注入配置
本地运行验证:
git clone https://github.com/spcent/plumegodocker build -t plumego-ref ./reference/standard-servicedocker run -p 8080:8080 plumego-refcurl http://localhost:8080/healthz