<p>I have an API in a container and when I create a cluster the api works fine, but the pods constantly restart and there isn't any specific reason why in the logs.</p>
<p>I'm on an m5.Large EC2 instance and using k3d. I was following a demo so after installing k3d I ran this command</p>
<pre><code>k3d cluster create test -p &quot;80:80@loadbalancer&quot;
</code></pre>
<p>Here is my deployment file</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: stock-api
  labels:
    app: stock-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: stock-api
  template:
    metadata:
      labels:
        app: stock-api
    spec:
      containers:
      - name: stock-api
        image: mpriv32/stock-api:latest
        envFrom:
        - secretRef:
            name: api-credentials
</code></pre>
<p>Service file</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: stock-api
  labels:
    app: stock-api
spec:
  type: ClusterIP
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: stock-api
</code></pre>
<p>My third file is just my secrets file which I just removed the values for this post</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: api-credentials
stringData:
  ACCESS_KEY:
  SECRET_KEY:
  EMAIL_PASSWORD:
  EMAIL_USER:
  API_KEY:
</code></pre>
<p>I applied all of the files and the API works fine, but then my pods constantly restart.</p>
<p>First I ran this command and the reason I got stock information is because I had a print statement in my application to test the api response and forgot to remove it.</p>
<pre><code>kubectl logs -p stock-api-7f5c45776b-gc67c 
{'Company': {'S': 'AAPL'}, 'DailyPrice': {'S': '166.02'}}
</code></pre>
<p>Getting the logs didn't help, so then I ran describe and got this output</p>
<pre><code>  Normal   Scheduled  16m                 default-scheduler  Successfully assigned default/stock-api-7f5c45776b-gc67c to k3d-test-server-0
  Normal   Pulled     15m                 kubelet            Successfully pulled image &quot;mpriv32/stock-api:latest&quot; in 16.509616605s
  Normal   Pulled     15m                 kubelet            Successfully pulled image &quot;mpriv32/stock-api:latest&quot; in 696.527075ms
  Normal   Pulled     15m                 kubelet            Successfully pulled image &quot;mpriv32/stock-api:latest&quot; in 734.334806ms
  Normal   Pulled     15m                 kubelet            Successfully pulled image &quot;mpriv32/stock-api:latest&quot; in 823.429206ms
  Normal   Started    15m (x4 over 15m)   kubelet            Started container stock-api
  Normal   Pulling    14m (x5 over 16m)   kubelet            Pulling image &quot;mpriv32/stock-api:latest&quot;
  Normal   Pulled     14m                 kubelet            Successfully pulled image &quot;mpriv32/stock-api:latest&quot; in 698.883126ms
  Normal   Created    14m (x5 over 15m)   kubelet            Created container stock-api
  Warning  BackOff    62s (x67 over 15m)  kubelet            Back-off restarting failed container
</code></pre>
<p>It constantly keeps &quot;backoff restarting&quot;</p>
<pre><code>When I run `describe po` I get this

Name:         stock-api-7f5c45776b-gc67c
Namespace:    default
Priority:     0
Node:         k3d-test-server-0/172.18.0.2
Start Time:   Mon, 23 May 2022 06:44:42 +0000
Labels:       app=stock-api
              pod-template-hash=7f5c45776b
Annotations:  &lt;none&gt;
Status:       Running
IP:           10.42.0.9
IPs:
  IP:           10.42.0.9
Controlled By:  ReplicaSet/stock-api-7f5c45776b
Containers:
  stock-api:
    Container ID:   containerd://846d4c5c282274453c4b2ad8b834f20d2c673468ca18386d7404b07915b81a9c
    Image:          mpriv32/stock-api:latest
    Image ID:       docker.io/mpriv32/stock-api@sha256:98189cdf972ed61af79505b58aba2a0166fd012f5be4e0f012b2dffa0ea3dd5f
    Port:           &lt;none&gt;
    Host Port:      &lt;none&gt;
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Mon, 23 May 2022 08:23:16 +0000
      Finished:     Mon, 23 May 2022 08:23:17 +0000
    Ready:          False
    Restart Count:  24
    Environment Variables from:
      api-credentials  Secret  Optional: false
    Environment:       &lt;none&gt;
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-czkv9 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  kube-api-access-czkv9:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       &lt;nil&gt;
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              &lt;none&gt;
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason   Age                    From     Message
  ----     ------   ----                   ----     -------
  Warning  BackOff  3m8s (x457 over 102m)  kubelet  Back-off restarting failed container
