Memcached内存数据库群集配置(理论 实践篇)

时间:2022-06-02 04:19:23

Memcached内存数据库

  • 一套开源的高性能分布式内存对象缓存系统
  • 所有的数据都存储在内存中
  • 支持任意存储类型的数据
  • 提高网址访问的速度

Memcached缓存机制

  • 当程序写入缓存数据请求时,Memcached的API接口将KEY输入路由算法模块路由到集群中的一台服务,之后由API接口与服务器进行通信,完成一次分布式缓存写入
  • Key索引建立在API中,值value数据存在后面的memcached中

Memcached分布式

  • 要依赖于Memcached的客户端来实现
  • 多个Memcached服务器是独立的
  • 分布式数据如何存储是路由算法所决定

Memcached路由算法

求余数hash算法

  • 先用key做hash运算的到一个整数,再去做hash算法,根据余数进行路由。不适合在动态变化的环境中

一致性hash算法

  • 按照hash算法把对应key通过一定hash算法处理后映射形成一个首尾接闭合循环,然后通过使用与对象存储一样的hash算法将机器也映射到环中,顺时针方向计算将所有对象存储到离自己最近的机器中。适合在动态变化中使用

Memcached是什么

  • 在阐述这个问题之前,我们首先要清楚它“不是什么”。很多人把它当作和SharedMemory那种形式的存储载体来使用,虽然memcached使用了同样的“Key=>Value”方式组织数据,但是它和共享内存、APC等本地缓存有非常大的区别。Memcached是分布式的,也就是说它不是本地的。它基于网络连接(当然它也可以使用localhost)方式完成服务,本身它是一个独立于应用的程序或守护进程(Daemon方式)。
  • Memcached使用libevent库实现网络连接服务,理论上可以处理无限多的连接,但是它和Apache不同,它更多的时候是面向稳定的持续连接的,所以它实际的并发能力是有限制的。在保守情况下memcached的最大同时连接数为200,这和Linux线程能力有关系,这个数值是可以调整的。关于libevent可以参考相关文档。 Memcached内存使用方式也和APC不同。APC是基于共享内存和MMAP的,memcachd有自己的内存分配算法和管理方式,它和共享内存没有关系,也没有共享内存的限制,通常情况下,每个memcached进程可以管理2GB的内存空间,如果需要更多的空间,可以增加进程数。

memcached群集搭建实践

实验环境

  • 主服务器IP地址:192.168.144.238
  • 从服务器IP地址:192.168.144.239
  • 客户端IP地址:192.168.144.185
  • 漂移IP地址:192.168.144.188

主服务器安装memcached,libevent事件库,mamgent代理包

[[email protected] ~]# mkdir /abc
[[email protected] ~]# mount.cifs //192.168.100.8/memcached /abc     //挂载
Password for [email protected]//192.168.100.8/memcached:  
[[email protected] ~]# cd /abc/
[[email protected] abc]# ls
LAMP-php5.6                   magent-0.5.tar.gz   memcached-1.5.6.tar.gz
libevent-2.1.8-stable.tar.gz  memcache-2.2.7.tgz
[[email protected] abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt   //解压事件库软件包,memcached依赖于事件库
[[email protected] abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt/     //解压服务端memcached软件包
[[email protected] abc]# yum install gcc gcc-c   make -y     //安装环境包
[[email protected] abc]# mkdir /opt/magent
[[email protected] abc]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
[[email protected] opt]# cd libevent-2.1.8-stable/
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr 
[[email protected] libevent-2.1.8-stable]# make && make install       //编译安装
[[email protected] libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[[email protected] memcached-1.5.6]# ./configure --with-libevent=/usr
make && make install

从服务器安装memcached,libevent事件库

[[email protected] ~]# mkdir /abc
[[email protected] ~]# mount.cifs //192.168.100.8/memcached /abc
Password for [email protected]//192.168.100.8/memcached:  
[[email protected] ~]# cd /abc/
[[email protected] abc]# ls
LAMP-php5.6                   magent-0.5.tar.gz   memcached-1.5.6.tar.gz
libevent-2.1.8-stable.tar.gz  memcache-2.2.7.tgz
[[email protected] abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt //解压事件库软件包
[[email protected] abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt/   /解压服务端memcached软件包
[[email protected] abc]# yum install gcc gcc-c   make -y
[[email protected] opt]# cd libevent-2.1.8-stable/
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr

[[email protected] libevent-2.1.8-stable]# make && make install
[[email protected] libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[[email protected] memcached-1.5.6]# ./configure --with-libevent=/usr
make && make install

主服务器配置magent

[[email protected] memcached-1.5.6]# cd /opt/
[[email protected] opt]# ls
libevent-2.1.8-stable  magent  memcached-1.5.6  rh
[[email protected] opt]# cd magent/
[[email protected] magent]# vim ketama.h
#ifndef SSIZE_MAX 
#define SSIZE_MAX 32767 
[[email protected] magent]# vim Makefile
LIBS = -levent -lm              //指定makefile文件
make                 //编译
[[email protected] magent]# ls
ketama.c  ketama.h  ketama.o  magent  magent.c  magent.o  Makefile
[[email protected] magent]# yum install openssh-clients -y     //安装scp远程同步软件包
[[email protected] magent]# cp magent /usr/bin/      //把magent脚本放到/usr/local中,让系统能识别
[[email protected] magent]# scp magent [email protected]:/usr/bin/   //把mangent文件拷贝到从服务器上

主从都关闭防火墙

[[email protected] bin]# systemctl stop firewalld.service 
[[email protected] bin]# setenforce 0

在主服务器、从服务器同时安装

[[email protected] bin]# yum install keepalived -y

主服务器配置主从同步

[[email protected] magent]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

vrrp_script magent {          //编辑函数脚本
        script "/opt/shell/magent.sh"      //指定脚本位置
        interval 2              //检测脚本时间间隔
}

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        //主服务器id,两台不能一样
}

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
        magent
}       
    virtual_ipaddress {
        192.168.144.188        //客户端访问的漂移地址
    }   
}

从服务器安装openssh客户端

[[email protected] bin]# cd /etc/keepalived/
[[email protected] keepalived]# mv keepalived.conf keepalived.conf.bak   //更改名称
[[email protected] keepalived]# yum install openssh-clients -y

主服务器用scp把keepalived文件传到从服务器

[[email protected] magent]# cd /etc/keepalived/
[[email protected] keepalived]# scp keepalived.conf [email protected]:/etc/keepalived/

从服务器配置主从同步

[[email protected] keepalived]# ls
keepalived.conf  keepalived.conf.bak
[[email protected] keepalived]# vim keepalived.conf
! Configuration File for keepalived

vrrp_script magent {
        script "/opt/shell/magent.sh"
        interval 2
}

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_HB        //routed_id不能相同
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 52        //从服务器虚拟id不能和主服务器一样
    priority 90         //优先级比主服务器低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }   
    track_script {
        magent
}       
    virtual_ipaddress {
        192.168.144.188
    }   
}

主服务器配置magent脚本

[[email protected] keepalived]# mkdir /opt/shell
[[email protected] keepalived]# cd /opt/shell/
[[email protected] shell]# vim magent.sh
#!/bin/bash
k=`ps -ef | grep keepalived | grep -v grep | wc -l`     //检查keepaliveed进程,如果开启
if [ $k -gt 0 ]; then             
        magent -u root -n 51200 -l 192.168.144.188 -p 12000 -s 192.168.144.238:11211 -b
        // -n连接数量 -l指定漂移地址,-p指定端口映射到主从服务器的地址
        192.168.144.239:11211
else
pkill -9 magent
fi
[[email protected] shell]# chmod  x magent.sh 
[[email protected] shell]# systemctl start keepalived.service 
[[email protected] shell]# netstat -ntap | grep 12000
tcp        0      0 192.168.144.188:12000   0.0.0.0:*               LISTEN      124720/magent       

从服务器同样的操作

[[email protected] keepalived]# mkdir /opt/shell
[[email protected] keepalived]# cd /opt/shell/
[[email protected] 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.144.188 -p 12000 -s 192.168.144.238:11211 -b 192.168.144.239:11211
else
pkill -9 magent
fi
[[email protected] shell]# chmod  x magent.sh 
[[email protected] shell]# systemctl start keepalived.service 
[[email protected] shell]# netstat -ntap | grep 12000        //查看magent端口
tcp        0      0 192.168.144.188:12000   0.0.0.0:*               LISTEN      11660/magent 

开启主服务器memcached

[[email protected] shell]# memcached -m 512k -u root -d -l 192.168.144.238 -p 11211    //启动主,-m指定空间大小
[[email protected] shell]# netstat -ntap | grep 11211 
tcp        0      0 192.168.144.238:11211   0.0.0.0:*               LISTEN      44647/memcached

开启从服务器memecached

[[email protected] shell]# memcached -m 512k -u root -d -l 192.168.144.239 -p 11211 #启动从
[[email protected] shell]# netstat -ntap | grep 11211
tcp        0      0 192.168.144.239:11211   0.0.0.0:*               LISTEN      42654/memcached    

客户端中验证用telnet去连接memcached 内存数据库

[[email protected] ~]# telnet 192.168.144.188 12000
Trying 192.168.144.188...
Connected to 192.168.144.188.
Escape character is ‘^]‘.
add username 0 0 7           //编写键值对数据
1234567 
STORED

查看主服务器查看数据有没有

[[email protected] shell]# telnet 192.168.144.238 11211
Trying 192.168.144.238...
Connected to 192.168.144.238.
Escape character is ‘^]‘.
geer^H^H
ERROR
get username
VALUE username 0 7
1234567
END

验证从服务器数据有没有同步

[[email protected] shell]# telnet 192.168.144.239 11211
Trying 192.168.144.239...
Connected to 192.168.144.239.
Escape character is ‘^]‘.
get username
VALUE username 0 7
1234567
END