Purpose of the magent agent:
If memcached one of the cache nodes of the machine down, then the client's cached data will be lost part of the cache, we can use the Magent cache proxy, to prevent a single point of the phenomenon, the cache proxy can also do backup, through the client connection to the cache proxy server, The cache proxy server connects to the cache server, and the cache proxy server can connect multiple memcached machines, enabling high availability and preventing single points of failure.
Use of keepalived:
However, if the Magent proxy server is down, it cannot continue to provide services, so it is used keepalived, the keepalived configuration file to set priorities to determine who magent or from Magent, When the main magent normal operation VIP in the main magent, when the main magent downtime VIP automatically switch to from Magent, the main magent after the VIP automatically return to the main magent.
Experiment Preparation
Magent Agent and memcached service installed on the same host, only for experimental reference.
Host |
system |
IP |
role |
Software |
Memcached 1 |
CentOS 7 |
192.168.100.130 |
Master Node |
Memcached-1.5.6.tar.gz, libevent-2.1.8-stable.tar.gz |
Magent 1 |
CentOS 7 |
192.168.100.130 |
Primary proxy Server |
Magent-0.5.tar.gz |
Memcached 2 |
CentOS 7 |
192.168.100.131 |
From the node |
Memcached-1.5.6.tar.gz, libevent-2.1.8-stable.tar.gz |
Magent 2 |
CentOS 7 |
192.168.100.131 |
From a proxy server |
Magent-0.5.tar.gz |
Client |
CentOS 7 |
192.168.100.132 |
Test host |
Telnet |
VIP 192.168.100.100
The experiment steps are as follows: 1.yum Install software support Package
yum install gcc gcc-c++ -y
2. All hosts shut down the firewall
systemctl stop firewalld.service setenforce 0
3.client Host installation Telnet software
yum install telnet -y
3. Install the memcached and libevent software packages (1) in memcached 1 and memcached 2 to extract the software to the OPT directory.
tar xzvf memcached-1.5.6.tar.gz -C /opt/tar xzvf libevent-2.1.8-stable.tar.gz -C /opt/
(2) Compile and install Libevent
cd /opt/libevent-2.1.8-stable./configure --prefix=/usr/local/libevent #指定安装路径make && make install
(3) Compile and install memcached
cd memcached-1.5.6./configure --with-libevent=/usr/local/libevent make && make install
(4) The Libevent-2.1.so.6 module needs to be copied to the/usr/lib64 directory, otherwise it will error.
ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
4. Install Magent (1) on memcached 1 to create the Magent directory under the OPT directory and store the extracted files of the Magent package.
mkdir /opt/magenttar xzvf magent-0.5.tar.gz -C /opt/magent
(2) Modify the Ketama.h configuration file.
cd /opt/magentvim ketama.h #修改头部文件 #ifndef SSIZE_MAX #define SSIZE_MAX 32767
(3) Modify the Makefile configuration file
vim Makefile LIBS = -levent -lm -L /usr/local/libevent/lib #修改 INCLUDE=-I /usr/local/libevent/include #添加
(4) Compiling magent
make
(5) The magent command file generated after compilation is copied to/usr/bin for the system to recognize the magent command.
cp /opt/magent/magent /usr/bin ##方便系统识别命令
5. Start the memcached service
memcached -m 512k -u root -d -l 192.168.100.130 -p 11211memcached -m 512k -u root -d -l 192.168.100.131 -p 11211netstat -ntap |grep 11211有11211端口表示服务开启成功
6.client Client test magent Solve single point of failure problem (1) Start the Magent service on memcached 1 only
magent -u root -n 51200 -l 192.168.100.130 -p 12000 -s 192.168.100.130:11211 -b 192.168.100.131:11211
(2) test on the client host
(3) Stop the service of the master node and test on the client host
pkill -9 memcached
The experimental steps are as follows: 1. Then the above operation continues, the master and slave nodes have to install the Keepalived software
yum install keepalived -y
2. No need to install magent software from magent, just copy the magent command file generated by the memcached on the 1 master node to the Magent directory of the main/usr/bin
scp /opt/magent/magent [email protected]:/usr/bin
3. Edit the keepalived configuration file on the main magent
vim /etc/keepalived/keepalived.confrouter—_id MAGENT_HA#删除以下内容 vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0#定义新函数magentvrrp_script magent{script"/opt/shell/magent.sh" interval 2 ##时间间隔}#修改vrrp_instance实例INTERFACE ens33#添加track_script { magent} # 使用新函数magent#定义一个虚拟IPvirtual_ipaddress { 192.168.100.100 }#下面多余的部分可以全部删除
4. Edit the keepalived profile from the Magent
#从主magent上把配置脚本复制到从magent上scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/keepalived.confvim /etc/keepalivedrouter_id MAGENT_HB #修改为MAGENT_HBstate BACKUP #修改为BACKUPvirtual_router_id 52 #ID不同priortity 90 #优先级比主magent低
5. Configure the Magent script on the main magent
mkdir /opt/shell vim /opt/shell/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.100 -p 12000 -s 192.168.100.130:11211 -b 192.168。100.131:11211elsepkill -9 magentfi-n 51200 //定义用户最大连接数-l 192.168.100.100 //指定虚拟IP-p 12000 //指定端口号-s //指定主缓存服务器-b //指定从缓存服务器chmod +x magent.sh
6. Configuring the Magent script from Magent
mkdir /opt/shell vim /opt/shell/magent.sh #!/bin/bash K=`ip addr | grep 192.168.100.100 | grep -v grep | wc -l`if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.100.100 -p 12000 -s 192.168.100.130:11211 -b 192.168.100.131:11211elsepkill -9 magentfi chmod +x magent.sh
7. Master-Slave magent server to open keepalived service
systemctl start keepalived.servicenetstat -ntap |grep 12000如果有12000端口表示服务开启成功
8. Test keepalived High Availability on the client host (1) Turn off the keepalived service on the main magent, and the virtual IP address drifts from the magent.
(2) Stop the main magent service, the client host can still enter the proxy service port 12000.
(3) Viewing from the Magent log, you can see the switch from the Magent server to the master state
This memcached+magent+keepalived high availability cluster is complete
Memcached+magent+keepalived implementing a highly available cluster