我有一个Redis群集,其中3个主服务器和3个从服务器在minikube中运行。
PS D:\redis\main\kubernetes-redis-cluster> kubectl exec -ti redis-1-2723908297-prjq5 -- /bin/bash
root@redis-1:/data# redis-cli -p 7000 -c
127.0.0.1:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:9131
cluster_stats_messages_pong_sent:9204
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:18338
cluster_stats_messages_ping_received:9202
cluster_stats_messages_pong_received:9134
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:18338
127.0.0.1:7000> cluster nodes
de9a4780d93cb7eab8b77abdaaa96a081adcace3 172.17.0.7:7000@17000 slave ee4deab0525d054202e612b317924156ff587021 0 15099603
02577 4 connected
b3a3c05225e0a7fe8ae683dd4316e724e7a7daa6 172.17.0.5:7000@17000 myself,master - 0 1509960301000 2 connected 5461-10922
8bebd48850ec77db322ac51501d59314582865a3 172.17.0.6:7000@17000 master - 0 1509960302000 3 connected 10923-16383
ee4deab0525d054202e612b317924156ff587021 172.17.0.4:7000@17000 master - 0 1509960303479 1 connected 0-5460
28a1c75e9976bc375a13e55160f2aae48defb242 172.17.0.8:7000@17000 slave b3a3c05225e0a7fe8ae683dd4316e724e7a7daa6 0 15099603
02477 5 connected
32e9de12324b8571a6256285682fa066d79161ab 172.17.0.9:7000@17000 slave 8bebd48850ec77db322ac51501d59314582865a3 0 15099603
02000 6 connected
127.0.0.1:7000>
我可以通过redis-cli设置/获取键/值而不会出现任何问题。 现在我尝试从一个从eclipse运行的简单java程序连接到redis集群。
我知道我需要转发端口。我执行了以下命令。
kubectl port-forward redis-0-334270214-fd4k0 7000:7000
现在我执行以下程序。
public class Main {
public static void main(String[] args) {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(500);
config.setMaxWaitMillis(60000);
config.setTestOnBorrow(true);
config.setMaxWaitMillis(20000);
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("192.168.99.100", 31695));
JedisCluster jc = new JedisCluster(jedisClusterNode, config);
jc.set("prime", "1 is primeee");
String keyVal = jc.get("prime");
System.out.println(keyVal);
}
}
然后我得到以下异常。
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:53)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:66)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:116)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:141)
at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:31)
at redis.clients.jedis.JedisCluster.set(JedisCluster.java:103)
at com.redis.main.Main.main(Main.java:25)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:819)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:429)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 10 more
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at redis.clients.jedis.Connection.connect(Connection.java:184)
... 17 more
Redis服务也已创建并正在运行..
PS C:\Users\rootmn> kubectl get services
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.0.0.1 <none> 443/TCP 11h
redis-0 10.0.0.105 <pending> 7000:31695/TCP,17000:31596/TCP 10h
redis-1 10.0.0.7 <pending> 7000:30759/TCP,17000:30646/TCP 10h
redis-2 10.0.0.167 <pending> 7000:32591/TCP,17000:30253/TCP 10h
redis-3 10.0.0.206 <pending> 7000:31644/TCP,17000:31798/TCP 10h
redis-4 10.0.0.244 <pending> 7000:30186/TCP,17000:32701/TCP 10h
redis-5 10.0.0.35 <pending> 7000:30628/TCP,17000:32396/TCP 10h
Telnet到redis ip port工作正常。 我在这里做错了什么。什么会导致这个问题?
答案 0 :(得分:0)
您需要使用Nodeport类型定义服务。从下面的yaml文件更新selection
部分以匹配您的redis pod标签。
apiVersion: v1
kind: Service
metadata:
name: redis-master-nodeport
labels:
app: redis
spec:
ports:
- port: 7000
selector:
app: redis
role: master
tier: backend
type: NodePort
创建服务kubectl.exe create -f redis-master-service.yaml
然后检查服务它会给你端口号。 例如
kubectl.exe get svc redis-master-nodep
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-master-nodep 10.0.0.62 <nodes> 6379:30277/TCP 16s
现在使用minkikube ip:30277端口,您可以连接到redis。
希望这有帮助