<p>I am trying to execute some scripts as part of statefulset deployment kind. This script I have added as configmap and I use this as volumeMount inside the pod definition. I use the lifecycle poststart exec command to execute this script. It fails with the permission issue.</p>
<p>based on certain articles, I found that we should copy this file as part of InitContainer and then use that (I am not sure why should we do and what will make a difference)
Still, I tried it and that also gives the same error.</p>
<p>Here is my ConfigMap:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-configmap-initscripts
data:
  poststart.sh: |
     #!/bin/bash
     echo &quot;It`s done&quot;
</code></pre>
<p>Here is my StatefulSet:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres-statefulset
spec:
  ....
  serviceName: postgres-service
  replicas: 1
  template:
    ...
    spec:
      initContainers:
      - name: &quot;postgres-ghost&quot;
        image: alpine
        volumeMounts:
        - mountPath: /scripts
          name: postgres-scripts
      containers:
      - name: postgres
        image: postgres
        lifecycle:
            postStart:
              exec:
                command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;/scripts/poststart.sh&quot; ]
        ports:
        - containerPort: 5432
          name: dbport
        ....
        volumeMounts:
        - mountPath: /scripts
          name: postgres-scripts
volumes:
  - name: postgres-scripts
    configMap:
      name: postgres-configmap-initscripts
      items:
      - key: poststart.sh
        path: poststart.sh
</code></pre>
<p>The error I am getting:</p>
<p><a href="https://i.stack.imgur.com/cuHDY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cuHDY.png" alt="enter image description here" /></a></p>

Brijesh Shah
<p><code>postStart</code> hook will be call at least once but may be call <strong>more than once</strong>, this is not a good place to run script.</p>
<p>The <code>poststart.sh</code> file that mounted as ConfigMap will not have execute mode hence the permission error.</p>
<p>It is better to run script in <code>initContainers</code>, here's an quick example that do a simple <code>chmod</code>; while in your case you can execute the script instead:</p>
<pre><code>cat &lt;&lt; EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
  name: busybox
data:
  test.sh: |
    #!/bin/bash
    echo &quot;It's done&quot;
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox
  labels:
    run: busybox
spec:
  volumes:
  - name: scripts
    configMap:
      name: busybox
      items:
      - key: test.sh
        path: test.sh
  - name: runnable
    emptyDir: {}
  initContainers:
  - name: prepare
    image: busybox
    imagePullPolicy: IfNotPresent
    command: [&quot;ash&quot;,&quot;-c&quot;]
    args: [&quot;cp /scripts/test.sh /runnable/test.sh &amp;&amp; chmod +x /runnable/test.sh&quot;]
    volumeMounts:
    - name: scripts
      mountPath: /scripts
    - name: runnable
      mountPath: /runnable
  containers:
  - name: busybox
    image: busybox
    imagePullPolicy: IfNotPresent
    command: [&quot;ash&quot;,&quot;-c&quot;]
    args: [&quot;while :; do . /runnable/test.sh; sleep 1; done&quot;]
    volumeMounts:
    - name: scripts
      mountPath: /scripts
    - name: runnable
      mountPath: /runnable
EOF
</code></pre>
