skip to Main Content

I have deployed a backend API that is connected to the Redis pod and Mongodb pod. The API has successfully established a connection to the mongodb but not Redis, with the following error presented, anyone know how to fix it?

Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1088:14) 
2021-01-18 09:22:36 error: uncaughtException: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1088:14) {"error":{"errno":"ECONNREFUSED","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":6379},"stack":"Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1088:14)","exception":true,"date":"Mon Jan 18 2021 09:22:36 GMT+0000 (Coordinated Universal Time)","process":{"pid":1,"uid":0,"gid":0,"cwd":"/usr/src/app","execPath":"/usr/local/bin/node","version":"v11.14.0","argv":["/usr/local/bin/node","/usr/src/app/dist/server.js"],"memoryUsage":{"rss":101421056,"heapTotal":73146368,"heapUsed":37365856,"external":19092480}},"os":{"loadavg":[1.232421875,0.8720703125,0.69482421875],"uptime":281868},"trace":[{"column":14,"file":"net.js","function":"TCPConnectWrap.afterConnect [as oncomplete]","line":1088,"method":"afterConnect [as oncomplete]","native":false}]}

backend.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend-api
  template:
    metadata:
      labels:
        app: backend-api
    spec:
      containers:
      - image: <image_name>
        name: backend-test
        imagePullPolicy: Never
        ports:
        - containerPort: 8080
        env: 
        - name: MONGODB_URL
          value: mongodb://localhost:27017/dt?authSource=admin
        - name: REDIS_URL
          value: redis://localhost:6379      
      restartPolicy: Always
---
kind: Service
apiVersion: v1
metadata:
  name: backend-svc
  labels:
    app: backend-svc
spec:
  selector:
    app: backend-api
  ports:
    - name: http 
      port: 8080
      targetPort: 8080

redis.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: master
        image: k8s.gcr.io/redis:e2e  # or just image: redis
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  ports:
  - name: redis
    port: 6379
    targetPort: 6379
  selector:
    app: redis

I have also tried to forward a port to a port on the pod but have no luck

kubectl port-forward redis-master-765d459796-258hz 6379:6379

2

Answers


  1. localhost is for communication between containers in one pod. Try redis-master:6379

    Login or Signup to reply.
  2. localhost is only for local connection. thats mean you can only connect from inside a pod with localhost. when you need to access from outside you need to use service. service will then resolve your ip address. when you will try to access via service . here is the code need to update in your yaml.

            - name: REDIS_URL
              value: redis://redis-master.default.svc.cluster.local:6379  
    

    ref

    the template is like my_Service_Name.my_Namespace.svc.cluster-domain.example , but you can skip the cluster-domain.example part. Only Service_Name.Namespace.svc will work fine. so for you it will be : redis-master.default.svc

    here is the updated code :

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: backend-api
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: backend-api
      template:
        metadata:
          labels:
            app: backend-api
        spec:
          containers:
          - image: <image_name>
            name: backend-test
            imagePullPolicy: Never
            ports:
            - containerPort: 8080
            env: 
            - name: MONGODB_URL
              value: mongodb://localhost:27017/dt?authSource=admin
            - name: REDIS_URL
              value: redis://redis-master.default.svc.cluster.local:6379      
          restartPolicy: Always
    ---
    kind: Service
    apiVersion: v1
    metadata:
      name: backend-svc
      labels:
        app: backend-svc
    spec:
      selector:
        app: backend-api
      ports:
        - name: http 
          port: 8080
          targetPort: 8080
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search