max*_*722 3 deployment postgresql containers docker kubernetes
我正在尝试将 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.
Run Code Online (Sandbox Code Playgroud)
就我而言,@Rico 的答案并没有帮助我开箱即用,因为我不使用带有持久存储挂载的 postgres,这意味着没有 /var/lib/postgresql/data 文件夹和预先存在的数据库(所以在我的例子中,两个提议的选项都失败了)。
为了成功应用 postgres 设置,我仅使用了 args (没有command部分)。
在这种情况下,k8s 会将这些参数传递给 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"]
Run Code Online (Sandbox Code Playgroud)
要检查设置是否已应用:
$ kubectl exec -it postgres -- bash
root@postgres:/# su postgres
$ psql -c 'show max_connections;'
max_connections
-----------------
207
(1 row)
Run Code Online (Sandbox Code Playgroud)
您没有共享您的 Pod/Deployment 定义,但我相信您想shared_buffers在 Pod 定义中从实际容器(而不是 init 容器)的命令行进行设置。如果您使用的是部署,则类似如下:
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
Run Code Online (Sandbox Code Playgroud)
请注意,如果您使用ConfigMap,您也可以执行此操作(请注意,除了共享缓冲区之外,您可能还想添加更多配置选项):
apiVersion: v1
kind: ConfigMap
metadata:
name: postgresql-config
data:
postgresql.conf: |
shared_buffers=256MB
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2884 次 |
| 最近记录: |