Why?


Search This Blog

Saturday, June 16, 2018

Galera Cluster on CentOS 7

Galera Cluster on CentOS 7

Resource links

https://linuxadmin.io/galeria-cluster-configuration-centos-7/
https://mariadb.com/kb/en/library/getting-started-with-mariadb-galera-cluster/
https://mariadb.com/kb/en/library/galera-cluster-system-variables/

I will be using three servers

mysql1 = 192.168.10.160
mysql2 = 192.168.10.161
mysql3 = 192.168.10.162

With mysql1 being the master node.


Turn off firewalld

Disable selinux

reboot

Add the MariaDB repository to each of the 3 or more nodes

vi /etc/yum.repos.d/MariaDB.repo

Insert the following repository information and save the file

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Install the packages from yum, Galera is included when these are installed when using 10.1 or greater

yum -y install MariaDB-server MariaDB-client MariaDB-common

rsync is the default to perform the replication so install that. Also lsof (list open files)

yum install -y rsync lsof

Make sure each of the MariaDB instances starts on reboot

systemctl enable mariadb

Galera Master Node Configuration

After installing MariaDB on the master node edit the server.cnf file

vi /etc/my.cnf.d/server.cnf

add the following under the [galera]

binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.10.160,192.168.10.161,192.168.10.162"
#
## Galera Cluster Configuration
wsrep_cluster_name="cluster1"
## Galera Synchronization Configuration
wsrep_sst_method=rsync
## Galera Node Configuration
####unigue per node
wsrep_node_address="192.168.10.160"
####unigue per node
wsrep_node_name="mysql1"

wsrep_on=ON – Setting this to ON enables replication. In MariaDB 10.1, replication is turned off as a default, so this needs to be explicitly stated.
wsrep_cluster_address  – This is where we specify each of the IP addresses for the nodes delineated by a comma. The primary node is always the first IP address, this this case its 192.168.10.160
wsrep_cluster_name – Is the name of the cluster, you can name this anything you want
wsrep_node_address – Is the IP address of the node you are configuring
wsrep_node_name – This is the name of the node you are currently configuring, it can be named anything you want, it just needs to be unique.


Under the [mysqld] section add a log location (if you don’t, it will log to the main syslog)

log_error=/var/log/mariadb.log

Once you have finished editing and saved server.cnf, go ahead and create the error log

touch /var/log/mariadb.log

Give the error log the appropriate permissions:

chown mysql:mysql /var/log/mariadb.log

You can now start the new master node by typing the following

galera_new_cluster

After you have started it, make sure it has bound to the correct ports using lsof

Port 4567 is for replication traffic:

# lsof -i:4567
 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
 mysqld 4121 mysql 11u IPv4 34770 0t0 TCP *:tram (LISTEN)
Port 3306 is for MySQL client connections:

# lsof -i:3306
 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
 mysqld 4121 mysql 26u IPv4 34787 0t0 TCP *:mysql (LISTEN)

mysql

Then check the cluster size

MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';
 +--------------------+-------+
 | Variable_name      | Value |
 +--------------------+-------+
 | wsrep_cluster_size | 1     |
 +--------------------+-------+
It should say 1 at this point because only the primary node is connected.

Adding Additional Nodes To Galera

After installing MariaDB on the addtional nodes, you will want to copy the [galera] section of /etc/my.cnf.d/server.cnf that We created earlier and insert it into the server.cnf on each of the additional nodes. The only lines that will each on each of the additional nodes will be the the following:

wsrep_node_address="192.168.10.161"
wsrep_node_name="mysql2"

The wsrep_node_address will be the IP address of the node you are configuring and the wsrep_node_name will be the name of that node.

After you have finished each of the servers configuration files, you can start them normally

systemctl start mariadb

As each node connects to the cluster you should see the wsrep_cluster_size increase:

MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';
 +--------------------+-------+
 | Variable_name      | Value |
 +--------------------+-------+
 | wsrep_cluster_size | 3     |
 +--------------------+-------+

You will also see nodes join in the log:

WSREP: Member 1.0 (centos7-vm2) synced with group.
The logs will also indicate when a node as left the group:

WSREP: forgetting 96a5eca6 (tcp://192.168.1.101:4567)
WSREP: New COMPONENT: primary = yes, bootstrap = no, my_idx = 0, memb_num = 2

You can view the full configuration of Galera by typing the following:

MariaDB [(none)]> show status like 'wsrep%';

Testing Replication On The Galera Cluster. On any node create a new db with

MariaDB [(none)]> create database testdb1;

You should see the testdb1 database appear on the other nodes as well.

No comments:

Post a Comment