Getting Started w/ Docker & Rancher

Docker provides the most concise way to create portable containers for all types of software applications. When you start moving into large, distributed production environments, you quickly find that provisioning, deployment and orchestration can become a bit complex and confusing within the Docker paradigm. That’s where Rancher comes in.

What is Rancher?

Rancher is open source software that makes it simple for organizations to deploy a private container service and deliver Docker orchestration to users. Within a team, authorized users are able to create resource pools from any hosts, and then launch containers or application templates from Rancher’s UI or CLI. Users have complete control over how their applications are deployed, and Rancher provides all of the necessary infrastructure services such as networking, load balancing, and storage to ensure the application runs brilliantly on any infrastructure.

Rancher natively supports Docker

Whether you like starting containers with the Docker CLI or prefer Docker Compose, Rancher works in the background to provide consistent infrastructure services.

Rancher offers native Load Balancing and Service Discovery

Rancher implements a global, DNS-based service discovery function that allows containers to automatically register themselves as services, as well as services to dynamically discover each other over the network. It also provides an integrated, elastic load balancing service to distribute traffic between containers or services. Based on HA proxy, the service allows session management, health checks and can be scaled across containers to meet demand.

Today we’re going to take a look at deploying Rancher to AWS and then spinning up a small Rails application with a Postgres DB. We’ll use the same container that we’ve been developing in my previous blogposts about Rails+Docker Development.

PREPARE LINUX HOST

The first thing we’ll do is log into the AWS console and spin up a server which will act as our Rancher server.

Provision a Linux host with 64-bit Ubuntu 14.04, which must have a kernel of 3.10+. Please make sure the Linux host has at least 1GB memory.

I’ll be using Ubuntu Server 14.04 LTS (HVM), SSD Volume Type – ami-d05e75b8 as a t2.micro instance.

Once it’s spun up, SSH into the instance so we can install Docker and Rancher.

$ wget -qO- https://get.docker.com/ | sh

START RANCHER SERVER

All you need is one command to launch Rancher server. After launching the container, we’ll tail the logs to see when the server is up and running.

sudo docker run -d --restart=always -p 8080:8080 rancher/server

Once that container has been started, you can run a sudo docker ps to see the running process.

4da21cc4a03c rancher/server “/usr/bin/s6-svscan /” 4 minutes ago Up 4 minutes 3306/tcp, 0.0.0.0:8080->8080/tcp modest_torvalds

Now that port 8080 is exposing our Rancher container, we can navigate to _your_ec2_ip:8080 and login to the Rancher UI. First we’ll need to modify our Security Group for this instance to allow incoming TCP connections on port 8080:

Screen-Shot-2016-01-06-at-2.45.46-PM

Now you can navigate to port 8080 of your EC2 instance in your browser. You should see the Rancher UI with a warning about Access Control.

Screen-Shot-2016-01-06-at-2.53.01-PM

Now we want to create a Host and it’s probably a good idea to create an IAM user with AdministratorAccess first and download the credentials. This IAM user will execute things on behalf of Rancher and you’ll need the User Name, Access Key ID and Secret Access Key.

Hosts are the most basic unit of resource within Rancher and is represented as any Linux server, virtual or physical, with the following minimum requirements:

  • Any modern Linux distribution that supports Docker 1.6+.
  • Ability to communicate with a Rancher server via http or https through the pre-configured port. Default is 8080.
  • Ability to be routed to any other hosts under the same environment to leverage Rancher’s cross-host networking for Docker containers.
  • Rancher also supports Docker Machine and allows you to add your host via any of its supported drivers.

Click on Add Host and you should see the following:

Screen-Shot-2016-01-06-at-2.58.44-PM

Save, click Next and you’ll be taken to the Add Hosts page. Digital Ocean will be highlighted by default but we’re going to select AWS EC2 to begin. You’ll want to enter your IAM user credentials here so that Rancher can create and manage EC2 instances on your behalf.

Screen-Shot-2016-01-06-at-2.59.47-PM

Click Next to Authenticate and select a network. If your IAM user has sufficient access, you’ll see this below:

Screen-Shot-2016-01-06-at-3.02.35-PM

Now we can click next to set up our Security Groups. Here it’s best to let the default rancher-machine group be created as it has all the necessary ports opened.

Screen-Shot-2016-01-06-at-3.04.24-PM

The next section will ask for some basic instance info and all you really need to worry about is the name and the Docker install url which should be http://get.docker.com.

Screen-Shot-2016-01-06-at-3.05.57-PM

Click Create and you’ll see Rancher reaching out to AWS to create a new host on an EC2 instance.

Screen-Shot-2016-01-06-at-3.06.46-PM

This process takes a few minutes as it spins up an EC2 instance, installs the Rancher agent and Docker and then networks it in.

Once this is complete, you’ll see the host listed.

Screen-Shot-2016-01-06-at-3.24.45-PM

Now we can tab over to the AWS console and verify that this new EC2 instance has been created. If you look below, you see that one instance running the Rancher server was able to provision an EC2 instance directly from the GUI. That’s pretty cool and provides a great PaaS-like abstraction from EC2 (or DigitalOcean and a bunch of other Cloud providers).

Screen-Shot-2016-01-06-at-3.26.32-PM

Now we want to actually do something with this Host, i.e. deploy some Docker container apps to it.

If we click on Applications in the Rancher GUI, we will be able to deploy an app.

Screen-Shot-2016-01-06-at-3.29.14-PM

We’re going to deploy a Rails app and a PostgresDB. Let’s start by adding a Stack. We’ll use docker-compose.yml, slightly modified from our project to deploy this.

db:
  image: postgres
web:
  image: denmanjd/rails_app
  ports:
    - "3000:3000"
  links:
    - db

Screen-Shot-2016-01-06-at-3.43.56-PM

Click Create and we’ll see Rancher starting to spin this Stack up. Once it’s done you’ll see both containers started and you can navigate to port 3000 of this host to view your application.

Screen-Shot-2016-01-06-at-3.52.07-PM

This deployment process is much more intuitive than using Amazon ECS or something manual and Rancher handles instantiating and deleting instances for you. While this is a simple overview, things really get interesting when you network multiple hosts over multiple cloud providers and Rancher deploys distributed container applications for you.

We here at Levvel are really passionate about container technology and managing, orchestrating and deploying containerized apps in a DevOps paradigm. Feel free to reach out to us for help in modernizing your DevOps development efforts.

About Levvel

Levvel is an IT consulting firm that combines the innovative DNA of a start up with the wisdom, scalability, and process rigor of a Fortune 100 company. We offer both technical implementation services as well as strategic advisory services. Levvel offers you an “unfair advantage” over your competition with comprehensive services including DevOps, Cloud, Mobile, UI/UX, Big Data, Analytics, Payment Strategy, and more.

James Denman

James Denman

VP of Technology

James Denman is the VP of Technology at Levvel and is located in Sydney, Australia. He has over 10 years of experience working in the Defense, Diplomatic, and NGO technology sectors across more than 12 countries.

Previous Post →