how to pass environment variable in kubectl deployment?

ghz 1years ago ⋅ 2736 views

Question

I am setting up the kubernetes setup for django webapp.

I am passing environment variable while creating deployment as below

kubectl create -f deployment.yml -l key1=value1 

I am getting error as below

error: no objects passed to create

Able to create the deployment successfully, If i remove the env variable -l key1=value1 while creating deployment.

deployment.yaml as below

#Deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
 labels: 
   service: sigma-service
 name: $key1

What will be the reason for causing the above error while creating deployment?


Answer

I used envsubst (<https://www.gnu.org/software/gettext/manual/html_node/envsubst- Invocation.html>) for this. Create a deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: $NAME
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Then:

export NAME=my-test-nginx
envsubst < deployment.yaml | kubectl apply -f -

Not sure what OS are you using to run this. On macOS, envsubst installed like:

brew install gettext
brew link --force gettext