<p>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?</p>
<pre><code>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) {&quot;error&quot;:{&quot;errno&quot;:&quot;ECONNREFUSED&quot;,&quot;code&quot;:&quot;ECONNREFUSED&quot;,&quot;syscall&quot;:&quot;connect&quot;,&quot;address&quot;:&quot;127.0.0.1&quot;,&quot;port&quot;:6379},&quot;stack&quot;:&quot;Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379\n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1088:14)&quot;,&quot;exception&quot;:true,&quot;date&quot;:&quot;Mon Jan 18 2021 09:22:36 GMT+0000 (Coordinated Universal Time)&quot;,&quot;process&quot;:{&quot;pid&quot;:1,&quot;uid&quot;:0,&quot;gid&quot;:0,&quot;cwd&quot;:&quot;/usr/src/app&quot;,&quot;execPath&quot;:&quot;/usr/local/bin/node&quot;,&quot;version&quot;:&quot;v11.14.0&quot;,&quot;argv&quot;:[&quot;/usr/local/bin/node&quot;,&quot;/usr/src/app/dist/server.js&quot;],&quot;memoryUsage&quot;:{&quot;rss&quot;:101421056,&quot;heapTotal&quot;:73146368,&quot;heapUsed&quot;:37365856,&quot;external&quot;:19092480}},&quot;os&quot;:{&quot;loadavg&quot;:[1.232421875,0.8720703125,0.69482421875],&quot;uptime&quot;:281868},&quot;trace&quot;:[{&quot;column&quot;:14,&quot;file&quot;:&quot;net.js&quot;,&quot;function&quot;:&quot;TCPConnectWrap.afterConnect [as oncomplete]&quot;,&quot;line&quot;:1088,&quot;method&quot;:&quot;afterConnect [as oncomplete]&quot;,&quot;native&quot;:false}]}
</code></pre>
<p>backend.yaml</p>
<pre><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: &lt;image_name&gt;
        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
</code></pre>
<p>redis.yaml</p>
<pre><code>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
</code></pre>
<p>I have also tried to forward a port to a port on the pod but have no luck</p>
<pre><code>kubectl port-forward redis-master-765d459796-258hz 6379:6379
</code></pre>

efgdh
<p>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.</p>
<pre><code>        - name: REDIS_URL
          value: redis://redis-master.default.svc.cluster.local:6379  
</code></pre>
<p><a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#a-aaaa-records" rel="nofollow noreferrer">ref</a></p>
<p>the  template is like  <code>my_Service_Name.my_Namespace.svc.cluster-domain.example</code> , but you can skip the <code>cluster-domain.example</code> part. Only <code>Service_Name.Namespace.svc</code> will work fine. so for you it will be : <code>redis-master.default.svc</code></p>
<p>here is the updated code :</p>
<pre><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: &lt;image_name&gt;
        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
</code></pre>
