Redis & Spring Boot integration with K8S error

10/26/2019

I have the following docker file:

FROM openjdk:8-jdk-alpine
ENV PORT 8094
EXPOSE 8094
RUN mkdir -p /app/
COPY build/libs/fqdn-cache-service.jar /app/fqdn-cache-service.jar
WORKDIR /build
ENTRYPOINT [ "sh" "-c", "java -jar /app/fqdn-cache-service.jar" ]

docker-compose.yaml file:

version: '3'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: fqdn-cache-service
    ports:
      - "8094:8094"
    links:
      - "db:redis"
  db:
    image: "redis:alpine"
    #hostname: redis
    ports:
      - "6378:6378"

deployment.yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: fqdn-cache-service
spec:
  selector:
    matchLabels:
      run: spike
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        run: spike
    spec:
      containers:
        - name: fqdn-cache-service
          imagePullPolicy: Never
          image: fqdn-cache-service:latest
          ports:
            - containerPort: 8094
              protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  selector:
    matchLabels:
      run: spike
  replicas: 1
  template:
    metadata:
      labels:
        run: spike
    spec:
      hostname: redis
      containers:
        - name: redis
          image: redis:alpine
          ports:
            - containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
  name: fqdn-cache-service
  labels:
    run: spike
spec:
  type: NodePort
  ports:
    - port: 8094
      nodePort: 30001
  selector:
    run: spike
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    run: spike
    app: redis
spec:
  type: NodePort
  ports:
    - port: 6379
      nodePort: 30002
  selector:
    run: spike

And the cluster info ip is 127.0.0.1. I'm using microk8s over ubuntu OS. If I request for get by and ID (127.0.0.1/webapi/users/1) I get the error:

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

Although on regular java application with redis or dockerize spring boot with redis it's working.

Any help why this is happened?

This is the configuration of the spring boot:

@Configuration
public class ApplicationConfig {
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName("127.0.0.1");
        factory.setPort(30001);
        factory.setUsePool(true);
        return factory;
    }

    @Bean
    RedisTemplate redisTemplate() {
        RedisTemplate<String, FqdnMapping> redisTemplate = new RedisTemplate<String, FqdnMapping>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        return redisTemplate;
    }
}

The issue also happenes if the host name is localhost and\or the port is 6379...

Thanks!

-- Shay Zambrovski
docker
kubernetes
redis
spring-boot

1 Answer

10/27/2019

When you're running in a container, 127.0.0.1 usually refers to the container itself, not to the host the container is running on. If you're trying to connect to a service, try using its name and port: "redis" on port 6379 and "fqdn-cache-service" on 8094.

-- Burak Serdar
Source: StackOverflow