Skip to content

add subPath to volumeMount postgres-data#367

Open
xring wants to merge 1 commit into
iflytek:mainfrom
xring:fix-postgres-init-fail
Open

add subPath to volumeMount postgres-data#367
xring wants to merge 1 commit into
iflytek:mainfrom
xring:fix-postgres-init-fail

Conversation

@xring

@xring xring commented Apr 30, 2026

Copy link
Copy Markdown

Summary

  • What changed?

add subPath to postgres-data volumeMount

  • Why is this needed?

Full Deployment (with PostgreSQL + Redis) may fail because of lost+found:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
initdb: detail: It contains a lost+found directory, perhaps due to it being a mount point.
initdb: hint: Using a mount point directly as the data directory is not recommended.
Create a subdirectory under the mount point.

Validation

  • Backend tests passed
  • Frontend typecheck/build passed
  • OpenAPI SDK regenerated or checked when API contracts changed
  • Smoke test run when relevant

Commands run:

kubectl apply -k deploy/k8s/overlays/with-infra/

Risk

  • User-facing impact:
  • Deployment or migration impact:
  • Rollback approach:

Notes

  • Related issue:
  • Follow-up work:
  • Docs or operator runbooks updated when behavior changed:

@CLAassistant

CLAassistant commented Apr 30, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@XiaoSeS

XiaoSeS commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Review: subPath 对已有部署的兼容性

代码本身 LGTM,subPath: pgdata 是解决 lost+found 问题的标准做法。

潜在风险

对于已有数据的部署,加上 subPath 后 PG 会看到一个空的 pgdata/ 子目录,触发重新 initdb,老数据(在 PV 根目录)从容器视角"消失"。

建议:添加 init container 自动迁移

可以加一个 init container,在 PG 启动前检测并自动迁移数据:

initContainers:
  - name: migrate-pgdata
    image: busybox:1.36
    command:
      - sh
      - -c
      - |
        # If PG_VERSION exists at root but not in subPath, migrate
        if [ -f /data/PG_VERSION ] && [ ! -f /data/pgdata/PG_VERSION ]; then
          echo "Migrating existing data to pgdata/ subdirectory..."
          mkdir -p /data/pgdata
          # Move everything except pgdata/ and lost+found/
          for item in /data/*; do
            base=$(basename "$item")
            if [ "$base" != "pgdata" ] && [ "$base" != "lost+found" ]; then
              mv "$item" /data/pgdata/
            fi
          done
          echo "Migration complete."
        else
          echo "No migration needed."
        fi
    volumeMounts:
      - name: postgres-data
        mountPath: /data

这样:

  • 全新部署/data 为空(或只有 lost+found),init container 跳过,PG 正常 initdb 到 /data/pgdata/
  • 已有部署升级:init container 检测到 PG_VERSION 在根目录,自动把数据移到 pgdata/ 子目录,PG 启动后无缝衔接

如果觉得 init container 过重,也可以在 PR 描述的 Risk 栏里注明"仅适用于全新部署,已有部署需手动迁移数据"即可。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants