Run an Interactive Pod with Volumes

A pretty common situation that occurs when you're working with Kubernetes or containers of any kind, is that you have a locked down image with no root access, no apt or apt-get and no curl. But sometimes you want to put files into that container. For me, it was copying backups into an Elasticsearch container.

One way of doing it is having a shared volume. The Elasticsearch cluster already has a readwritemany volume for snapshots, i.e. the exact location where I want to put the files. The volume is a Longhorn volume though, so just finding the directory on one of the Kubernetes nodes isn't possible.

Interactive pod to the rescue! With kubectl run you can spin up a new pod with a bash shell running in the same namespace as your service. However, there's no -f argument to kubectl run, so how do you mount a volume into it?

There's a more convoluted way to adapt your pod: --overrides. Here's an override that mounts a volume:

kubectl run -n=elasticsearch -i --rm --tty ubuntu --overrides='{
  "apiVersion": "v1",
  "spec": {
    "containers": [
      {
        "name": "ubuntu",
        "image": "ubuntu",
        "args": [
          "bash"
        ],
        "stdin": true,
        "stdinOnce": true,
        "tty": true,
        "volumeMounts": [
          {
            "mountPath": "/snapshots",
            "name": "elasticsearch-snapshots"
          }
        ]
      }
    ],
    "volumes": [
      {
        "name": "elasticsearch-snapshots",
        "persistentVolumeClaim": {
          "claimName": "elasticsearch-snapshots"
        }
      }
    ]
  }
}
'  --image=ubuntu --restart=Never -- bash

Running this will take you into a new pod, where /data is mounted to the volume. Happy curling!

comments powered by Disqus
Find me on Mastodon