Redis connect ECONNREFUSED in NodeJS in Kubernetes cluster

9/27/2020

I am deploying a NodeJS server with 3 replicas and connect to Redis to use Sub/Pub function.

NodeJS:

const redis = require('redis');
const REDISPORT = 6379;
const subscriber = redis.createClient(REDISPORT, 'redis-service.default.svc.cluster.local');
const publisher = redis.createClient(REDISPORT, 'redis-service.default.svc.cluster.local');

Error in all three NodeJS pods:

Error: Redis connection to redis-service.default.svc.cluster.local:6379 failed - connect ECONNREFUSED 10.120.14.77:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
Emitted 'error' event on RedisClient instance at:
    at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:341:14)
    at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:222:14)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '10.120.14.77',
  port: 6379
}

You can see when I use 'redis-service.default.svc.cluster.local' in NodeJS pod, it will get my Redis service ip 10.120.14.77:6379 automatically.

And my Redis service ip is the same as 10.120.14.77:6379 when I check with "kubectl get svc redis"

- kubectl get svc redis
redis-service   ClusterIP   10.120.14.77   <none>        6379/TCP   74s

- kubectl logs redis
27 Sep 2020 08:41:20.309 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

server-deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: car-server-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: car-server
    spec:
      containers:
        - name: car-server
          image: gcr.io/PROJECT_ID/IMAGE_SERVER:TAG
          ports:
            - containerPort: 8080
              name: nodejs-port
  selector:
    matchLabels:
      app: car-server

server-service.yml:

apiVersion: v1
kind: Service
metadata:
  name: car-server-service
spec:
  ports:
    - port: 8080
      protocol: TCP
  selector:
    app: car-server
  type: NodePort

redis-pod.yml:

apiVersion: v1
kind: Pod
metadata:
  name: redis
  labels:
    app: redis
spec:
  containers:
  - name: redis
    image: redis:5.0.4
    command:
      - redis-server
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379

redis-service.yml:

apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  ports:
    - port: 6379
      protocol: TCP
  selector:
    app: redis
  type: ClusterIP
-- potato
kubernetes
node.js
redis

1 Answer

9/27/2020

Attempting for several hours and finally fix it. It is a small mistake when I create kustomization.yml. Therefore, basically not relevant to Redis or NodeJS.

Previous wrong file: kustomization.yml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  app: my-app
bases:
- ./yaml

I put the commonLabels here and kustomize covered my Redis label and led to network error.

This is because I also used "app label" in my redis pod.

New file: kustomization.yml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ./yaml
-- potato
Source: StackOverflow