Skip to content

Instantly share code, notes, and snippets.

@yuanying
Last active January 26, 2024 13:58
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save yuanying/3aa7d59dcce65470804ab43def646ab6 to your computer and use it in GitHub Desktop.
Save yuanying/3aa7d59dcce65470804ab43def646ab6 to your computer and use it in GitHub Desktop.
kubectl run with PVCs
#!/bin/bash
IMAGE="gcr.io/google-containers/ubuntu-slim:0.14"
COMMAND="/bin/bash"
SUFFIX=$(date +%s | shasum | base64 | fold -w 10 | head -1 | tr '[:upper:]' '[:lower:]')
usage_exit() {
echo "Usage: $0 [-c command] [-i image] PVC ..." 1>&2
exit 1
}
while getopts i:h OPT
do
case $OPT in
i) IMAGE=$OPTARG
;;
c) COMMAND=$OPTARG
;;
h) usage_exit
;;
\?) usage_exit
;;
esac
done
shift $(($OPTIND - 1))
VOL_MOUNTS=""
VOLS=""
COMMA=""
for i in $@
do
VOL_MOUNTS="${VOL_MOUNTS}${COMMA}{\"name\": \"${i}\",\"mountPath\": \"/pvcs/${i}\"}"
VOLS="${VOLS}${COMMA}{\"name\": \"${i}\",\"persistentVolumeClaim\": {\"claimName\": \"${i}\"}}"
COMMA=","
done
kubectl run -it --rm --restart=Never --image=${IMAGE} pvc-mounter-${SUFFIX} --overrides "
{
\"spec\": {
\"hostNetwork\": true,
\"containers\":[
{
\"args\": [\"${COMMAND}\"],
\"stdin\": true,
\"tty\": true,
\"name\": \"pvc\",
\"image\": \"${IMAGE}\",
\"volumeMounts\": [
${VOL_MOUNTS}
]
}
],
\"volumes\": [
${VOLS}
]
}
}
" -- ${COMMAND}
@miv
Copy link

miv commented Aug 18, 2018

Because this is debug tool, i suggest adding general NoSchedule toleration to spec, like this:

    \"tolerations\": [
      {
        \"effect\": \"NoSchedule\",
        \"operator\": \"Exists\"
      }
    ],

With this it would run on nodes that have any NoSchedule taints on them

@bskaggs
Copy link

bskaggs commented Aug 18, 2018

Exactly what I needed, thanks for sharing!

@EnigmaCurry
Copy link

mucho gracias.

@EnriqueTejeda
Copy link

Many thanks, very useful!!!

@guzmonne
Copy link

This was incredibly useful. Thank you!

@infa-salmeda
Copy link

thanks a lot

@infa-salmeda
Copy link

how to set the PodSpec.SecurityContext ?

@infa-salmeda
Copy link

infa-salmeda commented May 18, 2021

``found it
\"spec\": { \"securityContext\": { \"runAsUser\": 1000, \"runAsGroup\": 1000, \"fsGroup\": 2000 },

@beatsandpics
Copy link

beatsandpics commented Aug 26, 2021

I was unable to run a command with an argument like "ls /pvcs".
I've added the following and now it works

ARGS=`echo $COMMAND | jq -cR 'split(" ")'`

kubectl run -it --rm --restart=Never --image=${IMAGE} pvc-mounter-${SUFFIX} --overrides "
{
  \"spec\": {
    \"hostNetwork\": true,
    \"containers\":[
      {
        \"args\": ${ARGS},
        \"stdin\": true,
        \"tty\": true,
        \"name\": \"pvc\",
        \"image\": \"${IMAGE}\",
        \"volumeMounts\": [
          ${VOL_MOUNTS}
        ]
      }
    ],
    \"volumes\": [
      ${VOLS}
    ]
  }
}
" -- ${COMMAND}

@dmitry-mightydevops
Copy link

thanks! Time saver for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment