在Kubernetes中用Redis部署一个Spring boot Application--Jedis Connection Refused错误

Deploying a Spring boot Application with Redis in Kubernetes--Jedis Connection Refused Error

部署到kubernetes时,由于jedis连接拒绝错误,redis连接无法建立连接。

"message": "Cannot get Jedis connection; nested exception is 
redis.clients.jedis.exceptions.JedisConnectionException: 
java.net.ConnectException: Connection refused (Connection refused)",

部署yaml文件:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redis-master
spec:
  selector:
    matchLabels:
      app: redis
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis-master
        image: gcr.io/google_containers/redis:e2e

        ports:
        - containerPort: 6379

        volumeMounts:
        - name: redis-storage
          mountPath: /data/redis

      volumes:
      - name: redis-storage
---
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  ports:
    - port: 6379
  selector:
    app: redis

---项目中使用的示例Jedis代码:

JedisConnectionFactory jedisConnectionFactoryUpdated() {

        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("redis-master");
        redisStandaloneConfiguration.setPort(6379);

        JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
        jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60));// 60s connection timeout

        JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(redisStandaloneConfiguration,
                jedisClientConfiguration.build());

        return jedisConFactory;
    }

有人解决这个问题吗? TIA.

您需要先更新您的服务以反映:

apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    app: redis

完成后,您可以使用 nmap 检查您的 redis 服务是否启动并响应。这是一个使用我的 nmap 图像的示例:

kubectl run --image=appsoa/docker-alpine-nmap --rm -i -t nm -- -Pn 6379 redis-master

此外,请确保将 redis 和您的 spring 启动应用程序部署到同一命名空间。如果没有,您需要使用 明确定义您的主机名。 (即:"redis-master.mynamespace")。