how to set pod's file or directory access rights?

11/6/2018

I successfully created pod shinyinfo-jenkins-pod.yaml, and shinyinfo-jenkins-svc.yaml, and I could see pod is running. In the pod yaml file, I mount two volumes. But how to change pod's directory access right after pod runs?

I use command as follows:

[master@master1 ~]$ sudo kubectl exec -it shinyinfo-jenkins -- /bin/bash
jenkins@shinyinfo-jenkins:/$
jenkins@shinyinfo-jenkins:/$
jenkins@shinyinfo-jenkins:/$ chmod 777 /var/jenkins_home
chmod: changing permissions of '/var/jenkins_home': Operation not permitted
jenkins@shinyinfo-jenkins:/$ sudo chmod 777 /var/jenkins_home
bash: sudo: command not found
jenkins@shinyinfo-jenkins:/$ su
su: must be run from a terminal

As one could see from above, I have no way to change mounted directory access right.

shinyinfo-jenkins-pod.yaml file:

apiVersion: v1
kind: Pod
metadata:
 name: shinyinfo-jenkins
 labels:
   app: shinyinfo-jenkins
spec:
 containers:
   - name: shinyinfo-jenkins
     image: shinyinfo_jenkins
     imagePullPolicy: Never
     ports:
       - containerPort: 8080
         containerPort: 50000
     volumeMounts:
     - mountPath: /devops/password
       name: jenkins-password
     - mountPath: /var/jenkins_home
       name: jenkins-home
 volumes:
   - name: jenkins-password
     hostPath:
       path: /jenkins/password
   - name: jenkins-home
     hostPath:
       path: /jenkins
-- user84592
kubernetes

1 Answer

11/6/2018

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.(ref:https://kubernetes.io/docs/concepts/storage/volumes/#hostpath).

To enable privileged mode:

spec:
 containers:
   securityContext:
     privileged: true # Processes in privileged containers are essentially equivalent to root on the host.
-- nightfury1204
Source: StackOverflow