分类目录归档:memcached

magent构建memcache集群

magent功能相当于一个proxy调度器,通过一定的算法将数据存到不同后端。(另外一个由twitter开源的twemproxy,功能作用类似。可参考:twemproxy安装配置配置文件详解

部署:
memcached部署自行百度。
magent参考:repcache+magent构建高可用memcache

启动:

# memcached -u xiemx -d -p 11221
# memcached -u xiemx -d -p 11222
# memcached -u xiemx -d -p 11223
# magent -p 12000 -s 127.0.0.1:11221 -s127.0.0.1:11222 -b 127.0.0.1:11223

1、启动3个Memcached进程,端口11221、11222、11223,启动magent进程,端口11211.
2、11221、11222为master,11223为backup;
3、telnet 11211的magent,set key1和set key2,根据哈希算法,key1被写入11221和11213端口的Memcached,key2被写入11222和11213端口的Memcached;
4、kill 11221、11222端口的Memcached,telnet到11211端口的magent取数据,从11223端口的Memcached获得key1/key2;
5、当11221、11222端口的Memcached重启复活,连接到11211端口,magent会从11221或11222端口的Memcached取数据,由于这两台Memcached重启后无数据,因此magent取得的将是空值,尽管11223端口的Memcached还有数据。(可通过repcache解决,但repcache目前只能支持单节点主主)

[root@memcache ~]# telnet 127.0.0.1 11211
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:11221, pool size 0
matrix 2 -> 127.0.0.1:11222, pool size 0
END
set key1 0 0 1
1
STORED
set key2 0 0 1
2
STORED
quit
Connection closed by foreign host.


[root@memcache ~]# telnet 127.0.0.1 11221
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
VALUE key2 0 1
2
END
quit
Connection closed by foreign host.


[root@memcache ~]# telnet 127.0.0.1 11222
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 1
1
END
get key2
END
quit
Connection closed by foreign host.

[root@memcache ~]# telnet 127.0.0.1 11223
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 0 1
1
END
get key2
VALUE key2 0 1
2
END
quit
Connection closed by foreign host.

模拟11211、11212端口的Memcached死掉
[root@memcache ~]# ps -ef | grep memcached
root       6589    1   0 01:25 ?        00:00:00 memcached -u xiemx -d -p 11221
root       6591    1   0 01:25 ?        00:00:00 memcached -u xiemx -d -p 11222
root       6593    1   0 01:25 ?        00:00:00 memcached -u xiemx -d -p 11223
root       6609   6509   001:44 pts/0     00:00:00 grep memcached
[root@memcache ~]# kill -9 6589
[root@memcache ~]# kill -9 6591
[root@memcache ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1 
VALUE key1 0 1
1
END
get key2
VALUE key2 0 1
2
END
quit
Connection closed by foreign host.

模拟11211、11212端口的Memcached重启复活
[root@memcache ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11221
[root@memcache ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11222
[root@memcache ~]# 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
END
quit
Connection closed by foreign host.

repcache+magent构建高可用memcache

repcache+magnet

magent:一款开源的Memcached代理服务器软件,功能和mysql-proxy类似。
repcache:日本开发的一款开源工具,使memcache能够做主从复制。可以通过patch包升级memcache,也可以下载包含replication的memcache版本

先将master/slave通过replication构建自动复制的主主,通过magent将K/V,写入到master和backup中,当master宕机时,magent将所有读写请求交给slave,slave等待master启动,默认情况下master宕机重启后内存中的数据回丢失,但由于repcache的自动主主,master启动时会自动从salve端复制所有数据。

1.安装部署
    
    libevent:
      apt-get install -y libevent-dev

    magent:
      wget http://memagent.googlecode.com/files/magent-0.5.tar.gz  
      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  
      
    repcache:
      wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
      tar xf memcached-1.2.8-repcached-2.2.tar.gz
      cd memcached-1.2.8-repcached-2.2/
      ./configure --prefix=/opt/repcached/ --enable-replication --program-transform-name=s/memcached/repcached/
      make&&make install
      ln -s /opt/repcached/bin/repcached /usr/bin/


2.编译错误汇总
    magent

    1.[root@test magent]# make 
        gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c
        magent.c: In function ‘writev_list’:
        magent.c:729: error: ‘SSIZE_MAX’ undeclared (first use in this function)
        magent.c:729: error: (Each undeclared identifier is reported only once
        magent.c:729: error: for each function it appears in.)
        make: *** [magent.o] Error 1
        解决方法:
        [root@test magent]# vim ketama.h
        #ifndef SSIZE_MAX
        #define SSIZE_MAX      32767
        #endif
        #ifndef _KETAMA_H
        #define _KETAMA_H

    2.[root@test magent]# make 
        gcc -Wall -O2 -g  -c -o magent.o magent.c
        gcc -Wall -O2 -g  -c -o ketama.o ketama.c
        gcc -Wall -O2 -g -o magent magent.o ketama.o -levent
        ketama.o: In function `create_ketama':
        /opt/root/magent-0.5/ketama.c:399: undefined reference to `floorf'
        collect2: ld returned 1 exit status
        make: *** [magent] Error 1

        修改Makefile
        LIBS = -levent 为LIBS = -levent -lm
        #sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile 

    repcached

    1.[root@test magent]# make 
        make all-recursive    
        make[1]: Entering directory `/usr/local/memcached'    
        Making all in doc    
        make[2]: Entering directory `/usr/local/memcached/doc'    
        make[2]: Nothing to be done for `all'.    
        make[2]: Leaving directory `/usr/local/memcached/doc'    
        make[2]: Entering directory `/usr/local/memcached'    
        gcc -DHAVE_CONFIG_H -I. -DNDEBUG -m64 -g -O2 -MT memcached-memcached.o -MD     
        MP -MF .deps/memcached-memcached.Tpo -c -o memcached-memcached.o `test -f     
        memcached.c' || echo './'`memcached.c    
        memcached.c: In function ‘add_iov’:    
        memcached.c:697: error: ‘IOV_MAX’ undeclared (first use in this function)    
        memcached.c:697: error: (Each undeclared identifier is reported only once    
        memcached.c:697: error: for each function it appears in.)    
        make[2]: *** [memcached-memcached.o] Error 1    
        make[2]: Leaving directory `/usr/local/memcached'    
        make[1]: *** [all-recursive] Error 1    
        make[1]: Leaving directory `/usr/local/memcached'    
        make: *** [all] Error 2
           
        解决方案:    
        vi memcached.c
           
        将下面的几行    
        /* FreeBSD 4.x doesn't have IOV_MAX exposed. */    
        #ifndef IOV_MAX    
        #if defined(__FreeBSD__) || defined(__APPLE__)    
        # define IOV_MAX 1024    
        #endif    
        #endif
           
        修改为
        /* FreeBSD 4.x doesn't have IOV_MAX exposed. */    
        #ifndef IOV_MAX    
        # define IOV_MAX 1024    
        #endif

  2.启动服务

      1.启动repcached
        启动master:
        repcached -d -v -x 127.0.0.1 -u vagrant  
        启动slave:
        repcached -d -v -x 127.0.0.1 -u vagrant -p 11222

        参数说明:
          -x 设置从哪个IP上进行同步。
          -X 指定数据同步的端口。
          Memcached默认服务端口是11211,默认同步监听端口是11212。

      2.启动magent
        magent -u root -p 11233 -s 127.0.0.1:11211 -s 127.0.0.1:11222