fbpx

Introduction

Rancher is an open source platform for building a cross-cloud container service. It is able to create instances on any host, running in any cloud, and provides consistent networking and storage functionality around the container.

Before you start using this integration, please make sure to have:

  • An active Spotinst account.
  • A Rancher account with ‘rancher/server:v0.46.0-rc1’ version or above.
Step 1: Create an API Key Within Rancher

Create an Environment API Key:

For Rancher 1.2+  

  1. Spotinst expects an Environment API Key (and not an Account API Key). Please follow the following steps to create it.
  2. Store it in a secure location.

If your Rancher version is < 1.2  

  1. Login to Rancher UI.
  2. Choose API Keys from the drop-down menu on the top right corner:
  3. Create a new key and store it in a secure location:
  4. Navigate to the Infrastructure tab and click on Hosts.
  5. Add a new Host from the Custom Hosts type.
  6. Generate a command for registering a node:
  7. Follow the instructions and copy the host registration command:
Step 2: Modify The User Data
  1. Use the registration command that was generated by Rancher. It should look something like:
    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. Add the flag below in order to label your Rancher host:
    -e CATTLE_HOST_LABELS="spotinst.instanceId=`curl http://169.254.169.254/latest/meta-data/instance-id`"
  3. Optionally, you can use the code below to add multiple tags to the host:
    #! /bin/bash
    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:v0.8.2 http://rancher.domain.com:8080/v1/scripts/[token]
    Note: If you are using RancherOS, you should 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]

     

Step 3: Create an Elastigroup
  1. Create a new Elastigroup.
    Insert the modified script (from the previous step) to the User Data section under the compute tab.
    Note: if your Amazon Image does not contain docker – please add a command for installing docker.
  2. 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)
    3. When the instances are launched you should see that they are labeled with “spotinst.instanceId”:

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

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.