Introduction to Rancher

Getting Started with Rancher

Overview

Docker by itself is great tool for building scalable infrastructure. It helps you build pieces of stateless services, making it easier to maintain high availability and scalability, but in large infrastructures it can become difficult if done manually.

The official solutions to this problem, Docker Swarm and Compose are also great tools. They allow you to build giant and elastic docker clusters, which are also easily scaled to multiple machines. Having said this, they lack some crucial features like built-in load balancer and cross-machine service discovery.

This is the part where Rancher comes in.
Rancher is an open source Docker PaaS that includes this features and many more. It’s a great tool for managing and running your containers in production. It has nice enterprise level features, and it’s GUI makes it easy to manage your infrastructure. A vast variety of features like service discovery, DNS, load balancing, cross-host network, multi-node support, multi-tenancy, health checks makes Rancher a very compact tool that is also easily deployed in few minutes.

In this post we will be installing rancher, doing the basic configurations, managing environments, adding host and starting you first stack from a catalog.

Pre-reqs

– This guide assumes you have a linux machine running(even a VM)
– Docker installed
– Access to docker.com to pull images

Deploying Rancher using docker

In this tutorial on how to install rancher, we will deploy it using the prebuild docker containers. If you haven’t already installed docker, please do so by using the official docker guide here.
We are going to use two containers for this deployment: rancher/server and mysql for the database. First we want to start the database using the following command:

$ docker run -d –restart=unless-stopped –name mysql \
-v /my/data/dir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-pass mysql

This will start mysql and store its data locally so they will be persistent.

Now we need to create the rancher database. Get shell access to the container bu running the following command:

$ docker exec -it mysql bash

Once inside the container, open mysql cli by running:

$ mysql -u root -p

Run the following commands to initialize the rancher database:

> CREATE DATABASE IF NOT EXISTS cattle COLLATE = ‘utf8_general_ci’ CHARACTER SET = ‘utf8’;
> GRANT ALL ON cattle.* TO ‘cattle’@’%’ IDENTIFIED BY ‘password’;
> GRANT ALL ON cattle.* TO ‘cattle’@’localhost’ IDENTIFIED BY ‘password’;

The above sql queries will create a database named cattle and the user cattle which has access to it.

Now that we have our database ready, we can start the rancher/server container.

$ docker run -d –restart=unless-stopped -p 8080:8080 \
–link mysql:mysql rancher/server \
–db-host mysql –db-port 3306 –db-user cattle \
–db-pass password –db-name cattle

This will start rancher container, connect it to the database we set up earlier. The service should be reachable on port 8080. Using a browser go to you machine ip on port 8080 and you should see Rancher up and running:

Configuring Rancher

The first time you login in rancher, you need to create your first user account. To do so go to Admin – Access Control:

You can choose different ways of managing you Access Control, but for this demo we will go with the Local one.

We setup a local admin user and Enable the Access Control with Local Authentication

Next thing we want to look it’s Environments.
Rancher use Environments to group resources. Each environment has its own sets of services, infrastructure and network. Environments can be owned and used by one or more users or groups, depending on the Access Control you are using.
A good example, as pointed on the Environment tab in Rancher, would be to separate “dev”, “test” and “production” environments. This will help you keep thing isolated from each other and have different ways of providing and restricting access to your teams.

For this demo we will create a Testing Environment. Open the Enviroments Menu(right now we have only the “Default” Environment):

Adding a new environment is very simple. You need to provide the Name, the template that wil be used( we will use Cattle, the rancher default template in this demo) and the users that will have access:

After creating the new environment and and switching to it, the next thing we need to do is add a host. Adding a host is also very simple. Before adding the host you need to check the supported versions of Docker. To add a new host go to Infrastructure -> Hosts.
The first time you will try to add a new host, you will need to configure the Rancher Host Registration URL, which will be used by the agents to connect to.

After saving the configuration, you will be redirected to the new host menu.

You can add hosts from Amazon, Azure, etc but for this demo we will add the local machine where rancher is running.
All you need to is copy the command under section 5 and paste in the host. After the Agent is up and running we can see the new host added present in the Host tab:

Now that we have our environment and a host configured, we can continue to add our first stack.
You can add a stack manually by adding it.s services one by one, or by even adding a docker-compose file. One other way would be to use one of the rancher or community catalogs available. They cove a wide varsity of services, and have pre-configured to make deployment very easy.Go to Catalog and select ALL. Here you will have all the available catalogs:

For this demo we will use the wordpress catalog. Search for wordpress or scroll down at the end. When you choose the wordpress template, you will be presented with some configuration options:

Here you can customize your wordpress settings like database user/password, port that worpress will run and admin password for wordpress. After you done hit launch and in a few seconds you will have wordpress running.

As you can see, we have two services running for this stack, the database and wordpress. We can simply access our wordpress site on the port we configured.

Conclusion
Rancher makes it very easy and simple to maintain docker containers in small or large infrastructures. You can deploy full stacks in seconds. You can deploy stacks in multiple hosts in a cross-host network. Its own built-in features like service discovery,DNS, load balancing and more, makes it a fantastic tool when it comes to high availability and scalability

Leave a Reply

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

Scroll to top