0%

Memcached基于repcached的主从复制

由于 Memcached 自己没有防止单点的措施,因为为了保障 Memcached 服务的高可用,我们需要借助外部的工具来实现高可用的功能。本文引入 Repcached 这个工具,通过使用该工具我们可以完成 Memcached 服务的主从功能。

Repcached 它是由日本人开发的,用来实现 Memcached 复制功能的一个工具。它所构建的主从方案是一个单主单从的方案,不支持多主多从。但是,它的特点是,主从两个节点可以互相读写,从而可以达到互相同步的效果。

假设主节点坏掉,从节点会很快侦测到连接断开,然后它会自动切换到监听状态( listen)从而成为主节点,并且等待新的从节点加入。

假设原来挂掉的主节点恢复之后,我们只能人工手动以从节点的方式去启动。原来的主节点并不能抢占成为新的主节点,除非新的主节点挂掉。这也就意味着,基于 Repcached 实现的 Memcached 主从,针对主节点并不具备抢占功能。

假设从节点坏掉,主节点也会很快侦测到连接断开,然后它就会重新切换到监听状态(listen),并且等待新的从节点加入。

假设主从节点都挂掉,则数据就丢失了!因此,这是 Repcached 的一个短板,不过后期我们可以通过结合其它的工具来弥补这个缺点。

基础环境准备

1
2
172.16.0.8 node1
172.16.0.9 node2
1
2
yum -y install gcc gcc-c++
yum install libevent libevent-devel

repcached安装

这种方式集成了memcached,由于版本比较老,memcached为1.2.8,暂时未找到memcached1.5.X匹配的补丁包repcached

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
wget https://sourceforge.net/projects/repcached/files/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz
tar zxf memcached-1.2.8-repcached-2.2.1.tar.gz
cd memcached-1.2.8-repcached-2.2.1
./configure --enable-replication
make && make install

备注:
如make安装错误,提示
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.)

则修改memcached.c,大概60行
/* FreeBSD 4.x doesn't have IOV_MAX exposed. */
#ifndef IOV_MAX
#if defined(__FreeBSD__) || defined(__APPLE__)
# define IOV_MAX 1024
#endif
#endif

删掉
#if defined(__FreeBSD__) || defined(__APPLE__)
#endif
即可

启动配置

启动master

1
2
3
[root@node1 ~]# memcached -v -d -p 11211 -l 172.16.0.8 -u root -P /tmp/memcached1.pid 
[root@node1 ~]# replication: listen
[root@node1 ~]# replication: accept # node2 启动正常后,node1 将 accept

启动salve

1
2
3
4
[root@node2 ~]# memcached -v -d -p 11211 -l 172.16.0.9 -u root -x 172.16.0.8 -P /tmp/memcached1.pid 
[root@node2 ~]# replication: connect (peer=172.16.0.8:11212)
replication: marugoto copying
replication: start

repcache的参数说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@public01 ~]# memcached -help
memcached 1.2.8
repcached 2.2.1
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-s <file> unix socket path to listen on (disables network support)
-a <mask> access mask for unix socket, in octal (default 0700)
-l <ip_addr> interface to listen on, default is INDRR_ANY
-d run as a daemon
-r maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes, default is 64 MB
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections, default is 1024
-k lock down all paged memory. Note that there is a
limit on how much memory you may lock. Trying to
allocate more than that would fail, so be sure you
set the limit correctly for the user you started
the daemon with (not for -u <username> user;
under sh this is done with 'ulimit -S -l NUM_KB').
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-h print this help and exit
-i print memcached and libevent license
-P <file> save PID in <file>, only used with -d option
-f <factor> chunk size growth factor, default 1.25
-n <bytes> minimum space allocated for key+value+flags, default 48
-R Maximum number of requests per event
limits the number of requests process for a given con nection
to prevent starvation. default 20
-b Set the backlog queue limit (default 1024)
-x <ip_addr> hostname or IP address of peer repcached
-X <num:num> TCP port number for replication. <listen:connect> (default: 11212)