如何在Kubectl部署中传递环境变量? [英] how to pass environment variable in kubectl deployment?
问题描述
我正在为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屋!