K8s集群中多客户端与Hazelcast实例通信配置问题咨询
Hey there! Let's get your Hazelcast client talking properly to the 2 Hazelcast instances running in your K8s cluster. Here's a breakdown of what you need to do and key considerations:
1. Use the LoadBalancer Service's External Address for Client Configuration
Your hazelcast-service is a LoadBalancer type, which means it gets an external IP (or a cloud-provided DNS name) to route traffic to your Hazelcast pods.
To configure your client correctly:
- First, get the external address of your service by running this command in your K8s terminal:
Look for thekubectl get services hazelcast-serviceEXTERNAL-IPcolumn – this is the address your client needs to target. - Update your client code to use this address with the exposed port
10236:
If your cloud provider assigned a DNS name instead of a static IP, use that name (e.g.,clientConfig.getNetworkConfig().addAddress("*YOUR_LOAD_BALANCER_EXTERNAL_IP*:10236");*hazelcast-loadbalancer.example.com*:10236) instead of the IP.
2. Verify Service-Pod Matching
Double-check that your Hazelcast pods have the label app: hazelcast – this is what your service's selector uses to find and route traffic to them. You can confirm this with:
kubectl get pods --show-labels
If the labels don't match, update either the pod deployment's labels or the service's selector to align.
3. Optimizations for Different Client Types
- Internal K8s clients: If your client app is also running inside the same K8s cluster, you don't need to use the LoadBalancer's external address. Instead, use the service's internal DNS name for a more efficient connection:
(ReplaceclientConfig.getNetworkConfig().addAddress("*hazelcast-service.default.svc.cluster.local*:10236");defaultwith your namespace if you're not using the default one.) - Connection Resilience: Since you have 2 Hazelcast members, the LoadBalancer will handle distributing traffic between them automatically. For extra resilience, you can also configure the client to retry connections or use connection pooling – check Hazelcast's client docs for built-in settings like
connectionTimeoutorretryAttempts.
4. Firewall & Access Checks
Make sure that:
- The LoadBalancer's
10236port is open to your client's network (if connecting from outside K8s). Most cloud providers let you configure firewall rules for LoadBalancer services – verify those rules allow inbound traffic on port 10236. - Your Hazelcast pods are listening on port
5701(which they do by default) – your service'stargetPortis already set to this, so that's covered as long as the pods are configured correctly.
Quick Verification Step
After updating your client config, add a quick check in your code to confirm the connection is working and that the client can see all cluster members:
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig); System.out.println("Connected to Hazelcast cluster with " + client.getCluster().getMembers().size() + " members");
This should print "Connected to Hazelcast cluster with 2 members" if everything is set up right.
内容的提问来源于stack exchange,提问作者Michał S

