Search code examples
springspring-bootkubernetesjedisspring-data-redis

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


While deploying to kubernetes , redis connection is not able to establish connection because of jedis connection refused error.

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

Deployment yaml file:

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

---Sample Jedis code used in project:

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;
    }

Does anybody overcome this issue? TIA.


Solution

  • You need to first update your service to reflect:

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

    Once you have done so you can check whether or not your redis service is up and responding by using nmap. Here is an example using my nmap image:

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

    Also, make sure that both redis & your spring boot app are deployed to the same namespace. If not, you need to explicity define your hostname using . (i.e.: "redis-master.mynamespace").