Kubernetes 部署错误的 PostgreSQL 数据库

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

Kubernetes deploying wrong postgres database

问题

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

# PostgreSQL StatefulSet Service
apiVersion: v1
kind: Service
metadata:
  name: postgres-db-lb
spec:
  selector:
    app: postgres-stockticker
  ports:
    - name: "5432"
      port: 5432
      targetPort: 5432
  type: ClusterIP

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-stockticker
spec:
  selector:
    matchLabels:
      app: postgres-stockticker
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres-stockticker
    spec:
      containers:
      - name: postgres-stockticker
        image: goodwinmcd/postgres:1.0
        imagePullPolicy: Always
        ports:
        - containerPort: 5432
      # Data Volume
        volumeMounts:
        - name: postgresql-db-disk
          mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgresql-db-disk
          persistentVolumeClaim:
            claimName: postgres-pv-claim
      imagePullSecrets:
        - name: docker-hub-repo-key
---

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume  # 设置 PV 的名称
  labels:
    type: local  # 设置 PV 的类型为本地
    app: postgres-stockticker
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi # 设置 PV 容量
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim  # 设置 PVC 的名称
  labels:
    app: postgres-stockticker
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany  # 设置读写权限
  resources:
    requests:
      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:

# PostgreSQL StatefulSet Service
apiVersion: v1
kind: Service
metadata:
  name: postgres-db-lb
spec:
  selector:
    app: postgres-stockticker
  ports:
    - name: "5432"
      port: 5432
      targetPort: 5432
  type: ClusterIP

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-stockticker
spec:
  selector:
    matchLabels:
      app: postgres-stockticker
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres-stockticker
    spec:
      containers:
      - name: postgres-stockticker
        image: goodwinmcd/postgres:1.0
        imagePullPolicy: Always
        ports:
        - containerPort: 5432
      # Data Volume
        volumeMounts:
        - name: postgresql-db-disk
          mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgresql-db-disk
          persistentVolumeClaim:
            claimName: postgres-pv-claim
      imagePullSecrets:
        - name: docker-hub-repo-key
---

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume  # Sets PV's name
  labels:
    type: local  # Sets PV's type to local
    app: postgres-stockticker
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi # Sets PV Volume
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim  # Sets name of PVC
  labels:
    app: postgres-stockticker
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany  # Sets read and write access
  resources:
    requests:
      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:

确定