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
- Login to your Rancher UI.
- On the drop-down menu, select API Keys.
- Create new key and store its access key & secret key.
- Generate a command for registering a node Go to Rancher -> Hosts -> Add Host -> Choose custom
- Then – follow the instruction and copy the host registration command
Step 2: Create an Elastigroup within Spotinst
- 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:
- 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`"
- 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
- 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
- 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]
- Add this flag which will label your Rancher host:
- In the 3rd party section select Rancher and enter the following:
- Rancher master – Your rancher url (e.g. http://myRancher.myDomain:8080/v1)
- Access Key & Secret key (which you just created in the previous step above -> in Step 1)
- When the instances are launched you should see that they are labeled with “spotinst.instanceId”.
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.