DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Fast Deployments of Microservices Using Ansible and Kubernetes
  • Dockerizing an Ansible Playbook, Part 1
  • Dockerizing an Ansible Playbook, Part 2
  • Building a 24-Core Docker Swarm Cluster on Banana Pi Zero

Trending

  • BPMN 2.0 and Jakarta EE: A Powerful Alliance
  • Building a Performant Application Using Netty Framework in Java
  • Architecture: Software Cost Estimation
  • Sprint Anti-Patterns
  1. DZone
  2. Data Engineering
  3. IoT
  4. Orange Pi Cluster With Docker Swarm and MariaDB

Orange Pi Cluster With Docker Swarm and MariaDB

In this article, I’ll show you how to build a (surprisingly cheap) 4-node cluster packed with 16 cores and 4GB RAM to deploy a MariaDB replicated topology.

By 
Alejandro Duarte user avatar
Alejandro Duarte
DZone Core CORE ·
May. 01, 23 · Tutorial
Like (7)
Save
Tweet
Share
22.4K Views

Join the DZone community and get the full member experience.

Join For Free

Building a cluster of single-board mini-computers is an excellent way to explore and learn about distributed computing. With the scarcity of Raspberry Pi boards, and the prices starting to get prohibitive for some projects, alternatives such as Orange Pi have gained popularity.

In this article, I’ll show you how to build a (surprisingly cheap) 4-node cluster packed with 16 cores and 4GB RAM to deploy a MariaDB replicated topology that includes three database servers and a database proxy, all running on a Docker Swarm cluster and automated with Ansible.

This article was inspired by a member of the audience who asked my opinion about Orange Pi during a talk I gave in Colombia. I hope this completes the answer I gave you.

What Is a Cluster?

A cluster is a group of computers that work together to achieve a common goal. In the context of distributed computing, a cluster typically refers to a group of computers that are connected to each other and work together to perform computation tasks.

Building a cluster allows you to harness the power of multiple computers to solve problems that a single computer cannot handle. For example, a database can be replicated in multiple nodes to achieve high availability—if one node fails, other nodes can take over. It can also be used to implement read/write splitting to make one node handle writes, and another reads in order to achieve horizontal scalability.

What Is Orange Pi Zero2?

The Orange Pi Zero2 is a small single-board computer that runs on the ARM Cortex-A53 quad-core processor. It has 512MB or 1GB of DDR3 RAM, 100Mbps Ethernet, Wi-Fi, and Bluetooth connectivity. The Orange Pi Zero2 is an excellent choice for building a cluster due to its low cost, small size, and good performance.

The only downside I found was that the Wi-Fi connection didn’t seem to perform as well as with other single-board computers. From time to time, the boards disconnect from the network, so I had to place them close to a Wi-Fi repeater. This could be a problem with my setup or with the boards. I’m not entirely sure. Having said that, this is not a production environment, so it worked pretty well for my purposes.

What You Need

Here are the ingredients:

  • Orange Pi Zero2: I recommend the 1GB RAM variant and try to get at least 4 of them. I recently bought 4 of them for €30 each. Not bad at all! Give it a try!
  • MicroSD cards: One per board. Try to use fast ones — it will make quite a difference in performance! I recommend at least 16GB. For reference, I used SanDisk Extreme Pro Micro/SDXC with 32GB, which offers a write speed of 90 MB/s and reads at 170 MB/s.
  • A USB power hub: To power the devices, I recommend a dedicated USB power supply. You could also just use individual chargers, but the setup will be messier and require a power strip with as many outlets as devices as you have. It’s better to use a USB multi-port power supply. I used an Anker PowerPort 6, but there are also good and cheaper alternatives. You’ll have to Google this too. Check that each port can supply 5V and at least 2.4A.
  • USB cables: Each board needs to be powered via a USB-C port. You need a cable with one end of type USB-C and the other of the type your power hub accepts.
  • Bolts and nuts: To stack up the boards.
  • Heat sinks (optional): These boards can get hot. I recommend getting heat sinks to help with heat dissipation.

Materials needed for building an Orange Pi Zero2 cluster

Materials needed for building an Orange Pi Zero2 cluster

Assembling the Cluster

One of the fun parts of building this cluster is the physical assembly of the boards on a case or some kind of structure that makes them look like a single manageable unit. Since my objective here is to keep the budget as low as possible, I used cheap bolts and nuts to stack the boards one on top of the other. I didn’t find any ready-to-use cluster cases for the Orange Pi Zero2. One alternative is to 3D-print your own case.

