<p>I have been trying to query mysql through Fastapi using K8s: but in the swagger, I got an error</p>
<blockquote>
<p>504 Gateway Time-out</p>
</blockquote>
<p>I'll share with you all the conf I did, hoping to help me out find the pb. The conf is too long so bare with me and thanks in advance.</p>
<p>I already have an image for the database, and I created the fastapi image and push it to docker hub:</p>
<p>main.py</p>
<pre><code>from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from sqlalchemy.engine import create_engine
import os
# creating a FastAPI server
server = FastAPI(title='User API')

# creating a connection to the database

MYSQL_ROOT_USER = os.getenv('MYSQL_ROOT_USER')
MYSQL_ROOT_PASSWORD = os.getenv('MYSQL_ROOT_PASSWORD')
MYSQL_ROOT_HOST = os.getenv('MYSQL_ROOT_HOST')
MYSQL_ROOT_DB = os.getenv('MYSQL_ROOT_DB')


# recreating the URL connection
connection_url = 'mysql://{user}:{password}@{url}/{database}'.format(
user=MYSQL_ROOT_USER,
password=MYSQL_ROOT_PASSWORD,
url=&quot;10.100.252.148&quot;,# this is Ip address of mysql-service
database=MYSQL_ROOT_DB
)

# creating the connection
mysql_engine = create_engine(connection_url)


# creating a User class
class User(BaseModel):
   user_id: int = 0
   username: str = 'daniel'
   email: str = 'daniel@datascientest.com'


@server.get('/status')
async def get_status():
   &quot;&quot;&quot;Returns 1
   &quot;&quot;&quot;
   return 1

@server.get('/users')
async def get_users():
   try:
       with mysql_engine.connect() as connection:
        results = connection.execute('SELECT * FROM Users;')

    results = [
        User(
            user_id=i[0],
            username=i[1],
            email=i[2]
            ) for i in results.fetchall()]
      return results
  except Exception as e:
    raise HTTPException(status_code=500, detail=str(e))
</code></pre>
<p>the deployment.yml for both mysql and fastapi:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
app: k8s-deployment 
 name: k8s-deployment  
spec:
replicas: 3
selector:
 matchLabels:
  app: k8s-deployment 
 template:
 metadata:
  labels:
    app: k8s-deployment 
 spec:
  containers:
  - image: raouf001/fastapi:2
    name: k8s-deployment
    imagePullPolicy: Always        
    env:
      - name: FAST_API_PORT
        valueFrom:
          configMapKeyRef:
            name: app-config
            key: FAST_API_PORT
      - name: MYSQL_ROOT_USER
        valueFrom:
          configMapKeyRef:
            name: app-config
            key: MYSQL_ROOT_USER
      - name: MYSQL_ROOT_PASSWORD
        valueFrom:
          configMapKeyRef:
            name: app-config
            key: MYSQL_ROOT_PASSWORD
      - name: MYSQL_ROOT_HOST
        valueFrom:
          configMapKeyRef:
            name: app-config
            key: MYSQL_ROOT_HOST
      - name: MYSQL_ROOT_PORT
        valueFrom:
          configMapKeyRef:
            name: app-config
            key: MYSQL_ROOT_PORT
      - name: MYSQL_ROOT_DB
        valueFrom:
          configMapKeyRef:
            name: app-config
            key: MYSQL_ROOT_DB
  ---
 apiVersion: apps/v1
 kind: Deployment
 metadata:
 name: k8s-mysql
 spec:
  replicas: 3
 selector:
 matchLabels:
  app: lbl-k8s-mysql
 template:
 metadata:
  labels:
    app: lbl-k8s-mysql
spec:
  containers:
  - name: mysql
    image: datascientest/mysql-k8s:1.0.0
    imagePullPolicy: Always
    env:
    - name: MYSQL_DATABASE
      value: Main
    - name: MYSQL_ROOT_PASSWORD
      valueFrom:
        secretKeyRef:
          name: my-secret
          key: password
    ports:
    - containerPort: 3306
      protocol: TCP
</code></pre>
<p>now the service for both:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
 name: mysql-fastapi-service
 labels:
 app: k8s-deployment
 spec:
   type: ClusterIP
 selector:
 app: k8s-deployment
 ports:
- port: 8001
  protocol: TCP      
  targetPort: 8000
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
labels:
name: lbl-k8s-mysql
spec:
 ports:
- port: 3306
 protocol: TCP      
 targetPort: 3306
 selector:
 name: lbl-k8s-mysql
 type: ClusterIP
</code></pre>
<p>ingress.yml</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
 name: my-ingress
spec:
  defaultBackend:
    service:
  name: mysql-fastapi-service
  port:
    number: 8000
  name: mysql-service
  port:
    number: 3306
</code></pre>
<p>configmap.yml</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
 FAST_API: app.py
 MYSQL_ROOT_USER: root
 MYSQL_ROOT_PASSWORD: datascientest1234
 MYSQL_ROOT_HOST: &quot;k8s-deployment.service.default&quot;
 MYSQL_ROOT_PORT: &quot;3306&quot;
 MYSQL_ROOT_DB: Main
 FAST_API_PORT: &quot;8000&quot;
</code></pre>
<p>secret.yml</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
 password: ZGF0YXNjaWVudGVzdDEyMzQ=
</code></pre>
<p>I am ready to share with you any information you need thank you in advance.</p>

Raouf Yahiaoui
<p>I guess the ip address keeps changing every time u apply. Hence it takes time for it to search and eventually leads to session timeout . Best solution is to give your service name as hostname while connecting and make sure both your services are in the same namespace.</p>
