<pre><code>2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py&quot;, line 407, in run_asgi
2023-01-07 21:05:14     result = await app(  # type: ignore[func-returns-value]
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py&quot;, line 78, in __call__
2023-01-07 21:05:14     return await self.app(scope, receive, send)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/fastapi/applications.py&quot;, line 270, in __call__
2023-01-07 21:05:14     await super().__call__(scope, receive, send)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/starlette/applications.py&quot;, line 124, in __call__
2023-01-07 21:05:14     await self.middleware_stack(scope, receive, send)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py&quot;, line 184, in __call__
2023-01-07 21:05:14     raise exc
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py&quot;, line 162, in __call__
2023-01-07 21:05:14     await self.app(scope, receive, _send)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py&quot;, line 79, in __call__
2023-01-07 21:05:14     raise exc
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py&quot;, line 68, in __call__
2023-01-07 21:05:14     await self.app(scope, receive, sender)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py&quot;, line 21, in __call__
2023-01-07 21:05:14     raise e
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py&quot;, line 18, in __call__
2023-01-07 21:05:14     await self.app(scope, receive, send)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/starlette/routing.py&quot;, line 706, in __call__
2023-01-07 21:05:14     await route.handle(scope, receive, send)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/starlette/routing.py&quot;, line 276, in handle
2023-01-07 21:05:14     await self.app(scope, receive, send)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/starlette/routing.py&quot;, line 66, in app
2023-01-07 21:05:14     response = await func(request)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/fastapi/routing.py&quot;, line 235, in app
2023-01-07 21:05:14     raw_response = await run_endpoint_function(
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/fastapi/routing.py&quot;, line 161, in run_endpoint_function
2023-01-07 21:05:14     return await dependant.call(**values)
2023-01-07 21:05:14   File &quot;/app/./main.py&quot;, line 49, in login
2023-01-07 21:05:14     stored_password = await redis_client.hget(name=user.username, key=&quot;password&quot;)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/aioredis/client.py&quot;, line 1082, in execute_command
2023-01-07 21:05:14     conn = self.connection or await pool.get_connection(command_name, **options)
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/aioredis/connection.py&quot;, line 1416, in get_connection
2023-01-07 21:05:14     await connection.connect()
2023-01-07 21:05:14   File &quot;/usr/local/lib/python3.10/site-packages/aioredis/connection.py&quot;, line 698, in connect
2023-01-07 21:05:14     raise ConnectionError(self._error_message(e))
2023-01-07 21:05:14 aioredis.exceptions.ConnectionError: Error 111 connecting to redis:6379. 111.
</code></pre>
<p>deployment.yaml file</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth-service
  template:
    metadata:
      labels:
        app: auth-service
    spec:
      containers:
      - name: auth-service
        image: localhost:5000/auth-service:latest
        ports:
        - containerPort: 8000
        resources:
          limits:
            cpu: &quot;1&quot;
            memory: &quot;1Gi&quot;
          requests:
            cpu: &quot;0.5&quot;
            memory: &quot;500Mi&quot;
        envFrom:
          - secretRef:
              name: auth-service-fastapi-secrets
      - name: redis
        image: redis:alpine
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: &quot;1&quot;
            memory: &quot;1Gi&quot;
          requests:
            cpu: &quot;0.5&quot;
            memory: &quot;500Mi&quot;
---
apiVersion: v1
kind: Service
metadata:
  name: auth-service
spec:
  type: NodePort
  selector:
    app: auth-service
  ports:
  - port: 8000
    targetPort: 8000
    nodePort: 30080
---
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    app: redis
  ports:
  - port: 6379
    targetPort: 6379
</code></pre>
<p>my docker-compose did work:
docker-compose.yml:</p>
<pre><code>version: &quot;3&quot;
services:
  auth-service:
    build: .
    ports:
      - &quot;8000:8000&quot;
    environment:
      REDIS_HOST: redis
      REDIS_PORT: 6379
    env_file: fastapi.env
    depends_on:
      - redis
  redis:
    image: &quot;redis:alpine&quot;
    volumes:
      - redis_data:/data

volumes:
  redis_data:
</code></pre>
<p>fastapi code:</p>
<pre><code>import os
import bcrypt
import jwt
import aioredis

from datetime import datetime
from fastapi import FastAPI, HTTPException, Cookie, Depends, Response
from pydantic import BaseModel, EmailStr


app = FastAPI()

# user model


class User(BaseModel):
    username: str
    password: str

# env variables
SECRET_KEY = os.environ[&quot;JWT_SECRET&quot;]

# redis connection
redis_client = aioredis.from_url(
    &quot;redis://redis:6379&quot;, encoding=&quot;utf-8&quot;, decode_responses=True)
</code></pre>
<p>So I am not sure what the problem is.</p>
<p>I have tried talking to chatGPT, didn't quite worked for me, also, I tried using the cluster ip of redis in the fastapi code instead of the name &quot;redis&quot;:</p>
<p><code>aioredis.from_url( &quot;redis://redis:6379&quot;, encoding=&quot;utf-8&quot;, decode_responses=True)</code></p>
<p>still not working:
<code>ConnectionRefusedError: [Errno 111] Connect call failed ('10.107.169.72', 6379)</code></p>
<p>saw some similar question on stackoverflow, but still confused after reading it.</p>

Wentao Zhang
<p>When your application code connects to <code>redis:6379</code>, in Kubernetes, it connects to the Service named <code>redis</code> in the same namespace.  In the setup you've shown, that forwards requests to Pods with a label <code>app: redis</code>.  There aren't any of those Pods, though, which results in your error.</p>
<p>You should also be able to see this comparing <code>kubectl describe service auth-service</code> and <code>kubectl describe service redis</code>.  The <code>redis</code> service should end with a line like</p>
<pre><code>Endpoints:                &lt;none&gt;
</code></pre>
<p>which is usually a sign that the Service's <code>selector:</code> doesn't match the Pods' <code>labels:</code>.</p>
<p>In your case, the right answer is to split the Deployment into two, with only one container each.  Especially:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  ...
spec:
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:alpine
</code></pre>
<p>This has a couple of other technical advantages.  If you rebuild your application and change the <code>image:</code> tag in your main Deployment, the restart won't also restart Redis, so you'll keep your cache.  If you set the main application to have multiple <code>replicas:</code>, they'll all share the same single Redis in the other Deployment.</p>
<p>(If you want to set up your Redis to also persist its data to disk, use a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a> rather than a Deployment.  This is a more complicated setup, and comes with requirements like an additional Service.  If you're fine with your Redis occasionally losing its state than a Deployment is fine.)</p>