When stacking the boards together, keep an eye on the antenna placement. Avoid crushing the cable, especially if you installed heat sinks.

An assembled Orange Pi Zero2 cluster with 4 nodes

An assembled Orange Pi Zero2 cluster with 4 nodes

Installing the Operating System

The second step is to install the operating system on each microSD card. I used Armbian bullseye legacy 4.9.318. Download the file and use a tool like balenaEtcher to make bootable microSD cards. Download and install this tool on your computer. Select the Armbian image file and the drive that corresponds to the micro SD card. Flash the image and repeat the process for each micro SD card.

Configuring Orange Pi WiFi Connection (Headless)

To configure the Wi-Fi connection, Armbian includes the /boot/armbian_first_run.txt.template file which allows you to configure the operating system when it runs for the first time. The template includes instructions, so it’s worth checking. You have to rename this file to armbian_first_run.txt. Here’s what I used:

Plain Text
 
FR_general_delete_this_file_after_completion=1
FR_net_change_defaults=1
FR_net_ethernet_enabled=0
FR_net_wifi_enabled=1
FR_net_wifi_ssid='my_connection_id>'
FR_net_wifi_key='my_password'
FR_net_wifi_countrycode='FI'
FR_net_use_static=1
FR_net_static_gateway='192.168.1.1'
FR_net_static_mask='255.255.255.0'
FR_net_static_dns='192.168.1.1 8.8.8.8'
FR_net_static_ip='192.168.1.181'


Use your own Wi-Fi details, including connection name, password, country code, gateway, mask, and DNS.

I wasn’t able to read the SD card from macOS. I had to use another laptop with Linux on it to make the changes to the configuration file on each SD card. To mount the SD card on Linux, run the following command before and after inserting the SD card and see what changes:

Shell
 
sudo fdisk -l


I created a Bash script to automate the process. The script accepts as a parameter the IP to set. For example:

Shell
 
sudo ./armbian-setup.sh 192.168.1.181


I run this command on each of the four SD cards changing the IP address from 192.168.1.181 to 192.168.1.184.

Connecting Through SSH

Insert the flashed and configured micro SD cards on each board and turn the power supply on. Be patient! Give the small devices time to boot. It can take several minutes the first time you boot them.

An Orange Pi cluster running Armbian

An Orange Pi cluster running Armbian

Use the ping command to check whether the devices are ready and connected to the network:

Shell
 
ping 192.168.1.181


Once they respond, connect to the mini-computers through SSH using the root user and the IP address that you configured. For example:

Shell
 
ssh root@192.168.1.181


The default password is:

Plain Text
 
1234


You’ll be presented with a wizard-like tool to complete the installation. Follow the steps to finish the configuration and repeat the process for each board.

Installing Ansible

Imagine you want to update the operating system on each machine. You’ll have to log into a machine and run the update command and end the remote session. Then repeat for each machine in the cluster. A tedious job even if you have only 4 nodes. Ansible is an automation tool that allows you to run a command on multiple machines using a single call. You can also create a playbook, a file that contains commands to be executed in a set of machines defined in an inventory.

Install Ansible on your working computer and generate a configuration file:

Shell
 
sudo su
ansible-config init --disabled -t all > /etc/ansible/ansible.cfg
exit


In the /etc/ansible/ansible.cfg file, set the following properties (enable them by removing the semicolon):

Plain Text
 
host_key_checking=False
become_allow_same_user=True
ask_pass=True


This will make the whole process easier. Never do this in a production environment!

You also need an inventory. Edit the /etc/ansible/hosts file and add the Orange Pi nodes as follows:

Plain Text
 
##############################################################################
# 4-node Orange Pi Zero 2 cluster
##############################################################################

[opiesz]
192.168.1.181        ansible_user=orangepi        hostname=opiz01
192.168.1.182        ansible_user=orangepi        hostname=opiz02
192.168.1.183        ansible_user=orangepi        hostname=opiz03
192.168.1.184        ansible_user=orangepi        hostname=opiz04

[opiesz_manager]
opiz01.local        ansible_user=orangepi

[opiesz_workers]
opiz[02:04].local    ansible_user=orangepi


