Memcached内存数据缓存群集实验

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Memcached内存数据缓存群集实验相关的知识,希望对你有一定的参考价值。

实验环境:
技术分享图片
实验目的:client端通过虚拟IP地址登陆memcached主和从缓存服务器插入数据,主缓存和从缓存是否有数据
步骤:主服务器
[[email protected] ~]# yum install -y gcc gcc-c++ make //搭建安装环境
[[email protected] ~]# tar xvf libevent-2.1.8-stable.tar.gz
[[email protected] ~]# tar xvf memcached-1.5.6.tar.gz
[[email protected] ~]# mkdir /opt/magent
[[email protected] ~]# tar xvf magent-0.5.tar.gz -C /opt/magent/ //解压软件包
[[email protected] ~]# 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] memcached-1.5.6]# cd ../memcached-1.5.6
[[email protected] memcached-1.5.6]# ./configure --with-libevent=/usr
[[email protected] memcached-1.5.6]# make && make install //编译安装
[[email protected] libevent-2.1.8-stable]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
[[email protected] ~]# cd /opt/magent/
[[email protected] magent]# vi ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
[[email protected] magent]# vi Makefile
LIBS = -levent -lm //第一行末尾加-lm (不是数字1)
[[email protected] magent]# make
[[email protected] magent]# ls //会产生magent可执行程序
[[email protected] magent]# cp magent /usr/bin/ //将编译好的magent程序复制到PATH路径中
[[email protected] magent]# scp magent [email protected]:/usr/bin/ //把产生的magent文件直接复制到从服务器
[[email protected] magent]# yum install keepalived -y //下载健康检查工具
[[email protected] shell]# vi /etc/keepalived/keepalived.conf //配置keepalived文件
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
global_defs {
notification_email {br/>[email protected]
[email protected]
br/>[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
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent
}
virtual_ipaddress {
192.168.100.188
}
} //定义漂移地址,×××字体要改或添加其余全删除
[[email protected] shell]# mkdir /opt/shell //创建magent脚本目录
[[email protected] shell]# cd /opt/shell/
[[email protected] shell]# vi 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.100.188 -p 12000 -s 192.168.100.10:11211 -b 192.168.100.20:11211
else
pkill -9 magent
Fi
[[email protected] shell]# chmod +x magent.sh //给执行权限
[[email protected] shell]# systemctl start keepalived.service //启动健康检查工具
[[email protected] shell]# netstat -anpt | grep 12000
tcp 0 0 192.168.100.188:12000 0.0.0.0:* LISTEN 9582/magent //说明magent 启动工作了
[[email protected] shell]# cat /var/log/messages //查看日志
……
Transition to MASTER STATE
……
[[email protected] shell]# ip addr //确定定义的漂移地址存在
---从服务器配置--
[[email protected] shell]# yum install -y gcc gcc-c++ make //搭建安装环境
[[email protected] shell]# tar xvf libevent-2.1.8-stable.tar.gz
[[email protected] shell]# tar xvf memcached-1.5.6.tar.gz
[[email protected] ~]# 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
[[email protected] memcached-1.5.6]# make && make install //编译安装
[[email protected] memcached-1.5.6]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
[[email protected] memcached-1.5.6]# yum install keepalived -y //下载健康检查工具
[[email protected] memcached-1.5.6]# cd /etc/keepalived/
[[email protected] keepalived]# mv keepalived.conf keepalived.conf.bak
[[email protected] keepalived]# scp [email protected]:/etc/keepalived/keepalived.conf ./ 把主服务器的配置文件复制过来
[[email protected] keepalived]# vi keepalived.conf
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
global_defs {
notification_email {
br/>[email protected]
[email protected]
br/>[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MAGENT_HB
}

vrrp_instance VI_1 {
state BACKUO
interface ens33
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent
}
virtual_ipaddress {
192.168.100.188
}
} //×××字体需要修改

[[email protected] keepalived]# mkdir /opt/shell
[[email protected] keepalived]# cd /opt/shell/
[[email protected] shell]# vi magent.sh
#!/bin/bash
K=ip addr | grep 192.168.100.188 | grep -v grep | wc -l
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.100.188 -p 12000 -s 192.168.100.10:11211 -b 192.168.100.20:11211
else
pkill -9 magent
Fi
[[email protected] shell]# chmod +x magent.sh
[[email protected] shell]# systemctl start keepalived.service
[[email protected] shell]# cat /var/log/messages //验证主从
……
Entering BACKUP STATE
……
[[email protected] shell]# memcached -m 512k -u root -d -l 192.168.100.10 -p 11211 //启动主
[[email protected] shell]# memcached -m 512k -u root -d -l 192.168.100.20 -p 11211 //启动从
---在客户端测试---
[[email protected] ~]# yum install -y telnet
[[email protected] ~]# telnet 192.168.100.188 12000
Trying 192.168.100.188...
Connected to 192.168.100.188.
Escape character is ‘^]‘.
add username 0 0 7
1234567
主服务器
[[email protected] shell]# telnet 192.168.100.10 11211
Trying 192.168.100.10...
Connected to 192.168.100.10.
Escape character is ‘^]‘.
get username
VALUE username 0 7
1234567
从服务器
[[email protected] shell]# telnet 192.168.100.20 11211
Trying 192.168.100.20...
Connected to 192.168.100.20.
Escape character is ‘^]‘.
get username
VALUE username 0 7
1234567
客户端再进
[[email protected] ~]# telnet 192.168.100.188 12000
Trying 192.168.100.188...
Connected to 192.168.100.188.
Escape character is ‘^]‘.
add ok 0 0 6
123456
STORED
get ok
VALUE ok 0 6
123456 //新建ok
从服务器跟主服务器都检证一下是否都有数据插入
主---
[[email protected] shell]# telnet 192.168.100.10 11211
Trying 192.168.100.10...
Connected to 192.168.100.10.
Escape character is ‘^]‘.
get ok
VALUE ok 0 6
123456
从---
[[email protected] shell]# telnet 192.168.100.20 11211
Trying 192.168.100.20...
Connected to 192.168.100.20.
Escape character is ‘^]‘.
get username
VALUE username 0 7
1234567
END
get ok
VALUE ok 0 6
123456

//把主停了业务不影响





以上是关于Memcached内存数据缓存群集实验的主要内容,如果未能解决你的问题,请参考以下文章

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

Mencached群集部署

Magent+keepalived+Memcached缓存高可用群集

Memcached+magent实现主从同步 +keepalived高可用群集

memcached群集

Centos7 下实现 memcached + keepalived 高可用群集