Install Rancher Server on CentOS 7

Written by Pim on Sunday April 16, 2017 - Comment - Permalink
Categories: docker, rancher, devops, howto - Tags: centos7, rancher-server

Every day, more people are using containerization software like Docker. Using Docker containers in a production environment can be hard, but Rancher fills in the gap by providing us a powerful platform to run Docker containers in production.

We use CentOS 7, a Redhat based operating system including the YUM packet manager. We are going to install a single node Rancher server on the CentOS 7 machine to run a few docker containers on it. Let's start!

CentOS 7

Get a copy of CentOS 7 from their website and install it onto your machine. Don't use the default partitioning scheme and change it in the setup of CentOS 7. Because CentOS 7 uses the LVM device mapper, it is possible to increase and decrease the amount of storage if your storage needs are changing in the future.

I used the following LVM partitioning:

  • /boot (default)
  • / = 50 GB (HDD)
  • /home = 10 GB (HDD)
  • /var/lib/docker = The rest (at least 100 GB)

The rest of the installation is simple. Just follow the setup wizard and configure as much as possible.

Pre-requirements

Before we're going to install Rancher, we have to do some configuring and we need to install some extra packages.

If you haven't configured the network yet, do it now.

$ sudo nmtui

Then, we configure the hostname of the machine and restart the systemd-hostnamed service.

# sudo hostnamectl set-hostname <hostname>
$ sudo hostnamectl set-hostname rancher01
$ sudo systemctl restart systemd-hostnamed

When you used the minimal ISO, an OpenSSH-server isn't available by default, so we have to install it.

$ sudo yum install openssh -y

Now all pre-requirements are met, and we're ready to install Docker.

Docker

Before we install the Rancher server, we have to install the docker machine. We install docker using the YUM packet manager. The default CentOS repository has only an outdated docker machine available, so we have to add the docker CentOS repository to get the latest stable version of Docker.

$ sudo yum install yum-utils -y
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum makecache fast

Now we're ready to install the docker machine itself.

$ sudo yum install docker-ce

The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.
If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

$ sudo groupadd docker
$ sudo usermod -aG docker <youruser>

Check which users are added to the docker group.

$ getent group docker

Start the Docker service.

$ systemctl start docker

And configure it to start at boot.

$ sudo systemctl enable docker

When Docker is installed and running, it is time to install the Rancher server.

Install Rancher server

Check out the rancher server installation guide.

You have a few options:

  • Install rancher server in a single container (including the database)
  • Install rancher server with an extern database
  • Install rancher server and mount the MySQL data volume

Your first option is to boot up Rancher server in a single container. The MySQL database is available in this container and is used by Rancher to store all the configuration in it.

$ docker run -d --restart=unless-stopped -p 8080:8080 rancher/server:stable

The second option is to use an external database. I used a second MySQL container for the MySQL database and linked it to the Rancher server container.

$ docker run -d --name rancher-mysql --restart=unless-stopped -e MYSQL_ROOT_PASSWORD=<password> -e MYSQL_DATABASE=cattle mysql:5.7
$ docker run -d --restart=unless-stopped --link rancher-mysql:rancher-mysql -p 8080:8080 rancher/server:stable --db-host rancher-mysql --db-port 3306 --db-user root --db-pass <password> --db-name cattle

Option 3 maps the MySQL data directory to the host.

$ docker run -d -v <path>:/var/lib/mysql --restart=unless-stopped -p 8080:8080 rancher/server:stable

Rancher server uses port 8080 for the web interface. Configure the firewall to allow port 8080 and reload it.

$ sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
$ sudo firewall-cmd --reload

Check if the port is configured correctly.

$ sudo firewall-cmd --list-ports

Check if ipv6 forwarding is enabled, otherwise it is possible that you can't connect to the Rancher web interface because Docker binds ports to IPv6 when available.

$ sudo sysctl -a | grep net.ipv6.conf.all.forwarding

If IPv6 forwarding is disabled, change the /etc/sysctl.conf configuration file to enable it.

net.ipv6.conf.all.forwarding=1

If you've changed the configuration, reboot the server to reload the configuration.

$ sudo reboot

If everything went well, you're ready to use the Rancher server. Go to http://<your ip>:8080 and configure user management.

If you found this post useful, or if you have any questions, leave a comment!