How to install Kubernetes K3s with external Database [MySQL] and add worker node to the cluster.

Suryansh Mathema
4 min readJun 16, 2021

--

System Requirements:

  • VMWare Machine — If you tend to work on a Virtualized environment.
  • OS — CentOS 7 Machine. (Do update the system using the command sudo yum update).
  • Database Service — MySQL Server — This will be the external datastore for K3s. MySQL should be installed on your Master node.
kubernetes.io
kubernetes.io

Some general terms before we get started:

What is Kubernetes and K3s?

Kubernetes is an open source system for automating deployment, scaling, and management of containerized application. It is also known as K8s. Similarly, K3s is a lightweight certified kubernetes distribution and a great tool to create Kubernetes cluster. It is designed for low resource environment; production workloads in unattended, remote locations or inside IoT appliances. It is packaged as an single <40MB binary that reduces the dependencies and steps needed to install, run and auto-update a production Kubernetes cluster.

k3s.io

You can simply install K3s using the following command on your shell: [Note: You can install the curl command using sudo yum install curl.]

COMMAND: curl -sfl https://get.k3s.io | sh -

After the installation is complete, additional utilities will be installed as well, including kubectl, crictl, ctr, k3s-killall.sh and k3s-uninstall.sh.

What is MySQL?

MySQL is a fully managed database service which is commonly used for the purpose of a web database. It is an open source relational database management system to deploy could-native applications, click here to know more.

Here, we are using MySQL as an external datastore for K3s. First of all, MySQL should be installed on your Master node. Use the link to install MySQL on your CentOS 7 Machine, How To Install MySQL on CentOS 7 | DigitalOcean.

Practical Demo on the topic:

Architecture:

At first, I recommend you to disable the SELinux and Firewalld service of your system. Or, you can grant access to the necessary ports on your firewall.

Once your database service i.e. MySQL is setup, you need to create a database. To create a database use the following command:

COMMAND: CREATE DATABASE k3s;

Here, I have named my database as ‘k3s’, you can give a suitable name accordingly.

Secondly, create a user on your database, below shown is the command and credentials used:

COMMAND: CREATE USER ‘k3s’@’localhost’ IDENTIFIED BY ‘K3s@1234’;

Here, I have created a user named ‘k3s’ and provided the authentication. You can use the name and password accordingly.

Now, you need to grant the privileges from ‘k3s’ database to the created user ‘k3s’, as shown below:

COMMAND: GRANT ALL PRIVILEGES ON k3s.* TO ‘k3s’@’localhost’;

Finally, flush the privileges in order to reload the grant tables in the MySQL database enabling the changes to take effect without reloading or restarting MySQL service.

COMMAND: FLUSH PRIVILEGES;

So now, lets install K3s with external database i.e. MySQL on our Master node. To do that, use the following command:

COMMAND: curl -sfL https://get.k3s.io | sh -s — server — datastore-endpoint=”mysql://username:password@tcp(hostname:3306)/database-name”

Similarly, I have used the following command with the information that I have:

COMMAND: curl -sfL https://get.k3s.io | sh -s — server — datastore-endpoint=”mysql://k3s:K3s@1234@tcp(localhost:3306)/k3s”

After the command is executed, k3s with external database which is MySQL will be successfully installed. Use the command sudo systemctl status k3s.service to check the status of your service. Here, the service has started successfully.

Now that the k3s with external database is active and running, we can now add worker nodes from which works can be scheduled to worker node from master node.

To install on worker nodes and add them to the cluster, we need to run the following command with the K3S_URL and K3S_TOKEN environment variables:

COMMAND: curl -sfL https://get.k3s.io | K3S_URL=https://myserver:6443 K3S_TOKEN=mynodetoken sh -

Note: The value to use the K3S_TOKEN is stored at /var/lib/rancher/k3s/server/node-token of your master node.

And here, after executing the command the k3s-agent has been successfully installed and is connected to the master node.

Its better you change the hostname of your machines at first so that the hostname of both the machines don’t contradict.

Here, I have changed the hostname of my worker node.

Hence, the worker node has been added.

Task Successful.

--

--

Suryansh Mathema
Suryansh Mathema

Written by Suryansh Mathema

I believe in rectitude and moral uprightness in character. I strongly want to establish myself in the field of DevOps and System Security.

Responses (1)