I heard that Memcache is very arrogant? -------------In-depth analysis of Memcache+keepalive high-availability cluster

One, memcache cluster introduction

The memcached cluster function mainly solves the single point of failure of the server. Without cluster deployment, there will be no replication and
synchronization of data between servers . Once downtime, data will be lost, and it cannot effectively reduce the pressure of concurrent connections for background web servers. Therefore,
we use cluster services to solve this problem, using multiple memcached for cluster deployment, even if one or two servers are down, it will not affect the user's normal experience, and also reduce the pressure of concurrent connections for background services.

1. Keepalived+memcached high availability

The client connects to the VIP address between the two servers. If the back-end server fails, it will automatically switch.

2. Keepalive principle:

1. Fault detection:
The function of Keepalived is to detect whether the status of the memcached server is normal.

2. Master-slave switching:
If Keepalived detects that the memcached service is down or crashes, it can move the VIP from the master server to the slave server

3. How does
Keepalived discover memcached exceptions: 1) Keepalived generates a virtual IP on the main memcached server

2) Keepalived can continuously check whether port 11211 of the memcached master server is working properly. If a memcached Down machine is found, the virtual IP will be moved from the master server to the slave server

3. High-availability architecture application scenarios:

If there are more distributed nodes in memcached, then there is no need to build a high-availability architecture based on replication.

The high-availability architecture based on replication is generally used in memcached single-node storage cache or session.

4. Introduction to magent software

Since there is no communication between the Memcached server and the server, and no data replication is performed, when any server node fails, there will be a single point of failure. If HA needs to be implemented, it needs to be solved in another way.

Through the Magent cache proxy, to prevent single points, the cache proxy can also do backups, connect to the cache proxy server through the client, the cache proxy server connects to the cache connection server, the cache proxy server can connect to multiple Memcached machines, and each Memcached machine can perform data synchronization. If one of the cache servers is down, the system can still continue to work. If one of the Memcached machines is down, the data will not be lost and the integrity of the data can be guaranteed.

memcache key-value storage: This stored procedure requires programmers to implement. The programmer generates the key-value. Generally memcache is used to cache static content. The service of the primary node is stopped, and the program must be automatically adjusted to the backup node. The cache needs to be regenerated after the service of the master node is restored.

Magent is a supplement to memcache. Memcache is divided into active and standby under magent. The master node stores all key-value data scatteredly, and the standby node stores a complete set of all key-value data. magent solves the problem that memcache cannot distribute nodes.

2. The specific construction is as follows

1. Experimental environment

VMware software
A centos7 virtual machine as the memcache master server, IP address: 192.168.110.132
A centos7 virtual machine as a memcache slave server, IP address: 192.168.110.133
A centos7 virtual machine as a memcache client, IP address: 192.168.110.134
Drift IP address: 192.168.110.100, which is the IP address that the client logs in

2. The experimental topology is as follows

Memcache master-slave server
setup The master server needs to be installed: memcached, libevent, keepalived, and the magent
slave server needs to be installed: memcached, libevent, keepalived

Insert picture description here

3. Configure memcache master cache node and slave cache node (the two servers have the same configuration)

1. Install dependent packages:

yum install gcc gcc-c++ make -y

2. Unzip the software package:

tar zxvf memcached-1.5.6.tar.gz -C /opt/

tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/

mkdir /opt/magent
tar zxvf magent-0.5.tar.gz -C /opt/magent/

3. Compile and install:

cd /opt/libevent-2.1.8-stable
./configure --prefix=/usr/
make && make install


cd /opt/memcached-1.5.6
./configure \
--with-libevent=/usr
make && make install

4. Create a soft connection:

ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6

4. Deploy the master server ----- install magent agent (not required from the slave server)

mkdir /opt/magent
tar zxvf magent-0.5.tar.gz -C /opt/magent/
1、cd /opt/magent
vim ketama.h
在开头处增减以下代码:
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif

vim Makefile
//运营库
LIBS = -levent -lm   //加上 -lm

2、修改完成后 make 编译

3、此时,make完成后,就会产生 magent可执行程序

4、将这个 magent 程序复制到path环境变量中

cp magent /usr/bin/

5、可以将安装好的 magent 复制到从服务器,这样从服务器就不需要再配置了

yum install openssh-clients -y   //安装工具包
scp magent root@192.168.110.133:/usr/bin/

5. Install keepalived on both the master and slave servers and deploy

(1) Main server:

yum install keepalived -y    //安装keepalived
先将配置文件备份一份  不然改错了麻烦
cp -p /etc/keepalived/keepalived.conf /opt

Modify the configuration file:

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
vrrp_script magent {
    
    
        script "/opt/shell/magent.sh"
        interval 2
      }
