Skip to main content

Creating a Galera Cluster with LXD

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:45:00

Difficulty

Intermediate

LXD offers a quick-to-deploy, highly-efficient environment in which we can host a fault-resistant Galera cluster. In this hands-on lab, we'll do just that by creating and configuring an initial container, then using that as a basis for a three-node Galera cluster.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating a Galera Cluster with LXD

Introduction

LXD offers a quick-to-deploy, highly-efficient environment in which we can host a fault-resistant Galera cluster. In this hands-on lab, we'll do just that by creating and configuring an initial container, then using that as a basis for a three-node Galera cluster.

The Scenario

You have been tasked with setting up an initial Galera cluster on an Ubuntu 18.04 container for use with a recently-containerized legacy application. Create and configure the initial Galera server, snapshot it, then create two additional containers and the configuration appropriately.

Logging In

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user.

Configure the Initial Cluster Containers

Create the container:

lxc launch ubuntu:18.04 galera01

Access the container and run initial updates:

lxc exec galera01 -- bash
apt update
apt upgrade

Add the MariaDB repository:

apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
add-apt-repository 'deb [arch=amd64,ppc64el] http://ftp.utexas.edu/mariadb/repo/10.1/ubuntu bionic main'
apt update

Install MariaDB:

apt install mariadb-server -y

Type in a root password when the prompt comes up.

Now, further secure the database install by running mysql_secure_installation:

mysql_secure_installation

We just set the root password, so we don't have to change it now. Answer yes to removing anonymous users, disallowing root login remotely, removing the test databases and access to it, and reloading privilege tables.

Update the innodb_buffer_pool_size for the MariaDB configuration:

vim /etc/mysql/my.cnf
innodb_buffer_pool_size = 16M

Configure the firewall:

ufw enable
ufw allow 3306/tcp
ufw allow 4444/tcp
ufw allow 4567/tcp
ufw allow 4568/tcp
ufw allow 4567/udp
ufw status

Make sure all those rules got added by running ufw status.

Exit the container and take a snapshot:

exit
lxc snapshot galera01 init

Create the two additional containers and start them:

lxc copy galera01/init galera02
lxc copy galera01/init galera03
lxc start galera02
lxc start galera03

Configure Galera

View all containers and make note of each IP address:

lxc list

Make a note of the IP addresses of each container, and which one they belong to.

Open the galera.cnf configuration in the cloud_user's home directory and update the wsrep_cluster_address to list all IP addresses. Update the wsrep_node_address and wsrep_node_name for the galera01 node:

vim galera.cnf
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0

# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Galera Cluster Configuration
wsrep_cluster_name="galera_cluster"
wsrep_cluster_address="gcomm://<IP_ADDRESS>,<IP_ADDRESS>,<IP_ADDRESS>"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="<IP_ADDRESS>"
wsrep_node_name="galera01"

Push to the first container:

lxc file push galera.cnf galera01/etc/mysql/conf.d/galera.cnf

Update the configuration file again, changing the address and name so it works with galera02:

vim galera.cnf
wsrep_node_address="<IP_ADDRESS>"
wsrep_node_name="galera02"

Then push this file to glaera02:

lxc file push galera.cnf galera02/etc/mysql/conf.d/galera.cnf

Do the same for galera03:

vim galera.cnf
wsrep_node_address="<IP_ADDRESS>"
wsrep_node_name="galera03"

Finally, push this file once more, but to garera03:

lxc file push galera.cnf galera03/etc/mysql/conf.d/galera.cnf

Start the Cluster

Stop MySQL on all nodes:

lxc exec galera01 -- systemctl stop mysql
lxc exec galera02 -- systemctl stop mysql
lxc exec galera03 -- systemctl stop mysql

Start the Galera cluster:

lxc exec galera01 -- galera_new_cluster
lxc exec galera02 -- systemctl start mysql
lxc exec galera03 -- systemctl start mysql

To confirm that everything is working, view the cluster size:

lxc exec galera01 -- mysql -u root -p -e "show status like 'wsrep_cluster_size'"

Conclusion

Our Galera cluster is up and running. Congratulations!