我尝试使用kubectl patch
将另外两个值添加到 kubernetes 部署的 args 列表中。我已经浏览了官方记录的 (https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/( 变体,但没有设法一次附加多个值。
假设以下简单部署:
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
replicas: 1
selector:
matchLabels:
name: test
template:
metadata:
labels:
name: test
name: test
spec:
containers:
- image: alpine
name: test
command:
- echo
args:
- my
- text
我现在想将其他值附加到args
部分。这一次适用于单个值:
添加单个附加值
kubectl patch deployments.apps test --type=json -p='[{"op": "add", "path": "/spec/t
emplate/spec/containers/0/args/-", "value": "additional" }]'
这有效并给我留下了以下内容:
...
args:
- my
- text
- additional
但是使用值数组运行补丁会给我一个错误:
# running:
k patch deployments.apps test --type=json -p='[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": ["additional","text"] }]'
# results in:
The request is invalid: patch: Invalid value: "...": v1.Deployment.Spec: v1.DeploymentSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.Args: []string: ReadString: expects " or n, but found [, error found in #10 byte of ...|itional",["additiona|..., bigger context ...|{"containers":[{"args":["my","text","additional",["additional","text"]],"command":["echo"],"image":"|...
有谁知道一种在不覆盖整个 args 数组的情况下将多个值添加到单个补丁命令中的数组的方法?感谢您的帮助。
下面使用单个补丁,但它不是很干燥:
kubectl patch deployment <deployment-name> -n <namespace> --type "json" -p '[
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-1"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-2"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-3"}]'
我一直在为证书管理器做类似的事情,以允许全自动 TLS:
kubectl patch deployment cert-manager -n cert-manager --type "json" -p '[
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-name=letsencrypt-prod"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-kind=ClusterIssuer"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-group=cert-manager.io"}]'
我发现最简单的方法是使用jq
来编辑 json,而不是:
kubectl patch deployment <deployment-name> -n <namespace> --type "json" -p '[
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-1"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-2"},
{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-3"}]'
您可以使用:
kubectl get deployment <deployment-name> -n <namespace> -o json
| jq '.spec.template.spec.containers[0].args += ["arg-1", "arg-2", "arg-3"]'
| kubectl apply -f -
这有一个优点:它甚至允许注入诸如修补权限之类的对象。示例(取自将 coredns 升级到 1.8.3 的要求(:
kubectl get clusterrole system:coredns -n kube-system -o json
| jq '.rules += [{"apiGroups":["discovery.k8s.io"],"resources":["endpointslices"],"verbs":["list","watch"]}]'
| kubectl apply -f -
另一个优点:在应用之前很容易进行测试,只需去除kubectl apply -f -
部分:
kubectl get clusterrole system:coredns -n kube-system -o json
| jq '.rules += [{"apiGroups":["discovery.k8s.io"],"resources":["endpointslices"],"verbs":["list","watch"]}]'