配置memcached集群负载均衡
时间:2014-09-08 21:47 来源:linux.it.net.cn 作者:it
memcached是针对数据库的缓存软件,可以有效降低数据库的负载。
本文介绍的内容是如何将多个memcached做成集群负载均衡,实现memcached的高可用。
一、安装步骤:
1、编译安装libevent:
复制代码代码如下:
tar zxvf libevent-1.4.9-stable.tar.gz
cd libevent-1.4.9-stable/
./configure --prefix=/usr
make && make install
2、编译安装Memcached:
复制代码代码如下:
tar zxvf memcached-1.2.6.tar.gz
cd memcached-1.2.6/
./configure --with-libevent=/usr
make && make install
3、编译安装magent:
复制代码代码如下:
mkdir magent
cd magent/
tar zxvf magent-0.5.tar.gz
/sbin/ldconfig
sed -i "s/LIBS = -levent/LIBS = -levent -lm/g" Makefile
make
cp magent /usr/bin/magent
二、实例:
复制代码代码如下:
memcached -m 1 -u root -d -l 10.1.94.100 -p 11211
memcached -m 1 -u root -d -l 10.1.94.101 -p 11211
memcached -m 1 -u root -d -l 10.1.94.102 -p 11211
magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 10.1.94.100:11211 -s 10.1.94.101:11211 -b 10.1.94.102:11211
1、分别在10.1.94.100 – 10.1.94.102 3台机器的11211端口启动3个Memcached进程,在12000端口开启magent代理程序;(以此类推可在不同机器上启动memcached)
2、10.1.94.100和101为主Memcached,10.1.94.102为备份Memcached;
3、连接上12000的magent,set key1和set key2,根据哈希算法,key1被写入10.1.94.100和10.1.94.102的Memcached,key2被写入
10.1.94.101和10.1.94.102的Memcached;
4、当10.1.94.100和101的Memcached死掉,连接到12000端口的magent取数据,数据会从10.1.94.102的Memcached取出;
5、当10.1.94.100和101的Memcached重启复活,连接到12000端口,magent会从10.1.94.100或10.1.94.101的Memcached取数据,
由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管10.1.94.102的Memcached还有数据(此问题尚待改进)。
三、整个测试流程:
先在magent中添加1个key 如下:
复制代码代码如下:
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
wangqi
STORED
set key2 0 0 8
wangqi
STORED
quit
Connection closed by foreign host.
然后在2个轮询的memcached中get刚才添加的key是否返回正确:
复制代码代码如下:
telnet 10.1.94.100 11211
Trying 10.1.94.100…
Connected to 10.1.94.100.
Escape character is ‘^]’.
get key1
END
get key2
VALUE key2 0 8
wangqi
END
quit
Connection closed by foreign host.
telnet 10.1.94.101 11211
Trying 10.1.94.101…
Connected to 10.1.94.101.
Escape character is ‘^]’.
get key1
VALUE key1 0 8
wangqi
END
get key2
END
quit
Connection closed by foreign host.
telnet 10.1.94.103 11211
Trying 10.1.94.103…
Connected to 10.1.94.103.
Escape character is ‘^]’.
get key1
VALUE key1 0 8
wangqi
END
get key2
VALUE key2 0 8
wangqi
END
quit
Connection closed by foreign host.
模拟10.1.94.100和101的Memcached死掉
直接kill进程然后:
复制代码代码如下:
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
wangqi
END
get key2
VALUE key2 0 8
wangqi
END
quit
Connection closed by foreign host.
模拟10.1.94.100和101的Memcached重启复活
复制代码代码如下:
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.
magent -u root(启动用户uid) -n 51200(最大连接数) -l 127.0.0.1(ip地址) -p 12000(监听端口) -s 127.0.0.1:11211(memcached的ip) -s 127.0.0.1:11212(memcached的ip) -b 127.0.0.1:11213(备份memcached的ip)
如果不需要轮询hash的话,只需要一主一备即可。
但是当主当掉后备份memcached接手,然后主服务恢复后数据会根据主服务器走。
(责任编辑:IT)
memcached是针对数据库的缓存软件,可以有效降低数据库的负载。
一、安装步骤:
复制代码代码如下:
tar zxvf libevent-1.4.9-stable.tar.gz
cd libevent-1.4.9-stable/ ./configure --prefix=/usr make && make install
2、编译安装Memcached:
复制代码代码如下:
tar zxvf memcached-1.2.6.tar.gz
cd memcached-1.2.6/ ./configure --with-libevent=/usr make && make install 3、编译安装magent:
复制代码代码如下:
mkdir magent
cd magent/ tar zxvf magent-0.5.tar.gz /sbin/ldconfig sed -i "s/LIBS = -levent/LIBS = -levent -lm/g" Makefile make cp magent /usr/bin/magent 二、实例:
复制代码代码如下:
memcached -m 1 -u root -d -l 10.1.94.100 -p 11211
memcached -m 1 -u root -d -l 10.1.94.101 -p 11211 memcached -m 1 -u root -d -l 10.1.94.102 -p 11211 magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 10.1.94.100:11211 -s 10.1.94.101:11211 -b 10.1.94.102:11211
1、分别在10.1.94.100 – 10.1.94.102 3台机器的11211端口启动3个Memcached进程,在12000端口开启magent代理程序;(以此类推可在不同机器上启动memcached) 由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管10.1.94.102的Memcached还有数据(此问题尚待改进)。
三、整个测试流程:
复制代码代码如下:
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 wangqi STORED set key2 0 0 8 wangqi STORED quit Connection closed by foreign host.
然后在2个轮询的memcached中get刚才添加的key是否返回正确:
复制代码代码如下:
telnet 10.1.94.100 11211
Trying 10.1.94.100… Connected to 10.1.94.100. Escape character is ‘^]’. get key1 END get key2 VALUE key2 0 8 wangqi END quit Connection closed by foreign host. telnet 10.1.94.101 11211 Trying 10.1.94.101… Connected to 10.1.94.101. Escape character is ‘^]’. get key1 VALUE key1 0 8 wangqi END get key2 END quit Connection closed by foreign host. telnet 10.1.94.103 11211 Trying 10.1.94.103… Connected to 10.1.94.103. Escape character is ‘^]’. get key1 VALUE key1 0 8 wangqi END get key2 VALUE key2 0 8 wangqi END quit Connection closed by foreign host. 模拟10.1.94.100和101的Memcached死掉
直接kill进程然后:
复制代码代码如下:
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 wangqi END get key2 VALUE key2 0 8 wangqi END quit Connection closed by foreign host.
模拟10.1.94.100和101的Memcached重启复活
复制代码代码如下:
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. magent -u root(启动用户uid) -n 51200(最大连接数) -l 127.0.0.1(ip地址) -p 12000(监听端口) -s 127.0.0.1:11211(memcached的ip) -s 127.0.0.1:11212(memcached的ip) -b 127.0.0.1:11213(备份memcached的ip)
如果不需要轮询hash的话,只需要一主一备即可。 |