Upgrade Rancher 2 workload from CLI

7/2/2018

I have created a workload on Rancher. This workload created from an image which is hosted on a gitlab-ci project registry.

I want to force rancher to download a new version of this image and upgrade workload.

I want to do this from a .gitlab-ci.yml script. How to do this with Rancher version 2? With Rancher 1.6 I used this script:

deploy:
  stage: deploy
  image: cdrx/rancher-gitlab-deploy
  script:
    - upgrade --stack mystack --service myservice --no-start-before-stopping
-- nidomo
gitlab
gitlab-ci
kubectl
kubernetes
rancher

2 Answers

7/4/2018

In rancher 2, much of the management of workloads is delegated to Kubernetes via its api or CLI (kubectl).

You could patch the deployment to specify a new image/version, but if you are using a tag like :latest which moves, you will need to force Kubernetes to redeploy the pods by changing something about the deployment spec.

One common way to do this is to change/add an environment variable, which forces a redeploy.

In Gitlab, set two variables in your gitlab project or group to pass authentication information into the build.

The kubectl patch will update or add an environment variable called FORCE_RESTART_AT on your deployment's container that will force a redeploy each time it is set because Gitlab's pipeline ID changes.

You will need to specify the namespace, the name of your deployment, the name of the container and the image. If the image tag is changing, there is no need to supply the environment variable. If you are using :latest, be sure that the your container's imagePullPolicy: Always is set, which is the default if Kubernetes detects an image using :latest.

The image diemscott/rancher-cli-k8s is a simple image derived from rancher/cli that also includes kubectl.

RANCHER_SERVER_URL=https://rancher.example.com
RANCHER_API_TOKEN="token-sd5kk:d27nrsstx6z5blxgkmspqv94tzkptnrpj7rkcrt7vtxt28tvw4djxp"

deploy:
  stage: deploy
  image: diemscott/rancher-cli-k8s:v2.0.2
  script:
    - rancher login "$RANCHER_SERVER_URL" -t "$RANCHER_API_TOKEN"
    - rancher kubectl --namespace=default patch deployment nginx --type=strategic -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image": "nginx","env":[{"name":"FORCE_RESTART_AT","value":"$CI_PIPELINE_ID"}]}]}}}}'
-- Scott Anderson
Source: StackOverflow

3/1/2019

@nidomo, just use the API to issue a pause then a resume, it will cause the workload to undergo an upgrade, boom, latest version of the container fetched and deployed.

-- charlesread
Source: StackOverflow