Memcached+magent implementation of Master-slave synchronization +keepalived High Availability cluster Introduction
Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.
Experimental deployment
| Host name |
IP Address |
main Service Software |
| Memcached Primary Server |
172.16.10.138 |
Memcached,libevent,magent,keepalived |
| Memcached from the server |
172.16.10.137 |
Memcached,libevent,keepalived |
| Client |
172.16.10.134 |
Telnet test |
- Configuring the Memcached Primary Cache node Server
tar xf libevent-2.1.8-stable.tar.gz -C /opttar xf memcached-1.5.6.tar.gz -C /opttar xf magent-0.5.tar.gz -C /optcd /opt/libevent-2.1.8./configure --prefix=/usr #指定安装路径make && make install #编译安装cd /opt/memcached-1.5.6./configure --with-libevent=/usr #指定安装路径make && make install #编译安装ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6 #主服务器上magent服务需要这个模块
Cd/opt/magent
vim ketama.h #ifndef SSIZE_MAX #修改#define SSIZE_MAX 32767 #修改 #endifvim MakefileLIBS = -levent -lm
CP Mgent/usr/bin #把生成的mgent程序让系统识别
- Configuring memcached from a cache node server
tar xzvf memcached-1.5.6.tar.gz -C /opttar xzvf libevent-2.1.8-stable.tar.gz -C /optcd /opt/libevent-2.1.8./configure --prefix=/usrmake && make installcd /opt/memcached-1.5.6./configure --with-libevent=/usrmake && make i nstallln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6 #主服务器上magent服务需要这个模块
- The master server copies the magent files to the slave server
Yum Install openssh-clients
SCP magent [Email protected]:/usr/bin///Copy the resulting magent file directly to the slave server.
- Master server Install keepalived service, modify configuration file
Yum-y Install keepalived
Vim/etc/keepalived/keepalived.conf
router_id MAGENT_HA #router_id主从不能相同,要修改vrrp_script magent { script "/opt/shell/magent.sh" interval 2 } #定义一个新函数,每2s自动执行这个脚本 vrrp_instance VI_1 {state MASTER #这边是主服务器,从服务器是BACKUP interface ens33 #网卡接口是ens33 virtual_router_id 51 priority 100 #优先级,从服务器小于主服务器 advert_int 1authentication { auth_type PASS auth_pass 1111 }track_script { magent #触发执行magent脚本 }virtual_ipaddress { 172.16.10.200 #虚拟IP(vip) } }将下面多余的配置全部删除
- Master server Install keepalived service, modify configuration file
Yum-y Install keepalived
Vim/etc/keepalived/keepalived.conf
router_id MAGENT_HB #router_id主从不相同 vrrp_script magent { script "/opt/shell/magent.sh" interval 2 } #定义一个新函数,每2s自动执行这个脚本 vrrp_instance VI_1 {state MASTER #这边是从服务器,是BACKUP interface ens33 #网卡接口是ens33virtual_router_id 51 priority 90 #优先级,从服务器小于主服务器 advert_int 1authentication {auth_type PASSauth_pass 1111 }track_script { magent #触发执行magent脚本 }virtual_ipaddress { 172.16.10.200 #虚拟IP(vip) } } 将下面多余的配置全部删除
- master server Writing Magnet script
Mkdir/opt/shell
vim /opt/shell/magent.sh #!/bin/bash K=`ps -ef | grep keepalived | grep -v grep | wc -l` if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.58.100 -p 12000 -s 192.168.58.135:11211 -b 192.168.58.132:11211 else pkill -9 magent fi -n 51200 #定义用户最大连接数 -l 192.168.58.100 #指定虚拟IP -p 12000 #指定端口号 -s #指定主缓存服务器 -b #指定从缓存服务器 #这个脚本意思就是一旦检测到系统进程中有keepalived进程,就执行magent这条命令,如果没有,则将magent进程杀死。总得来说就是,主服务器上没有keepslived进程,magent就会和从服务器连接。 chmod +x /opt/shell/magent.sh #给脚本添加执行权限
- Writing magent scripts from the server
Mkdir-p/opt/shell
vim /opt/shell/magent.sh#!/bin/bashK=`ip addr | grep 192.168.58.100 | grep -v grep | wc -l`if [ $K -gt 0 ]; thenmagent -u root -n 51200 -l 192.168.58.100 -p 12000 -s 192.168.58.135:11211 -b 192.168.58.132:11211elsepkill -9 magentfi #从服务器的脚本意思就是检测到虚拟ip漂移是否到自己身上,如果VIP进行漂移到从服务器之后,就会执行magent这条命令,目的就是在主服务器宕掉的时候,从服务器能够正常启动。
First use the client to install Telnet, remote connection VIP node, after writing to the user, the master-slave cache server will automatically obtain the download of the user.
Memcached+magent Implementing Master-Slave synchronization +keepalived high-availability clusters