Kubernetes 部署错误的 PostgreSQL 数据库

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

Kubernetes deploying wrong postgres database

问题

我觉得我快疯了。我设置了 Kubernetes 来部署一个 PostgreSQL 数据库。一切正常。我想添加一个初始化脚本,所以我制作了一个自定义镜像,部署到私有 Docker Hub 仓库,然后更新了部署以拉取新的镜像。但不管我做什么,Kubernetes 都会部署旧的数据库... 我更新了用户名、密码和数据库名称来确认它没有被升级。这是 kubectl 文件:

  1. # PostgreSQL StatefulSet Service
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: postgres-db-lb
  6. spec:
  7. selector:
  8. app: postgres-stockticker
  9. ports:
  10. - name: "5432"
  11. port: 5432
  12. targetPort: 5432
  13. type: ClusterIP
  14. ---
  15. apiVersion: apps/v1
  16. kind: Deployment
  17. metadata:
  18. name: postgres-stockticker
  19. spec:
  20. selector:
  21. matchLabels:
  22. app: postgres-stockticker
  23. replicas: 1
  24. template:
  25. metadata:
  26. labels:
  27. app: postgres-stockticker
  28. spec:
  29. containers:
  30. - name: postgres-stockticker
  31. image: goodwinmcd/postgres:1.0
  32. imagePullPolicy: Always
  33. ports:
  34. - containerPort: 5432
  35. # Data Volume
  36. volumeMounts:
  37. - name: postgresql-db-disk
  38. mountPath: /var/lib/postgresql/data
  39. volumes:
  40. - name: postgresql-db-disk
  41. persistentVolumeClaim:
  42. claimName: postgres-pv-claim
  43. imagePullSecrets:
  44. - name: docker-hub-repo-key
  45. ---
  46. kind: PersistentVolume
  47. apiVersion: v1
  48. metadata:
  49. name: postgres-pv-volume # 设置 PV 的名称
  50. labels:
  51. type: local # 设置 PV 的类型为本地
  52. app: postgres-stockticker
  53. spec:
  54. storageClassName: manual
  55. capacity:
  56. storage: 5Gi # 设置 PV 容量
  57. accessModes:
  58. - ReadWriteMany
  59. hostPath:
  60. path: "/mnt/data"
  61. ---
  62. kind: PersistentVolumeClaim
  63. apiVersion: v1
  64. metadata:
  65. name: postgres-pv-claim # 设置 PVC 的名称
  66. labels:
  67. app: postgres-stockticker
  68. spec:
  69. storageClassName: manual
  70. accessModes:
  71. - ReadWriteMany # 设置读写权限
  72. resources:
  73. requests:
  74. storage: 5Gi # 设置卷大小
  1. 我已验证以下:从本地 Docker 镜像中拉取镜像,并确认其中有正确的用户名、密码、数据库和表。
  2. 描述了 Pod,以确保它正在拉取和使用正确的镜像。
  3. 登录到 Pod 并确认新的用户名、密码和数据库在 Pod 上以正确的值存在。
  4. 尝试使用新的用户名和密码运行 psql 连接到数据库,但没有成功。使用旧凭证成功。
  5. 重新创建并挂载新的 PV 和新的 PVC 到 Pod。
  6. 我删除了服务、部署、PV 和 PVC,并使用 apply 命令重新创建它们。
  7. 尝试重启 PostgreSQL 服务(service postgres restart),但出现以下错误: No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning).

所以不知何故 Kubernetes 正在拉取包含新用户名、密码和数据库的容器,但然后在集群上启动旧的 PostgreSQL 服务器。对我来说毫无意义。请有人帮忙。我快要失去理智,确保我没有遗漏什么明显的东西。

如果需要其他信息,请告诉我。

英文:

