kubectl set image deployment doesn't set new container image (gcp)

10/26/2019
kubectl set image deployment/$DEPLOYMENT_EXTENSION $INSTANCE_NAME=gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest

I use this command do load new created image to my existing cluster (update version of my app) . But when I do it , and then go to the site , I don't see any changes .

spec:
      terminationGracePeriodSeconds: 30
      containers:
      - name: booknotes
        image: gcr.io/my-image:latest
        imagePullPolicy: Always

I've also added this 2 lines in deployment.yaml file and applied it for my cluser:

imagePullPolicy: Always
terminationGracePeriodSeconds: 30

But it still doesn't work. Can it be because I use :latest tag ? Or it isn't related ? If you have some idias pls let me know. And also if you need additional info , I will attach it !

gitlab-ci.yml

   stages:
      - build
      - docker-push
      - deploy

    cache:
      paths:
        - node_modules/

    build:
      stage: build
      image: node:latest
      script:
        - yarn install
        - npm run build
      artifacts:
        paths:
          - dist/
      only:
        - master

    docker:
      stage: docker-push
      image: docker:18.09.7

      services:
        - docker:18.09.7-dind
        - google/cloud-sdk:latest

      script:
        - echo $GCP_ACCESS_JSON > $CI_PIPELINE_ID.json
        - cat $CI_PIPELINE_ID.json | docker login -u _json_key --password-stdin $GCP_REGION
        - docker build -t gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest .
        - docker push gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest

      only:
        - master

    test:
      stage: deploy
      image: google/cloud-sdk:latest
      script:
        - echo $GCP_ACCESS_JSON > $CI_PIPELINE_ID.json
        - gcloud auth activate-service-account $GCP_CE_PROJECT_EMAIL --key-file $CI_PIPELINE_ID.json --project $GCP_PROJECT_ID
        - gcloud container clusters get-credentials $CLUSTER_NAME --zone $ZONE --project $PROJECT_NAME
        - kubectl set image deployment/$DEPLOYMENT_EXTENSION $INSTANCE_NAME=gcr.io/$PROJECT_ID/$DOCKER_REPOSITORY:latest

      only:
        - master
-- Andrey Radkevich
gitlab-ci
google-cloud-platform
kubernetes

2 Answers

10/26/2019

You are correct, the latest tag will not trigger a re-deployment of your pod. Instead you could set the image and then scale the deployment.

kubectl set image deployment/alpine alpine=alpine:latest
kubectl scale deployment --replicas=0
kubectl scale deployment --replicas=1

Another option would be to set the image and then patch an annotation or change some yaml field.

-- Rodrigo Loza
Source: StackOverflow

10/26/2019

This question, the symptoms and the reasons are very close to [this one],(Google cloud platform creating a pipeline with Kubernetes and replacing the same container)

Apply the same solution in your Gitlab-CI pipeline (use global variable to change the image tag and to deploy everytime a new one to force Kubernetes to pull and to deploy it).

I can help you on Gitlib-CI if you have difficulties to do this.

-- guillaume blaquiere
Source: StackOverflow