How to use environmental value inisde kubernetes yaml file

6/12/2019

I have one kubernetes deployment file for e.g:

I want that image_tag is passed at the command line when running the kubectl create -f deployment.yaml command. and suppose i did the export IMAGE_TAG=1.4.3 and want to use that ENV variable value is inserted at the position of image tag.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
     containers:
     - name: nginx
       image: nginx:{IMAGE_TAG}
    ports:
     - containerPort: 80
-- tarun mittal
docker
google-kubernetes-engine
kubernetes
kubernetes-helm
yaml

3 Answers

6/12/2019

most elegant way. you need to install envsubst binary

export key1=val1
export key2=val2
envsubst < deployment.yaml | kubectl apply -f -
-- P Ekambaram
Source: StackOverflow

6/12/2019

I do this:

sed -i "s/{IMAGE_TAG}/${IMAGE_TAG}/" deployment.yml
kubectl apply -f deployment.yml

as it is not supported by kubectl

-- Abhyudit Jain
Source: StackOverflow

6/12/2019

Kubectl doesn't support out of the box variables. Because it's tags based. For what you want to achieve you have different options. The most popular option is Helm, but,Kustomize, a new player is getting a lot of traction from the community. You can also use other tools like Terraform which I think a very decent option but unfortunately overlooked.

-- abelgana
Source: StackOverflow