5

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

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.

  • You should also explain why this answer addresses the OP's question. – Magellan Aug 5 '18 at 23:19
  • Works fine, thanks. Could you remove "protocol": "TCP" from your answer? It works fine without it. – Elouan Keryell-Even Aug 6 '18 at 9:10
  • Works great for adding (appending) args to a container as well! Just what I needed, thank you! – mcw0933 Apr 30 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.