6

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

1 Answer 1

10

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .