Experimental topology
Experimental environment
Server |
IP Address |
Operating System |
main software |
Memcached1 |
192.168.144.122 |
centos7.3 |
libevent2.1.8, memcached1.5.6, KeepAlive, Telnet, magent0.5 |
Memcached2 |
192.168.144.129 |
centos7.3 |
Libevent, memcached, KeepAlive, Telnet |
Client |
192.168.144.141 |
centos7.3 |
Lamp Architecture or Telnet |
Configuration of the Memcached1
Tar zvxf memcached-1.5.6.tar.gz-c/opt/
Tar zvxf libevent-2.1.8-stable.tar.gz-c/opt/
- 1, first install libevent, specify the installation path, MEMCACHED2 on the same operation
Cd/opt/libevent-2.1.8-stable
./configure--prefix=/usr/local/libevent
Make && make install
- 2, install the same operation on Memcached,memcached2
cd/opt/memcached-1.5.6
./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/#需要跟上事件通知库模块
Make && make install
- 3, let memcached command can let the system identification, MEMCACHED2 on the same operation
Ln-s/usr/local/memcached/bin/*/usr/local/bin/
- 4. Copy the compiled and installed libevent-2.1.so.6 module to the/usr/lib64, or you will get an error when starting the memcached service with replication function.
- Same operation on Memcached2.
Ln-s/usr/local/libevent/lib/libevent-2.1.so.6/usr/lib64/libevent-2.1.so.6
- 5, Memcached1 installs the magent, and sends the generated command to Memcached2, and on each server the command can be recognized by the system.
Tar magent-0.5.tar.gz-c/opt/
Cd/opt
VI ketama.h
#ifndef SSIZE_MAX#define SSIZE_MAX 32767 //前两行带#的改成这样#endif //位置上调
Systemctl Stop Firewalld.service
Setenforce 0
VI Makefile
LIBS = -levent -lm -L/usr/local/libevent/lib //注意libevent的安装路径,否则make会报错INCLUDE = -I/usr/local/libevent/include
- 6. Generate Magent Program
- It is important to note that you need to shut down the firewall before magent generates the executable program, or you cannot make it.
Make
- 7, let Magent program for system identification
CP magent/usr/local/bin/
- 8, the Magent program sent to Memcached2, so that its system identification
SCP magent [Email protected]:/usr/bin/
Two servers installed keepalived
- M1 and M2 Two server keepalive configuration files have been noted in different places.
Yum Install Keepalved-y
Vim/etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id 001 //两台服务器不同}vrrp_script magent { //添加主主数据复制实例,函数名为magent,只要keepalive启动就可启动复制 script "/opt/shell/magent.sh" //设置magent启动脚本,注意后面脚本位置要与之相同 interval 2}vrrp_instance VI_1 { state MASTER //从为BACKUP interface ens33 //网卡为ens33 virtual_router_id 51 //与从服务器需要区别开 priority 100 //从服务器优先级应当比主的低 advert_int 1 authentication { auth_type PASS auth_pass 1111 }track_script { //调用函数执行 magent} virtual_ipaddress { 192.168.144.188 //设置虚拟IP }}
Memcached1 Creating a primary master Replication monitoring script
Cd/opt/shell #没有该目录, you need to create
Vim 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.144.188 -p 12000 -s 192.168.144.122:11211 -b 192.168.144.129:11211elsepkill -9 magentfi
-N 51200//define user maximum number of connections
-L 192.168.175.188//Specify virtual IP
-P 12000//Specify port number
-S//Specify primary cache server
-b//specify from cache server
- Increase execution permissions.
chmod +x/opt/shell/magent.sh
- Memcached1 on KeepAlive Services, and Memcache services
Memcached-d-M 32m-p 11211-u Root
Systemctl Start Keepalived.service
Memcached2 Creating a primary master Replication monitoring script
VI magent.sh #注意脚本位置要跟配置文件中对应, otherwise call execution
#!/bin/bashK=`ip addr | grep 192.168.144.188 | grep -v grep | wc -l`if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.144.188 -p 12000 -s 192.168.144.122:11211 -b 192.168.144.129:11211elsepkill -9 magentfi
- Increase execution permissions.
chmod +x/opt/shell/magent.sh
- Memcached2 on KeepAlive Services, and Memcache services
Memcached-d-M 32m-p 11211-u Root
Systemctl Start Keepalived.service
In the client test
- Accessing virtual IP and virtual ports on clients
Telnet 192.168.144.188 12000
- Writing data on the client
- Access the database on both servers, and the data is automatically replicated synchronously
Add Username 0 0 7
1234567
- Analog fault, shutdown M1 keepalive, client no-sense, virtual IP can still log on, and read and write data
Systemctl Stop Keepalived.service
memcached Implementing primary master replication and keepalive high-availability methods