Using Swarm as Rancher Orchestrator

Walisson de Deus Casonatto
Quick Mobile Labs
Published in
3 min readJan 13, 2017

Rancher is a great container manager, but his default orchestrator is highly inferior. That’s why we have the option to use different orchestration agents on Rancher. In this article, we will talk about Swarm.

As an orchestrator, Swarm allows us to use his Networking Overlay, which means a single network shared between all hosts on your Swarm Cluster.

First of all, we need to install Rancher:

docker pull rancher/server
sudo docker run -d --restart=unless-stopped -p 8080:8080 rancher/server

Access rancher through http://{machine-ip}:8080 (do not use localhost)

After installing it, we need to add a new environment:

Then, name it as Swarm and choose Swarm as an orchestrator:

Swarm is going to ask for a host before building the environment:

You may add a host on Infrastructure > Add Host > Custom

You should change it to your IP.

Go back and click to add a host. Follow Rancher’s instructions:

Then click on Swarm>Infrastructure and Swarm will be building the infrastructure stacks.

Let’s add a stack and use Refresh, a simple go project. Redis and Refresh will be able to communicate without a explicit link.

Click on ADD Stack and use this docker-compose and rancher-compose:

version: '2'
services:
api-refresh:
image: walissoncasonatto/go-refresh
environment:
REDIS_HOST: redis-refresh
stdin_open: true
tty: true
ports:
- 8282:80/tcp
labels:
io.rancher.container.pull_image: always
redis-refresh:
image: redis
stdin_open: true
tty: true
labels:
io.rancher.container.pull_image: always
version: '2'
services:
api:
scale: 1
start_on_create: true
redis:
scale: 1
start_on_create: true

Note that Redis doesn’t publish any port. The only service that publishes a port is the API. There is an environment variable passing the redis host, which is the name of the service. It is from this name that Rancher will resolve to the Redis container. When developing APIs inside the container they can always respond on port 80, avoiding concern for ports. Rancher will not allow any service with the same name on the same stack to prevent errors on DNS solving. If you want a service to communicate with a service in another stack we should use <serviceName>.<stackName>

When you use Rancher DNS to establish a communication between services, it will redirect to the right container. So if we wish to communicate with refresh service we should call it on port 80 witch is the port used inside the container. The port 8282 is only used to publish it on the host.

After all these steps, you should be ready to test our refresh application on port 8282.

--

--