Error with redis and nodejs in kubernetes

2/18/2020

I need to deploy web (angularjs), api (node.js) , redis. However, i am getting error reaching redis to api (node.js) using kubernetes.

[ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND redis redis:6379
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)

I tried multiple configuration but nothing worked.

const redis = new Redis('redis://redis:6379');
const redis = new Redis({ port: 6379, host: 'redis', connectTimeout: 10000 });
const client = redis.createClient(6379,"redis");

Also, since standard redis image dont have the redis.conf, I added the redis.conf and bind 0.0.0.0 in it, protected-mode yes. I am still getting the error.

I put all the containers in one deployment i.e, web, api, redis and created the service using type: load balancer. I am able to access the application but i am getting the [ioredis] error.

Kubernetes version: 1.15.7
Cloud being used: (aws) 
Installation method: kops
Host OS: ubuntu

Any ideas and suggestion to resolve the redis error. What is the best deployment strategy? i.e., how the web, api, redis to be distributed in k8s?

deployment yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
  labels:
    app: app1
    env: prod
spec:
  selector:
    matchLabels:
      app: app1
      env: prod
  replicas: 3
  template:
    metadata:
      labels:
        app: app1
        env: prod
    spec:
      containers:
      - name: web-angular-nginx
        image: xxxx.dkr.ecr.us-east-1.amazonaws.com/web
        ports:
        - containerPort: 8080
      - name: api-nodejs
        image: xxxx.dkr.ecr.us-east-1.amazonaws.com/api
        ports:
        - containerPort: 7000
      - name: xxxx.dkr.ecr.us-east-1.amazonaws.com/redis
        image: redis
        ports:
        - containerPort: 6379

Service yaml

apiVersion: v1
kind: Service
metadata:
  name: app-svc
  labels:
    app: app1
spec:
  ports:
  - port: 80
    nodePort: 30001
    protocol: TCP
  selector:
    app: app1
    env: prod
  type: LoadBalancer

Thanks

-- Rajspeeder
docker
kubernetes
node.js
redis

2 Answers

2/19/2020

ENOTFOUND is a connection issue - your node script is not able to connect to the Redis server at the specified address - check your redis server.

Add following line to your configuration:

const redis = require('ioredis');

Also try delete the networks and replace redis section by:

redis:
    image: redis:latest
    command: ["redis-server", "--bind", "redis", "--port", "6379"]

Edit NetworkManager.conf file:

$ vim /etc/NetworkManager/NetworkManager.conf

Comment this line:

#dns=dnsmasq

Finally

$ sudo service network-manager restart
$ sudo service docker restart

More infromation you can find here: redis-kubernetes, ENOTFOUND, redis-enotfound.

-- MaggieO
Source: StackOverflow

2/22/2020

The problem is fixed.

The mistake is i created all the containers in one pod. Create each container in separate pods and separate services solved the issue.

In Node.js, the app.js file is set to const Redis = new Redis({ host: 'redis' });

-- Rajspeeder
Source: StackOverflow