CentOS 7.6-build memcache cluster to achieve high availability

One: Overview of Memcache

  • Memcache is a high-performance distributed memory object caching system. By maintaining a unified huge hash table in memory, it can be used to store data in various formats, including images, videos, files, and database retrieval results. . Simply put, the data is called into the memory and then read from the memory, thereby greatly improving the reading speed.
  • Memcache is a project of danga. It was originally a LiveJournal service. It was originally developed to accelerate the access speed of LiveJournal and was later adopted by many large websites.
  • Memcached runs in one or more servers as a daemon, and will receive client connections and operations at any time.

1.1: The difference between memcache and memcached

  • Memcache is the name of this project, and memcached is the name of the main program file on the server side
  • Memcached is a high-performance, distributed memory object caching system used to reduce database load and increase access speed in dynamic applications. Memcached is developed by Danga Interactive to improve the access speed of LiveJournal.com. LJ has thousands of dynamic page views per second and 7 million users. Memcached greatly reduces the database load, better allocates resources, and faster access.

2: Introduction to the practical environment

2.1: Environment

  • VMware software
  • A centos7 virtual machine as the main memcache server, IP address: 20.0.0.51
  • A centos7 virtual machine acts as a memcache slave server, IP address: 20.0.0.52
  • A centos7 virtual machine acts as a memcache client, IP address: 20.0.0.20
  • Drift IP address: 20.0.0.100, which is the IP address that the client logs in

2.2: Experimental purpose

  • By building a memcache high-availability cluster, master-slave synchronization and cluster high-availability functions are realized

2.3: Overview of magent

  • Magent is an open source Memcached proxy server software, which uses the principle of Consistent Hashing, which can be used to synchronize cached data

Three: Experimental configuration

3.1: Memcache master-slave server construction

  • The main server needs to be installed: memcached, libevent, keepalived, magent
  • Need to install from the server: memcached, libevent, keepalived

3.2: Build memcached with master and slave servers

  • Note that: the master and slave servers are configured, only the operation of the master server is demonstrated
'解压源码包'
[root@master memcache]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
[root@master memcache]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[root@master memcache]# mkdir /opt/magent	'此步骤从服务器不需要操作'
[root@master memcache]# tar zxvf magent-0.5.tar.gz -C /opt/magent/	'此步骤从服务器不需要操作'
'编译安装libevent和memcached'
[root@master memcache]# yum install gcc gcc-c++ make -y
[root@master memcache]# cd /opt/libevent-2.1.8-stable/
[root@master libevent-2.1.8-stable]# ./configure --prefix=/usr	'路径不在/usr的话,后面magent在make的时候会报错:magent.c:64:19: 致命错误:event.h:没有那个文件或目录'
[root@master libevent-2.1.8-stable]# make && make install
[root@master libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[root@master memcached-1.5.6]# ./configure \
> --prefix=/usr/local/memcached \
> --with-libevent=/usr/local/libevent
[root@master memcached-1.5.6]# make && make install
[root@master memcached-1.5.6]# systemctl stop firewalld.service '关闭防火墙'
[root@master memcached-1.5.6]# setenforce 0

3.3: Install magent agent on the main server

  • Install magent on the master server, no need to install the slave server
[root@master memcached-1.5.6]# cd /opt/magent/
[root@master magent]# ls
ketama.c  ketama.h  magent.c  Makefile
[root@master magent]# vim ketama.h	'修改magent配置文件'
    '前两行修改如下'
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
...省略内容
[root@master magent]# vim Makefile 	'修改Makefile配置文件'
 LIBS = -levent -lm	  '首行后面添加-lm'
...省略内容
[root@master magent]# make	'编译后会产生一个magent可执行程序'
[root@master magent]# yum install openssh-clients -y
[root@master magent]# cp magent /usr/bin/	'将magent可执行程序复制到/usr/bin中'
[root@master magent]# scp magent root@20.0.0.52:/usr/bin	'将magent可执行程序复制到从服务器的/usr/bin中'
'此命令需要输入yes,输入从服务器的密码才能将文件拷贝过去,根据提示操作即可'

3.4: Master-slave server build keepalived

[root@master ~]# yum install keepalived -y
'主从服务器都要安装keepalived'
  • Modify the keepalived.conf configuration file in the main server
[root@master ~]# 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 MAGENT_HA	'router_id自定义,不可与从服务器相同'
}
'实例区域'
vrrp_instance VI_1 {
    
    	
    state MASTER	'主服务器为MASTER,从服务器要改为BACKUP'
    interface ens33	'网卡改为本机网卡ens33'
    virtual_router_id 51	'主不可相同,从服务器需要修改'
    priority 100	'优先级要高于从服务器'
    advert_int 1
    track_script {
    
    	'此三行为调用函数的段落,配置文件开头已经添加了函数,此处需要调用'
                magent
    }
    authentication {
    
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
    
        20.0.0.100	'定义漂移地址'
    }
}
  • Modify the keepalived.conf configuration file from the server