</code></pre>
<p>Dockerfile for my application</p>
<pre><code>FROM python:3.8

COPY app.py .

RUN pip install requests python-dotenv

ARG API_KEY
ARG EMAIL_USER
ARG EMAIL_PASSWORD

ENV API_KEY $API_KEY
ENV EMAIL_USER $EMAIL_USER
ENV EMAIL_PASSWORD $EMAIL_PASSWORD

COPY database.py .

RUN pip install boto3

ARG ACCESS_KEY
ARG SECRET_KEY

ENV ACCESS_KEY $ACCESS_KEY
ENV SECRET_KEY $SECRET_KEY

CMD [&quot;python&quot;, &quot;database.py&quot;]

EXPOSE 80
</code></pre>
<p>app.py</p>
<pre><code>from datetime import datetime
import smtplib
import os
import requests

today_date = {datetime.today().strftime(&quot;%Y-%m-%d&quot;)}


url = (
    'https://api.polygon.io/v1/open-close/{stock}/2022-05-10?adjusted=true&amp;apiKey={API_key}'
)

if os.path.isfile('.env'):
    from dotenv import load_dotenv
    load_dotenv()

def __send_email(stock_data: str) -&gt; None:
    gmail_user = os.getenv('EMAIL_USER')
    gmail_password = os.getenv('EMAIL_PASSWORD')

    mail_from = gmail_user
    mail_to = gmail_user

    mail_subject = f'Your stock update for {datetime.today().strftime(&quot;%m/%d/%Y&quot;)}'
    mail_message = f'Subject: {mail_subject}\n\n{stock_data}'

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(gmail_user, gmail_password)
    server.sendmail(mail_from, mail_to, mail_message)
    server.close()

class api_data:
    response = requests.get(url.format(
        stock=os.getenv('stock'),
        API_key=os.getenv('API_key')
    ))

    data = response.json()
    ticker = data['symbol']
    daily_high = data['high']
    daily_low = data['low']
    daily_close = data['close']

def __email_body():

    stock_data = (
        f&quot;&quot;&quot;For the stock: {api_data.ticker}
        \nToday's high was {api_data.daily_high}
        \nToday's low was $ {api_data.daily_low} 
        \n{api_data.ticker} closed at $ {api_data.daily_close}&quot;&quot;&quot;
    )

    __send_email(stock_data)

__email_body()
</code></pre>
<p>database.py</p>
<pre><code>import boto3
import app
import os

#Pulling stock name and daily price from API
stock_name = str(app.api_data.ticker)
stock_price = str(app.api_data.daily_close)

if os.path.isfile('.env'):
    from dotenv import load_dotenv
    load_dotenv()

ACCESS_KEY = os.getenv('ACCESS_KEY')
SECRET_KEY = os.getenv('SECRET_KEY')

#DynamoDB table name
table_name = &quot;stock_tracker&quot;
dynamodb_client = boto3.client('dynamodb', region_name='us-west-2', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)

stock_info ={
    'Company': {'S': stock_name}
    ,'DailyPrice': {'S': stock_price}
}

print(stock_info)


if __name__ == &quot;__main__&quot;:
    
    dynamodb_client.put_item(TableName = table_name, Item = stock_info )
</code></pre>

<p>Both your python files are executing batch actions.</p>
<p>The <code>app.py</code> file sends an email, the <code>database.py</code> stores something in a database.</p>
<p>That is not how Pods are supposed to work. Pods are meant to be persistent, or in other words to run continuously. Examples of workloads that are meant to run inside a pod are webservers or DBMSes.</p>
<p>For the two applications you showed us, you might want to use a <code>Job</code> instead (a Job runs your container once and than exits) or a <code>CronJob</code> maybe if you need your scripts to run multiple times on a schedule.</p>
