Search code examples
kuberneteskubernetes-helmpersistent-volumes

How can I browse a persistent volume in kubernetes and edit files with GUI?


I have a Bitnami WordPress helm release, I need to browse the files and edit some from the persistent volumes it created. Ideally I'd be able to browse the files using a GUI (like Nautilus/GNOME files) and edit the files using VS Code for ease of use.

Should I edit the files from within a mounted container? How can I do that? Are there other ways to go about this?


Solution

  • Before all I should mention that everything below is for development environment. Do not think of doing manual changes into container apps in production. For production you should make it so that all the necessary changes are applied automatically.

    Copy files from remote machine and back

    # Copy TO local machine
    kubectl cp <namespace_name>/<pod_name>:<remote_file_path> <local_file_path> -c <container_name>
    
    # Copy FROM local machine
    kubectl cp <local_file_path> <namespace_name>/<pod_name>:<remote_file_path> -c <container_name>
    
    # More examples
    kubectl cp --help
    

    -c <container_name> can be omitted if there's only one container in the pod.

    If you unsure which <namespace_name> or <pod_name> to use, you can try figuring that out with kubectl get pods --all-namespaces.

    If you are unsure which remote path you should type, you can try using ls and pwd in remote container:

    kubectl exec -n <namespace_name> <pod_name> -c <container_name> ls <remote_path>` 
    # e.g.
    kubectl exec -n my_namespace my_pod -c wordpress ls /var/www
    kubectl exec -n my_namespace my_pod -c wordpress pwd
    

    Edit files remotely with a console editor

    Works if your container image has a shell in it and main process started by root user. Start console session with:

    kubectl exec -it -n <namespace_name> <pod_name> -c <container_name> sh
    

    Install some console text editor (vim, nano, etc.) and use it. How to install depends on what Linux was used as the base for the image, you can find out using cat /etc/os-release.

    Editing with GUI

    One way to work with GUI that I see, is to launch a web-based text editor (like jupyter) alongside the app. You would need to modify the Deployment (or StatefulSet, DaemonSet, etc) and then you can launch proxy to the new container.

    Here's an example how to add jupyter to a pod:

    1. Modify the deployment by adding jupyter container:
    - name: jupyter
      image: jupyter/base-notebook
      securityContext:
        runAsUser:  # insert UID that uses your app
      args:
      - jupyter
      - notebook
      - --ip=0.0.0.0
      - --allow-root # if the UID is 0
      workingDir: /data
      ports:
      - containerPort: 8888
      volumeMounts:
        - mountPath: /data
          name: # insert name of the volume with files you want to modify
    
    1. Use kubectl port-forward to establish a connection with the pod:
    kubectl port-forward -n <namespace> <pod_name> 8888:8888
    

    After that you can access jupyter on localhost:8888. See jupyter logs to obtain the access key (kubectl logs -n <namespace> <pod_name> -c jupyter).