【问题标题】:How to upgrade postgresql inside a Kubernetes pod?如何在 Kubernetes pod 中升级 postgresql?
【发布时间】:2020-11-15 22:53:23
【问题描述】:

我有一个运行应用程序的 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

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).

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

【问题讨论】:

标签: postgresql kubernetes


【解决方案1】:

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

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

以下是一一步骤:

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

  2. 在旧版本的 pod 中:

pg_dumpall -U postgresadmin -h localhost -p 5432 > dumpall.sql
  1. 在主机中:
kubectl cp postgres-old-pod:/dumpall.sql dumpall.sql
kubectl cp dumpall.sql postgres2-new-pod:/dumpall.sql
  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';

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

  1. 最后更新数据库:
psql -U postgres -W -f dumpall.sql

【讨论】:

    【解决方案2】:

    使用这个How to upgrade postgresql database from 10 to 12 without losing data for 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 
    

    在容器内运行以下代码

    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
    

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

    1. 交换新旧 postgres 版本的端口。
         vim /etc/postgresql/12/main/postgresql.conf
         #change port to 5432
         vim /etc/postgresql/10/main/postgresql.conf
         #change port to 5433
    
    1. 启动postgresql服务
         service postgresql start
    
    1. 以 postgres 用户身份登录
         su postgres
    
    1. 检查您的新 postgres 版本
         psql -c "SELECT version();"
    
    1. 运行生成的新集群脚本
         ./analyze_new_cluster.sh
    
    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
    
    1. 现在将部署 YAML 图像引用更改为 Postgres 12 和 kubectl apply

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

    【讨论】:

    猜你喜欢
    • 2018-12-13
    • 1970-01-01
    • 2019-11-24
    • 2020-09-22
    • 2018-03-04
    • 2019-01-12
    • 1970-01-01
    • 2023-02-02
    • 2021-11-09
    相关资源
    最近更新 更多