Pod 重启 IP 就变——Nacos 上 k8s 的三个致命问题与完整解决方案


Deployment 部署 Nacos,重启后集群全员失联

团队决定把 Nacos 迁移到 k8s。我想着"三个 Deployment 不就完了"——结果踩了一串连环坑。

第一版配置:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nacos
spec:
  replicas: 3

三个 Pod 跑起来,控制台能看到所有节点。一切正常。直到有一天——Pod-0 被 k8s 调度器驱逐重启,新 Pod 拿到了新 IP。

cluster.conf 里还写着旧 IP。三个节点互相找不到对方,集群裂了。

后来发现三个问题:

  1. Pod IP 会变,重启后 cluster.conf 里存的是旧地址。
  2. 不能用 Deployment,需要稳定的 Pod 标识来维护集群拓扑。
  3. 自发现机制和 Readiness Probe 会打架——启动中就被 Service 路由了流量。

下面把 k8s 部署 Nacos 的正确姿势完整拆开。


第一个问题:Pod IP 不稳定

cluster.confnacos-pod-2(IP: 10.244.1.25)nacos-pod-1(IP: 10.244.1.24)nacos-pod-0(IP: 10.244.1.23)k8s Schedulercluster.confnacos-pod-2(IP: 10.244.1.25)nacos-pod-1(IP: 10.244.1.24)nacos-pod-0(IP: 10.244.1.23)k8s Scheduler集群正常运行nacos-pod-0 拿到新 IP新 IP: 10.244.1.58不通!纳维斯自杀式连接集群裂→只剩两个节点内部通信→一切仍看似正常驱逐 Pod-0重建 Pod-0读 cluster.conf → 10.244.1.23尝试连接 10.244.1.23(旧 IP)发现 Pod-0 失联发现 Pod-0 失联

Pod 重启后 IP 变化,cluster.conf 里的旧 IP 失效。如果两个以上节点同时受此影响——集群完全崩溃。

解决:StatefulSet + Headless Service

StatefulSet 给每个 Pod 分配固定网络标识

nacos-0.nacos-headless.nacos.svc.cluster.local
nacos-1.nacos-headless.nacos.svc.cluster.local
nacos-2.nacos-headless.nacos.svc.cluster.local

Pod 重启后域名不变,即使 IP 变了。

# headless-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: nacos
spec:
  clusterIP: None          # Headless Service:不分配 ClusterIP
  selector:
    app: nacos
  ports:
    - name: http
      port: 8848
      targetPort: 8848
    - name: grpc-client
      port: 9848
      targetPort: 9848
    - name: grpc-cluster
      port: 9849
      targetPort: 9849

坑1:不能用普通 Service,必须 Headless。 普通 Service 有 ClusterIP,所有流量经过它代理,Pod 不知道自己的域名。Headless Service(clusterIP: None)让每个 Pod 获得独立 DNS 记录。


第二个问题:Deployment 不适合有状态集群

Deployment 管的是"无状态副本"——Pod 从哪个节点启动无所谓,名称随机,IP 随机。但集群模式需要:

  • 启动顺序可控(先启动的当 Leader)
  • 每个节点有固定身份标识
  • 滚动更新时知道"现在重启的是哪个"

StatefulSet 完美匹配这三个需求:

StatefulSet 保障

固定 Pod 名称
nacos-0 / nacos-1 / nacos-2

有序启动
nacos-0 → nacos-1 → nacos-2

有序终止
nacos-2 → nacos-1 → nacos-0

独立 PVC
每个 Pod 绑定专属存储

cluster.conf 用 DNS 名
重启后不丢节点

避免脑裂:第一个 Pod
先成为 Leader

优雅下线:从集群
中正常退出

StatefulSet 四大保障:固定名称、有序启动、逆序终止、独立存储。每一项都命中集群部署的痛点。

完整 StatefulSet 配置

# statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: nacos
spec:
  serviceName: nacos-headless    # 绑定 Headless Service
  replicas: 3
  podManagementPolicy: Parallel  # 并行启动(非严格有序,但第一个先启)
  selector:
    matchLabels:
      app: nacos
  template:
    metadata:
      labels:
        app: nacos
    spec:
      # 反亲和:三个 Pod 尽量分布在不同节点上
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  app: nacos
              topologyKey: kubernetes.io/hostname
      containers:
        - name: nacos
          image: nacos/nacos-server:v2.3.2
          env:
            - name: MODE
              value: "cluster"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: NACOS_SERVERS
              value: "nacos-0.nacos-headless.nacos.svc.cluster.local:9849 nacos-1.nacos-headless.nacos.svc.cluster.local:9849 nacos-2.nacos-headless.nacos.svc.cluster.local:9849"
            - name: SPRING_DATASOURCE_PLATFORM
              value: "mysql"
            - name: MYSQL_SERVICE_HOST
              value: "mysql.nacos.svc.cluster.local"
            - name: MYSQL_SERVICE_PORT
              value: "3306"
            - name: MYSQL_SERVICE_DB_NAME
              value: "nacos_config"
            - name: MYSQL_SERVICE_USER
              valueFrom:
                secretKeyRef:
                  name: nacos-mysql-secret
                  key: username
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: nacos-mysql-secret
                  key: password
          ports:
            - containerPort: 8848
              name: http
            - containerPort: 9848
              name: grpc-client
            - containerPort: 9849
              name: grpc-cluster
          readinessProbe:
            httpGet:
              path: /nacos/v1/console/health/readiness
              port: 8848
            initialDelaySeconds: 30
            periodSeconds: 10
            failureThreshold: 5
          livenessProbe:
            httpGet:
              path: /nacos/v1/console/health/liveness
              port: 8848
            initialDelaySeconds: 60
            periodSeconds: 20
            failureThreshold: 3

