【问题标题】:kubectl create pod using override return error: Invalid JSON Patchkubectl create pod using override return error: Invalid JSON Patch
【发布时间】:2021-11-26 21:39:19
【问题描述】:

我正在尝试使用以下命令运行我的 pod,但不断出现错误:

error: Invalid JSON Patch
kubectl run -i tmp-pod --rm -n=my-scripts --image=placeholder --restart=Never --overrides= "$(cat pod.json)"

这是我的pod.json 文件:

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "name": "test",
        "namespace": "my-ns",
        "labels": {
            "app": "test"
        }
    },
    "spec": {
        "containers": [
            {
                "name": "test",
                "image": "myimage",
                "command": [
                    "python",
                    "/usr/bin/cma/excute.py"
                ]
            }
        ]
    }
}

我在这里做错了什么?

【问题讨论】:

    标签: json kubernetes kubectl kubernetes-pod


    【解决方案1】:

    它对我有用,但是我尝试删除命名空间

    pod.json

    {
        "apiVersion": "v1",
        "kind": "Pod",
        "metadata": {
            "name": "test",
            "labels": {
                "app": "test"
            }
        },
        "spec": {
            "containers": [
                {
                    "name": "test",
                    "image": "myimage",
                    "command": [
                        "python",
                        "/usr/bin/cma/excute.py"
                    ]
                }
            ]
        }
    }
    

    命令

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

    【讨论】:

    • 您正在运行哪个操作系统,我在 cmder 上的 Windows 上运行。但仍然出现错误
    • @user1591156 您使用的是某种虚拟机还是 WSL? $()cat 是特定于 bash 的命令,在 Windows 中不起作用
    • 对,我用的是 ubuntu,所以 cat 可以工作
    • 我正在使用cmder,其中cat 有效,但仍然出现错误。无论如何我可以调试kubectl 命令来检查哪一行导致error: Invalid JSON Patch.
    【解决方案2】:

    我做了一些测试,似乎 Cmder 没有正确执行 $() 存在问题 - 要么根本不工作,要么将换行符视为 Enter,因此之前执行了一个命令整个 JSON 被传递。

    您可能想尝试在 PowerShell 中运行您的命令:

    kubectl run -i tmp-pod --rm -n=my-scripts --image=placeholder --restart=Never --overrides=$(Get-Content pod.json -Raw)
    

    在 GitHub [Windows] kubectl run not accepting valid JSON as --override on Windows (same JSON works on Mac) #519 上有一个类似的问题。不幸的是,对此没有明确的解决方案。

    可能的解决方案是:

    • 直接将 JSON 作为字符串传递
      kubectl run -i tmp-pod --rm -n=my-scripts --image=placeholder --restart=Never --overrides='{"apiVersion":"v1","kind":"Pod","metadata":{...}}'
      
    • 在覆盖周围使用' 而不是"
    • 在 JSON 文件中使用三引号 """ 而不是单引号 "

    【讨论】:

      猜你喜欢
      • 2021-01-08
      • 2022-11-20
      • 1970-01-01
      • 2021-07-07
      • 2018-05-02
      • 1970-01-01
      • 2017-02-04
      • 2014-05-18
      • 2019-11-08
      相关资源
      最近更新 更多