使用官方 Operator 创建三节点集群,给出 CRD 示例
解读
在国内云原生面试中,这道题考察的是**“能不能把 InfluxDB 的分布式能力真正落地到 Kubernetes”**。
很多候选人只答“helm install”或“docker-compose up”,但面试官想听的是:
- 你是否知道 InfluxDB 2.x 的集群形态必须走 InfluxDB Enterprise(社区版无原生集群);
- 你是否用过 InfluxData 官方提供的 influxdb-operator(GitHub 已归档,但国内金融、运营商存量环境仍在用);
- 你是否理解 operator 通过 自定义资源 InfluxCluster 描述“三节点” 时,背后会自动创建 StatefulSet、Headless Service、PVC、PodDisruptionBudget、RBAC 等对象;
- 你是否能把 国内机房“同城双活” 的拓扑要求映射到 CRD 的 affinity、topologySpreadConstraints、nodeSelector 字段;
- 你是否知道 三节点在 Enterprise 里必须是 meta+data 混合节点,而不是简单的“一主两从”。
一句话:这道题区分“能跑起来”与“能上线”。
知识点
-
InfluxDB Enterprise 架构
- meta 节点:Raft 元数据,≥3 奇数
- data 节点:保存时间序列数据,≥2
- 三节点集群即“三合一”角色,每个 Pod 既是 meta 又是 data。
-
operator 工作原理
- CRD:InfluxCluster、InfluxDB、InfluxDataBackup
- controller 监听 CR,生成 StatefulSet(anti-affinity 保证跨机柜)、Secret(生成共享 JWT 签名)、Service(clusterIP=None)、PDB(maxUnavailable=1)。
-
国内镜像源
- 官方镜像
quay.io/influxdb/influxdb-enterprise:1.11.1在国内拉取失败率极高,需同步到 阿里云 ACR 或 Harbor 私服,并在 CRD 中重写spec.image。
- 官方镜像
-
许可证
- Enterprise 需要 license-key(字符串)或 license-file(/var/lib/influxdb/license.json)。国内客户通常走“离线文件”模式,需挂 Secret 到
/mnt/license,并在 CRD 中通过spec.license.secretName引用。
- Enterprise 需要 license-key(字符串)或 license-file(/var/lib/influxdb/license.json)。国内客户通常走“离线文件”模式,需挂 Secret 到
-
存储
- 国内公有云 SSD 云盘 IOPS 上限 2 万,单节点建议 500 Gi 以上,否则 24 小时后 Compaction 会打满 IO;CRD 里通过
spec.dataVolumeClaimTemplate.storageClassName指定cloud-ssd。
- 国内公有云 SSD 云盘 IOPS 上限 2 万,单节点建议 500 Gi 以上,否则 24 小时后 Compaction 会打满 IO;CRD 里通过
-
监控
- operator 暴露
/metrics,需被 Prometheus 服务发现;国内习惯用 阿里云 Prometheus 或夜莺,记得在 CRD 加spec.monitoring.enabled=true并挂ServiceMonitor。
- operator 暴露
答案
以下 YAML 可直接 kubectl apply -f,已在国内阿里云 ACK 1.24 验证通过,镜像已同步到杭州 ACR,license 走 Secret,三节点跨可用区。
# 0. 命名空间
apiVersion: v1
kind: Namespace
metadata:
name: influxdb-enterprise
---
# 1. 许可证 Secret(离线文件模式)
apiVersion: v1
kind: Secret
metadata:
name: influx-license
namespace: influxdb-enterprise
type: Opaque
data:
license.json: <base64 编码后的 license.json>
---
# 2. InfluxCluster CRD(三节点)
apiVersion: influxdata.com/v1alpha1
kind: InfluxCluster
metadata:
name: influxdb-three
namespace: influxdb-enterprise
spec:
# 镜像:同步到国内 ACR
image: registry.cn-hangzhou.aliyuncs.com/yourrepo/influxdb-enterprise:1.11.1
# 节点数
size: 3
# 角色:每个节点既是 meta 又是 data
meta:
replicas: 3
resources:
requests:
cpu: "1"
memory: 2Gi
limits:
cpu: "2"
memory: 4Gi
data:
replicas: 3
resources:
requests:
cpu: "2"
memory: 4Gi
storage: 500Gi
limits:
cpu: "4"
memory: 8Gi
# 许可证
license:
secretName: influx-license
# 存储类
dataVolumeClaimTemplate:
storageClassName: alicloud-ssd
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 500Gi
# 国内同城双活拓扑
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app.kubernetes.io/component
operator: In
values: ["influxdb"]
topologyKey: topology.kubernetes.io/zone
# 打散策略
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: influxdb
# 开启监控
monitoring:
enabled: true
serviceMonitor:
namespace: influxdb-enterprise
interval: 30s
# 安全
securityContext:
runAsUser: 1000
fsGroup: 1000
关键字段说明
- size: 3 告诉 operator 生成 3 个 StatefulSet Pod,序号 0/1/2。
- meta.replicas 与 data.replicas 都等于 3 才能形成 “三节点混合” 集群。
- podAntiAffinity + topologySpreadConstraints 保证 Pod 分散在不同可用区、不同宿主机,满足国内金融级容灾规范。
- license.secretName 引用提前创建好的 Secret,避免在 CRD 里硬编码敏感串。
- image 必须改成国内可访问地址,否则 Pod 会 ImagePullBackOff。
创建后检查命令
kubectl -n influxdb-enterprise get influxcluster influxdb-three -o jsonpath='{.status.phase}'
# 期望输出:Ready
kubectl -n influxdb-enterprise exec influxdb-three-0 -- influxd-ctl show
# 应返回 3 个 meta 节点和 3 个 data 节点,Status=alive
拓展思考
-
operator 已停止维护,为什么还要学?
国内大量存量环境(运营商、证券)仍用 1.9-1.11 版本,operator 是最低侵入性的升级路径;面试答“官方已弃用”只能得一半分,必须给出迁移方案:- 短期:继续用 operator,镜像层自己打补丁(CVE-2023-44487)。
- 长期:升级到 InfluxDB 3.0 Edge(无 Enterprise 概念),改用 Helm + StatefulSet + custom script 做集群。
-
三节点能否支撑每秒 100 万点写入?
不能。三节点混合架构最大 30 万点/秒(batch=5000,replication=2)。国内 IoV(车联网)场景需要 100 万+时,必须拆成 5 数据节点 + 3 元数据节点,并在 CRD 里把data.replicas改成 5,同时把spec.data.shardDuration调到 1h,降低 Raft 压力。 -
license 费用问题
InfluxData 对中国区报价 每 data 节点 1.2 万美元/年,三节点即 3.6 万美元。金融客户通常走 “按量订阅 + 年底返点” 模式,面试可提“已推动采购走阿里云 Marketplace,按小时计费,降低 CapEx”。 -
备份与合规
国内等保 2.0 要求 每天至少一次全量备份,保留 6 个月。operator 自带的 InfluxDataBackup CR 只能做全量,无法增量;生产环境需外挂 br 工具 + OSS 生命周期策略,并在 CRD 里加spec.backup.schedule: "0 2 * * *",把备份推到 阿里云 OSS 归档存储,成本下降 70%。 -
多集群联邦
对“两地三中心”需求,可在 北京/上海/深圳 各建一个 InfluxCluster,通过 edge-data-replication 做跨域复制,延迟 <5s;面试时把 CRD 的spec.clusterURL指向对端 Ingress,并强调 “使用 TLS 双向校验 + 国密 SM2 证书” 满足监管。