Memcached主主复制 + Keepalived 高可用架构(内附软件包)

发布时间:2020-08-04 06:03:57 作者:LIUZabc123
来源:网络 阅读:548

Memcached 简介

          Memcached 主主复制是指在任意一台 Memcached 服务器修改数据都会被同步到另外一台,但是Memcached  API 客户端是无法判断连接到那一台 Memcached 服务器的,所以需要设置 VIP (虚拟IP)地址,提供给 Memcached  API 客户端进行连接。可以使用 Keepalived 产生 VIP 地址连接主 Memcached 服务器,并提供高可用架构。

          Memcached 的复制功能支持多个 Memcached 之间进行相互复制(双向复制,主备都是可读可写的),可以解决 Memcached 的容灾问题。

         Keepalive 不断检测 Memcached 主服务器的 11211 端口,如果检测到 Memcached 服务器发生宕机或者死机等情况,就会将 VIP 从主服务器移至从服务器,从而实现 Memcached 的高可用性

Memcached 高可用架构

Memcached主主复制 + Keepalived 高可用架构(内附软件包)


软件包连接:链接:https://pan.baidu.com/s/10yic_9NDmhBbWCVhERlgPw 密码:gf1l


实验环境:两台 Memcached 服务器 和一台客户机,如下所示。



名称
          IP地址操作系统主要软件包

Memached 1(主)

192.168.91.148

VIP :192.168.91.188

Centos 7libevent-2.1.8-stable.tar.gz
memcached-1.5.6.tar.gz
 magent-0.5.tar.gz
Memached 2(从)192.168.91.150
VIP :192.168.91.188
Centos 7libevent-2.1.8-stable.tar.gz
memcached-1.5.6.tar.gz
客户端192.168.91.149Centos 7


搭建 Memcached 主主复制架构


1.首先安装所需的编译环境,关闭防火墙


[root@localhost ~]# systemctl stop firewalld.service 

[root@localhost ~]# setenforce 0

[root@master memcached]# yum install gcc gcc-c++ make –y

2.解压软件包

