【问题标题】:kubectl patch existing container commandkubectl 修补现有容器命令
【发布时间】:2023-01-13 01:15:36
【问题描述】:

我已经启动并运行了 Kubernetes 部署: (为简洁起见省略了一些字段)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: argocd-server
  namespace: argocd
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: argocd-server
  template:
    metadata:
      creationTimestamp: null
      labels:
        app.kubernetes.io/name: argocd-server
    spec:
      containers:
        - name: argocd-server
          image: quay.io/argoproj/argocd:v2.2.5
          command:
            - argocd-server

我想为现有部署创建一个补丁,以将某些参数添加到容器的 command

            - '--insecure'
            - '--basehref'
            - /argocd

我阅读了有关 kubectl patch 命令 here 的文档,但我不确定如何实际选择我想要修补的容器(按名称或索引)。
覆盖完整的command:列表(在补丁文件中给出- argocd-server行)会很好,但我想阻止在补丁文件中给出完整的containers:规范。

【问题讨论】:

    标签: kubernetes kubectl


    【解决方案1】:

    您可以按索引选择容器,例如:

    kubectl patch deployment argocd-server -n argocd --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/command", "value": ["argocd-server", "--insecure"]}]'
    

    【讨论】:

    • 补丁 JSON 无效,因为它在路径中引用了 command,但指定了一个包含字段 command 的对象作为替换值。这是更正后的版本:kubectl patch deployment argocd-server -n argocd --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/command", "value": ["argocd-server", "--insecure"]}]'
    • 是的,我有点太快了。我会更新命令
    【解决方案2】:

    感谢@Blokje5 的启发,我能够构建这两个选项:

    JSON 方法

    排队

    kubectl patch deployment argocd-server -n argocd --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/command", "value": ["argocd-server", "--insecure", "--basehref", "/argocd"]}]'
    

    有补丁文件

    patch.json

    [
      {
        "op": "replace",
        "path": "/spec/template/spec/containers/0/command",
        "value": [
          "argocd-server",
          "--insecure",
          "--basehref",
          "/argocd"
        ]
      }
    ]
    
    kubectl -n argocd patch deployment argocd-server --type='json' --patch-file patch.json
    

    YAML 方法

    yaml文件

    patch.yaml

    ---
    op: replace
    spec:
      template:
        spec:
          containers:
            - name: argocd-server
              command:
                - argocd-server
                - --insecure
                - --basehref
                - /argocd
    
    kubectl -n argocd patch deployment argocd-server --patch-file patch.yaml
    

    【讨论】:

      猜你喜欢
      • 2018-05-12
      • 2019-01-09
      • 2020-12-14
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多