I want to deploy postgres using kubernetes
This is my postgres pod yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
labels:
app: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
securityContext:
runAsUser: 70
runAsGroup: 70
fsGroup: 70
fsGroupChangePolicy: "Always"
containers:
- image: docker.io/postgres:14.8-alpine3.18
name: postgres
resources:
limits:
hugepages-2Mi: 512Mi
memory: 2Gi
cpu: "8"
requests:
memory: 128Mi
cpu: "1"
env:
- name: POSTGRES_DB
value: postgres_db_name
- name: POSTGRES_USER
value: postgres_db_user
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secrets
key: root_password_key
- name: PGDATA
value: /some/path/here
ports:
- containerPort: 5432
name: postgres
volumeMounts:
- name: postgres-volume-name
mountPath: /some/path/here
volumes:
- name: postgres-volume-name
persistentVolumeClaim:
claimName: postgres-pv-claim
After running
kubectl get pods
I POD status is terminating, so I have checked logs and
It shows
mkdir: can’t create directory ‘/some/path/here’: Permission denied
How can I solve this?
Thanks!
2
Answers
The error you are seeing is due to the file permission and you are not able to create a directory.
You can change the security context first & if not you can use the init container to change the file permission too.
Try this stateful set if it’s fine with you
As per official Kubernetes doc on Allow users to skip recursive permission changes on mount:
While inspecting the YAML used for the
StatefulSet
, noticed there’s the use of afsGroup
inside the pod’ssecurity context
, which makes sure that the volume’s content can be readable and writable by each new pod. One side-effect of setting ‘fsGroup’ is that, each time a volume is mounted, Kubernetes must recursively change the owner and permission of all the files and directories inside the volume. This happens even if group ownership of the volume already matches the requested ‘fsGroup’, and can be pretty expensive for larger volumes with lots of small files, which causes pod startup to take a long time.Solution : As per Configure volume permission and ownership change policy for Pods. Suggest setting
'fsGroupChangePolicy' to "OnRootMismatch"
so if the root of the volume already has the correct permissions, the recursive permission change can be skipped.*Also refer to the System Admin blog by LiveStream related to the Error, which may help to resolve your issue.