k8s: Use parameterized image tag when creating deployment

9/27/2018

I want to run a kubernetes deployment in the likes of the following:

apiVersion: v1
kind: Deployment
metadata:
  name: my-deployment
  namespace: my-namespace
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: my-app
        image: our-own-registry.com/somerepo/my-app:${IMAGE_TAG}
        env:
        - name: FOO
          value: "BAR"

This will be delivered to the developers so that they can perform on demand deployments using the image tag of their preference.

What is best way / recommended pattern to pass the tag variable?

performing an export on the command line to make it available as env var on the shell from which the kubectl command will run?

-- pkaramol
deployment
kubernetes
shell

2 Answers

9/28/2018

For simple use cases envsubst will do just fine:

IMAGE_TAG=1.2 envsubst < deployment.yaml | kubectl apply -f -`
-- Jukka
Source: StackOverflow

9/27/2018

Unfortunately, it's impossible via native kubernetes tools. From here:

kubectl will never support variable substitution.

But, that issue case also has some good workarounds. The best way is deploy your apps via Helm charts using templates

-- Konstantin Vustin
Source: StackOverflow