- 用python创建一个应用程序,使用docker构建镜像,使用k8s管理运行这个程序。
docker build -t k8s_python_code .
docker tag k8s_python_code:latest 172.27.233.13:35000/k8s_python_code:1.6
docker push 172.27.233.13:35000/k8s_python_code:1.6
kubectl delete -f k8s_python_sample_code.service.yml
kubectl create -f k8s_python_sample_code.service.yml
整个文件的目录结构
/Dockerfile
/k8s
/k8s/k8s_python_sample_code.service.yml
/src
/src/app.py
/src/as.sh
/src/requirements.txt
编写docker的配置文件,Dockerfile
WORKDIR /k8s_python_code/src
# Installing python dependencies
COPY ./src/requirements.txt /k8s_python_code/src
RUN pip install --no-cache-dir -r requirements.txt
# Copying src code to Container
COPY ./src/ /k8s_python_code/src
# Application Environment variables
ENV APP_ENV development
# Exposing Ports
EXPOSE 60001
# Setting Persistent data
VOLUME ["/app-data"]
# Running Python Application
# CMD python app.py
# ENTRYPOINT ["shell", "as.sh"]
# ADD startup /k8s_python_code/src
RUN chmod a+x /k8s_python_code/src/as.sh
ENTRYPOINT ["/k8s_python_code/src/as.sh"]
WORKDIR为docker镜像的工作目录,expose为暴漏端口,cmd和ENTRYPOINT都是运行代码。
进行打镜像操作
docker build -t k8s_python_code .
docker tag k8s_python_code:latest 172.27.233.13:35000/k8s_python_code:0.1
docker push 172.27.233.13:35000/k8s_python_code:0.1
k8s_python_sample_code.service.yml的文件内容。
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s
spec:
selector:
matchLabels:
run: k8s
replicas: 1
template:
metadata:
labels:
run: k8s
spec:
containers:
- name: k8s
image: 172.27.233.13:35000/k8s_python_code:0.1
# command: [ "/bin/bash", "/k8s_python_code/src/as.sh" ]
ports:
- containerPort: 60001
进行pod的部署
执行:kubectl create -f k8s_python_sample_code.service.yml
执行:k get pod -n default 查看pod的状态
看到pod没有启动,执行: k describe pod k8s-5b555cbc89-zxbrs -n default 查看具体原因
获取报错信息:Container image "172.27.233.13:35000/k8s_python_code:1.6" already present on machine
将上面的k8s_python_sample_code.service.yml中注释的 #command放开。
再执行重新部署,执行:
kubectl delete -f k8s_python_sample_code.service.yml
kubectl create -f k8s_python_sample_code.service.yml
在查看pod的状态
其中app.py中的代码
import sys
import os
import datetime
if __name__ == "__main__":
fo = open("foo.txt", "w")
fo.write( "www.runoob.com!\nVery good site!\n")
while(1):
print("1")
fo.close()
pdfPath = './pdf/mm.pdf'
imagePath = './image'
print("xxxxxxxxxxxxxxxxxxxxx")
如果将app.py中的循环代码,注释。查看pod 的状态
查看具体原因,执行
k describe pod k8s-db4c4b5cd-kzgsv -n default
结尾感谢忠兴同学帮助调bug。