如何在 Pod 重新启动时保持变量的值不变?

huangapple go评论82阅读模式
英文:

How to keep a variable value across pod restarts?

问题

我正在尝试构建一个应用程序,将某些 Kubernetes 资源的规格存储在一个变量中。

然后,它会休眠一段预定义的时间,比如7天,然后应用程序再次运行,并将现在的资源规格与之前存储在变量中的规格进行比较。

问题是:

1)如果应用程序 pod 由于任何原因重新启动,比如节点轮换,它将丢失其存储的规格,并且无法进行比较。

2)如何确保应用程序在死机时能够返回到它的确切位置?例如,如果应用程序的 pod 正好在休眠函数期间死机了怎么办?

我听说 StatefulSet 是答案,但它能保证问题2不会发生吗?

英文:

I'm trying to build an application that stores the specs of certain Kubernetes resources in a variable.

It then sleeps for a pre-defined time like 7 days, then the application runs again, and compares the specs of the same resources now to the specs previously stored in a variable.

The problems are:

  1. If the application pod restarts for any reason, like a node rotation, it will lose its stored specs and won't be able to compare them.

  2. How to make sure that the application will return to the exact point it was when it died? For example, what if the application's pod dies right during the sleep function?

I've heard that StatefulSet is the answer, but does it guarantee problem number 2 doesn't happen?

答案1

得分: 2

存储持久数据的Pod应该使用卷(Volumes)。如果每个Pod都需要自己的卷,那么StatefulSets是正确的选择,因为Kubernetes会在重启后重新连接Pod和它们的卷。

在Kubernetes中,Pod不会自动从上次离开的地方继续执行,因为Pod重启会触发原始应用程序进程的终止。Kubernetes不支持暂停进程。因此,您需要在应用程序代码中处理这种行为。

英文:

Pods that store durable data should make use of Volumes. If each Pod needs its own volume than StatefulSets are the right choice, since K8s will take care of reattaching Pods to their volumes after restarts.

Pods in K8s will not natively resume where they left off, since a Pod restart triggers the termination of the original application process(es). There is no support for pausing processes. So, you would need to take care of that kind of behavior in your application code.

答案2

得分: 1

需要更多关于你的情况的细节,但一般来说,Kubernetes并不用于管理Pod的内部状态,例如具有“休眠”功能。

你可能想考虑一下https://github.com/spf13/viper#remote-keyvalue-store-example---unencrypted中列出的方法。

例如,通过利用consul、etcd、firestore等。

总的来说,你需要考虑如何在应用程序级别上维护状态,无论你是使用“远程配置”还是k8s卷的方法。

英文:

Going to need more details about your case, but in general k8s is not for managing pod's internal states as in have the capability of "hibernation".

You might want to consider approaches listed in https://github.com/spf13/viper#remote-keyvalue-store-example---unencrypted

For example by leveraging consul, etcd, firestore, etc.

In general you need to look at on how to maintain the states at application level regardless whether if you are going to use "remote config" or k8s volume approach.

huangapple
  • 本文由 发表于 2022年5月20日 11:12:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/72313183.html
  • go
  • kubernetes
  • kubernetes-pod
  • kubernetes-statefulset
  • memory
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定