Percona XtraDB Cluster (PXC) itself manages quorum and node failure. Minorities of nodes in a network partition situation will move themselves into a Non-primary state and not allow any DB activity. Nodes in such a state will be easily detectable via SHOW GLOBAL STATUS variables.
It’s common to use HAproxy with PXC for load balancing purposes, but what if you are planning to just send traffic to a single node? We would standardly use keepalived to HA HAproxy, and keepalived supports track_scripts that can monitor whatever we want, so why not just monitor PXC directly?
If we have clustercheck working on all hosts:
1 2 3 4 5 6 7 8 9 10 | mysql> GRANT USAGE ON *.* TO 'clustercheck'@'localhost' IDENTIFIED BY PASSWORD '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19'; [root@node1 ~]# /usr/bin/clustercheck clustercheck password 0; echo $? HTTP/1.1 200 OK Content-Type: text/plain Connection: close Content-Length: 40 Percona XtraDB Cluster Node is synced. 0 |
Then we can just install keepalived and the this config on all nodes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | vrrp_script chk_pxc { script "/usr/bin/clustercheck clustercheck password 0" interval 1 } vrrp_instance PXC { state MASTER interface eth1 virtual_router_id 51 priority 100 nopreempt virtual_ipaddress { 192.168.70.100 } track_script { chk_pxc } notify_master "/bin/echo 'now master' > /tmp/keepalived.state" notify_backup "/bin/echo 'now backup' > /tmp/keepalived.state" notify_fault "/bin/echo 'now fault' > /tmp/keepalived.state" } |
And start the keepalived service. The virtual IP above will be brought up on an active node in the cluster and moved around if clustercheck fails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@node1 ~]# cat /tmp/keepalived.state now backup [root@node2 ~]# cat /tmp/keepalived.state now master [root@node3 ~]# cat /tmp/keepalived.state now backup [root@node2 ~]# ip a l | grep 192.168.70.100 inet 192.168.70.100/32 scope global eth1 [root@node3 ~]# mysql -h 192.168.70.100 -u test -ptest test -e "show global variables like 'wsrep_node_name'" +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | wsrep_node_name | node2 | +-----------------+-------+ |
If I shutdown PXC on node2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [root@node2 keepalived]# service mysql stop Shutting down MySQL (Percona XtraDB Cluster)....... SUCCESS! [root@node2 ~]# /usr/bin/clustercheck clustercheck password 0; echo $? HTTP/1.1 503 Service Unavailable Content-Type: text/plain Connection: close Content-Length: 44 Percona XtraDB Cluster Node is not synced. 1 [root@node1 ~]# cat /tmp/keepalived.state now master [root@node2 ~]# cat /tmp/keepalived.state now fault [root@node3 ~]# cat /tmp/keepalived.state now backup [root@node1 ~]# ip a l | grep 192.168.70.100 inet 192.168.70.100/32 scope global eth1 [root@node2 ~]# ip a l | grep 192.168.70.100 [root@node2 ~]# [root@node3 ~]# ip a l | grep 192.168.70.100 [root@node3 ~]# |
We can see node2 moves to a FAULT state and the VIP moves to node1 instead. This provides us with a very simple way to do Application to PXC high availability.
A few additional notes:
- You can disqualify donors (i.e., make clustercheck fail on Donor/Desynced nodes) by setting the 3rd argument to clustercheck to 0. Setting this to 1 means Donors can retain the VIP.
- Each keepalived instance monitors its own state only, hence the @localhost GRANT. This is much cleaner than exposing clustercheck as a web port via xinetd.
- It’s possible to more complex things with keepalived like multiple vips, node weighting, etc.
- Keepalived can track over multiple network interfaces (in this example, just eth1) for better reliability.
Team,
Please help me how can sync multiple extraDB cluster between two location.
Thanks
Ashok
Hi again, Ashok,
Once again our forums are the place to ask questions like this – however please try to include as much detail as possible. Here’s the url to the Percona XtraDB Cluster forums category: https://www.percona.com/forums/questions-discussions/percona-xtradb-cluster
Hi guys,
the conf is slightly bugged. If you don’t start slaves forcing “state BACKUP”, the IP will be configured on all interfaces.
Marco
Hi Jay!
Trying to get this working on a Docker container – is this anything you’ve done?
What’s are the benefits running ha-proxy/percona inside the docker container ? I see quite a lot of people neding a lot of applications for what reason ? There is always a nat and linux bridge between the container and the underlying os which is additional layer causing unnecessary complexity and slowness. I see people use docker under wery inappropriate circumstances.
@CaptTofu — can’t say that I have.