Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 views
in Technique[技术] by (71.8m points)

kubernetes - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...