73

I have defined a Deployment for my app:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myapp-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: 172.20.34.206:5000/myapp_img:2.0
        ports:
        - containerPort: 8080

Now, if I want update my app's image 2.0 to 3.0, I do this:

  1. $ kubectl edit deployment/myapp-deployment
  2. vim is open. I change the image version from 2.0 to 3.0 and save.

How can it be automated? Is there a way to do it just running a command? Something like:

$ kubectl edit deployment/myapp-deployment --image=172.20.34.206:5000/myapp:img:3.0

I thought using Kubernetes API REST but I don't understand the documentation.

4 Answers 4

85

You could do it via the REST API using the PATCH verb. However, an easier way is to use kubectl patch. The following command updates your app's tag:

kubectl patch deployment myapp-deployment -p \
  '{"spec":{"template":{"spec":{"containers":[{"name":"myapp","image":"172.20.34.206:5000/myapp:img:3.0"}]}}}}'

According to the documentation, YAML format should be accepted as well. See Kubernetes issue #458 though (and in particular this comment) which may hint at a problem.

4
  • Thanks, I'm getting an error but I'm going to post another question
    – Héctor
    Commented Apr 29, 2016 at 6:19
  • 1
    To be honest, I didn't have the time to try the command out myself yet. Will try to do so later and come back to you! Commented Apr 29, 2016 at 14:56
  • 1
    My JSON spec wasn't completely correct: The container definition is more deeply nested. I updated my answer and double-checked it's working correctly. Commented Apr 29, 2016 at 21:51
  • 2
    Is there a way to also update automatically the yaml when patch?
    – Héctor
    Commented May 2, 2016 at 7:01
34

There is a set image command which may be useful in simple cases

Update existing container image(s) of resources. Possible resources include (case insensitive): pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), job, replicaset (rs)

kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N

http://kubernetes.io/docs/user-guide/kubectl/kubectl_set_image/

$ kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
deployment "nginx-deployment" image updated

http://kubernetes.io/docs/user-guide/deployments/

4

(I would have posted this as a comment if I had enough reputation)

Yes, as per http://kubernetes.io/docs/user-guide/kubectl/kubectl_patch/ both JSON and YAML formats are accepted.

But I see that all the examples there are using JSON format. Filed https://github.com/kubernetes/kubernetes.github.io/issues/458 to add a YAML format example.

1
  • 1
    I amended my answer with links to the documentation and the issue you created. Thanks! Commented May 17, 2016 at 0:03
3

I have recently built a tool to automate deployment updates when new images are available, it works with Kubernetes and Helm:

https://github.com/rusenask/keel

You only have to label your deployments with Keel policy like keel.sh/policy=major to enable major version updates, more info in the readme. Works similarly with Helm, no additional CLI/UI required.

1
  • 1
    so here is where keel started, nice.
    – Moulick
    Commented Nov 11, 2020 at 15:19

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