Persistent Volume Claim permissions
Due to some internal networking and firewall issues, I cannot currently use the postgres managed azure service so I need to deploy my own postgres db.
I followed the following guide pretty closely to setup a persistent volume mount on a postgres image: https://docs.microsoft.com/en-us/azure/aks/azure-files-dynamic-pv
When I got my deployment running, my pod (a postgresql container) gives me permission errors to the volume:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
initdb: could not change permissions of directory "/var/lib/postgresql/data": Operation not permitted
fixing permissions on existing directory /var/lib/postgresql/data ...
Yaml (names have been changed to protect the innocent):
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: footestdb
provisioner: kubernetes.io/azure-file
parameters:
storageAccount: footestdb
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: footestdb
spec:
accessModes:
- ReadWriteOnce
storageClassName: footestdb
resources:
requests:
storage: 60Gi
---
kind: Service
apiVersion: v1
metadata:
name: foo-test-db
annotations:
service.beta.kubernetes.io/azure-load-balancer-internal: "true"
labels:
app: foo-test-db
spec:
type: LoadBalancer
selector:
app: foo-test-db
ports:
- port: 5432
name: postgresql
---
kind: Deployment
apiVersion: apps/v1beta1
metadata:
name: foo-test-db
spec:
replicas: 1
template:
metadata:
name: foo-test-db
labels:
app: foo-test-db
spec:
containers:
- name: foo-test-db
image: postgres:10.3
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: footestdb
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: hive
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: foo-db-creds
key: username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: foo-db-creds
key: password
volumes:
- name: footestdb
persistentVolumeClaim:
claimName: footestdb
Does this help?
Mount options
If using a cluster of version 1.8.5 or greater, mount options can be specified on the storage class object. The following example sets 0777.
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: azurefile
provisioner: kubernetes.io/azure-file
mountOptions:
- dir_mode=0777
- file_mode=0777
- uid=1000
- gid=1000
parameters:
skuName: Standard_LRS
@neilpeterson I'm using 1.7.x so it should be 777 (permissive) by default
Maybe I should upgrade?
Hi @AXington, the permission of azure file is set in the mounting moment, after that, azure file permission could not be changed. So for postgres container, it could not run on azure file.
In stead, you could use azure disk, I have tried it works well. And pls note that if you mount azure disk directly mountPath: /var/lib/postgresql/data, it will fail either with following error:
nitdb: directory "/var/lib/postgresql/data" exists but is not emptyIt contains a lost+found directory, perhaps due to it being a mount point
Instead, use subPath will work, it will create a new directory in the new azure disk.
So here is the two places that I have modifed in your config
- use azure disk
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: footestdb
provisioner: kubernetes.io/azure-disk
parameters:
skuName: Standard_LRS
kind: Managed
cachingmode: None
- use subPath (it will create a new directory in the newly attached azure disk)
volumeMounts:
- mountPath: /var/lib/postgresql/
subPath: data
name: footestdb
Complete config is here, I have verified, it works well: https://gist.github.com/andyzhangx/4d4c01683a4e7de305b086568cbffcf8
BTW, before redeploy, you need to delete all resources, paste the delete command here:
kubectl delete deployment foo-test-db
kubectl delete pvc footestdb
kubectl delete sc footestdb
kubectl delete service foo-test-db
I also tried to get jenkins image mount directories as non root with azure file. So responses in here make me little bit unsure should e.g. StorageClass actually work or not?
I think I tried it as above but didn't see any affect on permissions. Can you verify does aks support any working way to mount volumes (azure file) as non root. By googling there is multiple ways to do it with kubernetes itself but trial and error takes forever and no guarantee to make it still work..
@kattelus it depends on k8s version and for dynamic provision, mountOptions should be set in storage class, while for static provision(use existing file share), mountOptions should be set in PersistentVolume.
Details are here: https://github.com/andyzhangx/demo/blob/master/linux/azurefile/azurefile-mountoptions.md
Ok thank you.. my mistake probably was to try use storage class with existing volume. I haven't tested does options on persistent volume work because changed to use root for image but I will try it at some point .
close the issue now since it's already resolved, let me know if you have any question.