如何在 kubectl 部署中传递环境变量? [英] how to pass environment variable in kubectl deployment?

查看:90
本文介绍了如何在 kubectl 部署中传递环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 django webapp 设置 kubernetes.

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 

我收到如下错误

error: no objects passed to create

能够成功创建部署,如果我在创建部署时删除环境变量 -l key1=value1.

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

deployment.yaml 如下

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?

推荐答案

我使用了 envsubst (https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html)为此.创建一个 deployment.yaml

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

那么:

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

不确定您使用什么操作系统来运行它.在 macOS 上,envsubst 安装如下:

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

brew install gettext
brew link --force gettext 

这篇关于如何在 kubectl 部署中传递环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