我正在尝试在 Minikube 上设置 PostgreSQL,数据路径是我安装在 Minikube 上的主机文件夹(我想将我的数据保存在主机上)。
创建 kubernetes 对象(如下)后,我收到权限错误,与此处相同 How to solve permission trouble when running Postgresql from minikube?尽管提到的问题没有回答问题。它建议挂载 minikube 的 VM 目录。
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
labels:
app: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: storage
env:
- name: POSTGRES_PASSWORD
value: user
- name: POSTGRES_USER
value: pass
- name: POSTGRES_DB
value: k8s
volumes:
- name: storage
hostPath:
path: /data/postgres
除了在 Postgres 之上构建自己的图像并以某种方式使用权限之外,还有其他方法可以做到这一点吗?我在装有 Minikube 0.30.0 的 macOS 上使用 Virtualbox 和 Minikube 的 hyperkit 驱动程序时遇到了这种情况。
最佳答案
从这里查看这些行:hostPath
the files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged Container or modify the file permissions on the host to be able to write to a
hostPath
volume
所以,要么你必须以 root 身份运行,要么你必须更改 /data/postgres
目录的文件权限。
但是,您可以在不重建 docker 镜像的情况下以 root 身份运行 Postgres 容器。
您必须将以下内容添加到您的容器中:
securityContext:
runAsUser: 0
您的 yaml 应如下所示:
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
labels:
app: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: storage
env:
- name: POSTGRES_PASSWORD
value: user
- name: POSTGRES_USER
value: pass
- name: POSTGRES_DB
value: k8s
securityContext:
runAsUser: 0
volumes:
- name: storage
hostPath:
path: /data/postgres
关于postgresql - 在 Minikube 上挂载 Postgres 的主机目录 - 权限问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53378779/