[经验分享] Memcached+Magent 缓存集群配置过程!
摘要:
Memcached+Magent集群配置
Package
libevent-1.4.9-stable.tar.gz
memcached-1.2.6.tar.gz
magent-0.5.tar.gz
一、配置ip
#Vi/etc/sysconfig/network-scripts/ifcfg-th0
IPADDR=192.168.1.90
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
TYPE=Ethernet
ONBOOT=yes
二、配置DNS上网
#Vi/etc/resolv.conf
nameserver 192.168.1.1
三、安装包:
# yum -y install gcc* glibc* autoconflibtool pkgconfig make
# reboot
四、安装步骤:
1、编译安装libevent:
# tar–zxvf libevent-1.4.9-stable.tar.gz
–C /usr/local
# cd /usr/local
# mv libevent-1.4.9-stable libevent
# cd libevent
# ./configure --prefix=/usr
# cd libevent
# ./configure --prefix=/usr
# make
# make install
2、编译安装Memcached:
# wget http://danga.com/memcached/dist/ memcached-1.2.6.tar.gz
# tar -zxvf memcached-1.2.6.tar.gz –C /usr/local
# tar -zxvf memcached-1.2.6.tar.gz –C /usr/local
# cd /usr/local
# mv memcached-1.2.6 memcached
# cd memcached
# ./configure --with-libevent=/usr/local
# make
# cd memcached
# ./configure --with-libevent=/usr/local
# make
# make install
# exportPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
# exportPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
3、编译安装magent:
http://code.google.com/p/memagent/downloads/list
# cd /usr/local
# mkdir magent
# tar -zxf magent-0.5.tar.gz –C /usr/local/ magent
# tar -zxf magent-0.5.tar.gz –C /usr/local/ magent
# cd /usr/local/ magent
# /sbin/ldconfig
# sed -i "s#LIBS = -levent#LIBS = -levent-lm#g" Makefile
# vi magetn.c
# /sbin/ldconfig
# sed -i "s#LIBS = -levent#LIBS = -levent-lm#g" Makefile
# vi magetn.c
添加:
# make
# cp magent /usr/bin/magent
# . /etc/init.d/iptables stop
# cp magent /usr/bin/magent
# . /etc/init.d/iptables stop
测试
magent -u root -n 51200 -l 192.168.1.90-p 12000 -s 192.168.1.111:11211 -s 192.168.1.112:11212 -b 192.168.1.112:11211
五、使用实例:
# memcached -m 1 -u root -d -l127.0.0.1 -p 11211
# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
# memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
# magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 127.0.0.1:11211 -s127.0.0.1:11212 -b 127.0.0.1:11213
# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
# memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
# magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 127.0.0.1:11211 -s127.0.0.1:11212 -b 127.0.0.1:11213
1、分别在11211、11212、11213端口启动3个Memcached进程,在12000端口开启magent代理程序;
2、11211、11212端口为主Memcached,11213端口为备份Memcached;
3、连接上12000的magent,set key1和setkey2,根据哈希算法,key1被写入11212和11213端口的Memcached,key2被写入11212和11213端口的Memcached;
4、当11211、11212端口的Memcached死掉,连接到12000端口的magent取数据,数据会从11213端口的Memcached取出;
5、当11211、11212端口的Memcached重启复活,连接到12000端口,magent会从11211或11212端口的Memcached取数据,由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管11213端口的Memcached还有数据(此问题尚待改进)。
六、整个测试流程:
[root@centos52 ~]# telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
memcached agent v0.4
matrix 1 -> 127.0.0.1:11211, pool size 0
matrix 2 -> 127.0.0.1:11212, pool size 0
END
set key1 0 0 8
zhangyan
STORED
set key2 0 0 8
zhangyan
STORED
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11212
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
END
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11213
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
模拟11211、11212端口的Memcached死掉
[root@centos52 ~]# ps -ef | grep memcached
root 6589 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
root 6591 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
root 6593 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
root 6609 6509 001:44 pts/0 00:00:00 grep memcached
[root@centos52 ~]# kill -9 6589
[root@centos52 ~]# kill -9 6591
[root@centos52 ~]# telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
模拟11211、11212端口的Memcached重启复活
[root@centos52 ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
[root@centos52 ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
[root@centos52 ~]# telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
END
quit
Connection closed by foreign host.
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
memcached agent v0.4
matrix 1 -> 127.0.0.1:11211, pool size 0
matrix 2 -> 127.0.0.1:11212, pool size 0
END
set key1 0 0 8
zhangyan
STORED
set key2 0 0 8
zhangyan
STORED
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11212
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
END
quit
Connection closed by foreign host.
[root@centos52 ~]# telnet 127.0.0.1 11213
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
模拟11211、11212端口的Memcached死掉
[root@centos52 ~]# ps -ef | grep memcached
root 6589 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
root 6591 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
root 6593 1 0 01:25 ? 00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11213
root 6609 6509 001:44 pts/0 00:00:00 grep memcached
[root@centos52 ~]# kill -9 6589
[root@centos52 ~]# kill -9 6591
[root@centos52 ~]# telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 8
zhangyan
END
get key2
VALUE key2 0 8
zhangyan
END
quit
Connection closed by foreign host.
模拟11211、11212端口的Memcached重启复活
[root@centos52 ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11211
[root@centos52 ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212
[root@centos52 ~]# telnet 127.0.0.1 12000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
END
quit
Connection closed by foreign host.
本文来自:腾讯云论坛 bbs.qcloud.com
本文作者:苏州微信 http://www.dajiake.com
腾讯云服务商邀请关注:
http://partners.qcloud.com/invitation/99377079157835da208bd0
关注服务商的链接,提供下您关注的QQ,我们这边审核下即可快速回复您的工单问题与技术支持! 感谢您的信任与支持!