Kubernetes-在所有Pod上生成文件 [英] Kubernetes - Generate files on all the pods
问题描述
我有Java API,可将数据导出到excel并在POD上生成一个文件,该文件用于处理请求.现在,下一个请求(下载文件)可能转到另一个POD,下载失败.
I have Java API which exports the data to an excel and generates a file on the POD where the request is served. Now the next request (to download the file) might go to a different POD and the download fails.
我该如何解决?如何在所有POD上生成文件?或者,如何确保后续请求到达生成文件的同一POD?我无法提供直接的POD URL,因为客户端将无法访问它.
How do I get around this? How do I generate files on all the POD? Or how do I make sure the subsequent request goes to the same POD where file was generated? I cant give the direct POD URL as it will not be accessible to clients.
谢谢.
推荐答案
您需要使用永久卷在容器之间共享相同的文件.您可以使用安装在容器上的节点存储(最简单的方法)或其他分布式文件系统,例如NFS,EFS(AWS),GlusterFS等...
You need to use a persistent volumes to share the same files between your containers. You could use the node storage mounted on containers (easiest way) or other distributed file system like NFS, EFS (AWS), GlusterFS etc...
如果您需要最简单的共享文件,并且吊舱位于同一节点中,则可以使用主机路径以存储文件并与其他容器共享卷.
If you you need a simplest to share the file and your pods are in the same node, you could use hostpath to store the file and share the volume with other containers.
假设您有一个仅具有一个节点的kubernetes集群,并且希望与您的Pod共享节点的路径/mtn/data
:
Assuming you have a kubernetes cluster that has only one Node, and you want to share the path /mtn/data
of your node with your pods:
创建一个持久卷:
hostPath PersistentVolume使用节点上的文件或目录来模拟网络附加存储.
A hostPath PersistentVolume uses a file or directory on the Node to emulate network-attached storage.
apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
创建一个PersistentVolumeClaim:
Pods使用PersistentVolumeClaims请求物理存储
Pods use PersistentVolumeClaims to request physical storage
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
看看PersistentVolumeClaim:
Look at the PersistentVolumeClaim:
kubectl获取pvc任务-pv声明
输出显示PersistentVolumeClaim已绑定到您的PersistentVolume, task-pv-volume
.
The output shows that the PersistentVolumeClaim is bound to your PersistentVolume, task-pv-volume
.
NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS AGE
task-pv-claim Bound task-pv-volume 10Gi RWO manual 30s
创建具有两个副本的部署,例如:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/mnt/data"
name: task-pv-storage
现在,您可以在两个容器中检查路径/mnt/data
具有相同的文件.
Now you can check inside both container the path /mnt/data
has the same files.
If you have cluster with more than 1 node I recommend you to think about the other types of persistent volumes.
这篇关于Kubernetes-在所有Pod上生成文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!