Kubernetes部署-传递参数 [英] Kubernetes Deployment - Pass arguments

查看:190
本文介绍了Kubernetes部署-传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 kubectl 命令应用部署文件时,我想将参数传递给Kubernetes部署.

示例:在我的部署.yaml中,我具有以下参数,并且当我使用 kubectl apply-f< my-deployment> .yaml 运行时,我想传递参数值>

因此,在下面的示例中,我想在运行上述kubectl命令时覆盖args-用户名和角色.

 规范:容器:-名称:testimage图片:<我的图片名称>:<标签>args:---userid = testuser---role =经理 

解决方案

简单的答案是.你不能那样做.

kubectl 不是模板引擎.正如某些人提到的那样,您可以使用Helm或Kustomize之类的选项来解决此问题.我请您研究Helm3,因为它可以通过 helm upgrade --install ... --set userid = xxx --set role = yyy 这样的命令很好地解决了您的问题.

如果您只对kubectl感兴趣,则可能要使用它从stdin提取yaml并将yaml首先通过任何类型的模板传递的功能.IE.如下:

  ...args:---userid = $ USER---role = $ ROLE... 

  cat resource.yaml |USER = testuser ROLE =管理器环境|kubectl适用-f- 

很明显,任何其他字符串替换方法(sed,awk等)都可以做到

I want to pass arguments to Kubernetes deployment when I use kubectl command to apply the deployment file.

Example: In my deployment .yaml, I have arguments as below and I want to pass the argument values when I run with the kubectl apply - f <my-deployment>.yaml

So, in the below example, I want to override the args - userid and role when I run the above kubectl command.

spec:
      containers:
        - name: testimage
          image: <my image name>:<tag>
          args:
          - --userid=testuser
          - --role=manager

解决方案

The simple answer is. You can't do that.

kubectl is not a template engine. As some people mentioned, you have options like Helm or Kustomize which can solve this. I'd encurage you to look into Helm3 since it nicely solves your problem with a command like helm upgrade --install ... --set userid=xxx --set role=yyy.

If you're stuck with kubectl only though, you might want to use it's ability to ingest yaml from stdin and pass your yaml through any type of templating first. ie. as follows :

...
args:
- --userid=$USER
- --role=$ROLE
...

cat resource.yaml | USER=testuser ROLE=manager envsubst | kubectl apply -f -

obviously any other string replacement method would do (sed, awk, etc.)

这篇关于Kubernetes部署-传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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