Python 3.x 如何通过kubectl运行python脚本?
我最近被指派在Kubernetes集群上安装R和python软件包。我不知道什么是Docker和Kubernetes以及它是如何运作的。而且,我只是这个集群中的一个用户,没有任何root权限。在过去的两周里,我一直在学习这些主题,直到现在,我仍然无法执行一个简单的python脚本。我不明白在配置Dockerfile和kubectl时出现了什么问题。如果你有任何建议,请在评论中告诉我 在集群中,我正在处理两个目录(RunScript和example_py)和以下文件:Python 3.x 如何通过kubectl运行python脚本?,python-3.x,kubernetes,dockerfile,Python 3.x,Kubernetes,Dockerfile,我最近被指派在Kubernetes集群上安装R和python软件包。我不知道什么是Docker和Kubernetes以及它是如何运作的。而且,我只是这个集群中的一个用户,没有任何root权限。在过去的两周里,我一直在学习这些主题,直到现在,我仍然无法执行一个简单的python脚本。我不明白在配置Dockerfile和kubectl时出现了什么问题。如果你有任何建议,请在评论中告诉我 在集群中,我正在处理两个目录(RunScript和example_py)和以下文件: ls /home/ext/g
ls /home/ext/gruenlab3/example_py
Dockerfile requirements.txt
ls /home/ext/gruenlab3/RunScript
test.sh test1.py test1.yaml
Dockerfile的内容
FROM python:3.8-slim
RUN useradd --create-home --shell /bin/bash app_user
WORKDIR /home/app_user
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
USER app_user
COPY . .
CMD ["bash"]
requirements.txt的内容
numpy==1.19.2
test.sh的内容
python test1.py
test1.py的内容
f=open('Myoutput.dat','w')
for i in range(10):
f.write(str(i)+'\n')
f.close()
test1.yaml文件的内容
apiVersion: batch/v1
kind: Job
metadata:
name: test-py
spec:
backoffLimit: 0
template:
metadata:
name: test-py
spec:
restartPolicy: "Never"
priorityClassName: research-med
# Volumes are declared by the pod. They share its lifecycle
# and are communal across containers.
volumes:
# Volumes have a name and configuration based on the type of volume.
# In this example, we use the emptyDir volume type
- name: RunScript
configMap:
name: RunScript
defaultMode: 0777
containers:
- name: test-py
image: ls6-***/extgruenlab3/base_python
imagePullPolicy: "IfNotPresent"
env:
- name: TEST
value: "hello"
- name: MYDIR # example usage of env variable
value: "/mydir"
resources:
limits:
nvidia.com/gpu: "1"
cpu: "4"
memory: "2Gi"
requests:
nvidia.com/gpu: "1"
cpu: "4"
memory: "2Gi"
#command:
# - sleep 10
# - infinity
volumeMounts:
- mountPath: /RunScript/test.sh # directory IN the container
name: localdir # matches volume-name from below
subPath: test.sh
imagePullSecrets:
- name: lsx-registry
restartPolicy: "Never"
volumes:
- name: localdir
cephfs:
monitors:
- 1320 # Not important for you, just copy along
user: extgruenlab3 # <namespace>
path: "/home/ext/gruenlab3/RunScript" # The path you want to mount
secretRef: # The name of the secret for auth. Is always "ceph-secret"
name: ceph-secret
然后我使用命令运行yaml文件
kubectl -n extgruenlab3 create -f test1.yaml
然后我用“kubectl get pods”检查pods
但我在RunScript目录中没有看到任何输出文件名“Myoutput.dat”。我想我在Dockerfile或yaml文件中做了一些错误的事情。您能指出我的错误吗?您的容器启动一个bash shell,然后退出,从不运行Python代码。您应该首先在Kubernetes之外测试Docker映像,在容器开始旋转时,在脚本上的Docker文件中添加一个入口点,以便首先运行。如前所述,可以直接将Python脚本作为您正在创建的Docker映像的入口点运行,也可以将命令和参数添加到作业定义中。如果您需要将Pod闲置,让您能够在内部执行并自行测试,请添加as命令和args,例如while循环,使Pod无限期休眠。谢谢大家。我将entrypoint[“python”]放在Dockerfile上,当我用命令检查打印是否正确时,但当我用kubectl提交时,它仍然不起作用。我应该在yaml文件中做什么更改?
ENTRYPOINT[“python”]
真的没有意义。我设置了CMD[“/test1.py”]
,确保脚本是可执行的,并且具有正确的shebang行,并省略入口点。明确删除卷:
和卷数:
覆盖图像内容。
kubectl -n extgruenlab3 create -f test1.yaml
NAME READY STATUS RESTARTS AGE
test-py-qvzbq 0/1 Completed 0 27m