Memcached +keepalived High Availability cluster
Memcached Primary master replication This architecture, when the program is connected to not know which primary server, so need to add VIP address on the front-end to achieve a high-availability architecture. This is achieved 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.
System environment
Host name |
Operating System |
IP Address |
Package |
VIP Address |
Memcached1 Server |
CentOS 7.4 x86_64 |
192.168.100.201 |
Libevent-2.1.8-stable.tar.gz, memcached-1.5.9.tar.gz, magent-0.5.tar.gz, keepalived |
192.168.100.200 |
Memcached2 Server |
CentOS 7.4 x86_64 |
192.168.100.202 |
Libevent-2.1.8-stable.tar.gz, memcached-1.5.9.tar.gz, keepalived |
192.168.100.200 |
Client clients |
CentOS 7.4 x86_64 |
192.168.100.203 |
Telnet |
|
Deployment Services
Configure the Memcached Primary cache node and-----Two configurations from the cache node
# tar zxvf 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 # cd /opt/libevent-2.1.8-stable# ./configure --prefix=/usr/local/libevent# make && make install # cd /opt/memcached-1.5.6# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/# make && make install # ln -s /usr/local/memcached/bin/* /usr/local/bin/
Install the Libevent-2.1.so.6 module into a soft link to the/usr/lib64 directory, or you will get an error when starting Magent
# ln -s /usr/local/libevent/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
Installing Magent on the primary server
Magent is an open-source Memcached agent software that can be used to build Memcached services for highly available clustered applications, back up Memcached data, and even if the Memcached service hangs up, the front end can also get data, and the client connects to the Magent Proxy service first. The Magent proxy server can connect multiple Memcached servers, and then data can be saved and backed up. This data is not lost and data integrity is preserved.
- Primary server
# wget http://memagent.googlecode.com/files/magent-0.5.tar.gz# mkdir /opt/magent# tar zxvf magent-0.5.tar.gz -C /opt/magent# cd /opt/magent
- Before compiling the installation, you need to modify the file, otherwise it will error
# vim ketama.h //修改下面的行 #ifndef SSIZE_MAX #define SSIZE_MAX 32767 #endif # vim Makefile
- After compiling, an executable file is generated and we make the generated magent command to be recognized by the system and sent to the MEMCACHED2 server.
# cp magent /usr/bin/ //把生成的magent命令让系统识别# scp /opt/magent/magent [email protected]:/usr/bin //把生成的magent命令复制到memcached2服务器
Install the configuration on two memcached servers keepalived
- Memcached Two servers are already marked in different configurations
# yum install keepalived -y# vim /etc/keepalived/keepalived.conf router_id MAGENT_HA //主从不同 自定义下面删除4行} //调用这个脚本每2秒检查一次magent状态vrrp_script magent { script "/opt/shell/magent.sh" interval 2}vrrp_instance VI_1 { state MASTER // 另一台服务器这里是BACKUP interface ens33 virtual_router_id 51 //虚拟路由ID ,两台相同 priority 100 //优先级 从要小于主 advert_int 1 authentication { auth_type PASS auth_pass 1111 }track_script { //调用上面定义的脚本 magent} virtual_ipaddress { //定义VIP地址192.168.100.200}}
- Creating a magent.sh script on two servers facilitates keepalived invocation
- Memcached1 Server
# mkdir -p /opt/shell/# cd /opt/shell/# vi 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.100.200 -p 12000 -s 192.168.100.201:11211 -b 192.168.100.202:11211elsepkill -9 magentfi
Script to add execute permissions and turn on the keepalived service
# chmod +x magent.sh# systemctl start keepalived.service# ip addr //查看VIP的IP地址
- Scripting Options Explained
-n 51200 //定义用户最大连接数-l //指定虚拟IP-p 12000 //指定端口号-s //指定主缓存服务器-b //指定从缓存服务器
- Memcached2 Server
# mkdir -p /opt/shell/# cd /opt/shell/# vi magent.sh #!/bin/bashK=`ip addr | grep 192.168.100.200 | grep -v grep | wc -l`if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.100.200 -p 12000 -s 192.168.100.201:11211 -b 192.168.100.202:11211elsepkill -9 magentfi
- Script to add execute permissions and turn on the keepalived service
# chmod +x magent.sh# systemctl start keepalived.service
Testing on the client
- Test Primary master replication
We use Telnet to log in to the VIP address on the client and write the data, and we can see that the data is synchronized on both memcached cache servers.
- Testing for high Availability
memcached1 server down, log on to the MEMCACHED2 server, view the IP address, the VIP has drifted to the MEMCACHED2 server
Memcached Primary master replication + keepalived Implementation Memcached High-availability architecture cluster