我有一个部署正在运行一个由唯一容器组成的 Pod。该部署当前已启动并正在运行,我想修改其 Pod 模板以向容器添加端口。
以下是当前定义的端口:
$ kubectl get deployment -o yaml spark-master | yq -r -y '.spec.template.spec.containers[] | select(.name=="spark-master").ports'
- containerPort: 7077
protocol: TCP
- containerPort: 8080
protocol: TCP
这是我尝试添加端口的补丁6066
:
$ kubectl patch deployment spark-master -p '{"op": "add", "path": "/spec/template/spec/containers/0/ports/-", "value": {"containerPort": 6066}}'
deployment "spark-master" not patched
$ kubectl get deployment -o yaml spark-master | yq -r -y '.spec.template.spec.containers[] | select(.name=="spark-master").ports'
- containerPort: 7077
protocol: TCP
- containerPort: 8080
protocol: TCP
...没有成功。
再次尝试,这次指定端口协议:
$ kubectl patch deployment spark-master -p '{"op": "add", "path": "/spec/template/spec/containers/0/ports/-", "value": {"containerPort": 6066, "protocol": "TCP"}}'
deployment "spark-master" not patched
$ kubectl get deployment -o yaml spark-master | yq -r -y '.spec.template.spec.containers[] | select(.name=="spark-master").ports'
- containerPort: 7077
protocol: TCP
- containerPort: 8080
protocol: TCP
...仍然没有成功。
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.4", GitCommit:"bee2d1505c4fe820744d26d41ecd3fdd4a3d6546", GitTreeState:"clean", BuildDate:"2018-03-12T16:29:47Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9+", GitVersion:"v1.9.7-gke.3", GitCommit:"9b5b719c5f295c99de68ffb5b63101b0e0175376", GitTreeState:"clean", BuildDate:"2018-05-31T18:32:23Z", GoVersion:"go1.9.3b4", Compiler:"gc", Platform:"linux/amd64"}
答案1
这个怎么样:
kubectl patch deployment spark-master --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/ports/-", "value": {"containerPort": 6066}}]'
编辑:"protocol": "TCP"
按建议删除。
编辑 #2(针对 Magellan 的评论):的默认类型patch
是--type='strategic'
。尝试的补丁使用JSON 补丁因此--type='json'
必须添加参数才能使命令起作用。