部署到 Kubernetes
Kubernetes 提供了声明式的容器编排能力,适合数据墙DBW 的多实例水平扩展和自动化运维。本指南假设你已有一个运行中的 Kubernetes 集群和 kubectl 工具。
架构概览
text
用户 → Ingress / Service (80/443)
→ 负载均衡
→ Pod 1 (DAB)
→ Pod 2 (DAB)
→ Pod 3 (DAB)
→ Redis (L2 缓存, 可选)
→ 数据库Kubernetes 通过 Service 负载均衡将请求分发到多个 Pod,配合存活和就绪探测实现自动恢复和滚动更新。
第一步:创建 ConfigMap
将配置文件存入 ConfigMap,实现配置与镜像分离:
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: dab-config
data:
dab-config.json: |
{
"$schema": "https://github.com/Azure/data-api-builder/releases/latest/download/dab.draft.schema.json",
"data-source": {
"database-type": "mssql",
"connection-string": "@env('SQL_CONN_STRING')"
},
"runtime": {
"rest": { "enabled": true },
"graphql": { "enabled": true },
"host": { "mode": "production" },
"telemetry": {
"log-level": {
"default": "Warning"
}
}
},
"entities": {
"Book": {
"source": { "object": "dbo.Books", "type": "table" },
"permissions": [
{ "role": "anonymous", "actions": ["read"] }
]
}
}
}bash
kubectl apply -f configmap.yamlNOTE
配置文件中的连接字符串使用了 @env('SQL_CONN_STRING')。Kubernetes 通过 Secret 注入环境变量来提供实际值,不在 ConfigMap 中写入密码。
第二步:创建 Secret
将敏感信息存入 Secret:
yaml
apiVersion: v1
kind: Secret
metadata:
name: dab-secrets
type: Opaque
stringData:
SQL_CONN_STRING: "Server=db-host,1433;Database=Library;User Id=app;Password=prod-password;Encrypt=true;"bash
kubectl apply -f secret.yamlIMPORTANT
生产环境中,推荐使用外部密钥管理服务(如 Vault、云平台密钥管理)配合 External Secrets Operator,而不是在 Secret YAML 中直接写入明文密码。
第三步:创建 Deployment
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: dab-api
labels:
app: dab-api
spec:
replicas: 3
selector:
matchLabels:
app: dab-api
template:
metadata:
labels:
app: dab-api
spec:
containers:
- name: dab-api
image: mcr.microsoft.com/azure-databases/data-api-builder:latest
ports:
- containerPort: 5000
env:
- name: SQL_CONN_STRING
valueFrom:
secretKeyRef:
name: dab-secrets
key: SQL_CONN_STRING
volumeMounts:
- name: config
mountPath: /App/dab-config.json
subPath: dab-config.json
resources:
requests:
cpu: "250m"
memory: "128Mi"
limits:
cpu: "1"
memory: "512Mi"
livenessProbe:
httpGet:
path: /health
port: 5000
initialDelaySeconds: 10
periodSeconds: 30
readinessProbe:
httpGet:
path: /health
port: 5000
initialDelaySeconds: 5
periodSeconds: 10
volumes:
- name: config
configMap:
name: dab-config关键配置说明
| 配置项 | 说明 |
|---|---|
replicas: 3 | 启动 3 个实例,实现高可用 |
env + secretKeyRef | 从 Secret 注入连接字符串环境变量 |
volumeMounts + subPath | 将 ConfigMap 中的 dab-config.json 挂载为文件 |
livenessProbe | 进程存活探测,失败后重启 Pod |
readinessProbe | 服务就绪探测,通过后才分配流量 |
resources | CPU 和内存的请求/限制 |
bash
kubectl apply -f deployment.yaml第四步:创建 Service
yaml
apiVersion: v1
kind: Service
metadata:
name: dab-api
spec:
selector:
app: dab-api
ports:
- name: http
port: 80
targetPort: 5000
type: ClusterIPbash
kubectl apply -f service.yamlClusterIP 类型仅在集群内部可访问。如需外部访问,改用 LoadBalancer 或配置 Ingress。
水平扩展
手动扩缩
bash
kubectl scale deployment dab-api --replicas=5自动扩缩(HPA)
基于 CPU 使用率自动调整副本数:
yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: dab-api-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: dab-api
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70bash
kubectl apply -f hpa.yaml配合 Redis 二级缓存
多实例场景下启用 Redis 实现跨 Pod 缓存共享:
配置文件中添加 L2 配置:
json
{
"runtime": {
"cache": {
"enabled": true,
"ttl-seconds": 30,
"level-2": {
"enabled": true,
"provider": "redis",
"connection-string": "@env('REDIS_CONN_STR')"
}
}
}
}在 Secret 中添加 Redis 连接信息:
yaml
stringData:
SQL_CONN_STRING: "..."
REDIS_CONN_STR: "redis-service:6379"滚动更新
修改 ConfigMap 或镜像后执行滚动更新,逐个替换 Pod 而不中断服务:
bash
# 更新 ConfigMap
kubectl apply -f configmap.yaml
# 触发滚动重启(ConfigMap 变更不会自动触发)
kubectl rollout restart deployment dab-api
# 观察更新进度
kubectl rollout status deployment dab-apiIngress 配置
通过 Ingress 将服务暴露到集群外部:
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: dab-api-ingress
spec:
rules:
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: dab-api
port:
number: 80配合 cert-manager 自动管理 TLS 证书。
资源建议
| 场景 | replicas | CPU request | Memory request |
|---|---|---|---|
| 开发测试 | 1 | 250m | 128Mi |
| 低流量生产 | 2–3 | 500m | 256Mi |
| 中流量生产 | 3–5 | 1 | 512Mi |
| 高流量生产 | 5+ | 2 | 1Gi |
实际值应根据监控数据调整。关注数据库连接池消耗——实例数越多,数据库侧的连接数越多,需要适当调整数据库的最大连接数。
部署检查清单
| 检查项 | 命令 |
|---|---|
| Pod 是否正常运行 | kubectl get pods |
| 日志是否正常 | kubectl logs -l app=dab-api --tail=50 |
| Service 端点是否就绪 | kubectl get endpoints dab-api |
| 健康检查是否通过 | kubectl exec <pod> -- curl -f http://localhost:5000/health |
| 从集群内测试 API | kubectl run test --rm -it --image=curlimages/curl -- curl http://dab-api/api/Book |
