Tools
Tools

Auto Connect Rancher Hosts


This script was extremely useful for me, you would run this script on a new host for your rancher server. It will automatically connect to the rancher server, and add it as a node. This script will automatically install Docker (for ubuntu-trusty) and add host in your rancher server.

Script for Ubuntu 14.04 (trusty)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
 
# EDIT LINES BELOW TO MATCH YOUR RANCHER SERVER CREDS
RANCHER_URL="http://rancher.domain.com:8080"
RANCHER_ACCESS_KEY="RANCHER_ACCESS_KEY_HERE"
RANCHER_SECRET_KEY="RANCHER_SECRET_HERE"
 
apt-get install apt-transport-https ca-certificates -y
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo 'deb https://apt.dockerproject.org/repo ubuntu-trusty main' >> /etc/apt/sources.list.d/docker.list
apt-get update
apt-get install jq docker-engine -y
 
KEY="$RANCHER_ACCESS_KEY:$RANCHER_SECRET_KEY"
# Get project ID
PROJECT_ID=$(curl -s -u $KEY $RANCHER_URL/v1/projects | jq -r ".data[0].id")
# Create registration token
curl -s -X POST -u $KEY $RANCHER_URL/v1/registrationtokens?projectId=$PROJECT_ID
# Get registration token
TOKEN=`curl -s -u $KEY $RANCHER_URL/v1/registrationtokens?projectId=$PROJECT_ID | jq -r '.data[0].token'`
 
docker run -d --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.0.1 $RANCHER_URL/v1/scripts/$TOKEN

Script for Ubuntu 16.04 (Xenial)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
 
# EDIT LINES BELOW TO MATCH YOUR RANCHER SERVER CREDS
RANCHER_URL="http://rancher.domain.com:8080"
RANCHER_ACCESS_KEY="RANCHER_ACCESS_KEY_HERE"
RANCHER_SECRET_KEY="RANCHER_SECRET_HERE"
 
apt-get install apt-transport-https ca-certificates -y
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo 'deb https://apt.dockerproject.org/repo ubuntu-xenial main' >> /etc/apt/sources.list.d/docker.list
apt-get update
apt-get install jq docker-engine -y
 
KEY="$RANCHER_ACCESS_KEY:$RANCHER_SECRET_KEY"
# Get project ID
PROJECT_ID=$(curl -s -u $KEY $RANCHER_URL/v1/projects | jq -r ".data[0].id")
# Create registration token
curl -s -X POST -u $KEY $RANCHER_URL/v1/registrationtokens?projectId=$PROJECT_ID
# Get registration token
TOKEN=`curl -s -u $KEY $RANCHER_URL/v1/registrationtokens?projectId=$PROJECT_ID | jq -r '.data[0].token'`
 
docker run -d --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.0.1 $RANCHER_URL/v1/scripts/$TOKEN

View Comments
There are currently no comments.