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.
-
Configure memcached Primary cache node Server
Tar XF libevent-2.1.8-stable.tar.gz-c/opt
Tar xf memcached-1.5.6.tar.gz-c/opt
Tar XF magent-0.5.tar.gz-c/opt
cd/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 #修改
#endif
Vim Makefile
LIBS =-levent-lm #修改, add-lm
Make
cp mgent/usr/bin #把生成的mgent程序让系统识别
Configuring memcached from a cache node server
Tar xzvf memcached-1.5.6.tar.gz-c/opt
Tar xzvf libevent-2.1.8-stable.tar.gz-c/opt
cd/opt/libevent-2.1.8
./configure--PREFIX=/USR
Make && make install
cd/opt/memcached-1.5.6
./configure--WITH-LIBEVENT=/USR
Make && make I nstall
Ln-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.
-
Primary Server Install keepalived service, modify configuration file
Yum-y install keepalived
vim/etc/keepalived/keepalived.conf
router_id Magent_ha #router_id主从不能相同, to modify the
Vrrp_script magent {
Script "/opt/shell/magent.sh"
Interval 2
} #定义一个新函数, automatically execute this script every 2s
vrrp_ Instance vi_1 {
State MASTER #这边是主服务器, from server is Backup
interface ens33 #网卡接口是ens33
virtual_router_id *
Priority #优先级, from server smaller than primary server
Advert_int 1
Authentication {
Auth_type PASS
auth_pass 1111
}
Track_script {
magent #出发执行magent脚本
}
Virtual_ipaddress {
172.16.10.200 #虚拟IP (VIP)
}
}
Remove all of the following additional configuration
-
Primary 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
} #定义一个新函数, automatically execute this script every 2s
vrrp_ Instance vi_1 {
State MASTER #这边是从服务器, Backup
interface ens33 #网卡接口是ens33
virtual_router_id *
Priority 90 #优先级, the server is smaller than the primary server
Advert_int 1
Authentication {
Auth_type PASS
auth_pass 1111
}
Track_script {
magent #出发执行magent脚本
}
Virtual_ipaddress {
172.16.10.200 #虚拟IP (VIP)
}
}
Remove all of the following redundant configuration
-
Home 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 5120 0-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 51 #定义用户最大连接数
-L 192.168.58.100 #指定虚拟IP
-P 12000 #指定端口号
-S #指定主缓存服务器
-B #指定从缓存服务器
# This script means that once the keepalived process is detected in the system process, the magent command is executed, and if not, the magent process is killed. In a way, there is no keepslived process on the primary server, and Magent is connected to the server.
chmod +x/opt/shell/magent.sh
#给脚本添加执行权限
Writing magent scripts from the server
Mkdir-p/opt/shell
vim/opt/shell/magent.sh
#!/bin/bash
k=ip addr | grep 192.168.58.100 | 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
#从服务器的脚本意思就是检测到虚拟ip漂移是否到自己身上, if the VIP is drifting to the slave server, the magent command will be executed to start the server normally when the primary server is down.
- Experimental validation
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