如何在 Kubernetes pod 内升级 postgresql?

jus*_*dev 5 postgresql kubernetes

我有一个运行应用程序的 kubernetes 集群。集群的一部分是 postgresql pod,当前运行版本 10.4。不幸的是,我发现我需要升级postgresql版本。

postgres yaml 如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:10.4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim

Run Code Online (Sandbox Code Playgroud)

postgresql 数据库中已经有一些数据。我需要找到一种在生产过程中升级集群的方法。

如果我只是尝试将映像更改为 12.0 并运行,kubectl apply则会出现错误:

2020-11-15 22:48:08.332 UTC [1] DETAIL:  The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 12.5 (Debian 12.5-1.pgdg100+1).
Run Code Online (Sandbox Code Playgroud)

所以我明白我需要手动升级集群内的postgres数据库,然后我才能修复yaml。那是对的吗?

jus*_*dev 6

我尝试了@Justin方法,但遇到了一个问题,我无法停止pod内当前正在运行的postgres进程(由于容器内的某种原因,无法访问postgresql服务。您可以在此处查看有关该问题的更多信息

由于我无法专门在 pod 内升级 postgresql,所以我最后所做的是在 Kubernetes 中创建一个并行的 postgres pod,以保存新版本。然后我从旧服务器转储数据库,将其复制到新服务器,并用它来初始化那里的数据库。

以下是一一步骤:

  1. 使用新版本创建并行 postgres 服务

  2. 在旧版本的 Pod 中:

pg_dumpall -U postgresadmin -h localhost -p 5432 > dumpall.sql
Run Code Online (Sandbox Code Playgroud)
  1. 在主机中:
kubectl cp postgres-old-pod:/dumpall.sql dumpall.sql
kubectl cp dumpall.sql postgres2-new-pod:/dumpall.sql
Run Code Online (Sandbox Code Playgroud)
  1. ssh 到新 Pod

  2. 我需要额外的步骤,因为由于某种原因新的 pod 没有创建“postgres”用户:使用您的凭据进入 postgres 客户端:

psql postgresql://postgresadmin:pass1234@127.0.0.1:5432/postgresdb?sslmode=disable
postgresdb=# CREATE ROLE postgres LOGIN SUPERUSER PASSWORD 'somepassword123';
Run Code Online (Sandbox Code Playgroud)

然后退出postgres并退出到普通用户

  1. 最后更新数据库:
psql -U postgres -W -f dumpall.sql
Run Code Online (Sandbox Code Playgroud)


Jus*_*lyn 0

使用这个如何将 postgresql 数据库从 10 升级到 12 而不会丢失 openproject 的数据作为我的帖子的基础。我正在将其转换为容器与卷友好的方法。我假设您正在使用 Docker Hub 上的官方 Postgresql 映像。

  1. 备份数据 - 超出了本答案的范围。还有其他人更适合回答这个问题。

  2. 从 pod 内部升级 postgres 并迁移数据 在 postgres pod 中获取 shell

# insert your pod and namespace here
kubectl exec -it postgresl-shsdjkfshd -n default /bin/sh 
Run Code Online (Sandbox Code Playgroud)

在容器内运行以下命令

apt update
apt-get install postgresql-12 postgresql-server-dev-12
service postgresql stop
# Migrate the data
su postgres
/usr/lib/postgresql/12/bin/pg_upgrade \
     --old-datadir=/var/lib/postgresql/10/main \
     --new-datadir=/var/lib/postgresql/12/main \
     --old-bindir=/usr/lib/postgresql/10/bin \
     --new-bindir=/usr/lib/postgresql/12/bin \
     --old-options '-c config_file=/etc/postgresql/10/main/postgresql.conf' \
     --new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf'
exit # exits the postgres user
Run Code Online (Sandbox Code Playgroud)

下一点是从链接的帖子中逐字摘录的:

  1. 交换新旧 postgres 版本的端口。
     vim /etc/postgresql/12/main/postgresql.conf
     #change port to 5432
     vim /etc/postgresql/10/main/postgresql.conf
     #change port to 5433
Run Code Online (Sandbox Code Playgroud)
  1. 启动postgresql服务
     service postgresql start
Run Code Online (Sandbox Code Playgroud)
  1. 以 postgres 用户身份登录
     su postgres
Run Code Online (Sandbox Code Playgroud)
  1. 检查你的新 postgres 版本
     psql -c "SELECT version();"
Run Code Online (Sandbox Code Playgroud)
  1. 运行生成的新集群脚本
     ./analyze_new_cluster.sh
Run Code Online (Sandbox Code Playgroud)
  1. 返回为普通(默认用户)用户并清理旧版本的混乱
     apt-get remove postgresql-10 postgresql-server-dev-10
     #uninstalls postgres packages
     rm -rf /etc/postgresql/10/
     #removes the old postgresql directory
     su postgres
     #login as postgres user
     ./delete_old_cluster.sh
     #delete the old cluster data
Run Code Online (Sandbox Code Playgroud)
  1. 现在将部署 YAML 映像引用更改为 Postgres 12 并kubectl apply

  2. 检查日志以查看其是否正确启动。