K8s 部署
本文说明 ovra-zero 使用 Helm 部署到标准 Kubernetes 集群的方式。这里不再包含 k3d 本地集群细节;本地 k3d 验证请查看 k3d 部署 目录。
准备工作
| 工具 | 用途 |
|---|---|
| Kubernetes | 目标集群 |
| kubectl | 操作集群资源 |
| Helm | 安装和升级 Chart |
| 镜像仓库 | 存放应用镜像 |
| MySQL | 推荐使用外部数据库 |
Chart 目录:
deploy/helm/ovra-zero
默认部署内容:
| 组件 | Kubernetes 资源 | 默认规模 |
|---|---|---|
| auth | Deployment + Service | 1 |
| system | Deployment + Service | 1 |
| demo | Deployment + Service | 1 |
| etcd | StatefulSet + Service | 3 |
| Redis Cluster | StatefulSet + 初始化 Job | 3 |
| MySQL | 内置 Deployment 或外部 EndpointSlice | 1 |
| Ingress | Ingress | 1 |
构建并推送镜像
生产或共享集群中,建议将镜像推送到正式镜像仓库:
mkdir -p .deploy/k8s/bin
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags='-s -w' -tags no_k8s -o .deploy/k8s/bin/app-auth app/auth/auth.go
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags='-s -w' -tags no_k8s -o .deploy/k8s/bin/app-system app/system/system.go
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags='-s -w' -tags no_k8s -o .deploy/k8s/bin/app-demo app/demo/demo.go
可以复用 deploy/k3d/Dockerfile 构建统一应用镜像:
cp .deploy/k8s/bin/* .deploy/k3d/bin/
docker build --platform linux/amd64 -t registry.example.com/ovra-zero:v1.0.0 -f deploy/k3d/Dockerfile .
docker push registry.example.com/ovra-zero:v1.0.0
如果目标节点是 ARM64,将 GOARCH 和 --platform 改为 arm64。
安装 Chart
使用外部 MySQL
推荐生产环境使用外部 MySQL。安装时关闭内置 MySQL:
helm upgrade --install ovra-zero deploy/helm/ovra-zero \
--namespace ovra-zero \
--create-namespace \
--wait \
--timeout 5m \
--set image.repository=registry.example.com/ovra-zero \
--set image.tag=v1.0.0 \
--set mysql.internal.enabled=false \
--set mysql.external.enabled=true \
--set mysql.external.ip=<MYSQL_IP> \
--set ingress.className=nginx \
--set ingress.host=api.example.com
当前 Chart 的外部 MySQL 通过 EndpointSlice 指向 IP 地址。如果你的生产数据库使用域名、云数据库私网地址或 Secret 管理连接信息,建议基于 Chart 模板进一步调整。
使用内置 MySQL
开发测试可以启用内置 MySQL:
helm upgrade --install ovra-zero deploy/helm/ovra-zero \
--namespace ovra-zero \
--create-namespace \
--wait \
--timeout 5m \
--set image.repository=registry.example.com/ovra-zero \
--set image.tag=v1.0.0 \
--set mysql.internal.enabled=true \
--set mysql.external.enabled=false
caution
内置 MySQL 当前使用 emptyDir,数据不会长期持久化,只适合开发测试。
核心配置
配置文件:
deploy/helm/ovra-zero/values.yaml
镜像:
image:
repository: registry.example.com/ovra-zero
tag: v1.0.0
pullPolicy: IfNotPresent
Redis:
redis:
enabled: true
password: Pl@1221view
cluster:
enabled: true
replicas: 3
replicasPerMaster: 0
etcd:
etcd:
enabled: true
replicas: 3
Ingress:
ingress:
enabled: true
className: nginx
host: api.example.com
应用配置模板位于:
deploy/helm/ovra-zero/files/config/*.gotmpl
Helm 会渲染 ConfigMap ovra-zero-config 并挂载到应用容器。
验证部署
查看 release:
helm status ovra-zero -n ovra-zero
helm history ovra-zero -n ovra-zero
查看资源:
kubectl -n ovra-zero get pods,svc,statefulset,ingress,endpointslice
等待应用启动:
kubectl -n ovra-zero rollout status deployment/auth --timeout=5m
kubectl -n ovra-zero rollout status deployment/system --timeout=5m
kubectl -n ovra-zero rollout status deployment/demo --timeout=5m
验证 etcd:
kubectl -n ovra-zero exec etcd-0 -- etcdctl \
--endpoints=http://etcd-0.etcd-headless.ovra-zero.svc.cluster.local:2379,http://etcd-1.etcd-headless.ovra-zero.svc.cluster.local:2379,http://etcd-2.etcd-headless.ovra-zero.svc.cluster.local:2379 \
member list
验证 Redis:
kubectl -n ovra-zero exec redis-0 -- redis-cli -a 'Pl@1221view' cluster info
验证入口:
curl -sS http://api.example.com/auth/code
升级、回滚与卸载
升级:
helm upgrade ovra-zero deploy/helm/ovra-zero \
--namespace ovra-zero \
--wait \
--timeout 5m
回滚:
helm history ovra-zero -n ovra-zero
helm rollback ovra-zero <REVISION> -n ovra-zero
卸载:
helm uninstall ovra-zero -n ovra-zero
生产建议
- MySQL 使用外部高可用数据库。
- Redis 和 etcd 使用 PVC,并配置备份策略。
- 密码、JWT secret、数据库连接信息使用 Kubernetes Secret。
- Ingress 使用正式域名和 TLS。
- 设置
resources requests/limits。 - 增加日志采集、指标监控、告警和备份巡检。
- Redis 生产建议至少 6 节点:3 master + 3 replica。