You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker模拟Kubernetes环境下Hazelcast无法创建3节点集群问题求助

Hazelcast Kubernetes集群组建失败解决方案

根因梳理

你的集群无法正常组建的核心原因是Hazelcast的Kubernetes服务发现配置未生效,目前配置仅开启了发现策略开关,没有指定任何可用于定位集群内其他Hazelcast实例的查询规则,导致每个Pod只能发现自身,无法找到另外两个副本。

修复步骤

  • 第一步:修改hazelcast.xml配置,取消发现策略的属性注释,将服务DNS替换为你实际部署的Service地址,正确配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast
    xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.7.xsd"
    xmlns="http://www.hazelcast.com/schema/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <properties>
        <property name="hazelcast.discovery.enabled">true</property>
    </properties>
    <network>
        <join>
            <multicast enabled="false" />
            <tcp-ip enabled="false"/>
            <discovery-strategies>
                <discovery-strategy enabled="true"
                    class="com.hazelcast.kubernetes.HazelcastKubernetesDiscoveryStrategy">
                    <properties>
                        <!-- 替换为你实际的服务 endpoint DNS,格式为<服务名>.<命名空间>.endpoints.cluster.local -->
                        <property name="service-dns">test-service.default.endpoints.cluster.local</property>
                        <property name="service-dns-timeout">10</property>
                    </properties>
                </discovery-strategy>
              </discovery-strategies>
        </join>
    </network>
</hazelcast>
  • 第二步:如果你选择使用Kubernetes API标签匹配的方式做服务发现(不需要配置DNS),需要额外配置RBAC权限,给Pod对应的ServiceAccount授予访问集群资源的权限,配置如下:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: hazelcast-cluster-role
rules:
- apiGroups: [""]
  resources: ["endpoints", "pods"]
  verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: hazelcast-cluster-role-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: hazelcast-cluster-role
subjects:
- kind: ServiceAccount
  name: default
  namespace: default

对应修改hazelcast.xml的发现配置即可:

<properties>
    <property name="namespace">default</property>
    <property name="service-label-name">app</property>
    <property name="service-label-value">test</property>
</properties>
  • 第三步:重新打包应用镜像,确认修改后的配置文件被正确打包到镜像中,删除原有部署资源后重新应用配置:
kubectl delete -f <你的部署文件名>.yaml
kubectl apply -f <你的部署文件名>.yaml
  • 第四步:验证集群状态,查看任意一个Pod的Hazelcast日志,正常会输出包含3个成员的集群信息。

额外排查点

  • 确认你引入的hazelcast-kubernetes依赖版本和Hazelcast 3.7版本兼容,3.7版本对应1.x系列的发现插件,不要使用2.0及以上版本。
  • 进入Pod内执行DNS解析验证,确认K8s DNS服务正常:nslookup test-service.default.svc.cluster.local,正常会返回3个Pod IP。
  • 检查你的Docker Desktop Kubernetes环境是否开启了NodePort、DNS等核心组件,没有异常报错。

内容的提问来源于stack exchange,提问作者nee nee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 11:36:03