查看mian函数与requirements.txt文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 这两个文件被封装在压缩包里 cat main.py from flask import Flask app = Flask(__name__)
@app.route("/") def hello(): return "Hello from Python!"
if __name__ == "__main__": app.run(host='0.0.0.0')
cat requirements.txt Flask
|
创建Dockerfile并构建镜像
1 2 3 4 5 6 7 8
| cat dockerfile FROM python:3.7 WORKDIR /app ADD . /app/ RUN /usr/local/bin/python -m pip install --upgrade pip RUN pip install -r requirements.txt EXPOSE 5000 CMD ["python","/app/main.py"]
|
docker build -t hello-python:v1 .
传送到目标节点或上传至私有仓库
docker save -o hello.tar.gz hello-python:v1
或
docker tag hello-python:v1 192.168.10.130/wangsheng/hello-python:v1
docker push 192.168.10.130/wangsheng/hello-python:v1
封装成deployment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| apiVersion: apps/v1 kind: Deployment metadata: name: hello-python spec: selector: matchLabels: app: hello-python replicas: 1 template: metadata: labels: app: hello-python spec: containers: - name: hello-python image: 192.168.10.130/wangsheng/hello-python:v1 imagePullPolicy: IfNotPresent ports: - containerPort: 5000 --- apiVersion: v1 kind: Service metadata: name: hello-python-service spec: selector: app: hello-python ports: - protocol: "TCP" port: 6000 targetPort: 5000 type: NodePort
|
1 2 3 4 5 6 7 8 9
| kubectl apply -f deployment.yaml kubectl get pods -owide
kubectl get svc
|
测试
