将Postgres参数传递到Kubernetes部署中

3
我正在尝试将一个postgres参数(shared_buffers)设置到我的postgres数据库pod中。我想设置一个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文件吗? - Matt
2个回答

8

在我的情况下,@Rico的回答并没有对我有所帮助,因为我没有使用具有持久存储挂载的postgres,这意味着没有/var/lib/postgresql/data文件夹和预存在的数据库(因此两个提出的选项在我的情况下都失败了)。

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

在这种情况下,k8s将把这些args传递给docker镜像中定义的默认入口点(文档),至于postgres入口点,它被设计成任何传递给docker命令的选项都将传递给postgres服务器守护进程(请查看https://hub.docker.com/_/postgres中的Database Configuration部分)

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)

5

你没有分享你的Pod/Deployment定义,但我相信你想在实际容器(而不是init容器)的命令行中设置shared_buffers。如果你使用的是deployment,可以像这样操作:

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用户身份运行该命令? - mmiara
您可以尝试使用使用postgres用户/组的securityContext运行容器。我已更新部署定义。 - Rico

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接