how to pass environment variable in kubectl deployment?

你说的曾经没有我的故事 提交于 2019-12-11 07:26:37

问题


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?


回答1:


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 



回答2:


This isn't a right way to use the deployment, you can't provide half details in yaml and half in kubectl commands. If you want to pass environment variables in your deployment you should add those detail in the deployment spec.template.spec:

You should add following block to your deployment.yaml

spec:
  containers:
  - env:
    - name: var1
      value: val1

This will export your environment variables inside the container.

The other way to export the environment variable is use kubectl run (not advisable) as it is going to be depreciated very soon. You can use following command:

 kubectl run nginx --image=nginx --restart=Always --replicas=1 --env=var1=val1

The above command will create a deployment nginx with replica 1 and environment variable var1=val1




回答3:


You cannot pass variables to "kubectl create -f". YAML files should be complete manifests without variables. Also you cannot use "-l" flag to "kubectl create -f".

If you want to pass environment variables to pod you should do like that:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        env:
        - name: MY_VAT
          value: MY_VALUE
        ports:
        - containerPort: 80

Read more here: https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/




回答4:


Follow the below steps

create test-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: MYAPP
  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

using sed command you can update the deployment name at deployment time

sed -e 's|MYAPP|my-nginx|g' test-deploy.yaml | kubectl apply -f -


来源:https://stackoverflow.com/questions/56003777/how-to-pass-environment-variable-in-kubectl-deployment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!