How to Build MySQL Master‑Master HA with LVS and Keepalived
This guide explains how to achieve high‑availability MySQL master‑master replication by combining LVS load balancing with Keepalived failover, covering installation, configuration, script creation, service management, testing, and a concise summary of the solution.
MySQL replication can provide data redundancy and read/write separation, but native master‑master replication lacks a unified access point and automatic failover when a master fails.
Keepalived and LVS Introduction
Keepalived is a VRRP‑based high‑availability solution that avoids single‑point failures and is often used with lightweight HA setups without shared storage, such as LVS+Keepalived or Nginx+Keepalived.
LVS (Linux Virtual Server) is a high‑availability virtual server cluster system that operates at the network layer. It consists of a Director (load‑balancing layer) and Real Servers (backend servers). The diagram below shows the basic LVS architecture.
LVS supports three operating modes: Direct Routing (DR), Tunneling (TUN), and Network Address Translation (NAT). DR and TUN allow multiple real servers, while NAT has limited scalability. LVS provides ten scheduling algorithms, such as rr, wrr, lc, wlc, lblc, lblcr, dh, sh, sed, and nq.
Environment Preparation
LVS1: 192.168.1.2
LVS2: 192.168.1.11
MySQL Server1: 192.168.1.5
MySQL Server2: 192.168.1.6
VIP: 192.168.1.100
OS: CentOS 6.4
Install Keepalived
Install required packages: # yum install -y kernel-devel openssl openssl-devel Download, extract, configure, and compile Keepalived:
# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.5.1.el6.x86_64/Sample configuration output: # make Copy the init script and configuration files:
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ # cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ # chkconfig mysqld on # chkconfig keepalived onInstall LVS
Install required packages: # yum install -y libnl* popt* Load LVS kernel modules: # modprobe -l | grep ipvs Extract and compile ipvsadm:
# ln -s /usr/src/kernels/2.6.32-431.5.1.el6.x86_64/ /usr/src/linux # tar -zxvf ipvsadm-1.26.tar.gz # makeVerify installation:
# ipvsadm -L -nConfigure Keepalived
Edit /etc/keepalived/keepalived.conf with the following content:
global_defs { router_id LVS1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS; auth_pass 1111; } virtual_ipaddress { 192.168.1.100 } virtual_server 192.168.1.100 3306 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.1.5 3306 { weight 3; TCP_CHECK { connect_timeout 3; nb_get_retry 3; delay_before_retry 3; connect_port 3306; } } real_server 192.168.1.6 3306 { weight 3; TCP_CHECK { connect_timeout 3; nb_get_retry 3; delay_before_retry 3; connect_port 3306; } } } }Create LVS Startup Script
Save the following script as /etc/init.d/realserver and make it executable:
#!/bin/sh VIP=192.168.1.100 case "$1" in start) ifconfig lo up echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/all/arp_announce ifconfig lo:$VIP netmask 255.255.255.255 up route add -host $VIP dev lo echo "LVS-DR real server starts successfully." ;; stop) ifconfig lo down route del -host $VIP dev lo echo "LVS-DR real server stopped." ;; status) # check status (implementation omitted) ;; *) echo "Usage: $0 {start|stop|status}" exit 1 ;; esac exit 0Make the script executable and add it to startup:
# chmod +x /etc/init.d/realserver # echo "/etc/init.d/realserver" >> /etc/rc.d/rc.localStart Services
Start LVS and Keepalived services:
# service realserver start # service keepalived startAfter starting, the virtual IP will be bound to the real server and the LVS cluster will show two real servers with their weights and connection statistics.
Testing and Verification
Functional verification
Stop MySQL Server2: # service mysqld stop Keepalived logs on LVS1 will show a failed TCP connection to 192.168.1.6:3306 and automatically remove the faulty node from the LVS pool.
Restart MySQL Server2; Keepalived will detect the recovery and add the node back to the pool.
Stop Keepalived on LVS1 to simulate a node failure. Logs show Keepalived removing the VIP from LVS1, and LVS2 transitions to MASTER state, taking over the VIP.
Verify the LVS cluster status on LVS2 with # ipvsadm -ln; both real servers are listed and traffic is balanced.
Conclusion
MySQL master‑master replication forms the basis of the cluster, creating a server array where each node acts as a real server.
LVS provides load balancing, distributing client requests across real servers; a single real‑server failure does not affect the whole cluster.
Keepalived adds HA for the LVS nodes, preventing a single point of failure and enabling automatic failover to a healthy node.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
