Postgres用作所有服务的数据库.
当应用程序升级到更新版本时,我通过initContainers运行数据库迁移脚本.
当迁移脚本需要对DB的独占访问(应终止所有其他连接)时,会出现此问题,否则会阻止脚本.
理想的解决方案是停止所有pod,运行迁移并重新创建它们.但我不确定如何使用Kubernetes正确实现它.
TNX
解决方法IDeal solution would be to stop all pods,run the migration and
recreate them. But I am not sure how to achIEve it properly with
Kubernetes.
我从其中一条评论中看到你使用Helm,所以我想提出一个利用Helm钩子的解决方案:
Helm provIDes a hook mechanism to allow chart developers to intervene
at certain points in a release’s life cycle. For example,you can use
hooks to:Load a ConfigMap or Secret during install before any other charts are
loaded.Execute a Job to back up a database before installing a new
chart,and then execute a second job after the upgrade in order to
restore data.Run a Job before deleting a release to gracefully take a
service out of rotation before removing it.
https://github.com/kubernetes/helm/blob/master/docs/charts_hooks.md
您可以将迁移打包为k8s作业,并利用预安装或升级前挂钩来运行作业.这些钩子在渲染模板后运行,但在Kubernetes中创建任何新资源之前运行.因此,您的迁移将在部署Pod之前运行.
要在运行迁移之前删除部署,请使用较低的helm.sh/hook-weight创建第二个预安装/升级前挂钩,以删除目标部署:
APIVersion: batch/v1kind: JobMetadata: name: "pre-upgrade-hook1" annotations: "helm.sh/hook": pre-upgrade "helm.sh/hook-weight": "-1" "helm.sh/hook-delete-policy": hook-succeededspec: template: Metadata: name: "pre-upgrade-hook1" spec: restartPolicy: Never serviceAccountname: "<an SA with delete RBAC permissions>" containers: - name: kubectl image: "lachlanevenson/k8s-kubectl:latest" command: ["delete","deployment","deploy1","deploy2"]
较低的钩子重量将确保此作业在迁移作业之前运行.这将确保以下一系列事件:
>你运行头盔升级
>具有最低挂钩权重的helm挂钩运行并删除相关部署
>第二个钩子运行并运行您的迁移
>您的图表将安装新的Deployments,Pods等.
只需确保将所有相关的部署保留在同一个图表中.
总结以上是内存溢出为你收集整理的postgresql – 在Kubernetes集群上管理数据库迁移全部内容,希望文章能够帮你解决postgresql – 在Kubernetes集群上管理数据库迁移所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)