<p>I'm having trouble deploying my fastapi app to a k8s container in GCP.
Even though I have green checkmark indicating its up and running my log shows this:<br />
<a href="https://i.stack.imgur.com/JKEzE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JKEzE.png" alt="enter image description here" /></a></p>
<p>When I run my app locally, it builds.
When I build my image and container locally, Docker desktop shows no issues and its not hitting this alive endpoint over and over.
Before I added this endpoint to my app. The logs were showing the app stopping and restarting again and again.</p>
<p>Other endpoints that are deployed don't have any of these logs. So I feel like something keeps making the app restart over and over and checking this health liveness endpoint, what am I doing wrong here?</p>
<p>Dockerfile:</p>
<pre><code>FROM python:3.9

#need to run virtualenv
RUN python3 -m venv /opt/venv



# Install system dependencies
RUN apt-get update

ENV PATH=&quot;${PATH}:/root/.poetry/bin&quot;

WORKDIR .app work directory for code

ARG PIPCONF_B64
RUN mkdir -p ~/.pip &amp;&amp; echo $PIPCONF_B64 | base64 -d &gt; ~/.pip/pip.conf
RUN pip install poetry
# Copy over the requirements.txt so we can install it Copy project files and test files
COPY . app
COPY requirements.txt /requirements.txt
COPY poetry.lock pyproject.toml
COPY pyproject.toml pyproject.toml
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

# Install requirements
RUN pip3 install --upgrade pip
RUN pip3 install -r /requirements.txt
RUN . /opt/venv/bin/activate &amp;&amp; pip install -r requirements.txt


ENV PYTHONPATH=&quot;${PYTHONPATH}:/appstuff&quot;

EXPOSE 80
CMD [&quot;uvicorn&quot;, &quot;main:app&quot;, &quot;--proxy-headers&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;80&quot;]

</code></pre>
<p>my deployment.yaml:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: &quot;{{ .Release.Name }}-deployment&quot;
spec:
  revisionHistoryLimit: 5
  {{- if not .Values.hpa.enabled }}
  replicas: {{ .Values.replicas }}
  {{ end }}
  selector:
    matchLabels:
      app: &quot;{{ .Release.Name }}&quot;
{{ toYaml .Values.labels | indent 6 }}
  template:
    metadata:
      annotations:
        ad.datadoghq.com/postgres.logs: '[{&quot;source&quot;: ...}]'
      labels:
        app: &quot;{{ .Release.Name }}&quot;
{{ toYaml .Values.labels | indent 8 }}
    spec:
      serviceAccountName: &quot;{{ .Release.Name }}-sa&quot;
      containers:
        - name: &quot;{{ .Release.Name }}&quot;
          image: &quot;{{ .Values.image.repository }}:{{ .Values.image.tag }}&quot;
          imagePullPolicy: &quot;{{ .Values.image.pullPolicy }}&quot;
          command: [&quot;uvicorn&quot;]
          args: [&quot;main:app&quot;, &quot;--proxy-headers&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;80&quot;]
          ports:
            - name: ui
              containerPort: 80
              protocol: TCP
          env:

          envFrom:
            - configMapRef:
                name: &quot;{{ .Release.Name }}-configmap&quot;
#            - secretRef:
#                name: my-secret-name
          livenessProbe:
            httpGet:
              path: /alive
              port: 80
            initialDelaySeconds: 120
          resources:
{{ toYaml .Values.resources | indent 12 }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 33%
      maxUnavailable: 33%
</code></pre>
<p>my config.yaml:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: &quot;{{ .Release.Name }}-configmap&quot;
  annotations:
    meta.helm.sh/release-name: &quot;{{ .Release.Name }}&quot;
    meta.helm.sh/release-namespace: &quot;{{ .Release.Namespace }}&quot;
  labels:
    app.kubernetes.io/managed-by: Helm
data:
{{ toYaml .Values.envConfig | indent 2 }}

</code></pre>

GustaMan9000
<p>5xx errors such as 502 occur when the server is unable to process or serve the client’s request. This <a href="https://komodor.com/learn/how-to-fix-kubernetes-502-bad-gateway-error/" rel="nofollow noreferrer"><strong>blog</strong></a> written by Nir Shtein briefs about various points of failures, which can cause 5xx errors when an application is deployed on a kubernetes cluster.</p>
<blockquote>
<p>Consider a typical scenario in which you map a Service to a container
within a pod, and the client is attempting to access an application
running on that container. This creates several points of failure:</p>
<ul>
<li>The pod</li>
<li>The container</li>
<li>Network ports exposed on the container</li>
<li>The Service</li>
<li>The Ingress</li>
</ul>
</blockquote>
<p>As per the description provided by you, the main reason for getting 502 errors is that your containers are restarting continuously.</p>
<p>A pod or container restart might occur because of reasons like over resource utilization(CPU or memory overshoot) or because of the pod shutting down prematurely due to an error in application code, follow this <a href="https://www.bluematador.com/docs/troubleshooting/kubernetes-pod#:%7E:text=on%20the%20container.-,Container%20Restarts,-In%20most%20cases" rel="nofollow noreferrer"><strong>blog</strong></a> for more information regarding container restarts.</p>
<p><strong>Troubleshooting steps:</strong></p>
<p>Check the pod logs for more information, on why the pod is getting restarted, you can use the below commands for this</p>
<pre><code> `kubectl get logs &lt;pod&gt;` or `kubectl describe pod &lt;pod&gt;`
</code></pre>
<p>Pod restarts might also happen due to high CPU or Memory utilization. Use the below command for checking the pod that is consuming higher resources</p>
<pre><code> `kubectl top pods`
</code></pre>
<p>Check if your application pods are showing in the list and login to the pod with high memory utilization and check the processes that are consuming more resources.</p>
<p>Sometimes improper auto-scaler configurations will also cause container or pod restarts, check your auto-scaler config and correct any misconfiguration found.</p>
<p><a href="https://dwdraju.medium.com/a-pod-restarts-so-whats-going-on-fa12bb8a57ea" rel="nofollow noreferrer">Here</a> is an additional reference on Pod restarts, go through this for additional information and debugging steps.</p>
