I am new in Kubernetes and exploring. I want to run a python container inside a pod in Kubernetes. I googled the same and it seems to me, first I need to create a docker image with python and then use that image in kubernetes deployment file. Whatever examples I went through all have described the same option.
My question is why need to create a docker file first and then use that in the K8s deployment? Instead why cannot use python image directly in the deployment?
Below is my K8s deployment file what I tried
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-python-deployment
labels:
app: test-python
spec:
replicas: 1
selector:
matchLabels:
app: test-python
template:
metadata:
labels:
app: test-python
spec:
containers:
- name: test-python
image: python:3.8.18
ports:
- containerPort: 5000
After applying the deployment, found below while describe the pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 21s default-scheduler Successfully assigned default/test-python-deployment-5bfdbf6c8f-n4zkw to minikube
Normal Pulled 6s (x3 over 21s) kubelet Container image "python:3.8.18" already present on machine
Normal Created 6s (x3 over 21s) kubelet Created container test-python
Normal Started 6s (x3 over 20s) kubelet Started container test-python
Warning BackOff 5s (x3 over 19s) kubelet Back-off restarting failed container test-python in pod test-python-deployment-5bfdbf6c8f-n4zkw_default(0e5e943b-0ec1-4ae0-80ff-a9c0aa963758)
It seems the cotainer was started some point of time, but then crashed. The pod status is CrashLoopBackOff. I tried to get logs from the pod using "kubectl get log " but did not get any log from pod. No log for the deployment as well.
2
Answers
You got everything right just didn’t got how your code will run in python image ?
If you just want to run the python for testing you can do that, but when you are deploying your own code you need to create the Docker file & Docker image and then deploy to Kubernetes.
It could be crashing due to it expecting the CMD or file input to execute when that image is starting.
Entrypoint is something you should check.
You can get an idea from the official documentation of image : https://hub.docker.com/_/python
Well, technically the deployment isn’t working because by default, the python container starts in REPL mode and since stdin isn’t kept open the container shuts down and thus the pod, which then occurs in a loop.
You could fix this like so:
and then you can connect to the pod to check the running python process:
however this is likely not what you really want, and you should either create your own Dockerfile (and then use this as the image in the deployment) or you should specify a command and/or args for the container.