Experimental environment
Memcached primary master replication refers to any one memcached server modified data will be synchronized to another, but Memcached API client is unable to determine which memcached server to connect to, so you need to set up a VIP address, Provided to the Memcached API client for connection. You can use the VIP address generated by kcepalived to connect to the primary memceached server and provide a highly available architecture.
This case is done using two memcahed servers and a single test host.
主Memcahed服务器IP:192.168.10.161备Memcahed服务器IP:192.168.10.157客户机IP:192.168.10.162
Memcached Primary Master replication schema
The Memcached replication feature enables replication between multiple memcached (bidirectional replication, both readable and writable), which resolves memcached disaster tolerance issues.
1, configure memcached Primary cache node and backup cache node, both configurations are the same
Unpack the package, install the build environment package
tar xzvf memcached-1.5.6.tar.gz -C /opt/tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/yum install gcc gcc -c++ make -y
Configuring the Installation Libevent,memcached
cd /opt/libevent-2.1.8-stable/./configure --prefix=/usr/ //指明安装路径make && make installcd /opt/memcached-1.5.6/ ./configure --with-libevent=/usr //指明安装路径make && make install
Copy the compiled and installed libevent-2.1.so.6 module to the/usr/lib64 directory
ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
2, install magent third-party plug-in in memcached Primary cache node, backup cache node does not need to do
mkdir /opt/magent //创建magent工作目录tar xzvf magent-0.5.tar.gz -C /opt/magent/cd /opt/magentvim ketama.h #ifndef SSIZE_MAX#define SSIZE_MAX 32767 //修改开头注释······#endifvi MakefileLIBS = -levent -lm //首行LIBS = -levent后加上-lmmake //make安装#安装完成后可使用ls查看到当前目录下有magent程序cp magent /usr/bin/ //把生成的mgent程序复制到/usr/bin目录下让系统识别
Copy the generated Magent program to the backup cache node
yum install openssh-clientsscp magent [email protected]:/usr/bin/ //把产生的magent文件直接复制到备份缓存节点。
Deploy memcached Primary master replication + keepalived high-availability architecture
Because the memcached master replicates this architecture, it is not known which primary server to connect to when the program is connected, so you need to add a VIP address to the front end to achieve a highly available architecture. This is implemented with keepalived, so keepalived is used to detect whether the state of the memcached server is normal
Keepalived continuously detects the port 11211 of the memcached master server, and if the memcached service is detected as down or down, it will move the VIP from the primary server to the slave server for high availability of memcached.
1. Install the configuration keepalived on the Memcached Primary cache node and the backup cache node and modify the keepalived master configuration file
yum install keepalived -y#修改keepalived主配置文件vim /etc/keepalived/keepalived.conf router_id MAGENT_HA //自行定义router_id,主备不同vrrp_script magent { script "/opt/shell/magent.sh" interval 2} //添加模块指定magent脚本运行路径vrrp_instance VI_1 { state MASTER //指明为主(备)缓存节点 interface ens33 //修改网卡名称 virtual_router_id 51 //自行指定virtual_router_id,主备不同 priority 100 //优先级,备低于从 advert_int 1 authentication { auth_type PASS auth_pass 1111 }track_script { //调用上方定义好的magent magent} virtual_ipaddress { //指明VIP 192.168.10.200 }
For convenience, you can use the following command to copy the keepalived configuration file that has been modified by the primary cache node for a little change.
#备缓存节点上执行cd /etc/keepalivedmv keepalived.conf keepalived.conf.bak //重命名keepalived配置文件,备份作用scp [email protected]:/etc/keepalived/keepalived.conf ./ //把主服务器的配置文件复制到当前目录下
2. Write magent run script on the primary cache node to enable and turn on the keepalived service
mkdir /opt/shellcd /opt/shellvi magent.sh#!/bin/bashK=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.10.200 -p 12000 -s 192.168.10.161:11211 -b 192.168.10.157:11211elsepkill -9 magentfi
Magent Optional Annotations
-n 51200 //定义用户最大连接数-l 192.168.10.200 //指定虚拟IP-p 12000 //指定端口号-s //指定主缓存服务器-b //指定从缓存服务器
Give the script executable permission to execute and turn on the keepalived service
chmod +x magent.sh systemctl stop firewalld.servicesetenforce 0./magent.shsystemctl start keepalived.servicenetstat -antp | grep 12000 //确认magent运行
3. Write magent run script from cache node to enable and turn on keepalived service
mkdir /opt/shellcd /opt/shell/vi magent.sh#!/bin/bashK=`ip addr | grep 192.168.10.200 | grep -v grep | wc -l`if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.10.200 -p 12000 -s 192.168.10.161:11211 -b 192.168.10.157:11211elsepkill -9 magentfi chmod +x magent.shsystemctl stop firewalld.servicesetenforce 0./magent.shsystemctl start keepalived.servicenetstat -antp | grep 12000 确认magent运行 比较慢
4. Start memcached on the primary cache node
memcached -m 512k -u root -d -l 192.168.10.161 -p 11211
5. Start memcached from the cache node
memcached -m 512k -u root -d -l 192.168.10.157 -p 11211
6, check the 11211 port, confirm the open
netstat -anpt | grep 11211
In client test 1, install Telnet, sign in to add content
yum install telnet -ytelnet 192.168.10.200 12000 add usename 0 0 71234567
2, to the main, standby cache node login can see the added content
yum install telnet -ytelnet 192.168.10.200 12000get usename
3, switch off the main keepalived service, the client can still log in to view content
#主缓存服务器:systemctl stop keepalived.service#客户端telnet 192.168.10.200 12000get usename
Deploy memcached Primary master replication + keepalived High-availability architecture on CentOS 7