Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass environment variable in kubectl deployment?

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?

like image 508
rakeshh92 Avatar asked May 06 '19 10:05

rakeshh92


People also ask

How do I set an environment variable in Kubernetes deployment?

Define an environment variable for a container To set environment variables, include the env or envFrom field in the configuration file. Note: The environment variables set using the env or envFrom field override any environment variables specified in the container image.

How do I update environment variables in Kubernetes?

When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the env or envFrom field in the configuration file. In your shell, run the printenv command to list the environment variables. To exit the shell, enter exit .


1 Answers

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  
like image 133
Max Lobur Avatar answered Sep 20 '22 08:09

Max Lobur