[英]Remove files periodically from Volume Mount in Kubernetes Pod
I have a kubernetes pod running which has two containers, say A and B. They share common volume of type emptyDir
.我有一个 kubernetes pod 正在运行,它有两个容器,比如说 A 和 B。它们共享类型emptyDir
公共卷。 I have a usecase in which application running in container A takes files uploaded by client and places them at mount point.我有一个用例,其中在容器 A 中运行的应用程序获取客户端上传的文件并将它们放置在安装点。 Then it signals to container B to process the file.然后它向容器 B 发出信号以处理该文件。 Container B then sends back response to A (The response is instantaneous and not batched).然后容器 B 向 A 发回响应(响应是即时的,不是批处理的)。 Now as the file is processed it can be deleted (It must be, due to obvious storage constraints).现在当文件被处理时,它可以被删除(它必须是,由于明显的存储限制)。 Deleting files one by one as they are processed could be lot of deletions (or not?) so thought of considering batch deletions periodically.在处理文件时一个一个地删除文件可能是大量删除(或不是?),因此考虑定期考虑批量删除。
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: FileProcessor
labels:
app: FileProcessor
spec:
selector:
matchLabels:
app: FileProcessor
template:
metadata:
labels:
app: FileProcessor
spec:
containers:
- image: processor:0.2
name: Container_B
ports:
- containerPort: 80
name: Container_B
volumeMounts:
- name: upload-files
mountPath: /app/upload-files
- image: sample:0.1
name: Container_A
ports:
- containerPort: 8000
name: Container_A
volumeMounts:
- name: upload-files
mountPath: /app/uploads
volumes:
- name: upload-files
emptyDir: {}
PS: This is to be deployed on GKE. PS:这将部署在 GKE 上。 Update 1: Still looking for a better solution更新 1:仍在寻找更好的解决方案
As you are using emptyDir
, files must be deleted from one of the sidecars.当您使用emptyDir
,必须从 sidecar 之一中删除文件。 Now, lets check what are your options here:现在,让我们检查一下您的选择:
Now, lets check advantage and disadvantage of these solutions:现在,让我们检查一下这些解决方案的优缺点:
If you go with solution 1, your container A or B will have to do little extra work after each processing.如果您采用解决方案 1,则您的容器 A 或 B 在每次处理后将不得不做一些额外的工作。 If the files size are not large enough this extra time shouldn't be significant如果文件大小不够大,这个额外的时间应该不会很重要
If you go with solution 2, you might save extra work after each processing.如果您使用解决方案 2,您可能会在每次处理后节省额外的工作。 However, after a certain period container A or B will require a relatively long time to cleanup those files.但是,经过一段时间后,容器 A 或 B 将需要相对较长的时间来清理这些文件。 Furthermore, you have to add logic when to cleanup the files.此外,您必须添加何时清理文件的逻辑。 If you can do it intelligently, let say when your containers are idle, then this solution should fit best.如果你能聪明地做到这一点,比如说当你的容器空闲时,那么这个解决方案应该是最合适的。
Now, if you go with solution 3, you have to ensure that your container C does not delete files that are being processed by container B.现在,如果您采用解决方案 3,则必须确保容器 C 不会删除容器 B 正在处理的文件。
In case, you want to use a different type of volume, which can be mounted from an external pod, then you can have a CronJob to periodically cleanup those data.如果您想使用不同类型的卷,可以从外部 pod 挂载,那么您可以使用 CronJob 定期清理这些数据。 In this case, same constraint of solution 3 is applicable.在这种情况下,解决方案 3 的相同约束也适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.