Linux系统架构----memcache高可用群集部署

前言:

之前我们部署了memcache单节点,本篇博客介绍memcache群集

一、memcached群集分析

1.1 memcached群集实现高可用组件
  • 由于Mamcached服务器与服务器之间没有任何通信,并且不进行任何数据复制备份,所以当任何服务器节点出现故障时,会出现单点故障,如果需要实现高可用,就需要通过其他方式。这里通过Magent缓存代理,防止单点现象,缓存代理也可以做备份功能,通过客户端连接到缓存代理了服务器,缓存代理服务器连接缓存连接复苏期,缓存代理服务器可以连接多台Memcached机器可以将每台Memcached机器进行数据同步,如果其中有一台缓存服务器down了,系统依然可以继续工作,如果其中一台Memcached机器down了,数据不会丢失并且可以保证数据的完整性。

  • magent(memcached代理服务器软件)工作原理是magent可以备份数据,而且magent可以同时连接多个memcached节点,当memcached重启或者宕机恢复后可以从magent指定的memcached的备份节点中恢复丢失的缓存数据

  • keepalived 功能:

    ① 故障检测:

    Keepalived可用检测memcached服务器的状态是否正常

    ② 主从切换

    Keepalived如果检测到memcached服务发生DOWN机或者死机等,能将VIP从主服务器移至从服务器

    ③ keepalived检测过程

    • keepalived在memcached主服务器产生一个虚拟IP
    • keepalived可以通过不断的检测memcached主服务器的11211端口是否正常工作,如果发现memcached Down机,虚拟IP就从主服务器移到从服务器

1.2 高可用架构应用场景

  • 如果memcached分布式节点比较多,那么完全不需要做基于复制的高可用架构。
  • 基于复制的高可用架构一般用在memcached单节点存放缓存或者session。

在这里插入图片描述

二、memcache集群实现原理

2.1 一致性Hash算法

在这里插入图片描述

  • 算法过程

    ① 先构造一个长度为2的32次方的整数环(这个环被称为一致性Hash环),根据节点名称的Hash值(其分布为[0,2^32 -1])将缓存服务器节点放置在这个Hash环上。

    ② 然后根据需要缓存的数据Key值计算得到其Hash值(其分布也为[0,2^32-1])

    ③ 最后在Hash环上顺时针查找到距离这个Key值最近的服务器节点,完成Key到服务器的映射查找

  • 如上图所示

    三个node节点分别位于Hash环上的三个位置,然后Key值根据HashCode,在Hash环上有一个固定位置,位置固定下之后,Key就会顺时针去寻找离他最近的一个Node,把数据存储在这个Node的Memcache服务器中

三、memcache集群部署

3.1 实验拓扑

在这里插入图片描述

3.2 实验环境
  • 本次实验使用三台虚拟机

  • memcached master节点 IP地址:192.168.226.128

    准备的软件包

    • ① magent-0.5.tar.gz
    • ② memcached-1.5.6.tar.gz
    • ③ libevent-2.1.8-stable.tar.gz
  • memcached slave节点 IP地址:192.168.226.132

    准备的软件包

    • ① memcached-1.5.6.tar.gz
    • ② libevent-2.1.8-stable.tar.gz
  • 客户端 client IP地址:192.168.226.133

3.3 配置memcached master节点
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# su
   
#安装环境包及keepalived(高可用软件)
[root@master ~]# yum install gcc gcc-c++ make keepalived -y

#关闭防火墙、增强性安全功能
[root@master ~]# systemctl stop firewalld
[root@master ~]# setenforce 0

#解压三个软件包
[root@master ~]# mount.cifs //192.168.226.1/LAMP-C7 /mnt
Password for root@//192.168.226.1/LAMP-C7:  
[root@master ~]# cd /mnt
[root@master mnt]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[root@master mnt]# tar xzvf libevent-2.1.8-stable.tar.gz -C /opt
[root@master mnt]# mkdir /opt/magent
[root@master mnt]# tar xzvf magent-0.5.tar.gz -C /opt/magent/
ketama.c
magent.c
ketama.h
Makefile
  • 手工编译安装事件通知库
