Rancher: Setup a multi-cloud environment

By | 01/12/2016

Introduction

In this post, we will setup a multi-cloud environment, which means we will install some hosts on EC2 and some hosts on DigitalOcean. This will just explain how to create these hosts, but the post will not (yet) focus on full high availability.

Install Rancher

Just as we did in previous posts, we will start with installing the Rancher host and Rancher server.

WAUTERW-M-G007:~ wauterw$ docker-machine create -d amazonec2 --amazonec2-vpc-id vpc-84fd6de0 --amazonec2-region eu-west-1 --amazonec2-ami ami-c5f1beb6 --amazonec2-ssh-user rancher Rancher-AWS
Running pre-create checks...
Creating machine...
(Rancher-AWS) Launching instance...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with rancheros...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env Rancher-AWS
WAUTERW-M-G007:~ wauterw$ eval $(docker-machine env Rancher-AWS)
WAUTERW-M-G007:~ wauterw$ docker run -d --restart=unless-stopped -p 8080:8080 rancher/server
Unable to find image 'rancher/server:latest' locally
latest: Pulling from rancher/server
96c6a1f3c3b0: Pull complete
ed40d4bcb313: Pull complete
...
Digest: sha256:d5a798d1274bcf6813fc9866660dc8559b7e17cdce47608bce28d134bd4f2dc1
Status: Downloaded newer image for rancher/server:latest
7f8de76097c1f91a508de09a1ac1e049a370794068728734d2b8bf038d575551

This will finally result in the EC2 host being added in AWS. Lookup the public IP in AWS and open a webbrowser on http://IP_ADDRESS:8080 to see the Rancher UI.

rancher-aws-01

Method 1: Add EC2 hosts via the UI

Next, we will add hosts to the Rancher setup. First, we will demonstrate how to do this via the Rancher UI which is very straightforward.

Click on ‘Add host’ and select ‘EC2’:
rancher-aws-03
Fill in the availability zone you want the host to be available in. Also select the proper VPC:

rancher-aws-04
Next, choose your security group. Rancher creates its own security group, but instead I re-used a security group I created earlier. Note that you need to open ports 22 (TCP), 8080 (TCP), 2376 (TCP), 500 (UDP) and 4500 (UDP).

rancher-aws-05
Next, provide some details on the EC2 hosts, like instance type, AMI Id etc… Note that I used the table in the README file on this link:

rancher-aws-06
Go to the Infrastructure tab to see an overview of all the hosts:
rancher-aws-07
Obviously you should also see the server added as an EC2 instance on AWS:
rancher-aws-08

Method 2: Add EC2 hosts via the AWS CLI

You could use the following command to create an EC2 host.

WAUTERW-M-G007:~ wauterw$ aws ec2 run-instances --image-id ami-c5f1beb6 --count 1 --instance-type t2.micro --security-groups docker-machine --key-name keypair_ireland

While the above command works, the annoying thing with this command is that the instance will have no name in the EC2 console. The problem is that the EC2 run-instances command does not support a –tag flag

If this is really something you will want, you’ll need to install jq (CLI JSON parser)

WAUTERW-M-G007:~ wauterw$ brew install jq
==> Installing dependencies for jq: oniguruma
==> Installing jq dependency: oniguruma
==> Downloading https://homebrew.bintray.com/bottles/oniguruma-6.1.1_1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring oniguruma-6.1.1_1.sierra.bottle.tar.gz
?  /usr/local/Cellar/oniguruma/6.1.1_1: 17 files, 1.3M
==> Installing jq
==> Downloading https://homebrew.bintray.com/bottles/jq-1.5_2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring jq-1.5_2.sierra.bottle.tar.gz
?  /usr/local/Cellar/jq/1.5_2: 18 files, 957.9K

Then run the following command

WAUTERW-M-G007:~ wauterw$ aws ec2 create-tags --resources `aws ec2 run-instances --image-id ami-c5f1beb6 --count 1 --instance-type t2.micro --security-group-ids docker-machine --key-name "keypair_wauters1978_ireland" | jq -r ".Instances[0].InstanceId"` --tags "Key=Name,Value=Rancher-AWS-Node-02"

You will see that a host is added to the EC2 console (see screenshot below):

rancher-aws-09

Next, go to the Rancher UI and click ‘Add host’ and then the ‘Custom’ method. Fill in all details and copy/paste the resulting command into the CLI (make sure you are ssh’ed into your newly created EC2 host)

