2
votes

I'm using a CI to update my kubernetes cluster whenever there's an update to an image. Whenever the image is pushed and has the latest tag it kubectl apply's the existing deployment but nothing gets updated.

this is what runs $ kubectl apply --record --filename /tmp/deployment.yaml

My goal is when the apply is ran that a rolling deployment gets executed.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
    name: api
spec:
    replicas: 1
    template:
    metadata:
        labels:
        app: api
    spec:
        containers:
        - name: api
        image: us.gcr.io/joule-eed41/api:latest
        imagePullPolicy: Always
        ports:
            - containerPort: 1337
        args:
            - /bin/sh
            - -c
            - echo running api;npm start
        env:        
        - name: NAMESPACE
            valueFrom:
            configMapKeyRef:
                name: config
                key: NAMESPACE
2
What's in your YAML file?kichik
@kichik added the yamlHannan Rhodes
the issue is that k8s doesn't know that actual image tagged changed. you have 2 solutions: 1) remove existing pods, then new version will be pulled 2) use new version each time.uthark
@uthark what do you mean by use the new version each time?Hannan Rhodes
@HannanRhodes your docker image should be published under strict version, not floating latest. e.g. 0.1, 0.2, 0.3, etc.uthark

2 Answers

1
votes

As others suggested, have a specific tag. Set new image using following command

kubectl set image deployment/deployment_name deployment_name=image_name:image_tag

In your case it would be

kubectl set image deployment/api api=us.gcr.io/joule-eed41/api:0.1

0
votes

As @ksholla20 mentionedm using kubectl set image is a good option for many (most?) cases.

But if you can't change the image tag consider using:

1 ) kubectl rollout restart deployment/<name> (reference).

2 ) kubectl patch deployment <name> -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"version\":\"$CURRENT_BUILD_HASH_OR_DATE\"}}}}}}" (reference)

(*) Notice that the patch command allow you to change specific properties in the deployment (or any other object chosen) like the label selector and the pod label or other properties like the value of the NAMESPACE environment variable in your example.