Painless Container Management with Rancher 2.0, Kubernetes, and IBM Cloud
Part one of becoming a Jolly Rancher®
In this first of a 3-part series on container orchestration with IBM Cloud and Rancher, we’ll take a sneak peek at Rancher 2.0, a visual container orchestration and management tool. We’ll also show you how to run and manage containers across a Kubernetes cluster, and show you how to deploy containers within an existing IBM Cloud Container Service Kubernetes cluster.
You can’t throw a rock at a technical conference today without hitting someone that’s building applications using Docker. Linux containers technology, and in particular the Docker implementation of them, has drastically changed the way server-side applications are being deployed and developed. Gone are the days of configuring multiple servers and spending hours tuning settings or creating Ansible recipes; now you can simply spin up a container and you have a pre-configured application that’s already running and ready-to-go. This has been one of the major drivers of the rise of distributed software architectures, and in turn has made concepts such as serverless into buzzwords worth actually paying attention to.
However, as with anything in tech, developers have to go and ruin it by trying to use it.
With Docker and micro-service architectures, the major problem is coordinating these connected containers across cloud resources so they can be deployed across multiple hosts while still communicating quickly and securely. Oftentimes, it feels like an entire DevOps team is required just to get a development environment set up. Orchestration layers like Kubernetes have stepped in to fill that gap, making it easier to connect containers across multiple servers using a secure overlay network, but even just setting up the orchestration layer itself can feel as complicated as building the applications.
In this article, we’ll take a look at the technical preview of Rancher 2.0, an open source enterprise-scale container orchestration and management platform that takes the pain and suffering out of deploying and scaling containerized apps across Kubernetes clusters. We’ll also discuss some of the vocabulary used by Rancher and demonstrate just how easy it is to deploy a Docker-ized application across multiple hosts and hosting providers.
Let’s get started…
Introduction to Rancher 2.0 and Kubernetes
Sometimes, it’s better to show than tell, and with Rancher 2.0 that’s a great place to start. Let’s look at a screenshot:
Better still, if you have a Linux host with Docker already installed, you can run Rancher itself using the rancher/server image:
sudo docker run -d — restart=unless-stopped -p 8080:8080 rancher/server:preview
In many ways, Rancher 2.0 feels more like an interface upgrade to 1.6 rather than a complete under-the-hood rework. In fact, Kubernetes is available in 1.6 as an option when deploying a new cluster, but most of the features in the interface required the use of the Rancher’s Cattle orchestration layer.
In 2.0, Kubernetes is native and there are lots of features that make it easy to not only deploy new Kubernetes clusters, but to manage and deploy across existing Kubernetes clusters as well.
Installing Rancher 2.0 Server using Docker
The easiest way to deploy Rancher 2.0 is by spinning up an instance of the Rancher server Docker container. You’ll need a Linux host with at least 2 GB of RAM and 20 GB of disk space for storing container-related data.
In the technical preview, authentication is not yet available so it’s strongly recommended that you try out this technical preview on a local server or virtual machine rather than on a cloud service. This will be remedied in the near future when the authentication connectors from 1.6 are ported to 2.0.
Start up the Rancher server on port 8080 by running the following Docker command on your host:
sudo docker run -d — restart=unless-stopped -p 8080:8080 rancher/server:preview
This spins up the Rancher Server container, which also acts as the master node in your default Kubernetes cluster. You can also use Rancher Server to manage an existing Kubernetes cluster on other services such the IBM Cloud Container Service as well.
Once the application is started, navigate to
http://localhost:8080 and you’ll be presented with the initial interface.
Before we dive into creating a new cluster, let’s take a brief detour and talk about terminology within Rancher.
Clusters and Environments
Rancher allows you to organize sets of resources into two different abstractions: Environments and Clusters.
According to the documentation, an Environment is:
A namespace where applications, services, and containers are defined. The containers in an environment can communicate with each other over a shared managed network, and different users/groups can be given access to manage the resources of the environment.
It’s helpful to think of an environment as a separate organization. If you’re a consultant that manages the servers and applications for multiple clients, you can create a different environment for each client. This allows you isolate resources for each client and, for example, ensure that you don’t accidentally deploy a container for one client on the server resources for another. You can share resources, such as clusters, across environments, but these resources must be explicitly shared between environments.
Rancher starts with an environment called Default, and you can manage environments (or switch between them) using the dropdown in the upper-right corner of the server UI:
To add a new environment, click on the Manage Clusters button at the bottom. From there, you can add a new environment:
A Cluster in Rancher 2.0 is:
A group of physical (or virtual) compute resources. Each environment is tied to one cluster and will run all its containers on the cluster’s hosts, and a cluster can be shared with more than one environment.
In Rancher each machine that you want to work with — whether it’s a physical server in a local data center or a cloud-based virtual server on Amazon AWS or IBM Cloud — is called a Host. When you add hosts to a cluster, Rancher will automatically begin deploying containers on that host. You can specify scheduling rules for containers as well, ensuring that certain containers only run on specific hosts. As you scale up your containers, Rancher will automatically scale up the running instances of those containers according to those scheduling rules.
This makes scaling your application a breeze; simply add another host to your cluster, and Rancher will automatically deploy containers to it. You can add hosts from multiple providers and multiple availability zones in the same cluster which is perfect for setting up your application to have high-availability.
You can add or update clusters from the Manage Clusters page by clicking on the Environments dropdown and clicking on Manage Clusters.
We’ll cover how to add clusters to your environment shortly, but for now knowing what they are and how to get to them in the interface is sufficient.
Creating your first Kubernetes Cluster
If you’ve already started your Rancher server, then congratulations! You’re already running your first Kubernetes cluster.
Ok, well almost. Rancher does start up your Kubernetes master node, but it doesn’t force you to use the built-in Kubernetes master right away. Rancher starts you out by giving you the choice of running a cluster managed by Rancher, or a managed service such as the IBM Cloud Container Service or Google Compute Engine.
We’ll take a look at how to use the IBM Cloud Container Service version shortly, but for now let’s use the Kubernetes master built into Rancher. Navigate to the Containers menu item. It’s also the first one you see when you navigate to your Rancher Server installation.
Click the Select button under the Add Hosts section to add some server resources to your existing Kubernetes cluster.
All of the hosts in your cluster must be able to reach your Rancher server so they can register with the Rancher daemon. This means you’ll need to make your server accessible on the network where your hosts reside.
The first page under the Add Hosts wizard asks you to specify a URL or IP address that can be used to access the Rancher Server.
NOTE: If you’re using a central Rancher server to manage hosts across providers (i.e., one in AWS and another in IBM Cloud), it would be a good idea to set up a VPN between your Rancher server and cloud hosts before continuing. If you’re running your hosts on the same network as Rancher, such as in the same IBM Cloud Security Group or Amazon VPC cluster, then just make sure your hosts can reach your Rancher server via the HTTP port and URL you specify here.
The next page in the wizard will provide you with options for creating your new host.
You can provision hosts directly from Rancher with any of the integrated services. You can also add a host manually by selecting the Custom option and running the given Docker command from the terminal on that host. This is helpful for registering existing hosts with Rancher, including existing on-premises servers or servers running on a cloud provider which Rancher isn’t yet integrated.
Running a container in Rancher
To run a container on our newly-created Kubernetes cluster, click on the Containers tab in the top navigation menu and click on the Add Container button. This will take you to the Add Container form, which allows you to configure which container you want to run. Let’s start by creating a simple container with mostly default settings.
testcontainer in the Name field. In the Docker Image field, we’ll enter
alpine, which is a very lightweight Linux distribution designed to run on Docker. We can create this container as a standalone container, or select from one of several other scaling configurations. Rancher automatically handles the Kubernetes configuration for each of these options so they should all be seamless. For now, let’s stick with the default Standalone setting.
You can add port mappings and environment variables to your container using the Add Port and Add Environment Variable buttons, respectively. Finally, you can specify rules for how the container will be run across various hosts and define health check rules to determine whether your container is still successfully running. We won’t address these options just yet, but feel free to add port mappings and environment variables to our test container to try it out.
When you’re satisfied with the settings of this container, click the Launch button. Rancher will automatically pull down the Docker image of the container from the dockerhub registry and run the container on one of your available hosts (we’ll cover how to use other container registries in the next part of this series).
You should now see your new container running in the Default stack on one of your hosts. You can interact with the container by clicking on the 3-dot menu on the right side of the container list.
The Execute Shell option is particularly useful for testing our your new container.
Building applications using containers and stacks
In Rancher, the term Stack is used to bundle related services that comprise a single application. For example, a container that relies on MQTT can also have a MQTT server container bundled with it in a single stack.
Start by creating a new container. In the container creation page, you’ll see an Add to a new stack link in the stack section of the configuration. This converts our Stack dropdown into a text field where we can enter the name of a new stack. Once you’ve created your container, the new stack name will be available in the dropdown when you create your related services.
Containers in the same stack are also grouped together in the containers listing, so you can see all of the containers that are related to each other.
Using an existing Kubernetes cluster on IBM Cloud
While learning on the default Kubernetes cluster in Rancher is convenient, you’re not bound to using it. If you have an existing Kubernetes cluster, you can still manage your containers across it using Rancher.
You’ll create a new Kubernetes cluster by opening up the Catalog from the top navigation menu and typing kubernetes into the search box.
Select the Containers in Kubernetes Clusters service and you’ll be prompted to create your cluster.
From the Create page, you can select whether you’re just trying things out using the Lite plan, or you can spin up a production-ready cluster. From here you’ll also choose the data center region you’d like to use, how many worker nodes you’d like, and other settings for your cluster. When you’re happy with the results, click Create Cluster to start your Kubernetes cluster.
It will take a few moments for your cluster to finish deploying. As the cluster is deploying, you’ll be redirected to the cluster deployment page. The Access tab contains a set of commands that you can use to interact with your new Kubernetes cluster. We’ll need to be able to run a
kubectl command in a terminal to connect Rancher to our existing Kubernetes cluster. Follow the instructions on the Access tab in your deployment page to do so.
Be sure that you are able to receive a response from the
kubectl get nodes command before you attempt the next step.
Now that you have your Kubernetes cluster deployed in IBM Cloud, we can head back to Rancher and configure Rancher to deploy applications within the new Kubernetes cluster we created.
First, you’ll need to create an API key so the cluster can access the Rancher API. Hover over the Resources tab in the navigation menu and click API Keys to bring up the API Keys page.
In Rancher, click on the Add Account API Key button. This will bring up a dialog asking you to name the key — any name is fine here. Once you’ve created the key, you’ll be presented with your credentials for the API Key. These are automatically generated, and are only available when you create the key, so make sure you save them off.
Now that we have an API key, let’s add our existing Kubernetes cluster. Go to the Hosts tab in your preferred environment. Click on the Select tab under the Use Existing Kubernetes section to start configuring Rancher to use your existing Kubernetes cluster.
Rancher will display a command that you can run to connect Rancher to your existing Kubernetes cluster. Run the command from your terminal, and your Kubernetes hosts will be automatically displayed in the Hosts tab.
A solid day’s work at the ranch
Even though Rancher 2.0 is still in technical preview at the time of this writing, it’s already jam-packed with features that will make orchestrating and managing your Kubernetes applications on IBM Cloud easier than ever. You should now have enough information to give Rancher 2.0 a try, and in the next part we’ll take things a step further by deploying a complete multi-container app using a Rancher stack.