如何在 kubernetes 中运行 docker 镜像从另一个启动并传递参数



我有两个需要在 kubernetes 中运行的 dockerized 应用程序。

这是需要实现的方案。

Docker-1,这是烧瓶应用。

Docker-2 是 python 脚本,它将从 Docker-1 获取输入并执行,并且需要在 Docker-1 容器的共享卷中写入一些文件。

这是烧瓶网络应用程序代码。

from flask import Flask, request, Response, jsonify
app = Flask(__name__)
@app.route('/')
def root():
  return "The API is working fine"
@app.route('/run-docker')
def run_docker_2():
   args = "input_combo"
   query = <sql query>
   <initiate the docker run and pass params>
   exit
   #No return message need run as async
if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=8080, threaded=True)

码头工人文件

FROM ubuntu:latest
MAINTAINER Abhilash KK "abhilash.kk@searshc.com"
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential python-tk
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["/usr/bin/python"]
CMD ["app.py"]

要求.txt

flask

第二个码头工人的 Python 脚本。 start_docker.py

import sys
input_combo = sys.argv[1]
query = sys.argv[2]
def function_to_run(input_combination,query):
    #starting the model final creating file
function_to_run(input_combo, query)

码头工人文件 2

FROM python
COPY . /script
WORKDIR /script
CMD ["python", "start_docker.py"]

请帮助我连接码头工人映像。 或者让我知道解决此问题的任何其他方法。基本要求是将消息添加到某个队列中,该队列在时间间隔内侦听并以FIFO方式启动该过程。

GCP 服务中启动异步作业的任何其他方法都将从客户端获取输入并创建一个可从 Web 应用程序 python 访问的文件。

首先,创建一个运行"Docker-1"应用程序的 Pod。然后 Kubernetes python 客户端生成第二个带有"Docker-2"的 pod。您可以在 Pod 之间共享卷,以便将数据返回到 Docker1。在我的代码示例中,我使用的是host_path卷,但您需要确保两个 Pod 位于同一节点上。为了提高可读性,我确实添加了该代码。

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: docker1
  labels:
    app: docker1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: docker1
  template:
    metadata:
      labels:
        app: docker1
    spec:
      containers:
      - name: docker1
        image: abhilash/docker1
        ports:
        - containerPort: 8080
        volumeMounts:
        - mountPath: /shared
          name: shared-volume
      volumes:
      - name: shared-volume
        hostPath:
          path: /shared

run_docker_2处理程序的代码:

from kubernetes import client, config
...
args = "input_combo"
config.load_incluster_config()
pod = client.V1Pod()
pod.metadata = client.V1ObjectMeta(name="docker2")
container = client.V1Container(name="docker2")
container.image = "abhilash/docker2"
container.args = [args]
volumeMount = client.V1VolumeMount(name="shared", mount_path="/shared")
container.volume_mounts = [volumeMount]
hostpath = client.V1HostPathVolumeSource(path = "/shared")
volume = client.V1Volume(name="shared")
volume.host_path = hostpath
spec = client.V1PodSpec(containers = [container])
spec.volumes = [volume]
pod.spec = spec
v1.create_namespaced_pod(namespace="default", body=pod)
return "OK"

用于读取返回结果的处理程序:

@app.route('/read-results')
def run_read():
   with open("/shared/results.data") as file:
      return file.read()

请注意,添加一个观察程序以等待 pod 完成作业,然后执行一些清理(例如删除 pod(可能会很有用

据我所知,你想要所谓的"sidecar 模式",你可以在一个 pod 中运行多个容器并共享一个卷,例如:

apiVersion: v1
kind: Pod
metadata:
  name: www
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /srv/www
      name: www-data
      readOnly: true
  - name: git-monitor
    image: kubernetes/git-monitor
    env:
    - name: GIT_REPO
      value: http://github.com/some/repo.git
    volumeMounts:
    - mountPath: /data
      name: www-data
  volumes:
  - name: www-data
    emptyDir: {}

您还可以从了解 Kubernetes 工作原理的基础知识中受益:Kubernetes 基础知识

相关内容

最新更新