[root@master mnt]# cd /opt
[root@master opt]# cd 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
  • 手工编译安装memcached服务端
[root@master libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/

#指定存放位置
[root@master memcached-1.5.6]# ./configure --with-libevent=/usr/local
[root@master memcached-1.5.6]# make && make install
  • 修改magent插件
[root@master memcached-1.5.6]# cd /opt/magent/
[root@master magent]# vim ketama.h 
#修改前两行
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
------->wq
  • 指定Makefile文件
[root@master magent]# vim Makefile 
#在首行尾部添加-lm
LIBS = -levent -lm
------>wq

#直接make编译,编译完成后会生成一个可执行文件
[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
#生成了一个magent执行文件

#把magent复制到/usr/bin目录下
[root@master magent]# cp magent /usr/bin

#在把脚本使用SCP 推送到从节点
[root@master magent]# scp magent [email protected]:/usr/bin
The authenticity of host '192.168.226.132 (192.168.226.132)' can't be established.
ECDSA key fingerprint is SHA256:ZDueYh/RhhG5JxHA1Qj8kwGooD3NotleXDWOUAdAPl4.
ECDSA key fingerprint is MD5:54:54:c7:88:b6:65:73:73:0f:c1:b5:22:00:95:d4:03.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.226.132' (ECDSA) to the list of known hosts.
[email protected]'s password: 
magent                                         100%  112KB  11.5MB/s   00:00    
[root@master magent]# 
  • 修改keepalived配置文件

#31行以下我们这里暂时不需要,可以直接1000dd删除

! Configuration File for keepalived

vrrp_script magent{
        script "/opt/shell/magent.sh"	#脚本位置
        interval 2						#间隔时间为2秒
}
#以上为添加内容,定义一个函数,函数名为magent
global_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 MAGENT_HA
}

vrrp_instance VI_1 {					#实例模块
    state MASTER
    interface ens33						#网卡名称为ens33
    virtual_router_id 51
    priority 100 
    advert_int 1
    authentication {					#验证模块,直接使用默认的即可
        auth_type PASS
        auth_pass 1111
    }   
#此处添加调用模块magent
    track_script {
        magent							#调用的函数名
    }   
    virtual_ipaddress {					#定义漂移地址
        192.168.226.100
    }   
}
#配置文件加载过程,先越过我们添加的函数加载global模块,然后
  • 该配置文件的加载过程为
    • ① 先加载global_defs模块(越过我们添加的函数)
    • ② 加载实例模块vrrp_instance VI_1
    • ③ 加载到我们添加的调用模块后,会通过magent名称,调用我们设置的脚本
    • ④ 最后加载漂移地址模块virtual_ipaddress
  • 创建keepalived定义的脚本
[root@master magent]# mkdir /opt/shell
[root@master magent]# cd /opt/shell/
[root@master shell]# vim magent.sh
#!/bin/bash
#定义变量名K:检查keepalived是否开启
K=`ps -ef | grep keepalived | grep -v grep | wc -l`
#如果keepalived进程为开启状态
if [ $K -gt 0 ]; then
#加载magent插件
#-n(指定连接数量) 
#-p(映射的keepalived端口12000)
#-s (主服务器:mamcache端口号)
#-b(主服务器:memcache端口号)
magent -u root -n 51200 -l 192.168.226.100 -p 12000 -s 192.168.226.128:11211 -b 192.168.226.132:11211
else
#否则就关闭magent插件所有进程
pkill -9 magent
fi
----->wq
[root@master shell]# chmoe +x magent.sh

#建立libevent软链接,否则可能会出错
[root@master shell]# ln -s /lib/libevent-2.1.so.6* /usr/lib64/
3.4 配置memcache slave节点
[root@localhost ~]# hostnamectl set-hostname slave
[root@localhost ~]# su

#安装依赖环境和keepalived
[root@slave ~]# yum install gcc gcc-c++ make keepalived -y

#关闭防火墙、增强性安全功能
[root@slave ~]# systemctl stop firewalld.service 
[root@slave ~]# setenforce 0

[root@slave ~]# mount.cifs //192.168.226.1/LAMP-C7 /mnt
Password for root@//192.168.226.1/LAMP-C7:  
[root@slave ~]# cd /mnt
[root@slave mnt]# tar xzvf libevent-2.1.8-stable.tar.gz -C /opt
[root@slave mnt]# tar xzvf memcached-1.5.6.tar.gz -C /opt

#手工编译libevent
[root@slave mnt]# cd /opt/libevent-2.1.8-stable/
[root@slave libevent-2.1.8-stable]# ./configure --prefix=/usr
[root@slave libevent-2.1.8-stable]# make && make install
......省略部分内容

#手工编译memcached
[root@slave libevent-2.1.8-stable]# cd /opt/memcached-1.5.6
[root@slave libevent-2.1.8-stable]# ./configure --with-libevent=/usr/local
[root@slave libevent-2.1.8-stable]# make && make install
  • 备份keepalived配置文件,从master节点上覆制keepalived文件
[root@slave memcached-1.5.6]# cd /etc/keepalived/
[root@slave keepalived]# ls
keepalived.conf
[root@slave keepalived]# mv keepalived.conf keepalived.conf.bak
[root@slave keepalived]# scp [email protected]:/etc/keepalived/keepalived.conf ./
[root@slave keepalived]# ls
keepalived.conf  keepalived.conf.bak
[root@slave keepalived]# vim keepalived.conf
#稍微做下修改即可
#17行 router_id 修改
router_id MAGENT_HB

#21行 MASTER修改为BACKUP
state BACKUP

#23行 修改router_id
virtual_router_id 52

#24行 修改优先级,低于master系欸按
priority 95
----》wq

#创建软链接
[root@slave keepalived]# ln -s /lib/libevent-2.1.so.6* /usr/lib64/
  • 创建magent脚本文件目录,从master节点下载magent脚本
[root@slave keepalived]# mkdir /opt/shell
[root@slave keepalived]# cd /opt/shell
[root@slave shell]# scp [email protected]:/opt/shell/magent.sh ./
[root@slave shell]# chmod +x magent.sh 
[root@slave shell]# ls
magent.sh
  • 两台memcahe服务器均开启服务
[root@slave shell]# systemctl start keepalived.service 
[root@slave shell]# memcached -m 512k -u root -d -l 192.168.226.132 -p 11211

[root@slave shell]# netstat -natp | grep 12000
tcp        0      0 192.168.226.100:12000     0.0.0.0:*               LISTEN      14737/magent
[root@slave shell]# netstat -natp | grep 11211
tcp        0      0 192.168.226.132:11211   0.0.0.0:*               LISTEN      130493/memcached  
  • ip addr 查看虚拟IP
[root@slave shell]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fe:08:5b brd ff:ff:ff:ff:ff:ff
    inet 192.168.226.132/24 brd 192.168.226.255 scope global dynamic ens33
       valid_lft 1775sec preferred_lft 1775sec
    inet 192.168.226.100/32 scope global ens33
#inet 192.168.226.100 地址已经生成
3.5 验证
  • 所有服务器安装telnet软件
yum install telnet -y
  • 客户端以虚拟IP和magent代理端口连接
[root@client ~]# telnet 192.168.226.100 12000
Trying 192.168.226.100...
Connected to 192.168.226.100.
Escape character is '^]'.
#创建数据
add work 0 0 7
1234567
stored
get work
VALUE work 0 7
123456
  • 在主节点和从节点查看

    主从节点均可以查看到数据

[root@slave ~]# telnet 192.168.226.132 11211
Trying 192.168.226.132...
Connected to 192.168.226.132.
Escape character is '^]'.
get work
VALUE work 0 7
1234567
END
  • 如果在slave节点更新数据,master服务器是不会进行同步的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章