[英]Generate, store and clean data across multiple process
I have a simple application which stores it's runtime data at say /tmp
of host machine.我有一个简单的应用程序,它将运行时数据存储在主机的
/tmp
中。 This data is used by different workers of same application.此数据由同一应用程序的不同工作人员使用。
When ever I start my app I want to make sure no data from previous session is stored there so I have to clean up that data.当我启动我的应用程序时,我想确保没有来自以前的 session 的数据存储在那里,所以我必须清理这些数据。
But when I am deploying this on kubernetes if a pod is killed and re-starts the data is cleared.但是当我在 kubernetes 上部署它时,如果一个 pod 被杀死并重新启动,数据就会被清除。 How can we avoid this.
我们怎样才能避免这种情况。
# Sample of how things are getting done
class Work:
def __init__(self):
# Code Remove everything inside /tmp directory
def work(self):
# Generate userdata in /tmp directory
This app is hosted by flask
using gunicorn
.这个应用程序由
flask
使用gunicorn
托管。 This works perfectly when we run it in simple environment.当我们在简单的环境中运行它时,这非常有效。
But when we run it on kubernetes
if pod gets killed new pod is created and this will remove existing data from /tmp
directory.但是当我们在
kubernetes
上运行它时,如果 pod 被杀死,则会创建新的 pod,这将从/tmp
目录中删除现有数据。 Which resets the information we've collected till now.这会重置我们迄今为止收集的信息。
This looks like a general problem people must have encountered in time.这看起来像是人们必须及时遇到的普遍问题。 Please suggest me some existing methodology for this.
请为此建议我一些现有的方法。
If you are storing it on the Host machine, which means mounting the file system using the HostPath
then even if POD is restarting data should be there.如果您将它存储在主机上,这意味着使用
HostPath
挂载文件系统,那么即使 POD 正在重新启动数据也应该在那里。
You might be writing files or data to the POD file system so if POD restart it will be empty if i understood it clearly.您可能正在将文件或数据写入 POD 文件系统,因此如果 POD 重新启动,如果我清楚地理解它,它将为空。
In this case, you can use the HostPath which will mount the POD file system to Node's(Host) machine.在这种情况下,您可以使用 HostPath 将 POD 文件系统挂载到节点的(主机)机器上。 Also, keep the POD name as a prefix
另外,保留 POD 名称作为前缀
so your file system will be /tmp/<POD Name>/data-here
所以你的文件系统将是
/tmp/<POD Name>/data-here
To get the POD name you can use the Downward API , i think with this case there won't be any changes required on the APP side minor changes are required in YAML only.要获取 POD 名称,您可以使用Downward API ,我认为在这种情况下,APP 端不需要任何更改,仅 YAML 需要进行细微更改。
Below an example, you can notice the subPathExpr: $(pod_name)
and Env to get the POD name.在下面的示例中,您可以注意到
subPathExpr: $(pod_name)
和 Env 来获取 POD 名称。
Data will be written at /var/log/app-data/<POD-Name>/
数据将写入
/var/log/app-data/<POD-Name>/
Example例子
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
labels:
app.kubernetes.io/component: app
name: app
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/instance: app
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app.kubernetes.io/component: app
spec:
containers:
- env:
- name: pod_name
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.name
image: <IMAGE>
name: app
ports:
- containerPort: 443
name: app-tomcat-port
protocol: TCP
volumeMounts:
- mountPath: /tmp
name: app-data
subPathExpr: $(pod_name)
volumes:
- hostPath:
path: /var/log/app_data
type: DirectoryOrCreate
name: app-data
Read more about the hostpath : https://kubernetes.io/docs/concepts/storage/volumes/#hostpath阅读有关主机路径的更多信息: https ://kubernetes.io/docs/concepts/storage/volumes/#hostpath
Subpath expression doc: https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment子路径表达式文档: https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.