# Upgrade Rancher Service using REST API

**URL:** <https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459>\
**Category:** Rancher 1.x\
**Created:** [January 14, 2016, 3:32am UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459 "2016-01-14T03:32:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![yunspace](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/yunspace/32/304_2.png) [@yunspace](https://forums.suse.com/u/yunspace)\
**Post date:** [January 14, 2016, 3:32am UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/1 "2016-01-14T03:32:24Z")

</div>

I am trying to use the Rancher API to do automated deployments. Every time a new docker image is available, I want to upgrade the Service to use the new image version. Looking at the upgrade API, I can see in the inputs are:

```
HTTP/1.1 POST /v1/projects/1a8/services/1s6/?action=upgrade
Host: rancher.blah.com.au
Accept: application/json
Content-Type: application/json
Content-Length: 51

{
    "inServiceStrategy": null,
    "toServiceStrategy": null,
}

```

Please advise what are these input fields? All I want to do is update the following field and kick off the upgrade:

```
imageUuid: "docker:blah/blah:v0.1"

```

Thanks

---

<div class="post-metadata">

**Author:** ![alena](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/alena/32/50_2.png) [@alena](https://forums.suse.com/u/alena)\
**Post date:** [January 15, 2016, 5:51pm UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/2 "2016-01-15T17:51:11Z")

</div>

@yunspace for your use case, you would have to use “inServiceStrategy” for the upgrade, and:

- fetch service.launchConfig field of your service-to-upgrade
- update image field in the launchConfig fetched on the previous step, and pass it as enclosed field to inServiceStrategy parameter for the upgrade:

```auto
"inServiceStrategy": {
  "launchConfig": {
    "tty":true,
    "vcpu":1,
    "imageUuid":"docker:ubuntu:14.04.3"
    //...and other parameters of your existing service's launch config
  }
}

```

---

<div class="post-metadata">

**Author:** ![yunspace](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/yunspace/32/304_2.png) [@yunspace](https://forums.suse.com/u/yunspace)\
**Post date:** [January 20, 2016, 10:52am UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/3 "2016-01-20T10:52:55Z")

</div>

Thanks, that answered my question. I will try it out

---

<div class="post-metadata">

**Author:** ![Sadish\_Kumar](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Sadish\_Kumar](https://forums.suse.com/u/Sadish_Kumar)\
**Post date:** [August 29, 2016, 2:53pm UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/4 "2016-08-29T14:53:09Z")

</div>

Hi, need a small help  
how did u fetch the service.launchConfig of your corresponding service ?

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [August 29, 2016, 4:47pm UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/5 "2016-08-29T16:47:18Z")

</div>

@Sadish_Kumar - I simplified my life and I wrote a python script that does that - if that helps: [https://github.com/etlweather/gaucho](https://github.com/etlweather/gaucho)

---

<div class="post-metadata">

**Author:** ![luca\_leen](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/luca_leen/32/5112_2.png) [@luca\_leen](https://forums.suse.com/u/luca_leen)\
**Post date:** [August 30, 2016, 11:21am UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/6 "2016-08-30T11:21:47Z")

</div>

If can help, I’m using this with jenkins: [https://hub.docker.com/r/leen15/rancher-wrapper/](https://hub.docker.com/r/leen15/rancher-wrapper/)

---

<div class="post-metadata">

**Author:** ![yunspace](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/yunspace/32/304_2.png) [@yunspace](https://forums.suse.com/u/yunspace)\
**Post date:** [September 12, 2016, 4:42am UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/7 "2016-09-12T04:42:55Z")

</div>

@Sadish_Kumar similar to @etlweather, I also have a script [https://github.com/LittleBayDigital/little-builder/blob/master/bin/lib/\_rancher.sh](https://github.com/LittleBayDigital/little-builder/blob/master/bin/lib/_rancher.sh)

Basically what it does is:

1. Gets the service using the API ${RANCHER\_API\_URL}/projects/${environment}/services/${service}/
2. Updates the updatedServiceStrategy
3. Send a Update request

---

<div class="post-metadata">

**Author:** ![yunspace](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/yunspace/32/304_2.png) [@yunspace](https://forums.suse.com/u/yunspace)\
**Post date:** [October 5, 2016, 12:58pm UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/8 "2016-10-05T12:58:52Z")

</div>

Actually I think @etlweather’s script is a lot nicer than my one. Use his! 🕶

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [October 5, 2016, 1:55pm UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/9 "2016-10-05T13:55:56Z")

</div>

For automated deployment, in my case, upon tags in GitLab, I created a container which runs on Rancher using [webhook](https://github.com/adnanh/webhook) to create the web API and upon HTTP requests, it runs commands using [gaucho](https://github.com/etlweather/gaucho) to upgrade the containers.

Then in GltLab CI, upon build with a tag, I simply use `curl` to call the HTTP endpoint to cause the service to be upgraded.

---

<div class="post-metadata">

**Author:** ![gnoguchi](https://avatars.discourse-cdn.com/v4/letter/g/b5ac83/32.png) [@gnoguchi](https://forums.suse.com/u/gnoguchi)\
**Post date:** [December 1, 2017, 6:24pm UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/10 "2017-12-01T18:24:51Z")

</div>

hello

Is there a way, updating via rancher API, to keep previous configurations and only updating the docker image?

thanks in advance

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [December 2, 2017, 2:04am UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/11 "2017-12-02T02:04:21Z")

</div>

No in 1.x, yes in 2.0.

---

<div class="post-metadata">

**Author:** ![Amit\_J\_damitj](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/amit_j_damitj/32/5341_2.png) [@Amit\_J\_damitj](https://forums.suse.com/u/Amit_J_damitj)\
**Post date:** [May 23, 2018, 7:54am UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/12 "2018-05-23T07:54:22Z")

</div>

Hi Vincent,  
Can you share how can we do that ( update an worklaod or servic to use different contianer image and keep other config as it is ) or direct us to some links where the porcess is menioned.

---

<div class="post-metadata">

**Author:** ![myclau](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@myclau](https://forums.suse.com/u/myclau)\
**Post date:** [August 10, 2018, 5:21am UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/13 "2018-08-10T05:21:23Z")

</div>

I try a easy approach.

1. using rancher cli grep the service id  
~/rancher ps | grep service | grep -w ${RANCHER\_SERVICE\_NAME}
2. curl get api json with the serviceid from (1)
3. from the json in (2) use jq get .launchConfig (if you have sidekick, please also get .secondaryLaunchConfigs i.e is array)
4. create the request data json obj and pass in the data from (3)

> appendsecondaryLaunchConfigs=`["${secondaryLaunchConfigs}" = null] && echo "" || echo ",\"secondaryLaunchConfigs\": ${secondaryLaunchConfigs-n`ull}"`

> echo “{  
> "inServiceStrategy": {  
> "type": "inServiceUpgradeStrategy",  
> "batchSize": 1,  
> "intervalMillis": 2000,  
> "startFirst": false,  
> "launchConfig": ${launchConfig-null}  
> ${appendsecondaryLaunchConfigs}  
> }  
> ` }” | jq . \> update-request-data.json

1. the change the imageuuid in update-request-data.json

if not sidekick:

> updatedServiceStrategy=`cat update-request-data.json | jq ".inServiceStrategy.launchConfig.imageUuid=\"docker:${image}\""`

is sidekick (you need to specify which sidekick if you have mutiple, if only one sidekickindex is 0):

> updatedServiceStrategy=`cat update-request-data.json | jq ".inServiceStrategy.secondaryLaunchConfigs[${sidekickindex}].imageUuid=\"docker:${image}\""`

1. send request to api with ?action=upgrade

> curl --silent --write-out “Upgrade service - HTTP: %{http\_code}\n” -u “${RANCHER\_ACCESS\_KEY}:${RANCHER\_SECRET\_KEY}”   
> -X POST   
> -H ‘Accept: application/json’   
> -H ‘Content-Type: application/json’   
> -d “${updatedServiceStrategy}”   
> “${RANCHER\_API\_URL}/projects/${environment}/services/${serviceid}/?action=upgrade” -o upgrade.log

---

<div class="post-metadata">

**Author:** ![wuchencm](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@wuchencm](https://forums.suse.com/u/wuchencm)\
**Post date:** [August 20, 2018, 6:05pm UTC](https://forums.suse.com/t/upgrade-rancher-service-using-rest-api/1459/14 "2018-08-20T18:05:09Z")

</div>

curl -u “64247E71B0B579E7A393:8NTg8FHftrcUfiU5kXUN37ARjgHvmgVfnNezfhHp” -X POST -H ‘Accept: application/json’ -H ‘Content-Type: application/json’ -d ‘{“inServiceStrategy”:{“launchConfig”:{“tty”:true, “vcpu”:1, “imageUuid”:“docker:tomcat:9.0-jre8”}}’ ‘[http://10.10.1.8:8080/v2-beta/projects/1a7/services/1s88/?action=upgrade](http://10.10.1.8:8080/v2-beta/projects/1a7/services/1s88/?action=upgrade)’

{“id”:“f0d4f155-58c8-4377-8f1a-13a959e4c284”,“type”:“error”,“links”:{},“actions”:{},“status”:400,“code”:“InvalidBodyContent”,“message”:“InvalidBodyContent”,“detail”:null,“baseType”:“error”}