[root@slave ~]# cd /etc/keepalived/
[root@slave keepalived]# mv keepalived.conf keepalived.conf.bak	'备份配置文件'
'回到主服务器,将配置文件复制到从服务器'
[root@master ~]# scp /etc/keepalived/keepalived.conf root@20.0.0.52:/etc/keepalived
[root@slave keepalived]# vim keepalived.conf
'其他配置都相同,只需要修改如下几个配置'
router_id MAGENT_HB         'id名和第一台要不一样,从服务器改为MAGENT_HB'
state BACKUP               '从服务器为BACKUP'
virtual_router_id 52       'id号和第一台不一样即可'
priority 90                 '优先级低与主服务器 '
  • Create magent script on master and slave server
'主从服务器都要创建magent脚本'
[root@master ~]# mkdir /opt/shell
[root@master ~]# cd /opt/shell/
[root@master shell]# vim 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 20.0.0.100 -p 12000 -s 20.0.0.51:11211 -b 20.0.0.52:11211
else
pkill -9 magent
fi
'如下解释'
-n 51200             '定义用户最大连接数'
-l 20.0.0.100   '指定虚拟IP'
-p 12000             '指定端口号'
-s                   '指定主缓存服务器20.0.0.51:11211'
-b                   '指定从缓存服务器20.0.0.52.134:11211'
11211				'端口号'
  • Master-slave server start service
'主从服务器都要做'
[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 20.0.0.100:12000    0.0.0.0:*               LISTEN      67513/magent 
  • Verify master and slave
[root@master shell]# vim /var/log/messages 
搜索'Transition to MASTER STATE',有即成功
[root@master shell]# ip addr	'查看漂移地址是否绑定成功'
...省略内容
 inet 20.0.0.100/32 scope global ens33	'绑定成功 '
...省略内容
[root@slave keepalived]# vim /var/log/messages 
搜索'//Entering BACKUP STATE',有即成功
[root@slave keepalived]# ip addr	'查看漂移地址是否绑定成功'
...省略内容
 inet 20.0.0.100/32 scope global ens33	'绑定成功 '
...省略内容

3.5: The master-slave server opens memcache and tests the local connection

  • Master and slave servers enable memcache
'主服务器开启'
[root@master ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
[root@master ~]# memcached -m 512k -u root -d -l 20.0.0.51 -p 11211
[root@master ~]# netstat -ntap |grep 11211
tcp        0      0 20.0.0.51:11211    0.0.0.0:*               LISTEN      88178/memcached     
[root@master ~]# 
'从服务器开启'
[root@slave keepalived]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
[root@slave keepalived]# memcached -m 512k -u root -d -l 20.0.0.52 -p 11211
[root@slave keepalived]# netstat -ntap |grep 11211
tcp        0      0 20.0.0.52:11211    0.0.0.0:*               LISTEN      124927/memcached  
  • Test whether you can connect to memcache locally
'主服务器测试'
[root@master ~]# yum install telnet -y	'安装Telnet远程登陆程序'
[root@master ~]# telnet 20.0.0.51 11211
Trying 20.0.0.51...	'连接成功'
Connected to 20.0.0.51.
Escape character is '^]'.
quit	'退出'
Connection closed by foreign host.
'从服务器测试'
[root@slave keepalived]# yum install telnet -y
[root@slave keepalived]# telnet 20.0.0.52 11211
Trying 20.0.0.52...	'连接成功'
Connected to 20.0.0.52.
Escape character is '^]'.
quit	'退出'
Connection closed by foreign host.

Four: memcache client test

  • Client login to memcache
[root@client ~]# yum install telnet -y	
[root@client ~]# telnet 20.0.0.100 12000	'使用漂移地址登陆'
Trying 20.0.0.100...
Connected to 20.0.0.100.
Escape character is '^]'.
  • Test master-slave synchronization
[root@client ~]# telnet 20.0.0.100 12000	
Trying 20.0.0.100...
Connected to 20.0.0.100.
Escape character is '^]'.
add username 0 0 5	'客户端连接并新建一个数据'
12345
STORED
'返回主服务器查看是否同步生成数据'
[root@master ~]# telnet 20.0.0.51 11211
Trying 20.0.0.51...
Connected to 20.0.0.51.
Escape character is '^]'.
get username
VALUE username 0 5	
12345	'主服务器成功生成'
END
'返回从服务器查看是否同步生成数据'
[root@slave keepalived]# telnet 20.0.0.52 11211
Trying 20.0.0.52...
Connected to 20.0.0.52.
Escape character is '^]'.
get username
VALUE username 0 5	
12345	'从服务器成功生成'
END
'测试成功,主从都成功生成数据'
  • Test a highly available cluster
'宕机主服务器,看从服务器是否正常使用'
[root@master ~]# systemctl stop keepalived.service 	'关闭主服务器keepalived服务'
[root@client ~]# telnet 20.0.0.100 12000	'客户端登陆成功'
Trying 20.0.0.100...
Connected to 20.0.0.100.
Escape character is '^]'.
set username 
UNSUPPORTED COMMAND
set username 0 0 4	'修改一下数据'
1234
STORED
'从服务器查看数据是否同步'
get username
VALUE username 0 4
1234	'成功同步'
END

Then the high-availability cluster is successfully built

Guess you like

Origin blog.csdn.net/m0_47219942/article/details/108538145