Home > database >  run python file on k8s
run python file on k8s

Time:09-01

I have written a simple python file implementing a task with NumPy.

import numpy as np

a = np.arange(0,5, dtype=int)
print(a)

Now, I want to run it in k8s. I wrote the corresponding Dockerfile

FROM python:3.7

WORKDIR /app
COPY . .
RUN pip install -r requirements.txt

EXPOSE 5000

CMD ["python", "test.py"]

and then I wrote a yaml file for the deployment:

apiVersion: v1
kind: Pod
metadata:
  name: np-test
spec:
  containers:
    - name: client-np
      image: <image-name-on-DockerHub>
      ports:
        - containerPort: 5000

However, the pod is not running and its status is CrashLoopBackOff. I can't figure out why it is not working.

CodePudding user response:

CrashLoopBackOff tells that a pod crashes right after the start. Kubernetes tries to start pod again, but again pod crashes and this goes in loop.

As @larsks Suggested You are getting a CrashLoopBackOff because the pod exits immediately. You may be able to avoid this by adding a sleep to the end of your code, but generally a Pod is meant to implement a long-running process. A Job is more appropriate for something that you expect to exit

A Job creates one or more Pods and will continue to retry execution of the Pods until a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions. When a specified number of successful completions is reached, the task (ie, Job) is complete. Deleting a Job will clean up the Pods it created. Suspending a Job will delete its active Pods until the Job is resumed again.

You can also use a Job to run multiple Pods in parallel.

To fix kubernetes CrashLoopbackoff error refer to this link .

  • Related