Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass "--command" to "kubectl run" that also uses "--overrides"

I noticed a strange behavior while experimenting with kubectl run :

  • When the command to be executed is passed as option flag --command -- /bin/sh -c "ls -lah" > OK

    kubectl run nodejs --image=node:lts-alpine \
      --restart=Never --quiet -i --rm \
      --command -- /bin/sh -c "ls -lah"
    
  • When command to be executed is passed in --overrides with "command": [ "ls", "-lah" ] > OK

    kubectl run nodejs --image=node:lts-alpine \
      --restart=Never \
      --overrides='
      {
        "kind": "Pod",
        "apiVersion": "v1",
        "metadata": {
          "name": "nodejs"
        },
        "spec": {
          "volumes": [
            {
              "name": "host-volume",
              "hostPath": {
                "path": "/home/dferlay/Sources/df-sdc/web/themes/custom/"
              }
            }
          ],
          "containers": [
            {
              "name": "nodejs",
              "image": "busybox",
              "command": [
                "ls",
                "-lah"
              ],
              "workingDir": "/app",
              "volumeMounts": [
                {
                  "name": "host-volume",
                  "mountPath": "/app"
                }
              ],
              "terminationMessagePolicy": "FallbackToLogsOnError",
              "imagePullPolicy": "IfNotPresent"
            }
          ],
          "restartPolicy": "Never",
          "securityContext": {
            "runAsUser": 1000,
            "runAsGroup": 1000
          }
        }
      }
      ' \
      --quiet -i --rm
    
  • When the command to be executed is passed as option flag --command -- /bin/sh -c "ls -lah" and --overrides is used for something else (volume for instance) > KO

    kubectl run nodejs --image=node:lts-alpine --restart=Never \
      --overrides='
      {
        "kind": "Pod",
        "apiVersion": "v1",
        "metadata": {
          "name": "nodejs"
        },
        "spec": {
          "volumes": [
            {
              "name": "host-volume",
              "hostPath": {
                "path": "/home/dferlay/Sources/df-sdc/web/themes/custom/"
              }
            }
          ],
          "containers": [
            {
              "name": "nodejs",
              "image": "busybox",
              "workingDir": "/app",
              "volumeMounts": [
                {
                  "name": "host-volume",
                  "mountPath": "/app"
                }
              ],
              "terminationMessagePolicy": "FallbackToLogsOnError",
              "imagePullPolicy": "IfNotPresent"
            }
          ],
          "restartPolicy": "Never",
          "securityContext": {
            "runAsUser": 1000,
            "runAsGroup": 1000
          }
        }
      }
      ' \
      --quiet -i --rm --command -- /bin/sh -c "ls -lah"
    

So it looks like using --overrides prevents --command to be used.

However, I precisely need to use --command to bypass the array format expected by --overrides (ie. "command": [ "ls", "-lah" ]) because in my use case the command is a placeholder and cannot be known in advance.

  • How can I do that ? Is there something I'm missing ?

FYI: kubectl version=v1.23.1+k3s2

like image 680
David Avatar asked Oct 20 '25 02:10

David


1 Answers

You can bypass the array format by using the args field:

"command": [
 "sh",
 "-c"
],
"args": [ "pwd && id && node YOUR_COMMAND" ]
like image 171
Anton Avatar answered Oct 21 '25 21:10

Anton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!