我开始使用 kubernetes,我想创建一个带有单个 webserver 和 postgres 数据库的简单应用程序。我遇到的问题是 postgres 的部署给了我权限错误。以下是围绕这个问题的讨论:
- https://github.com/docker-library/postgres/issues/116
- https://github.com/docker-library/postgres/issues/103
- https://github.com/docker-library/postgres/issues/696
- 无法在 AKS 中获得 Postgres 权限或 PVC
- Kubernetes - 封装 DB 的 Pod 崩溃
- 将本地目录挂载到 minikube 的 pod 中
- https://serverfault.com/questions/981459/minikube-using-a-storageclass-to-provision-data-outside-of-tmp
编辑
规格:
- OSX - 10.15.4
- minikube - v1.9.2
- Kubernetes - v1.18.2
minikube设置
minikube start --driver=virtualbox --cpus=2 --memory=5120 --kubernetes-version=v1.18.2 --container-runtime=docker --mount=true --mount-string=/Users/holmes/kubernetes/pgdata:/data/pgdata
权限错误:chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
我正在尝试将本地操作系统目录挂载到 minikube 中,以便与 postgres 部署/pod/container 卷挂载一起使用。
运行上述设置后,我 ssh 进入 minikube ( minikube ssh
) 并检查权限
# minikube: /
drwxr-xr-x 3 root root 4096 May 13 19:31 data
# minikube: /data
drwx------ 1 docker docker 96 May 13 19:27 pgdata
chmod
通过在权限错误表面下运行脚本。如果我更改--mount-string=/Users/holmes/kubernetes/pgdata:/data
(省略/pgdata
)然后minikube ssh
创建pgdata
目录:
mkdir -p /data/pgdata
chmod 777 /data/pgdata
我在部署之前获得了一组不同的权限
# minikube: /
drwx------ 1 docker docker 96 May 13 20:10 data
# minikube: /data
drwxrwxrwx 1 docker docker 64 May 13 20:25 pgdata
并在部署后
# minikube: /
drwx------ 1 docker docker 128 May 13 20:25 data
# minikube: /data
drwx------ 1 docker docker 64 May 13 20:25 pgdata
不知道为什么会发生这种变化,并且chmod
权限错误仍然存在。似乎上面的参考链接在不同的虚拟机上的不同机器上跳来跳去,我不明白也不能让它工作。有人可以带我去工作吗?通过上述所有讨论超级困惑。
postgres.yaml
apiVersion: v1
kind: Namespace
metadata:
name: data-block
---
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
namespace: data-block
labels:
type: starter
data:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: docker
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres-pv
namespace: data-block
labels:
app: postgres
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /data/pgdata
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pv-claim
namespace: data-block
labels:
app: postgres
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: ""
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
namespace: data-block
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:12.2
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: postgres-config
volumeMounts:
- name: postgres-vol
mountPath: /var/lib/postgresql/data
volumes:
- name: postgres-vol
persistentVolumeClaim:
claimName: postgres-pv-claim
更新
我继续将部署脚本更新为一个简单的 pod。目标是将 postgres 映射/var/lib/postgresql/data
到我的本地文件目录/Users/<my-path>/database/data
以保存数据。
---
apiVersion: v1
kind: Pod
metadata:
name: postgres-pod
namespace: data-block
labels:
name: postgres-pod
spec:
containers:
- name: postgres
image: postgres:12.3
imagePullPolicy: IfNotPresent
ports:
- name: postgres-port
containerPort: 5432
envFrom:
- configMapRef:
name: postgres-env-config
- secretRef:
name: postgres-secret
volumeMounts:
- name: postgres-vol
mountPath: /var/lib/postgresql/data
volumes:
- name: postgres-vol
hostPath:
path: /Users/<my-path>/database/data
restartPolicy: Never
错误:initdb: error: could not access directory "/var/lib/postgresql/data": Permission denied
如何挂载本地文件目录?