Home > OS >  Kubernetes deployment helm running first command in shell then run outside the shell
Kubernetes deployment helm running first command in shell then run outside the shell

Time:10-14

I am trying to run some commands in my K8 deployment yaml.

spec:
  containers:
    - command:
      - /bin/sh
      - -c
      - export ABC=hijkl
      - command 2

Basically, I need to run the export command in the shell. After which, it should continue to run command 2 outside the shell. I can't seem to get the syntax right (eg. am I missing &&, or double quotes etc). Can anyone help? Thanks in advance!

CodePudding user response:

The Bourne shell sh -c option takes only a single command word, so anything you want to run in that shell needs to be in a single YAML list item.

spec:
  containers:
    - command:
        - /bin/sh
        - -c
        - export ABC=hijkl; command 2

You'll frequently see YAML block scalars used in a context like this, so you can have embedded newlines and it will look more like a normal shell script.

If you're just setting an environment variable to a fixed string, you can also do that at the Kubernetes layer and skip the intermediate shell:

spec:
  containers:
    - env:
        - name: ABC
          value: hijkl
      command:
        - command
        - '2' # (note, YAML single quotes so this is read as a string)

CodePudding user response:

Can you try using the export command within ` or '

Below is a reference :

spec:
  containers:
    - command:
      - /bin/sh
      - -c
      - export 'ABC=hijkl'
      - command 2
  • Related