global_defs {
    
    
   notification_email {
    
    
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_MAGENT_HA
}

vrrp_instance VI_1 {
    
    
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
    
    
        auth_type PASS
        auth_pass 1111
    }
  track_script {
    
    
  magent
}

    virtual_ipaddress {
    
    
        192.168.110.100
    }
}


//定义一个函数,建议写在最前面
vrrp_script magent {
    
    
        script "/opt/shell/magent.sh"
        interval 2
      }
      
做如下修改:
router_id MAGENT_HA        //修改id名
interface ens33            //修改网卡信息

virtual_ipaddress {
    
    
        192.168.220.100     //定义好虚拟ip地址
    }   
    
vrrp_instance VI_1 {
    
    
.....
//调用函数.以下三行代码写在vrrp模块内
track_script {
    
    
        magent
      }
.....
}

(2) From the server:

vim /etc/keepalived/keepalived.conf

做如下修改:
router_id MAGENT_HB         //id名和第一台要不一样
state BACKUP               //从服务器
virtual_router_id 52       //id号和第一台不一样
priority 90                 //优先级低与主服务器 

6, the master and slave servers must create magent scripts

[root@master ~]# mkdir /opt/shell
[root@master ~]# cd /opt/shell/
[root@master shell]# vim magent.sh
K=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.110.100 -p 12000 -s 192.168.110.132:11211 -b 192.168.79.133:11211
else
pkill -9 magent
fi
'//如下解释'
-n 51200             '//定义用户最大连接数'
-l 192.168.110.100   '//指定虚拟IP'
-p 12000             '//指定端口号'
-s                   '//指定主缓存服务器
-b                   '//指定从缓存服务器
11211				'//端口号'

7. Start keepalive to verify the master and slave of keepalive

'//主从服务器都要做'
[root@master shell]# chmod +x magent.sh 	'//增加magent脚本执行权限'
[root@master shell]# systemctl start keepalived.service	'//开启keepalived服务'
[root@master shell]# netstat -ntap |grep 12000	'//keepalived启动会有点慢,我们需要稍等一下'
tcp        0      0 192.168.110.100:12000    0.0.0.0:*               LISTEN      67513/magent  
[root@master shell]# vim /var/log/messages 
搜索'//Transition to MASTER STATE',有即成功
[root@master shell]# ip addr	'//查看漂移地址是否绑定成功'
...省略内容
 inet 192.168.110.100/32 scope global ens33	'//绑定成功 '
...省略内容
[root@slave keepalived]# vim /var/log/messages 
搜索'//Entering BACKUP STATE',有即成功
[root@slave keepalived]# ip addr	'//查看漂移地址是否绑定成功'
...省略内容
 inet 192.168.110.100/32 scope global ens33	'//绑定成功 '
...省略内容

8. Test whether you can connect to memcache locally

Three, memcache client test

1. The client logs in to memcache

#######验证主从memcache
主:
ln -s /usr/local/memcached/bin/* /usr/local/bin/
 memcached -m 512k -u root -d -l 192.168.110.132 -p 11211
[root@master ~]# yum install telnet -y	'//安装Telnet远程登陆程序'
[root@master ~]# telnet 192.168.110.132 11211


从:
ln -s /usr/local/memcached/bin/* /usr/local/bin/
//-d守护进程 ;-m缓存大小32M ;-p端口11211
 memcached -m 512k -u root -d -l 192.168.110.133 -p 11211
[root@master ~]# yum install telnet -y	'//安装Telnet远程登陆程序'
[root@master ~]# telnet 192.168.110.133 11211

2. Test master-slave synchronization

The main server creates a data

[root@promote keepalived]# telnet 192.168.110.132 11211
Trying 192.168.110.132...
Connected to 192.168.110.132.
Escape character is '^]'.
get username
VALUE username 0 5
12345
END

View from the server

[root@promote shell]# telnet 192.168.110.133 11211
Trying 192.168.110.133...
Connected to 192.168.110.133.
Escape character is '^]'.
get username
VALUE username 0 5
12345
END

View on the client

[root@promote ~]# telnet 192.168.110.100 12000
Trying 192.168.110.100...
Connected to 192.168.110.100.
Escape character is '^]'.
add username 0 0 5
12345
STORED

3. Test high availability

[root@master ~]# systemctl stop keepalived.service 	'//关闭主服务器keepalived服务'
[root@promote ~]# telnet 192.168.110.100 12000   //客户端依旧可以登录
Trying 192.168.110.100...
Connected to 192.168.110.100.
Escape character is '^]'.
get username
VALUE username 0 5
12345
END

Guess you like

Origin blog.csdn.net/weixin_47219935/article/details/108559995