Packet and Rancher Integration for Elastigroup

Learn how to create a cluster of bare metal Rancher hosts by using Spotinst Elastigroup

Aharon Twizer
Spotinst, Co-Founder and CTO

Here at Spotinst, we’ve partnered with Rancher to enable you to run your containers on Spot Instances.

In this integration, you’ll define your Rancher environment while Spotinst supplies the underlying infrastructure. That’s why you will have to write a command (in the startup script of the server we’re launching) that registers the instance as a host in Rancher. See below for important details. The integration uses the labeling capabilities of Rancher.

NOTE: To take advantage of the native integration between Spotinst & Rancher you must be running Rancher version rancher/server:v0.46.0-rc1 or above.

Step 1: Create an API Key within Rancher

  1. Login to your Rancher UI.
  2. On the drop-down menu, select API Keys.
  3. Create a new key and store its access key & secret key.
  4. Generate a command for registering a node Go to Rancher -> Hosts -> Add Host -> Choose custom
  5. Then – follow the instruction and copy the host registration command

Step 2: Create an Elastigroup within Spotinst

  1. In the User data section, instead of using the command you have copied from the previous section:
    sudo docker run -d --privileged -v /var/run/docker.sock:/var/run/docker.sock rancher/agent:v0.8.2 http://rancher.spotinst.com/v1/scripts/1F68A2DE49C324F22ABE:1447257600000:v4W9vNzpG3GYr0mF6o4

    Modify it as follows:

    1. Add this flag which will label your Rancher host for Packet.net
      -e CATTLE_HOST_LABELS=spotinst.instanceId=$(wget -qO- https://metadata.packet.net/metadata | jq -r .id)
    2. The final command that you should see in the User data section is as follows:
      sudo yum update -y
      sudo yum install -y docker
      sudo service docker start
      wget https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
      chmod +x ./jq-linux64xxxxxcp jq-linux64 /usr/bin/jq
      sudo docker run -e CATTLE_HOST_LABELS=spotinst.instanceId=$(wget -qO- https://metadata.packet.net/metadata | jq -r .id) -d --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.0
    3. NOTE: If you are using Rancher OS: you might want to use the following cloud init script:
       - path: /etc/rc.local
       permissions: "0755"
       owner: root
       content: |
       for i in {1..20}
       docker info && break
       sleep 1
       #Starting the Rancher Agent
       # Setting a CATTLE_HOST_LABELS of "spotinst.instanceId" which is REQUIRED for the Spotinst integration to work.
       sudo docker run -d -e CATTLE_HOST_LABELS="spotinst.instanceId=$(wget -qO- https://metadata.packet.net/metadata | jq -r .id)" --privileged -v /var/run/docker.sock:/var/run/docker.sock rancher/agent:v0.8.2 http://rancher.domain.com:8080/v1/scripts/[token]

From Security reasons, please make sure that the Rancher Master Security-Group is open specifically for the whitelisted Spotinst servers IP Addresses avilable here: Spotinst Whitelabeled IPs

JSON Example: Since the Spotinst Packet.net Elastigroups are by API only, here is an example of a working Elastigroup configuration in JSON format. This will create a two-node Elastigroup Cluster on Packet.net with Centos 7 running as your hosts. Docker will be installed and the CATTLE_HOST_LABELS will be added with the spotinst.instanceID tag set as the Packet.net instance ID. Note that you will need to modify your Rancher key and host endpoint.

  "name": "Rancher Test env api key",
  "capacity": {
    "minimum": 2,
    "maximum": 10,
    "target": 2
  "strategy": {
    "spotPercentage": 100,
    "fallbackToOd": false
  "compute": {
    "launchSpecification": {
      "operatingSystem": "centos_7",
      "userData": "#!/bin/sh\nsudo yum update -y\nsudo yum install -y docker\nsudo service docker start\nwget https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64\nchmod +x ./jq-linux64\ncp jq-linux64 /usr/bin/jq\nsudo docker run -e CATTLE_HOST_LABELS=spotinst.instanceId=$(wget -qO- https://metadata.packet.net/metadata | jq -r .id)  -d --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.2.0",
      "tags": [
      "hostName": "rancherhost2"
    "instanceTypes": {
      "spot": [
      "od": [
    "dataCenters": [
    "projectId": "bb44b30e-0a76-4c31-999-fffffff45ad9"
  "thirdPartiesIntegration": {
    "rancher": {
        "accessKey": "A4FFFFFFFF5FF6607",
        "secretKey": "uNngFFFFFFFFFFFFgYcDFFFFFFFFRaShsbz",
        "masterHost": ""


That’s all folks!

Now – Whenever Spotinst will perform an instance replacement, it will automatically communicate with Rancher API

  • Relocate the running containers to different hosts.
  • Delete and Purge the failed hosts.

Of course, if you have any questions, please don’t hesitate to ping us here at cs@spotinst.com.

Aharon Twizer
Spotinst, Co-Founder and CTO

Stay current

Sign up for our newsletter, and we'll send you the latest updates on Spotinst, tips, tutorials and more cool stuff!