April 27, 2016

341 words 2 mins read

User Data for automated RancherOS instances

Rancher-Logo
There are a lot of container management services poping up these days and right now Rancher is my choice in solutions (for the moment). One of the coolest accessories to go with this container ranch is RancherOS. The OS is purpose built to do nothing but Docker and, in fact, runs the few required system services in Docker as well. Comming in at a massive 27MB (For v0.4), it’s a perfect choice to run in AWS under something like spot instances and autoscaling. However, if you’re properly autoscaling — you need a way for your new hosts to join the RancherOS cluster… which is where user data (aka cloud-init) comes in.

rancher-create
The first step is to grab the command URL from your Rancher UI.

user-data
Now here’s where it gets really complicated. You’ll need to setup your spot instance requests or auto-scaling exactly as you normally do — but just add the following to the user data section (either via the UI or CLI):

#cloud-config
rancher:
  services:
    register:
  priviledged: true
  volumes:
  - /var/run/docker.sock:/var/run/docker.sock
  image: rancher/agent
  command: http://rancher.domain.tld/v1/scripts/F3somethingA6:14something00:S9something2Q

Now when the instances start, they should automatically register with your Rancher server. I’ve noticed it takes 5 minutes or so for the systems to register once they are fully started. Remember that the instances will show as running in AWS very quickly but that doesn’t mean RancherOS is actually ready to operate yet. On first boot it has to pull down all the system docker containers before it gets to pulling down and launching the agent.

via-api
For those that want to go full devops automation, asking the UI for a URL is sub-optimal. Fortunately anything you can do in the Rancher UI can be done via API. Access http://rancher.domain.tld/v1/registrationtokens and you’ll be able to pull down all details required for registration. The variables can then be easily passed into your automation solution of choice.

Remember: Treat your servers like cattle, not pets!