使用flask开发一个简单的应用,k8s进行部署这个应用。在这里只提供一个简单的框架结构。

整个项目的目录结构

/Dockerfile
/k8s
  /k8s/k8s_flask.server.yml
  /k8s/k8s_flask.yml
/src
  /src/app.py
  /src/requirements.txt

其中Dockerfile的内容:

WORKDIR /k8s_flask/src

# Installing python dependencies
COPY ./src/requirements.txt /k8s_flask/src
RUN pip install --no-cache-dir -r requirements.txt

# Copying src code to Container
COPY ./src/  /k8s_flask/src

# Application Environment variables
ENV APP_ENV development

# Exposing Ports
EXPOSE 5000

# 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_flask/src/as.sh
# ENTRYPOINT ["/k8s_python_code/src/as.sh"]

其中k8s_flask.yml为部署文件,deployment,能够显示pod的信息

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ksflask
  labels:
    app: ksflask
    serviceName: "ksflask"
    serviceVersion: "v1"
spec:
  selector:
    matchLabels:
      app: ksflask
  replicas: 1
  template:
    metadata:
      labels:
        app: ksflask
        serviceName: "ksflask"
        serviceVersion: "v1"
    spec:
      containers:
      - name: ksflask
        image: 172.27.233.13:35000/k8s_flask:0.4
        # command: [ "/bin/bash", "/k8s_python_code/src/as.sh" ]
        ports:
        - containerPort: 5000

其中k8s_flask.server.yml为暴漏服务的yml文件,部署后不会显示pod信息

apiVersion: v1
kind: Service
metadata:
  name: ksflask
spec:
  type: NodePort
  ports:
    - name: ksflask
      port: 5000
      targetPort: 5000
      nodePort: 32031
  selector:
    app: ksflask
    serviceName: "ksflask"

其中app.py是flask的简单服务

from flask import Flask

app = Flask(__name__)

@app.route('/')
def Project():
  return 'coming baby!'

@app.route('/pdf_jpg', methods=['post','get'])
def pdf_jpg():
    file_dir = request.args.get('file_dir')
    print(file_dir)
    return jsonify({'t': [file_dir]})

if __name__ == '__main__':
  app.run(host='0.0.0.0')

其中requirements.txt是运行所需的模块

flask
requests

运行过程:

在Dockerfile路径层,运行

docker build -t 172.27.233.13:35000/k8s_flask:0.1 .
docker tag k8s_flask:latest 172.27.233.13:35000/k8s_flask:0.1
docker push 172.27.233.13:35000/k8s_flask:0.1

进入k8s文件夹,运行

kubectl create -f k8s_flask.yml
kubectl create -f k8s_flask.server.yml

若需要删除

kubectl delete -f k8s_flask.server.yml

重新部署pod

如果需要pod内部和本地交互,那么设置同共享的文件目录:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ksflask
  labels:
    app: ksflask
    serviceName: "ksflask"
    serviceVersion: "v1"
spec:
  selector:
    matchLabels:
      app: ksflask
  replicas: 1
  template:
    metadata:
      labels:
        app: ksflask
        serviceName: "ksflask"
        serviceVersion: "v1"
    spec:
      containers:
      - name: ksflask
        image: ip:35000/k8s_flask:0.9
        # command: [ "/bin/bash", "/k8s_python_code/src/as.sh" ]
        ports:
        - containerPort: 5000
        volumeMounts:
        - name: ksflasks
          mountPath: '/k8s_flask/src/pdf'
      volumes:
      - name: ksflasks
        hostPath:
          path: "/root/wuhao/k8s_flask/src/pdf"

其中共享目录的地方

       volumeMounts:
        - name: ksflasks
          mountPath: '/k8s_flask/src/pdf'
      volumes:
      - name: ksflasks
        hostPath:
          path: "/root/wuhao/k8s_flask/src/pdf" 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