首页 > 解决方案 > Kubernetes(在 Docker for Windows 中)Postgres 的卷配置

问题描述

我有一个用 docker-compose 测试的 tomcat + postgres 应用程序。我正在尝试将应用程序打包在 kubernetes 配置文件中。

目前,我正在使用我的 Docker Desktop for Windows 安装运行 kubernetes(和 kubectl)。最终,我想部署到其他环境。

我目前正在尝试在以下配置文件中复制 docker-compose 中的一些卷功能。

apiVersion: v1
kind: Pod
metadata:
  name: pg-pod
spec:
  volumes:
  - name: "pgdata-vol"
    #emptyDir: {}
    hostPath:
      path: /c/temp/vols/pgdata
  containers:
  - image: postgres
    name: db
    ports:
    - containerPort: 5432
      name: http
      protocol: TCP
    volumeMounts:
    - mountPath: "/pgdata"
      name: "pgdata-vol"
    env:
    - name: PGDATA
      value: /pgdata

当 postgres 启动时,我看到以下错误。

fixing permissions on existing directory /pgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers ... 400kB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
2019-07-26 20:43:41.844 UTC [78] FATAL:  data directory "/pgdata" has wrong ownership
2019-07-26 20:43:41.844 UTC [78] HINT:  The server must be started by the user that owns the data directory.
child process exited with exit code 1
initdb: removing contents of data directory "/pgdata"
running bootstrap script ...

我假设我需要为我的卷定义提供一些额外的参数,或者我需要尝试不同类型的卷配置(localhostPath)。

标签: kubernetesdocker-volume

解决方案


我找到了这个问题的部分解决方案。

有趣的是,如果我指定一个 linux 风格的路径作为我的主机路径(在 Windows 上),那么我的 pgdata-vol 会一直存在,直到重新启动 Docker Desktop。

而不是安装到真正的 Windows 位置

  volumes:
  - name: "pgdata-vol"
    hostPath:
      path: /c/temp/vols/pgdata

我使用“linux”位置作为我的 Windows hostPath

  volumes:
  - name: "pgdata-vol"
    hostPath:
      path: /tmp/vols/pgdata

奇怪的是,我实际上无法从 Windows 中找到这条路径。我认为这个 /tmp 对于我的 Docker 桌面实例是本地的。

该解决方案不能提供真正的持久性,但它帮助我解决了影响测试的障碍。


推荐阅读