[root@master memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/               //libevent 事件通知库

[root@master memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/

[root@master memcached]# mkdir /opt/magent                                                      //创建magent 目录
[root@master memcached]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
ketama.c          
magent.c
ketama.h                //magent 代理插件
Makefile

3.先编译安装 Libevent, 然后安装 Memcached

[root@master memcached]# cd /opt/libevent-2.1.8-stable/
[root@master libevent-2.1.8-stable]# ./configure --prefix=/usr

[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 --with-libevent=/usr

[root@master memcached-1.5.6] # make && make install

以上步骤安装,主从 Memcached 基本一致,区别在于从服务器不需要解压安装 Magent 代理。


4.在Memcached 主服务器中修改 magent 代理插件

[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

#ifndef SSIZE_MAX                                         //在文件开头出新增
#define SSIZE_MAX 32767

[root@master magent]# vim Makefile

LIBS = -levent –lm                                        //在第一行末尾加 –lm (不是数字1)

将代理插件修改完后,直接 make ,会看到一个可执行文件 magent

[root@master magent]# make

gcc -Wall -O2 -g  -c -o magent.o magent.c
gcc -Wall -O2 -g  -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm
[root@master magent]# ls
ketama.c  ketama.h  ketama.o  magent magent.c  magent.o  Makefile

5.在 Memcached 主服务器上安装 openssh-clients 服务,可以将主服务器上的 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@192.168.91.150:/usr/bin/                  //从服务器地址及目录
The authenticity of host '192.168.91.150 (192.168.91.150)' can't be established.
ECDSA key fingerprint is SHA256:ABSTPGOHvqKvUsfwD/uf5ESPpd×××RjvucRpzMqcUuzI.
ECDSA key fingerprint is MD5:f5:3a:8c:8b:1e:d5:a3:33:24:32:03:2d:4d:3e:e8:68.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.91.150' (ECDSA) to the list of known hosts.
root@192.168.91.150's password:         //从服务器的登录密码
magent                                                        100%  112KB   4.3MB/s   00:00
   

6.安装配置 Keepalive (主从服务器都要安装)

[root@master magent]# yum install keepalived –y

(1). 配置主 Keepalived

[root@master magent]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_script magent {                                              //添加函数,名称为magent,以magnt 为调用
    script "/opt/shell/magent.sh"                         //脚本位置
    interval 2                                                             //检测脚本的时间间隔2s

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           //路由表示,主从不能一样

vrrp_instance VI_1 {
     state MASTER                    //主服务器状态为 : MASTER
     interface ens33                  //网卡名称 改为 ens33  (centos 7)
     virtual_router_id 51         //虚拟路由ID ,主从不能相同
     priority 100                         //优先级,主的高于从的
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }  
      track_script {
         magent                     //调用函数名称 magent

}  
     virtual_ipaddress {
         192.168.91.188                     //定义 VIP 地址
     } 

}

(2)配置从 Keepalived,在主服务器上把 keepalied 的配置文件导入从服务器

[root@master magent]# cd /etc/keepalived/
[root@master keepalived]# scp keepalived.conf root@192.168.91.150:/etc/keepalived/
root@192.168.91.150's password:
keepalived.conf                                               100%  660     2.0KB/s   00:00

回到从服务器修改 keepalived 的配置文件,主从 Keepalived 配置文件内容差不多,可直接复制进行修改,以下只把不一样的地方整理出来

[root@localhost keepalived]# vim keepalived.conf

  router_id MAGENT_HB                 //路由表示与主不同
}

vrrp_instance VI_1 {
     state BACKUP                                 //状态为 BACKUP
     interface ens33
     virtual_router_id 52                   //虚拟路由ID 与主不同
     priority 90                                     //优先级小于主

7.在主从设置脚本,(在主从Keepalived 中添加的名为 magent 的函数)

[root@master keepalived]# mkdir /opt/shell
[root@master keepalived]# 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 192.168.91.188 -p 12000 -s 192.168.91.148:11211 -b 192.168.91.150:11211
else
pkill -9 magent
fi

其中参数解释如下

-n   51200      // 定义用户最多连接数

-l    192.168.91.188    //指定虚拟IP

-p   12000           //指定端口

-s                        //指定主缓存服务器

-b                       //指定从缓存服务器

8.给执行权限,启动 keepalived 服务

[root@master shell]# chmod +x magent.sh
[root@master shell]# systemctl start keepalived.service                  //启动服务
[root@master shell]# netstat -ntap | grep 12000                               //查看端口,确认 magent  运行
tcp        0      0 192.168.91.188:12000    0.0.0.0:*               LISTEN      47938/magent
       

查看日志,验证主从

vim /var/log/messages    

  Memcached主主复制 + Keepalived 高可用架构(内附软件包)

使用 ip  addr 命令确认漂移地址生效 是否生效

Memcached主主复制 + Keepalived 高可用架构(内附软件包)

从服务器上的脚步也是一样的操作

[root@localhost shell]# netstat -ntap | grep 12000
tcp        0      0 192.168.91.188:12000    0.0.0.0:*               LISTEN      66801/magent
       

Memcached主主复制 + Keepalived 高可用架构(内附软件包)

9.分别在主从服务器上启动 memcached

[root@master shell]# memcached -m 512k -u root -d -l 192.168.91.148 -p 11211
[root@master shell]# netstat -ntap | grep 11211
tcp        0      0 192.168.91.148:11211    0.0.0.0:*               LISTEN      51398/memcached

    

从服务器

[root@localhost shell]# memcached -m 512k -u root -d -l 192.168.91.150 -p 11211
[root@localhost shell]# netstat -ntap | grep 11211
tcp        0      0 192.168.91.150:11211    0.0.0.0:*               LISTEN      54741/memcached
    

10.测试验证

(1)在主服务器在自测连接 Memcached 缓存数据库,需要安装 telnet

[root@master shell]# yum install telnet –y

[root@master shell]# telnet 192.168.91.148 11211        //自测连接
Trying 192.168.91.148...
Connected to 192.168.91.148.
Escape character is '^]'.                                //进入缓存数据库
quit
Connection closed by foreign host.

(2)在从服务器上测试

[root@localhost shell]# telnet 192.168.91.150 11211
Trying 192.168.91.150...
Connected to 192.168.91.150.
Escape character is '^]'.
quit
Connection closed by foreign host.
[root@localhost shell]#

(3)在客户端上安装 telnet,使用虚拟IP 登录,并添加语句,看在主从服务器上能否看到

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install telnet –y

[root@localhost ~]# telnet 192.168.91.188 12000
Trying 192.168.91.188...
Connected to 192.168.91.188.
Escape character is '^]'.
add username 0 0 7                  //添加一条键值数据
1234567
STORED
quit
Connection closed by foreign host.
[root@localhost ~]#

(4)分布登录主从 memcached 查看添加内容

[root@master shell]# telnet 192.168.91.148 11211
Trying 192.168.91.148...
Connected to 192.168.91.148.
Escape character is '^]'.
get username                      //查询键值数据
VALUE username 0 7
1234567
END

[root@localhost shell]# telnet 192.168.91.150 11211
Trying 192.168.91.150...
Connected to 192.168.91.150.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
END

总结:

(1)Memcached 是分布式内存对象缓存系统,因为所有数据都存储在内存中,从而通常用于网站加速。

(2)Memcached 分布式实现不是在服务端实现的而是在客户端实现的。

(3)Memcached 可以通过 Keepalived 实现 Memcached 服务的高可靠性。

推荐阅读:
  1. JS以及CSS对页面的阻塞
  2. 如何远程登陆管理服务器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

服务器 分布式

上一篇:Python操作数据库之 MySQL

下一篇:ASP.NET Web Deploy 发布项目二次编译出错: Publication (web): 未将对象引用设置到对象的实例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》