Kubernetes - Run chmod on deployment

4/9/2021

I have a Kubernetes pod running Laravel but I get the following permission error on load:

Permission denied

If I access the pod interactively and run chmod on /storage:

chmod on storage

The laravel app works. How can I get this command to run on deployment? I've tried the following but I get a 502 nginx error:

apiVersion: apps/v1
kind: Deployment
metadata:
    name: app
spec:
    replicas: 1
    selector:
        matchLabels:
            container: app
    template:
        metadata:
            labels:
                container: app
        spec:
            containers:
                - name: app
                  image: my/toolkit-app:test
                  command: ["chmod -R 777 /storage"]
                  securityContext:
                      runAsUser: 0
                  ports:
                      - containerPort: 80
            imagePullSecrets:
                - name: my-cred
-- Lee
kubernetes
yaml

1 Answer

4/9/2021

You can use a PostStart Container hook.

apiVersion: apps/v1
kind: Deployment
metadata:
    name: app
spec:
    replicas: 1
    selector:
        matchLabels:
            container: app
    template:
        metadata:
            labels:
                container: app
        spec:
            containers:
                - name: app
                  image: my/toolkit-app:test
                  securityContext:
                      runAsUser: 0
                  ports:
                      - containerPort: 80
                  lifecycle:
                      postStart:
                          exec:
                              command: ["/bin/sh", "-c", "chmod -R 777 /storage"]
            imagePullSecrets:
                - name: my-cred

One thing consider:

This hook is executed immediately after a container is created. However, there is no guarantee that the hook will execute before the container ENTRYPOINT. No parameters are passed to the handler.

-- Daniel Karapishchenko
Source: StackOverflow