Setting Up a Galera Cluster on Amazon AWS EC2

Through Amazon Web Services (AWS), you can create virtual servers (i.e., instances). You can install database and Galera software on them. In this article, we’ll create three nodes, the minimum recommended for a healthy cluster, and configure them to use Galera Cluster.

Incidentally, there is a more detailed version of this article in the Tutorial section of our Library.

Assumptions & Preparation

We’re assuming you have an AWS account and know the basics of the EC2 (Elastic Compute Cloud) platform.

To access the nodes, you’ll need an encryption key. Create a new one specifically for Galera, using a tool such as ssh-keygen. Add that key to AWS, under Key Pairs.

Creating AWS Instances

To start creating instances in AWS, click on Instances, then Launch Instances. First, choose the operating system distribution. We chose here “CentOS 7 (x86_64) – with Updates HVM”.

Next, choose an instance type. Because we’re using this cluster as a training tool, we chose t2.micro, which is free for a year.

Next is the instance details. In the first box, for the number of instances, enter 3. You can leave everything else at their default values.

Adding storage is next. If you chose the free tier, the default is 8 GB. For training, this is plenty. You can click past the screen on Adding Tags.

Next is Security Group (i.e., AWS’s firewall). Create a new one for Galera and add an SSH rule to allow you to log in. For the source, choose My IP.

With that done, click on Review and Launch to see the choices you made. If everything is fine, click Launch.

A message will ask for an encryption key. Click Choose an Existing Key Pair and select the Galera one. Read and accept the warning and then click Launch Instance.

When all three nodes are running, label them (e.g., galera1). Check each instance to get their external IP addresses.

Installing Software on Nodes

You’re now ready to install the database and Galera software. Use ssh to log into each node through their external IP addresses, using your encryption key.

Install rsync, which Galera uses to synchronize new nodes, and firewalld on each node with a package-management utility like yum:

sudo yum -y install rync firewalld

The database is next. You might install MySQL or MariaDB, depending on your preferences. Both work well with Galera Cluster. There are several methods by which you may install the database and Galera software. For instructions on this, go to our documentation page on Installing Galera Cluster.

Configuring the Nodes

You’ll need to edit the database configuration file (i.e., /etc/my.cnf.d/server.cnf) on each node. There are some parameters related to MySQL or MariaDB and the InnoDB storage engine that you might want to add for better performance and troubleshooting. See the Tutorial for these. As for Galera, add a [galera] section to the configuration file:

[galera]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so

wsrep_node_name='galera1'
wsrep_node_address="172.31.19.208"

wsrep_cluster_name='galera-training'
wsrep_cluster_address="gcomm://172.31.19.208,172.31.26.197,172.31.15.54"

wsrep_provider_options="gcache.size=300M; gcache.page_size=300M"
wsrep_slave_threads=4
wsrep_sst_method=rsync

The wsrep_on enables Galera. The file path for wsrep_provider may have to be adjusted to your server.

The wsrep_node_name needs to be unique for each node. The wsrep_node_address is the IP address for the node. For AWS, use the internal ones.

The wsrep_cluster_name is the cluster’s name. The wsrep_cluster_address contains the addresses of all nodes.

Security Settings

You now have to open certain ports. Galera Cluster uses four TCP ports: 3306 (MySQL’s default), 4444, 4567, and 4568. It also uses one UDP: 4567. For SELinux, open these ports by executing the following on each node:

semanage port -a -t mysqld_port_t -p tcp 3306
semanage port -a -t mysqld_port_t -p tcp 4444
semanage port -a -t mysqld_port_t -p tcp 4567
semanage port -a -t mysqld_port_t -p udp 4567
semanage port -a -t mysqld_port_t -p tcp 4568
semanage permissive -a mysqld_t

You’ll have to do the same for the firewall:

systemctl enable firewalld
systemctl start firewalld

firewall-cmd --zone=public --add-service=mysql --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=4444/tcp --permanent
firewall-cmd --zone=public --add-port=4567/tcp --permanent
firewall-cmd --zone=public --add-port=4567/udp --permanent
firewall-cmd --zone=public --add-port=4568/tcp --permanent

firewall-cmd --reload

Now you need to add some related entries to AWS. Click Security Groups and select the Galera group. Under the Actions, select Edit Inbound Rules.

Click Add Rule and select the type, MySQL/Aurora and enter the internal IP address for the first node (e.g., 172.31.19.208/32). Next, add another rule, but this time a Custom TCP Rule for port 4444 — using the same internal address. Now add another custom TCP entry, but for port, enter “4567 – 4568”. Last, add a custom UDP entry for port 4567.

Repeat these four entries for each node, adjusting the IP addresses. When finished, click Save.

Starting Galera

When starting a new cluster, you tell the first node that it’s first by using the --wsrep-new-cluster option with mysqld. To make it easy, if you’re using MariaDB 10.4 with version 4 of Galera, you can use the galera_new_cluster script. Execute it only on the first node. This will start MySQL and Galera on that one node. On the other nodes, execute the following:

systemctl start mysql

Once MySQL has started on each, enter the line below from the command-line on one of the nodes. There’s no password yet, so just hit Enter.

mysql -p -u root -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+

You can see here there are three nodes in the cluster. That’s what we want. Galera cluster was successfully installed using AWS.

Leave a Reply

Your email address will not be published.