Fail to connect to Redis in Kubernetes: Redis connection to localhost:6379 failed

yhshj

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:6379\n    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
H.R. Emon

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 nodejs

Redis error: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 Heroku

Could not connect to Redis at 127.0.0.1:6379: Connection refused

Cannot connect to redis://localhost:6379/0: Error 99 connecting to localhost:6379. Cannot assign requested address

Error occured with redis HMSEET, dial tcp :6379: connect: connection refused

Could not connect to Redis at 127.0.0.1:6379: Connection refused in docker

Could not connect to Redis at 127.0.0.1:6379: Connection refused with homebrew

Redis connection to my-redis:6379 failed - getaddrinfo ENOTFOUND when running seeds

Vapor cloud deploy failed: Sockets Error: Failed trying to connect to http://redis.eu.vapor.cloud:6379

RedisInsight on Docker and Redis on Docker: Could not connect: Error 99 connecting to localhost:6379. Cannot assign requested address

Error dial tcp 127.0.0.1:6379: connect: connection refused when starting Redis container without Docker Compose

Connection to Redis cluster failed

Redis connection is failed

Laravel + predis + Redis cluster - MOVED / no connection to 127.0.0.1:6379

Laravel : Redis No connection could be made : [tcp://127.0.0.1:6379]

Aws Elastic Cache (Redis) failed to connect (jedis connection error) when acessed locally through spring boot java

driver failed programming external connectivity on endpoint redis : Bind for 0.0.0.0:6379 failed: port is already allocated

Connect Python app to Redis on Kubernetes cluster

redis: dial tcp [REDIS ADDRESS] connect: connection refused

redis.exceptions.ConnectionError: Error -2 connecting to localhost:6379. Name or service not known

Using Resque-Scheduler with Docker-Compose - Error connecting to Redis on localhost:6379

redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address

docker-compose + django + redis - Error 111 connecting to 127.0.0.1:6379. Connection refused

TestRestTemplate HttpHostConnectException: Connect to localhost:8082 [localhost/127.0.0.1] failed: Connection refused

Catch Microsoft.Extensions.Caching.Redis connection fail?

Name or service not known [tcp://redis:6379]

GiLab CI - Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379

Redis at some-redis:6379: Name or service not known

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive