Search code examples
kuberneteskubectl

Kubernetes deployment rolling update with semantic container names


I want to use a Kubernetes deployment and reference container images with semantic tag names. E.g. application:latest, application:testing, application:production.

Setting this up is straight forward and with the imagePullPolicy: Always within my container spec I can also push out new versions quickly with something like this:

 $ REPLICAS=$(kubectl get deployment application --template="{{.spec.replicas}}")
 $ kubectl scale --replicas=0 deployment application
 deployment "application" scaled
 $ kubectl scale --replicas=$REPLICAS deployment application
 deployment "application" scaled

The drawback is that all pods are killed before the new ones are ready. Therefore I'm looking to get rolling updates going.

One solution would be to drop the semantic tag names and use sth. like application:v123. But I'd love to keep my scripts simple and keep the semantic names. Is there any way to do that?

...

Edit/update ... this is what my deployment.yaml configuration would look like:

apiVersion: extensions/v1beta1
 kind: Deployment
 metadata:
   name: nginx-deployment
 spec:
   replicas: 3
   template:
     metadata:
       labels:
         app: application
     spec:
       containers:
       - name: application
         image: application:latest
         imagePullPolicy: Always

It would basically stay the same all the time, but the related container image is updated in the background.

...

Cheers


Solution

  • Seems that this isn't possible using one semantic tag. Here's what I started using with two or more tags.

    1) Figure our what's currently deployed:

     DEPLOYED_IMAGE=$(kubectl get deployment/nginx-deployment -o jsonpath="{.spec.template.spec.containers[0].image}")
    

    2) Find the unused tag:

    if [[ "$DEPLOYED_IMAGE" =~ :green$ ]]; then 
       IMAGE="application:latest-blue"
    else 
       IMAGE="application:latest-green"
    fi
    

    3) Build the image with the new tag:

    docker build -t $IMAGE .
    docker push $IMAGE
    

    4) Deploy

    kubectl set image deployment/nginx-deployment application=$IMAGE
    

    5) Wait until the deployment is done

    kubectl rollout status deployment nginx-deployment -w