Connecting Node js application with redis using Kubernetes

10/1/2021

I am trying to learn Kubernetes and started implementing my node js application with redis. Below is my yaml document for both redis and node js app. I am able to connect my node js app to redis using k8s services. But my issue is, when node js service start it will get connected to redis service for 5 to 10 min, later it crash. if i restart it works fine for 5 min then again it crash. I have updated the dns of redis in node js app to connect the redis k8s service with proper port number.

> node-js deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-node
spec:
  selector:
    matchLabels:
      app: webapp-node
  replicas: 3
  template:
    metadata:
      labels:
        app: webapp-node
    spec:
      containers:
      - name: webapp-node
        image: webapp-node:server_v1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3001
        # data volume where redis writes data
        volumeMounts:
        - name: data
          mountPath: /data
          readOnly: false
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: webapp-redis-data

---
kind: Service
apiVersion: v1
metadata:
  # Unique key of the Service instance
  name: webapp-node
spec:
  ports:
    - name: http
      port: 3001
      nodePort: 30090
  selector:
    app: webapp-node
  type: NodePort
> redis-db

apiVersion: v1
kind: Service
metadata:
  name: webapp-redis
  labels:
    app: webapp-redis
spec:
  ports:
  - name: redis
    port: 6379
    nodePort: 30495
  type: NodePort
  selector:
    app: webapp-redis
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-redis
spec:
  selector:
    matchLabels:
      app: webapp-redis
  replicas: 3
  template:
    metadata:
      labels:
        app: webapp-redis
    spec:
      containers:
      - name: webapp-redis
        image: redis:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6379
        # data volume where redis writes data
        volumeMounts:
        - name: data
          mountPath: /data
          readOnly: false
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: webapp-redis-data
---
# data volume
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: webapp-redis-data
  labels:
    app: webapp-redis
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi

ERROR

events.js:377 throw er; // Unhandled 'error' event ^ Error: connect ECONNREFUSED 10.103.5.142:30495 at TCPConnectWrap.afterConnect as oncomplete Emitted 'error' event on RedisClient instance at: at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:342:14) at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:223:14) at Socket.emit (events.js:400:28) at emitErrorNT (internal/streams/destroy.js:106:8) at emitErrorCloseNT (internal/streams/destroy.js:74:3) at processTicksAndRejections (internal/process/task_queues.js:82:21) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '10.103.5.142', port: 30495 }

-- naga
kubernetes
node.js
redis

0 Answers