大网数据广告活动
服务器运维问答

kubectl patch: add port to deployment's pod template

· 52CE 我爱测

I have a deployment running one pod consisting of an unique container. The deployment is currently up & running, and I want to modify its pod template to add a port to the container. Here are the currently defined ports: $ kubect…

问题原文

原帖链接 · Elouan Keryell-Even · CC BY-SA 4.0

I have a deployment running one pod consisting of an unique container. The deployment is currently up & running, and I want to modify its pod template to add a port to the container.

Here are the currently defined ports:

$ 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

Here is the patch I tried to add port 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

...no success.

Another attempt, this time specifying the port protocol:

$ 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

...still no success.


$ 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"}

已采纳解答

原帖链接 · apisim · CC BY-SA 4.0

How about this:

kubectl patch deployment spark-master --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/ports/-", "value": {"containerPort": 6066}}]'

Edit: removed "protocol": "TCP" as suggested.

Edit #2 (to address Magellan's comment): The default type for patch is --type='strategic'. The attempted patch uses JSON Patch and hence the --type='json' argument had to be added to make the command work.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-08-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。