WAUTERW-M-G007:Belangrijk wauterw$ ssh -i keypair_wauters1978_ireland.pem rancher@52.209.194.184
The authenticity of host '52.209.194.184 (52.209.194.184)' can't be established.
ECDSA key fingerprint is SHA256:x9TkjtjSnT256EoomzOytE7SGP5SGnzOLdUQi+UWnYA.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '52.209.194.184' (ECDSA) to the list of known hosts.
[rancher@ip-172-31-42-152 ~]$
[rancher@ip-172-31-42-152 ~]$
[rancher@ip-172-31-42-152 ~]$
[rancher@ip-172-31-42-152 ~]$ sudo docker run -e CATTLE_HOST_LABELS='Name=Rancher-AWS-Node-02'  -d --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.0.2 http://52.213.72.36:8080/v1/scripts/B5920D373229E6F93AA1:1478606400000:LpxEbHAKHqfIy0SYV5lynZZAuM
Unable to find image 'rancher/agent:v1.0.2' locally
v1.0.2: Pulling from rancher/agent
5a132a7e7af1: Pull complete
fd2731e4c50c: Pull complete
28a2f68d1120: Pull complete
a3ed95caeb02: Pull complete
7fa4fac65171: Pull complete
33de63de5fdb: Pull complete
d00b3b942272: Pull complete
Digest: sha256:b0b532d1e891534779d0eb1a01a5717ebfff9ac024db4412ead87d834ba92544
Status: Downloaded newer image for rancher/agent:v1.0.2
35f59a67b353760a360fb3a47e5acb73b78fcce63f9c8d54cebdfb4824ebbe30
[rancher@ip-172-31-42-152 ~]$

Eventually this host will appear in the RancherUI:
rancher-aws-11

Note: if you see the hostname (in my case) ip-172-31-42-152…appear in the Rancher UI, than you can easily change this by setting it correctly into the /etc/hostname file once you SSH’ed into the host)

Method 3: Add EC2 hosts via the Docker-Machine

Another method to add Rancher hosts is via the good old Docker-Machine. Follow along with following steps:

WAUTERW-M-G007:Belangrijk wauterw$ docker-machine create -d amazonec2 --amazonec2-vpc-id vpc-84fd6de0 --amazonec2-region eu-west-1 --amazonec2-ami ami-c5f1beb6 --amazonec2-ssh-user rancher Rancher-AWS-Node-3
Running pre-create checks...
Creating machine...
(Rancher-AWS-Node-3) Launching instance...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with rancheros...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env Rancher-AWS-Node-3

Continue with SSH’ing into the host and then run the command you copied from the Rancher UI (Custom method). This procedure was explained already in the section “Adding hosts to Rancher server” of this post:

WAUTERW-M-G007:Belangrijk wauterw$ eval $(docker-machine env Rancher-AWS-Node-3)
WAUTERW-M-G007:Belangrijk wauterw$ docker-machine ssh Rancher-AWS-Node-3
[rancher@Rancher-AWS-Node-3 ~]$ sudo docker run -e CATTLE_HOST_LABELS='Name=Rancher-AWS-Node-03'  -d --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/rancher:/var/lib/rancher rancher/agent:v1.0.2 http://52.213.72.36:8080/v1/scripts/B5920D373229E6F93AA1:1478610000000:XCvREVTgaYyf7kcX6b6PhJ6Gpg
Unable to find image 'rancher/agent:v1.0.2' locally
v1.0.2: Pulling from rancher/agent
5a132a7e7af1: Pull complete
fd2731e4c50c: Pull complete
28a2f68d1120: Pull complete
a3ed95caeb02: Pull complete
7fa4fac65171: Pull complete
33de63de5fdb: Pull complete
d00b3b942272: Pull complete
Digest: sha256:b0b532d1e891534779d0eb1a01a5717ebfff9ac024db4412ead87d834ba92544
Status: Downloaded newer image for rancher/agent:v1.0.2
53253b9c2d10956147eca1a22d19ed747c8ad3746145c05b40fb33ca20e8b674
[rancher@Rancher-AWS-Node-3 ~]$

And the final result:
rancher-aws-12

Adding host on DigitalOcean

In previous section, I wanted to show mainly the three methods to add hosts (running on EC2) to Rancher. To create a true multi-cloud environment, obviously I need to also create some hosts on an alternative cloud provider. Luckily I also have an account on DigitalOcean. If you want to follow along, you’ll need to sign up to DigitalOcean.

First off, (after signing up) go to DigitalOcean dashboard and go to the “API” tab. You will need to create a token for Rancher to be able to create hosts on DigitalOcean. Click on the “Generate New Token” button and fill in the details. You will have to copy the token that was created. See also the below screenshot:

do1
Next, go to the Rancher UI and add an additional host by selecting the DigitalOcean option. You will need to provide some details to Rancher, such as your token, the image you would like to use and the region in which you would want to run the server.

do2
Eventually, you will see a fourth host in Rancher, but this time running on DigitalOcean.

do3

That’s it for this post. I mainly wanted to show how you could create multiple hosts on multiple cloud providers. Next post, I will launch some applications across the different hosts.

Leave a Reply

Your email address will not be published. Required fields are marked *