简体   繁体   English

只制作kubernetes部署的pod之一进行下载,只读取

[英]Make only one of the pods of kubernetes deployment make a download, and only reads

I have a docker container that first downloads file then it uses them, I wanted to make my application scalable so i used kubernetes depoyments and I created a Persistent volume so that all the pods of the deployment have a shared storage.我有一个 docker 容器,它首先下载文件然后使用它们,我想让我的应用程序可扩展,所以我使用了 kubernetes 部署并创建了一个持久卷,以便部署的所有 pod 都有一个共享存储。 I don't want every pod to download the files I just want only the first one that runs to download it and the other pods will only read id my docker container start.sh file contain those two commands:我不希望每个 pod 都下载文件,我只希望第一个运行的 pod 下载它,而其他 pod 只会读取 id 我的 docker 容器 start.sh 文件包含这两个命令:

python download.py
gunicorn my_application.wsgi:application --bind 0.0.0.0:8000

the configuration file of my deployment is as follows:我的部署的配置文件如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      volumes:
        - name: api-storage
          persistentVolumeClaim:
            claimName: storage-persistent-volume-claim
      containers:
        - name: dispatch-model
          image: mustaphadebbihyassir/dispatch-data-model:latest
          ports:
            - containerPort: 8000
          volumeMounts:
            - name: api-storage
              mountPath: /dispatch_server/Assets/Data
              subPath: data_storage
      imagePullSecrets:
        - name: regcred 

any suggestions on how to approch this problem关于如何解决这个问题的任何建议

The absolute easiest approach is just to build this file into your Docker image.最简单的方法就是将此文件构建到您的 Docker 映像中。

# At image-build time
RUN ./download.py
# Don't need to separately download at container startup time
CMD gunicorn my_application.wsgi:application --bind 0.0.0.0:8000

This doesn't need a PersistentVolumeClaim, or shared storage, or anything else special.这不需要 PersistentVolumeClaim、共享存储或其他任何特殊的东西。 You need to trigger your CI system whenever there's an updated copy of the file but that should be fairly routine.每当有文件的更新副本时,您都需要触发 CI 系统,但这应该是相当常规的。 As with anything else managed by a Deployment, it's possible to be running Pods that use two different versions of the file, and also possible to roll back to a setup that used an older version of the file.与 Deployment 管理的任何其他内容一样,可以运行使用两个不同版本文件的 Pod,也可以回滚到使用旧版本文件的设置。

But what if the file is multiple gigabytes, and it really doesn't make sense to build it into an image?但是,如果文件是多个千兆字节,并且将其构建到图像中真的没有意义怎么办? There are a bunch of questions you need to think about:有一堆问题你需要思考:

  • In the steady state (the system has been running successfully for months), when and how does the file get updated?在稳定的 state 中(系统已经成功运行了几个月),文件何时以及如何更新?
  • What happens if multiple processes try to do the update at the same time?如果多个进程尝试同时进行更新会发生什么?
  • Mechanically, just looking through the list of Kubernetes volume types , do you have access to something that supports ReadWriteMany access?机械地,仅查看Kubernetes 卷类型列表,您是否可以访问支持 ReadWriteMany 访问的内容?

In a comment, @Eugene suggests a Job and that could be a reasonable approach.在评论中,@Eugene 建议了一份 工作,这可能是一种合理的方法。 There will be only one copy of the Job running, which helps address the concurrency considerations, and you can delete and recreate it whenever you need to.只有一份 Job 正在运行,这有助于解决并发问题,您可以在需要时删除并重新创建它。 The Job can run the same image as the main service, if that helps your particular setup, and you can override the command it runs. Job 可以运行与主服务相同的映像,如果这有助于您的特定设置,并且您可以覆盖它运行的命令。

apiVersion: batch/v1
kind: Job
metadata:
  name: downloader
spec:
  template:
    spec:
      volumes: [ ... ]
      containers:
        - name: downloader
          image: registry.example.com/my-application:20220103
          args: # overrides Dockerfile CMD, one word to a list item
            - /app/downloader.py
          volumeMounts: [ ... ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM