如何将命令行参数传递给kubectl create命令 [英] How to pass command line argument to kubectl create command
问题描述
如果我使用以下命令运行 kubectl create -f deployment.yaml
命令以下 deployment.yaml
文件,一切成功.
If I run the kubectl create -f deployment.yaml
command with
the following deployment.yaml
file, everything succeeds.
apiVersion: v1
kind: Pod
metadata:
name: my_app
labels:
app: my_app
spec:
containers:
- name: my_app
image: docker:5000/path_to_my_custom_image
args: ["my_special_argument"]
但是,现在我想要一个自定义的"my_special_argument"如下
However, now I want to have a custom "my_special_argument" as follows
apiVersion: v1
kind: Pod
metadata:
name: my_app
labels:
app: my_app
spec:
containers:
- name: my_app
image: docker:5000/path_to_my_custom_image
args: ["$(ARG)"]
,当我执行 kubectl create -f deployment.yaml
命令时,我想以某种方式设置$ ARG $的值.该怎么做?
and I want to somehow set the value of $ARG$ when I execute the kubectl create -f deployment.yaml
command. How to do that?
我正在寻找类似的东西: kubectl create -f deployment.yaml --ARG = new_arg
I am looking for something like:
kubectl create -f deployment.yaml --ARG=new_arg
可以执行这样的命令吗?
Can such command be executed?
推荐答案
您可以在 deployment.yaml
apiVersion: v1
kind: Pod
metadata:
name: my_app
labels:
app: my_app
spec:
containers:
- name: my_app
image: docker:5000/path_to_my_custom_image
env:
- name: SPECIAL_ARG_VAL
value: "my_special_argument_val_for_my_app"
args: ["$(SPECIAL_ARG_VAL)"]
此外,您可以使用Secrets或Configmaps加载环境变量的值.
Also, you can load the value for environment variables using Secrets or Configmaps.
Here is an example loading value from configmap
apiVersion: v1
kind: Pod
metadata:
name: my_app
labels:
app: my_app
spec:
containers:
- name: my_app
image: docker:5000/path_to_my_custom_image
env:
- name: SPECIAL_ARG_VAL
valueFrom:
configMapKeyRef:
name: special-config
key: SPECIAL_VAL_KEY
args: ["$(SPECIAL_ARG_VAL)"]
您可以使用 kubectl
如下创建配置映射,但建议使用单独的yaml文件.
You can create the configmap using kubectl
as the following, but recommend to have a separate yaml file.
kubectl create configmap special-config --from-literal=SPECIAL_VAL_KEY=my_special_argument_val_for_my_app
如果您在镜像的Dockerfile中定义了相同的环境变量,甚至可以从上面的pod yaml中删除 args
.
You can even remove the args
from the pod yaml above if you had the same environment variable defined in the Dockerfile for the image.
这篇关于如何将命令行参数传递给kubectl create命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!