I think I'm going crazy. I had kubernetes setup to deploy a postgres database. Worked fine. I wanted to add an init script so I made a custom image, deployed to private docker hub repo, and then updated the deployment to pull new image. But no matter what I do kubernetes keeps deploying the old database.... I updated the user, password, and database name to confirm that its not getting upgraded. Here's the kubectl file:

  1. # PostgreSQL StatefulSet Service
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: postgres-db-lb
  6. spec:
  7. selector:
  8. app: postgres-stockticker
  9. ports:
  10. - name: "5432"
  11. port: 5432
  12. targetPort: 5432
  13. type: ClusterIP
  14. ---
  15. apiVersion: apps/v1
  16. kind: Deployment
  17. metadata:
  18. name: postgres-stockticker
  19. spec:
  20. selector:
  21. matchLabels:
  22. app: postgres-stockticker
  23. replicas: 1
  24. template:
  25. metadata:
  26. labels:
  27. app: postgres-stockticker
  28. spec:
  29. containers:
  30. - name: postgres-stockticker
  31. image: goodwinmcd/postgres:1.0
  32. imagePullPolicy: Always
  33. ports:
  34. - containerPort: 5432
  35. # Data Volume
  36. volumeMounts:
  37. - name: postgresql-db-disk
  38. mountPath: /var/lib/postgresql/data
  39. volumes:
  40. - name: postgresql-db-disk
  41. persistentVolumeClaim:
  42. claimName: postgres-pv-claim
  43. imagePullSecrets:
  44. - name: docker-hub-repo-key
  45. ---
  46. kind: PersistentVolume
  47. apiVersion: v1
  48. metadata:
  49. name: postgres-pv-volume # Sets PV's name
  50. labels:
  51. type: local # Sets PV's type to local
  52. app: postgres-stockticker
  53. spec:
  54. storageClassName: manual
  55. capacity:
  56. storage: 5Gi # Sets PV Volume
  57. accessModes:
  58. - ReadWriteMany
  59. hostPath:
  60. path: "/mnt/data"
  61. ---
  62. kind: PersistentVolumeClaim
  63. apiVersion: v1
  64. metadata:
  65. name: postgres-pv-claim # Sets name of PVC
  66. labels:
  67. app: postgres-stockticker
  68. spec:
  69. storageClassName: manual
  70. accessModes:
  71. - ReadWriteMany # Sets read and write access
  72. resources:
  73. requests:
  74. storage: 5Gi # Sets volume size
  1. I've verified the following: Pulled down the image in a local docker image and confirmed that it has the correct user, password, database, and tables in it.
  2. Described the pod to make sure it's pulling and using the correct image
  3. Logged onto the pod and confirmed that the new username, password, and database are present on the pod with the correct values.
  4. I tried running psql to connect to the database with the new username and password but no luck. It succeeded with the old credentials
  5. Recreated and mounted new pv and new pvc to the pod
  6. I deleted the service, the deployment, the pv, and the pvc and recreated them with the apply command
  7. I tried restarting the postgres service (service postgres restart) and get the following error: No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning).

So somehow kubernetes is pulling down this container that has the new username, password, and database baked into the image but then starting up the old postgres server on the cluster. It makes no sense to me. Someone please help. I'm losing my sanity making sure I'm not missing something obvious.

Let me know if you need any other information.

答案1

得分: 1

我明白了。卷的Kubernetes保留策略被设置为保留。我尝试将其设置为删除,但数据仍然保留。最终,我登录到实例上,删除了挂载PV的所有内容。这强制Pod重新启动,当它重新启动时,实际上经历了正确的PostgreSQL初始化过程。

英文:

Figured it out. The kubernetes retention policy of the volume was set to retain. I tried setting it to delete but the data was still persisting. I ended up logging on to the instance and blowing away everything where I mounted the PV. This forced the pod to restart, and when it did it actually went through the correct postgres init process.

huangapple
  • 本文由 发表于 2023年6月5日 08:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402971.html
匿名

发表评论

匿名网友

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

确定