【问题标题】:Pass postgres parameter into Kubernetes deployment将 postgres 参数传递到 Kubernetes 部署中
【发布时间】:2020-07-02 03:09:20
【问题描述】:

我正在尝试在我的 postgres 数据库 pod 中设置一个 postgres 参数 (shared_buffers)。我正在尝试设置一个 init 容器来设置 db 变量,但它不起作用,因为 init 容器以 root 用户身份运行。

在 pod 上编辑 db 变量的最佳方法是什么?我没有能力在图像中进行更改,因为不同实例的变量需要不同。如果有帮助,我需要运行的命令是“postgres -c”命令。

"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise.  See the documentation for
more information on how to properly start the server.

【问题讨论】:

  • 能否包含您正在尝试的部署 yaml?

标签: postgresql docker kubernetes deployment containers


【解决方案1】:

您没有共享您的 Pod/Deployment 定义,但我相信您想在 Pod 定义中从实际容器(而不是 init 容器)的命令行设置 shared_buffers。如果您使用的是部署,则类似这样:

apiVersion: v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:12.2
          imagePullPolicy: "IfNotPresent"
          command: ["postgres"] # <-- add this
          args: ["-D", "-c", "shared_buffers=128MB"] # <-- add this
          ports:
            - containerPort: 5432
          securityContext:
            runAsUser: 1000
            runAsGroup: 1000
            fsGroup: 1000
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
            - name: postgresql-config-volume # <-- use if you are using a ConfigMap (see below)
              mountPath: /var/lib/postgres/data/postgresql.conf
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim # <-- note: you need to have this already predefined
        - name: postgresql-config-volume # <-- use if you are using a ConfigMap (see below)
          configMap:
            name: postgresql-config

请注意,如果您使用的是ConfigMap,您也可以这样做(请注意,除了 shared_buffers 之外,您可能还需要添加更多配置选项):

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgresql-config
data:
  postgresql.conf: |
    shared_buffers=256MB

【讨论】:

  • 我在底部的问题中添加了我所看到的内容。看起来我无法以 root 用户身份使用该命令。以 postgres 用户身份运行命令的任何方式?
  • 您可以尝试使用使用 postgres 用户/组的 securityContext 运行容器。我更新了部署定义。
【解决方案2】:

在我的情况下,@Rico answer 并没有帮助我开箱即用,因为我不使用带有持久存储挂载的 postgres,这意味着没有 /var/lib/postgresql/data 文件夹和 pre-现有数据库(因此在我的情况下,两个提议的选项都失败了)。

为了成功应用 postgres 设置,我只使用了 args(没有 command 部分)。

在这种情况下,k8s 会将这些 args 传递给 docker 镜像中定义的默认入口点(docs),而对于 postgres 入口点,它会将传递给 docker 命令的任何选项传递给postgres 服务器守护进程(查看数据库配置部分:https://hub.docker.com/_/postgres

apiVersion: v1
kind: Pod
metadata:
  name: postgres
spec:
  containers:
    - image: postgres:9.6.8
      name: postgres
      args: ["-c", "shared_buffers=256MB", "-c", "max_connections=207"]

检查设置是否应用:

$ kubectl exec -it postgres -- bash
root@postgres:/# su postgres
$ psql -c 'show max_connections;'
 max_connections
-----------------
 207
(1 row)

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2020-09-21
    • 1970-01-01
    • 2019-06-12
    • 2021-02-22
    • 2020-11-07
    • 2021-07-05
    • 1970-01-01
    • 2020-09-08
    相关资源
    最近更新 更多