Kubectl apply does not update pods or deployments

Viewed 8952

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 Answers

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

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.

Related