【问题标题】:kubectl run pod with dynamic scriptkubectl 使用动态脚本运行 pod
【发布时间】:2021-12-07 11:29:32
【问题描述】:

我有一个在启动时运行我的 python 脚本的 pod。我想让我的kubectl 命令接受一个输入参数并传递给我的python 脚本。这是我的 pod 的示例

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "name": "mypod",
         
         
    },
    "spec": {
        "restartPolicy": "Never",

        "containers": [
            {
                "name": "mypod",
                "image": "myimage:1",
                "imagePullPolicy": "Always",
                "command": [
                    "python",
                    "/usr/bin/cma/excute.py", "argument"
                ]

            }
        ]
    }
}

这是我用来创建我的 pod 的 kubectl

kubectl run -i tmp-pod --rm -n=mynamespace --image=placeholder --restart=Never "--overrides=\"$(cat "mypod.json ")\" 

是否可以将 python 脚本参数与 kubeclt 命令一起传递?

【问题讨论】:

  • 不确定你想要的输出,但这条线上的一些东西可能会帮助k run mypod --image=myimage:1 --dry-run=client -ojson --command python -- /usr/bin/cma/excute.py argument1 --overrides="$(cat mypod.json)"
  • @P....感谢您的意见。无论如何我可以使用我的yaml 代替json 文件吗?
  • 再次不确定我是否理解正确,但是如果您的意思是使用 yaml,那么只需将 -ojson 更改为 -oyaml
  • @P.... 整个想法是每次使用动态参数运行我的 python 脚本,一旦脚本完成删除 pod。

标签: kubernetes command-line-arguments kubectl


【解决方案1】:

您可以看到如何使用 python 传递参数的示例。您可以在this link 中查看更多文档。

Consider the following script test.py −
#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)


    Now run the above script as follows 
$ python test.py arg1 arg2 arg3

This produces the following result 

Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

您可以使用 JSON 或 YAML 格式。

这里你可以看到如何用JSON创建一个Pod。你可以看到更多documentation

Create a pod using the data in pod.json
kubectl create -f ./pod.json

Create a pod based on the JSON passed into stdin
cat pod.json | kubectl create -f -

Edit the data in docker-registry.yaml in JSON then create the resource using the edited data
kubectl create -f docker-registry.yaml --edit -o json
And commands to  execute
$ kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2022-11-02
    • 2022-01-27
    • 2021-02-19
    相关资源
    最近更新 更多