In the ansible_user variable, specify the username that you created during the installation of Armbian. Also, change the IP addresses if you used something different.

Setting up a Cluster With Ansible Playbooks

A key feature of a computer cluster is that the nodes should be somehow logically interconnected. Docker Swarm is a container orchestration tool that will convert your arrangement of Orange Pi devices into a real cluster. You can later deploy any kind of server software. Docker Swarm will automatically pick one of the machines to host the software.

To make the process easier, I have created a set of Ansible playbooks to further configure the boards, update the packages, reboot or power off the machines, install Docker, set up Docker Swarm, and even install a MariaDB database with replication and a database cluster.

Clone or download this GitHub repository:

Shell
 
git clone https://github.com/alejandro-du/orange-pi-zero-cluster-ansible-playbooks.git


Let’s start by upgrading the Linux packages on all the boards:

Shell
 
ansible-playbook upgrade.yml --ask-become-pass


Now configure the nodes to have an easy-to-remember hostname with the help of Avahi, and configure the LED activity (red LED activates on SD card activity):

Shell
 
ansible-playbook configure-hosts.yml --ask-become-pass


Reboot all the boards:

Shell
 
ansible-playbook reboot.yml --ask-become-pass


Install Docker:

Shell
 
ansible-playbook docker.yml --ask-become-pass


Set up Docker Swarm:

Shell
 
ansible-playbook docker-swarm.yml --ask-become-pass


Done! You have an Orange Pi cluster ready for fun!

Deploying MariaDB on Docker Swarm

I have to warn you here. I don’t recommend running a database on container orchestration software. That’s Docker Swarm, Kubernetes, and others. Unless you are willing to put a lot of effort into it. This article is a lab. A learning exercise. Don’t do this in production! Now let’s get back to the fun…

Run the following to deploy one MariaDB primary server, two MariaDB replica servers, and one MaxScale proxy:

Shell
 
ansible-playbook mariadb-stack.yml --ask-become-pass


The first time you do this, it will take some time. Be patient.

SSH into the manager node:

Shell
 
ssh orangepi@opiz01.local


Inspect the nodes in the Docker Swarm cluster:

Shell
 
docker node ls


Inspect the MariaDB stack:

Shell
 
docker stack ps mariadb


A cooler way to inspect the containers in the cluster is by using the Docker Swarm Visualizer. Deploy it as follows:

Shell
 
docker service create
  --name=viz
  --publish=9000:8080
  --constraint=node.role==manager
  --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock
  alexellis2/visualizer-arm:latest


On your working computer, open a web browser and go to this URL. You should see all the nodes in the cluster and the deployed containers.

Docker Swarm Visualizer showing MariaDB deployed

Docker Swarm Visualizer showing MariaDB deployed

MaxScale is an intelligent database proxy with tons of features. For now, let’s see how to connect to the MariaDB cluster through this proxy. Use a tool like DBeaver, DbGate, or even a database extension for your favorite IDE. Create a new database connection using the following connection details:

  • Host: opiz01.local
  • Port: 4000
  • Username: user
  • Password: password

Create a new table:

MariaDB SQL
 
USE demo;
CREATE TABLE messages(
    id INT PRIMARY KEY AUTO_INCREMENT,
    content TEXT NOT NULL
);


Insert some data:

MariaDB SQL
 
INSERT INTO messages(content)
VALUES ("It works!"),
    ("Hello, MariaDB"),
    ("Hello, Orange Pi");


When you execute this command, MaxScale sends it to the primary server. Now read the data:

MariaDB SQL
 
SELECT * FROM messages;


When you execute this command, MaxScale sends it to one of the replicas. This division of reads and writes is called read-write splitting.

The MaxScale UI showing a MariaDB cluster with replication and read-write splitting

The MaxScale UI showing a MariaDB cluster with replication and read-write splitting

You can also access the MaxScale UI. Use the following credentials:

  • Username: admin
  • Password: mariadb

Watch the following video if you want to learn more about MaxScale and its features. You won’t regret it!


MariaDB Ansible (software) Docker (software) clusters IoT

Opinions expressed by DZone contributors are their own.

Related

  • Fast Deployments of Microservices Using Ansible and Kubernetes
  • Dockerizing an Ansible Playbook, Part 1
  • Dockerizing an Ansible Playbook, Part 2
  • Building a 24-Core Docker Swarm Cluster on Banana Pi Zero

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: