Memcached概述
-
一套开源的高性能分布式内存对象缓存系统
-
所有数据都存在内存中
-
支持任意存储类型的数据
-
提高网站的访问速度
-
数据存储方式与数据过期方式
- 数据存储方式:Slab Allocation
- 按组分配内存,每次先分配一个Slab,相当于一个大小为1M的页然后再1M的空间里根据数据划分大小相同的Chunk
- 数据存储方式:Slab Allocation
-
数据过期方式
- LRU:数据空间不足时,会根据LRU的情况淘汰最近最少使用的记录
- Lazy Expiration:惰性过期,是指使用get时查看记录时间,从而检查记录是否已经过期
-
Memcached缓存机制
-
当程序写入缓存数据请求时,Memcached的API接口将Key输入路由算法模块路由到集群中一台服务,之后由API接口与服务器进行通信,完成一次分布式缓存写入
-
-
Memcached路由算法
- 求余hash算法
- 先用key做hash运算得到一个整数,再做hash算法,根据玉树进行路由
- 适合大多数据需求,但是不适合用在动态变化的环境中
- 一致性hash算法
- 按照hash算法把对应的key通过一定hash算法处理后映射形成一个首位相接闭合循环,然后通过使用与对象存储一样的hash算法将机器也映射到环中,顺时针方向计算将所有对象存储到离自己最近的机器中
- 适合在动态辩护的环境中使用
- 求余hash算法
-
Memcached分布式
- 要依赖于Memcached的客户端来实现
- 多个Memcached服务器是独立的
- 分布式数据如何存储是由路由算法所决定的
单memcached节点缓存系统部署
shell
#安装 Libevent
[root@memcache1 ~]# systemctl stop firewalld
[root@memcache1 ~]# yum -y install gcc*
[root@memcache1 ~]# tar zxvf libevent-2.1.8-stable.tar.gz
[root@memcache1 ~]# cd libevent-2.1.8-stable
[root@memcache1 libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@memcache1 libevent-2.1.8-stable]# make && make install
#安装 Memcached
[root@memcache1 ~]# tar zxvf memcached-1.5.1.tar.gz
[root@memcache1 ~]# cd memcached-1.5.1
[root@memcache1 memcached-1.5.1]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@memcache1 memcached-1.5.1]# make && make install
#设置 Memcached 服务脚本
[root@memcache1 ~]# vim /usr/local/memcached/memcached_service.sh
#!/bin/bash
CMD="/usr/local/memcached/bin/memcached"
start(){
$CMD -d -m 128 -u root
}
stop(){
killall memcached;
}
ACTION=$1
case $ACTION in
'start')
start;;
'stop')
stop;;
'restart')
stop
sleep 2
start;;
*)
echo 'Usage:{start|stop|restart}'
esac
#启动memcached
[root@memcache1 ~]# chmod 755 /usr/local/memcached/memcached_service.sh
[root@memcache1 ~]# /usr/local/memcached/memcached_service.sh start
[root@memcache1 ~]# netstat -anpt | grep memcached
#安装MemcachedAPI
[rootmMemcached-api ~]# yum -y install httpd mariadb mariadb-server php php-devel php-mysql
[rootmMemcached-api ~]# systemctl start mariadb
[rootmMemcached-api ~]# mysqladmin -u root password 'pwd123'
[rootmMemcached-api ~]# systemctl start httpd
#创建php测试页面
[rootmMemcached-api ~]# vim /var/www/html/test1.php
<?php
phpinfo();
?>
#编译安装 Libmemcached
[root@memcached-api ~]# systemctl stop firewalld
[root@memcached-api ~]# setenforce 0
[root@memcached-api ~]# yum -y install gcc*
[root@memcached-api ~]# tar zxvf libmemcached-1.0.18.tar.gz
[root@memcached-api ~]# cd libmemcached-1.0.18
[root@memcached-api libmemcached-1.0.18]# ./configure --prefix=/usr/local/libmemcached --with-memcached=/usr/local/memcached
[root@memcached-api libmemcached-1.0.18]# make && make install
#编译安装 Memcached 扩展
[root@memcached-api ~]# yum -y install zlib-devel
[root@memcached-api ~]# tar zxvf memcached-2.2.0.tgz
[root@memcached-api ~]# cd memcached-2.2.0/
[root@memcached-api memcached-2.2.0]#phpize
[root@memcached-api memcached-2.2.0]# ./configure --with-php-config=/usr/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl --with-zlib-dir
[root@memcached-api memcached-2.2.0]# make && make install
#配置 PHP 添加 Memcached 组件
[root@memcached-api memcached-2.2.0]# vim /etc/php.ini
extension=memcached.so
[root@memcached-api memcached-2.2.0]# systemctl restart httpd
-
通过 PHPinfo 查看是否已经添加 Memcached 扩展模块
shell
#测试 Memcached-API 功能
[root@memcached-api ~]# vim /var/www/html/test.php
<?php
$memcache = new Memcached();
$memcache->addServer('192.168.10.101', 11211);
$memcache->set('key', 'Memcache test successful!', 0, 60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
-
访问测试
Memcached主主复制架构部署
shell
#设置主机名
主机:192.168.10.101
[root@localhost ~]# hostnamectl set-hostname memcache1
[root@localhost ~]# bash
主机:192.168.10.102
[root@localhost ~]# hostnamectl set-hostname memcache2
[root@localhost ~]# bash
#安装带有复制功能的 Memcached
#安装 Libevent
[root@memcache1 ~]# systemctl stop firewalld
[root@memcache1 ~]# setenforce 0
[root@memcache1 ~]# yum -y install gcc* psmisc
[root@memcache1 ~]# tar zxvf libevent-2.1.8-stable.tar.gz
[root@memcache1 ~]# cd libevent-2.1.8-stable
[root@memcache1 libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@memcache1 libevent-2.1.8-stable]# make && make install
#安装memcached
[root@memcache1 ~]# tar zxvf memcached-1.2.8-repcached-2.2.tar.gz
[root@memcache1 ~]# cd memcached-1.2.8-repcached-2.2
[root@Memcached1 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/memcached_replication --enable-replication --with-libevent=/usr/local/libevent
[root@memcache1 memcached-1.2.8-repcached-2.2]# vim memcached.c
#找到并删除以下内容
#if defined(__FreeBSD__) || defined(__APPLE__)
#endif
[root@memcache1 memcached-1.2.8-repcached-2.2]# make && make install
#启动memcached1服务
[root@memcache1 ~]# ln -s /usr/local/libevent/lib/libevent-2.1.so.6 /usr/lib64/
[root@Memcached1 ~]# /usr/local/memcached_replication/bin/memcached -d -u root -m 128 -x 192.168.10.102
#memcached2启动
[root@memcache1 ~]# ln -s /usr/local/libevent/lib/libevent-2.1.so.6 /usr/lib64/
[root@Memcached1 ~]# /usr/local/memcached_replication/bin/memcached -d -u root -m 128 -x 192.168.10.101
#使用 telnet 进行简单验证复制功能
[root@memcache1 ~]# yum -y install telnet
[root@memcache1 ~]# telnet 192.168.10.101 11211
set username 0 0 8 #自定义信息+永不过期+8个字节
12345678
STORED
get username
VALUE username 0 8
12345678
END
quit
#在 memcached2 上进行查看刚刚插入的键值
[root@memcache2 ~]# telnet 192.168.10.102 11211
get username
VALUE username 0 8
12345678
END
Memcached 主主复制+Keepalived高可用架构
shell
[root@memcache1 ~]# yum -y install keepalived
#配置主 keepalived
[root@memcache1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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_01
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_down { #定义要执行的脚本
script "/etc/keepalived/memcached.sh" #脚本路径和名称
interval 1 #间隔1秒执行一次
}
vrrp_instance VI_1 {
state BACKUP #master角色不能设置nopreempt,所以此处要设置为BACKUP
interface ens33
virtual_router_id 51
priority 100 #从主机的优先级要小于此数字
advert_int 1
nopreempt #添加,此语句关闭了抢占功能,从主机不需要
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100
}
track_script { #调用前面的脚本
check_down
}
}
#配置从keepalived
[root@memcache2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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_02
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_down { #定义要执行的脚本
script "/etc/keepalived/memcached.sh" #脚本路径和名称
interval 1 #间隔1秒执行一次
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 99 #从主机的优先级要小于此数字
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100
}
track_script { #调用前面的脚本
check_down
}
}
#脚本内容 memcached1 memcached2 位置内容是一样的
[root@memcached2 keepalived]# vim /etc/keepalived/memcached.sh
#!/bin/bash
if [ $(ps -C memcached --no-header | wc -l) -eq 0 ]; then
systemctl stop keepalived
fi
#测试验证
[root@Memcached1 ~]# systemctl start keepalived
[root@memcached2 ~]# systemctl start keepalived
[root@memcached2 keepalived]# vim /etc/keepalived/memcached.sh
#!/bin/bash
if [ $(ps -C memcached --no-header | wc -l) -eq 0 ]; then
systemctl stop keepalived
fi
#测试验证
[root@Memcached1 ~]# systemctl start keepalived
[root@memcached2 ~]# systemctl start keepalived