<p>I am working on an API that I will deploy with Kubernetes and I want to test it locally.</p>
<p>I created the Docker image, successfully tested it locally, and pushed it to a public Docker registry. Now I would like to deploy in a Kubernetes cluster and there are no errors being thrown, however, I am not able to make a request to the endpoint exposed by the Minikube tunnel.</p>
<p>Steps to reproduce:</p>
<ol>
<li>Start Minikube container: <code>minikube start --ports=127.0.0.1:30000:30000</code></li>
<li>Create deployment and service: <code>kubectl apply -f fastapi.yaml</code></li>
<li>Start minikube tunnel: <code>minikube service fastapi-server</code></li>
</ol>
<p>Encountered the following error: <code>192.168.49.2 took too long to respond.</code></p>
<p>requirements.txt:</p>
<pre><code>anyio==3.6.1
asgiref==3.5.2
click==8.1.3
colorama==0.4.4
fastapi==0.78.0
h11==0.13.0
httptools==0.4.0
idna==3.3
pydantic==1.9.1
python-dotenv==0.20.0
PyYAML==6.0
sniffio==1.2.0
starlette==0.19.1
typing_extensions==4.2.0
uvicorn==0.17.6
watchgod==0.8.2
websockets==10.3
</code></pre>
<p>main.py:</p>
<pre><code>from fastapi import FastAPI

app = FastAPI()

@app.get(&quot;/&quot;)
async def root():
    return {&quot;status&quot;: &quot;OK&quot;}
</code></pre>
<p>Dockerfile:</p>
<pre><code>FROM python:3.9

WORKDIR /

COPY . .

RUN pip install --no-cache-dir --upgrade -r ./requirements.txt

EXPOSE 8000

CMD [&quot;uvicorn&quot;, &quot;main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]
</code></pre>
<p>fastapi.yaml:</p>
<pre><code># deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: fastapi-server
  name: fastapi-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: fastapi-server
  template:
    metadata:
      labels:
        app: fastapi-server
    spec:
      containers:
        - name: fastapi-server
          image: smdf/fastapi-test
          ports:
            - containerPort: 8000
              name: http
              protocol: TCP
---
# service
apiVersion: v1
kind: Service
metadata:
  labels:
    app: fastapi-server
  name: fastapi-server
spec:
  type: LoadBalancer
  ports:
    - port: 8000
      targetPort: 8000
      protocol: TCP
      nodePort: 30000
</code></pre>

JSON_Derulo
<p>Your problem is that you did not set the service selector:</p>
<pre class="lang-yaml prettyprint-override"><code># service
apiVersion: v1
kind: Service
metadata:
  labels:
    app: fastapi-server
  name: fastapi-server
spec:
  selector:               &lt;------------- Missing part
    app: fastapi-server   &lt;-------------
  type: NodePort          &lt;------------- Set the type to NodePort 
  ports:
    - port: 8000
      targetPort: 8000
      protocol: TCP
      nodePort: 30000
</code></pre>
<hr />
<h3>How to check if your service is defined properly?</h3>
<p>I checked to see if there are any <code>endpoints</code>, and there weren't any since you did not &quot;attach&quot; the service to your deployment</p>
<pre class="lang-bash prettyprint-override"><code>kubectl get endpoints -A
</code></pre>
<p><a href="https://i.stack.imgur.com/sh12M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sh12M.png" alt="enter image description here" /></a></p>
<hr />
<p><a href="https://i.stack.imgur.com/VZpcc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VZpcc.png" alt="enter image description here" /></a></p>
<hr />
<p>For more info you can read this section under my GitHub
<a href="https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/05-Services" rel="nofollow noreferrer">https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/05-Services</a></p>
<hr />
