Rancher & Spotinst – How the integration works

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.
    1_ApiKeysMenu
  3. Create new key and store its access key & secret key.
    2_RancherApiKey
  4. Generate a command for registering a node Go to Rancher -> Hosts -> Add Host -> Choose custom
    3_rancher-add-custom-host
  5. Then – follow the instruction and copy the host registration command
    7_rancher-add-custom-host-2

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

     

  2. Modify it as follows:
    1. Add this flag which will label your Rancher host:
      -e CATTLE_HOST_LABELS="spotinst.instanceId=`curl http://169.254.169.254/latest/meta-data/instance-id`"
    2. The final command that you should see in the User data section is as follows:
      #! /bin/bash
      # If not existed, Installing Docker
      wget -qO- https://get.docker.com/ | sh
      
      # Starting the Rancher Agent
      # Setting a CATTLE_HOST_LABELS of "spotinst.instanceId" which is REQUIRED for the Spotinst integration to work.
      sudo docker run -e CATTLE_HOST_LABELS="spotinst.instanceId=`curl http://169.254.169.254/latest/meta-data/instance-id`" -d --privileged -v /var/run/docker.sock:/var/run/docker.sock rancher/agent:VERSION http://rancher.domain.com:8080/v1/scripts/TOKEN
      
      
    3. if you want to add multiple tags to the host us it like that:
      sudo docker run -e CATTLE_HOST_LABELS="spotinst.instanceId=`curl http://169.254.169.254/latest/meta-data/instance-id`&tagkey=name&tagkey2=names" -d --privileged -v /var/run/docker.sock:/var/run/docker.sock rancher/agent:VERSION http://rancher.domain.com:8080/v1/scripts/TOKEN
      
      
    4. NOTE: If you are using Rancher OS: you might want to use the following cloud-init script:
      #cloud-config
      write_files:
        - path: /etc/rc.local
          permissions: "0755"
          owner: root
          content: |
            #!/bin/bash
            for i in {1..20}
            do
            docker info && break
            sleep 1
            done
            #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- http://169.254.169.254/latest/meta-data/instance-id`" --privileged -v /var/run/docker.sock:/var/run/docker.sock rancher/agent:v0.8.2 http://rancher.domain.com:8080/v1/scripts/[token]
      

 

  1. 4_UserData

    NOTE: if your Amazon Image does not contain docker – please add a command for installing docker.

  1. In the 3rd party section select Rancher and enter the following:
    1. Rancher master – Your rancher url (e.g. http://myRancher.myDomain:8080/v1)
    2. Access Key & Secret key (which you just created in the previous step above -> in Step 1)
      5_Rancher3rdparty
    3. When the instances are launched you should see that they are labeled with “spotinst.instanceId”.
      6_LabeledHosts

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

 

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.