[英]how to pass environment variable in kubectl deployment?
I am setting up the kubernetes setup for django webapp.我正在为 django webapp 设置 kubernetes 设置。
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.能够成功创建部署,如果我在创建部署时删除了环境变量 -l key1=value1。
deployment.yaml as below deployment.yaml 如下
#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?创建部署时导致上述错误的原因是什么?
I used envsubst ( https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html ) for this.我为此使用了 envsubst ( https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html )。 Create a
deployment.yaml
创建一个
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:
在 macOS 上,envsubst 安装如下:
brew install gettext
brew link --force gettext
This isn't a right way to use the deployment, you can't provide half details in yaml and half in kubectl commands.这不是使用部署的正确方法,您不能在 yaml 中提供一半细节,在 kubectl 命令中提供一半细节。 If you want to pass environment variables in your deployment you should add those detail in the
deployment spec.template.spec
:如果要在部署中传递环境变量,则应在
deployment spec.template.spec
中添加这些详细信息:
You should add following block to your deployment.yaml您应该将以下块添加到您的 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.导出环境变量的另一种方法是使用 kubectl run(不推荐),因为它很快就会贬值。 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
上面的命令将创建一个具有副本 1 和环境变量
var1=val1
的部署 nginx
You cannot pass variables to "kubectl create -f".您不能将变量传递给“kubectl create -f”。 YAML files should be complete manifests without variables.
YAML 文件应该是没有变量的完整清单。 Also you cannot use "-l" flag to "kubectl create -f".
您也不能将“-l”标志用于“kubectl create -f”。
If you want to pass environment variables to pod you should do like that:如果你想将环境变量传递给 pod,你应该这样做:
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/在此处阅读更多信息: https ://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/
Follow the below steps请按照以下步骤操作
create test-deploy.yaml创建 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 命令,您可以在部署时更新部署名称
sed -e 's|MYAPP|my-nginx|g' test-deploy.yaml | kubectl apply -f -
File: ./deployment.yaml
文件:
./deployment.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
File: ./service.yaml
文件:
./service.yaml
apiVersion: v1
kind: Service
metadata:
name: MYAPP
labels:
app: nginx
spec:
type: ClusterIP
ports:
- port: 80
selector:
app: nginx
File: ./kustomization.yaml
文件:
./kustomization.yaml
resources:
- deployment.yaml
- service.yaml
If you're using https://kustomize.io/ , you can do this trick in a CI:如果您使用的是https://kustomize.io/ ,则可以在 CI 中执行此技巧:
sh '( echo "images:" ; echo " - name: $IMAGE" ; echo " newTag: $VERSION" ) >> ./kustomization.yaml'
sh "kubectl apply --kustomize ."
I chose yq
since it is yaml aware and gives a precise control where text substitutions happen.我选择
yq
是因为它可以识别 yaml 并且可以精确控制文本替换发生的位置。
To set an image from bash env var:要从 bash env var 设置图像:
export IMAGE="your_image:latest"
yq eval '.spec.template.spec.containers[0].image = "'$IMAGE'"' manifests/daemonset.yaml | kubectl apply -f -
yq
is available on MacPorts (as of 19/04/21 v4.4.1) with sudo port install yq
yq
在 MacPorts(截至 19/04/21 v4.4.1)上可用sudo port install yq
I was facing the same problem.我面临着同样的问题。 I created a python script to change simple/complex or add values to the YAML file.
我创建了一个 python 脚本来更改简单/复杂或向 YAML 文件添加值。 This became very handy in a similar situation that you describe.
在您描述的类似情况下,这变得非常方便。 Also, switching to the python domain can allow for more complex scenarios.
此外,切换到 python 域可以允许更复杂的场景。
The code and how to use it are available at this gist.代码和如何使用它可以在这个要点上找到。 https://gist.github.com/washraf/f81153270c80b0b4ecf90a53872abde7
https://gist.github.com/washraf/f81153270c80b0b4ecf90a53872abde7
Please try following请尝试以下
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: kdpd00201
name: frontend
labels:
app: nginx
spec:
replicas: 6
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: frontend
image: ifccncf/nginx:1.14.2
ports:
- containerPort: 8001
env:
- name: NGINX_PORT
value: "8001"
My solution is then我的解决方案是
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: frontend
name: frontend
namespace: kdpd00201
spec:
replicas: 4
selector:
matchLabels:
app: frontend
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: frontend
spec:
containers:
- env: # modified level
- name: NGINX_PORT
value: "8080"
image: lfccncf/nginx:1.13.7
name: nginx
ports:
- containerPort: 8080
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.