Memcached高可用群集
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Memcached高可用群集相关的知识,希望对你有一定的参考价值。
Memcached的特点:
1.在Memcached中可以保存的item数据量是没有限制的,只要内存足够;
2.Memcached单进程最大使用内存为2GB,要使用更多内存,可以分别在不同端口启动多个Memcached进程;
3.Memcached是一种无阻塞的socket通信方式的服务,基于libevent库,由于无阻塞通信,对内存读写速度非常之快;
4.Memcached分为服务器和客户端,可以配置多个服务器和客户端,应用于分布式的服务非常广泛;
5.Memcached作为小规模的数据分布式平台是非常高效的;
服务器 | IP地址 | 软件包 |
---|---|---|
主Memcached | 192.168.5.129 | libevent-2.1.8-stable.tar.gz、memcached-1.5.9.tar.gz、magent-0.5.tar.gz、keepalived |
从Memcached | 192.168.5.139 | libevent-2.1.8-stable.tar.gz、memcached-1.5.9.tar.gz、magent-0.5.tar.gz、keepalived |
Client | 192.168.5.141 | telnet |
搭建Memcached服务器
Memcached服务器在两台服务器上都需要搭建,方式完全相同,在安装memcached服务器是需先安装libevent,然后再安装memcached。
安装环境包
yum -y install gcc gcc-c++ make
编译安装libevent(必须先安装)
#解压
tar zxvf libevent-2.1.8-stable.tar.gz -C /opt#切换到libevent目录
cd /opt/libevent-2.1.8-stable/#配置
./configure --prefix=/usr/#编译及安装
make && make install
编译安装memcached
#解压
tar zxvf memcached-1.5.9.tar.gz -C /opt/#切换到memcached
cd /opt/memcached-1.5.9/#配置(指定libevent路径)
./configure --with-libevent=/usr/#编译及安装
make && make install
优化启动(支持复制功能的Mencached需要安装后的libevent-2.1.so.6模块,不然启动服务会报错)
ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
在主服务器上安装magent
magent是一个memcached代理软件,防止单点故障,缓存代理也可以着备份,通过客户端连接缓存代理服务器,缓存代理服务器连接缓存服务器。
#创建magent目录
mkdir /opt/magent
#解压
tar zxvf magent-0.5.tar.gz -C /opt/magent
#切换到目录
cd /opt/magent
#让动态链接库为系统所共享
/sbin/ldconfig
#编辑ketama.h,添加文件头部信息
vim ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
#编辑Makefile
vi Makefile
LIBS = -levent -lm
#编译(完成后会生成一个manage的可执行文件)
make
#让服务便于系统识别
cp /opt/magent/magent /usr/bin/
scp /opt/magent/magent [email protected]:/usr/bin/ //发送到从服务器上
安装keepalived
#主从服务器安装keepalived
yum -y install keepalived
#修改主从服务器配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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_H1 //router_id自定义,但是要确保主从一致
}
vrrp_script magent {
script "/root/shell/magent.sh"
interval 2 //定义一个函数,此时该脚本还未创建,要在实例外面定义
}
vrrp_instance VI_1 {
state MASTER //从为BACKUP
interface ens33
virtual_router_id 51 //此处id也相同
priority 100 //主服务器优先级要高于从服务器
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent //应用函数
}
virtual_ipaddress {
192.168.5.100 //定义vtp自定义,只要不是在使用中的地址即可
}
} //配置文件下面还有多余的内容此时就可以直接删除
#建立shell目录
mkdir /root/shell
#创建magent.sh脚本
vim /root/shell/magent.sh
#!/bin/bash
Keepalived=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
magent -u root -n 51200 -l 192.168.5.100 -p 12000 -s 192.168.5.129:11211 -b 192.168.5.139:11211
else
pkill -9 magent
fi
参数说明:
-n 51200 #定义用户最大连接数
-l 192.168.5.100 #指定虚拟IP
-p 12000 #指定端口号
-s #指定主缓存服务器
-b #指定从缓存服务器
#赋予脚本执行权限
chmod +x /root/shell/magent.sh
#启动keepalived服务
systemctl enable keepalived.service
systemctl start keepalived.service
在从服务器上操作
#将主服务器上keepalived配置文件复制过来
scp [email protected]:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
#编辑keepalived配置文件
vim keepalived.conf
! Configuration File for keepalived
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_H2 #指定router_id
}
vrrp_script magent { #定义函数
script "/root/shell/magent.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 #本地网卡名称
virtual_router_id 51 #id主从一致
priority 99 #主服务器优先级大于从服务器优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #vip地址
192.168.5.100
}
track_script { #调用函数
magent
}
}
#创建magent.sh脚本
mkdir /root/shell
vim /root/shell/magent.sh
#!/bin/bash
Keepalived=`ip addr | grep 192.168.5.100 | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
magent -u root -n 51200 -l 192.168.5.100 -p 12000 -s 192.168.5.129:11211 -b 192.168.5.139:11211
else
pkill -9 magent
fi
#赋予脚本执行权限
chmod +x /root/shell/magent.sh
#启动keepalived服务
systemctl enable keepalived.service
systemctl start keepalived.service
在主从服务器上开启memcached
memcached -m 512k -u root -d -l 192.168.5.129 -p 11211 //主服务器
memcached -m 512k -u root -d -l 192.168.5.139 -p 11211 //从服务器
netstat -ntap | grep 11211
客户端测试连通性
#安装telnet客户端
yum -y install telnet
#连接memcached
telnet 192.168.175.188 12000
#测试复制功能
//连接vip进行测试,插入user键值
[[email protected] ~]# telnet 192.168.5.100 12000
Trying 192.168.5.100...
Connected to 192.168.5.100.
Escape character is ‘^]‘.
set user 0 0 5
test1
STORED
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
//连接主Memcached,查询user键值,可以获取,成功!
[[email protected] ~]# telnet 192.168.5.129 11211
Trying 192.168.5.129...
Connected to 192.168.5.129.
Escape character is ‘^]‘.
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
//连接从Memcached,查询user键值,也可以获取,成功!
[[email protected] ~]# telnet 192.168.5.139 11211
Trying 192.168.96.17...
Connected to 192.168.96.17.
Escape character is ‘^]‘.
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
测试单点故障
关闭主服务器
查看从服务器
客户端连接
以上是关于Memcached高可用群集的主要内容,如果未能解决你的问题,请参考以下文章
Memcached高可用群集(Memcached主主复制+Keepalived)
Centos7 下实现 memcached + keepalived 高可用群集