第三个问题:Readiness Probe 与启动时间打架

坑2:Readiness Probe 太早探活。 Nacos 集群启动需要 30~60 秒(初始化数据库 + 选举)。如果 initialDelaySeconds 设太短,Pod 还没准备好就被打入 Service 后端,请求直接超时。

# 推荐值(生产环境)
readinessProbe:
  httpGet:
    path: /nacos/v1/console/health/readiness
    port: 8848
  initialDelaySeconds: 60     # 给足启动时间
  periodSeconds: 10
  failureThreshold: 5         # 容忍偶尔超时

livenessProbe:
  httpGet:
    path: /nacos/v1/console/health/liveness
    port: 8848
  initialDelaySeconds: 120    # 比 readiness 更晚
  periodSeconds: 20
  failureThreshold: 3

Nacos 内部有两个健康端点:

端点含义探活类型
/nacos/v1/console/health/readiness服务是否准备好接收请求Readiness Probe
/nacos/v1/console/health/liveness进程是否存活Liveness Probe

如果 Liveness Probe 失败,k8s 会杀死 Pod 重建。Readiness 失败只是暂时不路由流量。

坑3:Liveness Probe 设置太激进。 选举期间 Nacos 可能短暂不响应,不要因为一两次失败就 kill 容器。


对外暴露:集群内外双通道

k8s 集群外

k8s 集群内

直连

通过 NodePort/LoadBalancer

业务 Pod

Nacos Headless Service
域名:nacos-headless.nacos

nacos-0

nacos-1

nacos-2

集群外客户端

Nacos 对公 Service

# service-for-external.yaml
# 给集群外客户端和控制台访问
apiVersion: v1
kind: Service
metadata:
  name: nacos-external
  namespace: nacos
spec:
  type: NodePort           # 或 LoadBalancer
  selector:
    app: nacos
  ports:
    - name: http
      port: 8848
      targetPort: 8848
      nodePort: 30848      # 对外暴露的端口

两套 Service:Headless 给集群内 Pod 直连(固定 DNS),NodePort/LoadBalancer 给集群外客户端和控制台访问。


ConfigMap 统一管理配置

# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-config
  namespace: nacos
data:
  application.properties: |
    spring.sql.init.platform=mysql

    db.num=1
    db.url.0=jdbc:mysql://mysql.nacos.svc.cluster.local:3306/nacos_config?useSSL=false&serverTimezone=Asia/Shanghai

    nacos.core.auth.enabled=true
    nacos.core.auth.server.identity.key=nacos
    nacos.core.auth.server.identity.value=nacos

通过 volumeMounts 挂入 Pod:

volumeMounts:
  - name: nacos-conf
    mountPath: /home/nacos/conf/application.properties
    subPath: application.properties
volumes:
  - name: nacos-conf
    configMap:
      name: nacos-config

Helm 一键部署(懒人版)

# 添加 Nacos Helm 仓库
helm repo add nacos https://nacos-group.github.io/nacos-helm
helm repo update

# 一键安装
helm install nacos nacos/nacos \
  --namespace nacos \
  --create-namespace \
  --set global.mode=cluster \
  --set replicaCount=3 \
  --set mysql.enabled=true \
  --set mysql.mysqlUser=nacos \
  --set mysql.mysqlPassword=Nacos@2024

# 验证
kubectl get pods -n nacos -w
# nacos-0  nacos-1  nacos-2  Running

常见 k8s 部署问题速查

现象原因解决
Pod 反复 CrashLoopBackOffLiveness Probe 太激进initialDelaySeconds 设 120s+
集群成员列表只看到自己NACOS_SERVERS 写的是 Pod IP 不是 DNS改用 Headless Service DNS 名
注册的服务跨 Pod 查不到未共享 MySQL,各 Pod 用内置 Derby配置共享 MySQL
启动后 Pod 被 Service 路由但仍 503Readiness Probe 还没探活成功耐心等 30~60s,不要过早请求
podAntiAffinity 导致 Pending节点数少于 Pod 数改为 preferredDuringScheduling

总结

Nacos 上 k8s 的五个核心决策:

  1. StatefulSet 不是 Deployment:需要固定 Pod 名 + DNS 做集群发现。
  2. Headless Service 不是普通 ServiceclusterIP: None 给每个 Pod 独立 DNS。
  3. cluster.conf 用 DNS 名不是 IPnacos-0.nacos-headless.nacos.svc.cluster.local:9849
  4. Probe 要宽松initialDelaySeconds 给 60~120 秒,选举期间容忍失败。
  5. MySQL 是必须的:不能指望 Pod 本地的 Derby 来做集群数据同步。

从 Deployment 踩坑到 StatefulSet + Headless Service 组合,其实就改了三处:kind: StatefulSet + serviceName 绑定 + NACOS_SERVERS 换 DNS。但每一处都踩过坑才知道。


你们 Nacos 跑在什么环境?评论区留个数字:1=传统虚拟机部署 2=裸 Docker 容器 3=k8s StatefulSet 4=Nacos 托管服务(MSE/PolarisMesh 等)。顺带说一句你觉得 Nacos 上 k8s 最大的坑是什么。

更多推荐