由于 Memcached 自己没有防止单点的措施,因为为了保障 Memcached 服务的高可用,我们需要借助外部的工具来实现高可用的功能。本文引入 Repcached 这个工具,通过使用该工具我们可以完成 Memcached 服务的主从功能。
Repcached 它是由日本人开发的,用来实现 Memcached 复制功能的一个工具。它所构建的主从方案是一个单主单从的方案,不支持多主多从。但是,它的特点是,主从两个节点可以互相读写,从而可以达到互相同步的效果。
假设主节点坏掉,从节点会很快侦测到连接断开,然后它会自动切换到监听状态( listen)从而成为主节点,并且等待新的从节点加入。
假设原来挂掉的主节点恢复之后,我们只能人工手动以从节点的方式去启动。原来的主节点并不能抢占成为新的主节点,除非新的主节点挂掉。这也就意味着,基于 Repcached 实现的 Memcached 主从,针对主节点并不具备抢占功能。
假设从节点坏掉,主节点也会很快侦测到连接断开,然后它就会重新切换到监听状态(listen),并且等待新的从节点加入。
假设主从节点都挂掉,则数据就丢失了!因此,这是 Repcached 的一个短板,不过后期我们可以通过结合其它的工具来弥补这个缺点。
OK,简单介绍到这里。下面我们通过实验来看,基于 Repcached 的 Memcached 主从架构是如何部署,以后如何测试和管理的。
上篇:Memcached实战之单机部署----单实例/多实例
环境:
Ubuntu下安装Memcached及命令解释
一、基础环境准备
[root@master ~]# yum -y install gcc gcc-c++
二、Memcached安装
1、安装libevent
[root@master ~]#cd /usr/local/src
[root@master src]# wget ?orig
[root@master src]# tar zxvf libevent-2.0.21-stable.tar.gz
[root@master src]# cd libevent-2.0.21-stable
[root@master libevent-2.0.21-stable]# ./configure --prefix=/usr
[root@master libevent-2.0.21-stable]# make
[root@master libevent-2.0.21-stable]# make install
[root@master libevent-2.0.21-stable]# ll /usr/lib/libevent* # libevent安装完后,会在/usr/lib目录下出现如下内容
lrwxrwxrwx 1 root root 21 Aug 11 13:49 /usr/lib/libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x 1 root root 968690 Aug 11 13:49 /usr/lib/libevent-2.0.so.5.1.9
-rw-r--r-- 1 root root 1571802 Aug 11 13:49 /usr/lib/libevent.a
lrwxrwxrwx 1 root root 26 Aug 11 13:49 /usr/lib/libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x 1 root root 585225 Aug 11 13:49 /usr/lib/libevent_core-2.0.so.5.1.9
-rw-r--r-- 1 root root 978482 Aug 11 13:49 /usr/lib/libevent_core.a
-rwxr-xr-x 1 root root 970 Aug 11 13:49 /usr/lib/libevent_core.la
lrwxrwxrwx 1 root root 26 Aug 11 13:49 /usr/lib/libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx 1 root root 27 Aug 11 13:49 /usr/lib/libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 404852 Aug 11 13:49 /usr/lib/libevent_extra-2.0.so.5.1.9
-rw-r--r-- 1 root root 593392 Aug 11 13:49 /usr/lib/libevent_extra.a
-rwxr-xr-x 1 root root 977 Aug 11 13:49 /usr/lib/libevent_extra.la
lrwxrwxrwx 1 root root 27 Aug 11 13:49 /usr/lib/libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 935 Aug 11 13:49 /usr/lib/libevent.la
lrwxrwxrwx 1 root root 30 Aug 11 13:49 /usr/lib/libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x 1 root root 18430 Aug 11 13:49 /usr/lib/libevent_pthreads-2.0.so.5.1.9
-rw-r--r-- 1 root root 18670 Aug 11 13:49 /usr/lib/libevent_pthreads.a
-rwxr-xr-x 1 root root 998 Aug 11 13:49 /usr/lib/libevent_pthreads.la
lrwxrwxrwx 1 root root 30 Aug 11 13:49 /usr/lib/libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx 1 root root 21 Aug 11 13:49 /usr/lib/libevent.so -> libevent-2.0.so.5.1.9
[root@master libevent-2.0.21-stable]# cd ..
2、安装memcached
[root@master src]# tar zxvf memcached-1.4.20.tar.gz
[root@master src]# cd memcached-1.4.20
[root@master memcached-1.4.20]# ./configure --with-libevent=/usr
[root@master memcached-1.4.20]# wget ?orig
[root@master memcached-1.4.20]# make
[root@master memcached-1.4.20]# make install
[root@master memcached-1.4.20]# cd ..
[root@master src]# ll /usr/local/bin/memcached # 安装完成后会把memcached 放到 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 341907 Aug 11 13:52 /usr/local/bin/memcached
注意:如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径
三、repcached安装
方式一:使用repcached版本
[root@master src]# wget
[root@master src]# tar zxf memcached-1.2.8-repcached-2.2.tar.gz
[root@master src]# cd memcached-1.2.8-repcached-2.2
方式二:使用patch版本
[root@master memcached-1.2.8-repcached-2.2]# wget
[root@master memcached-1.2.8-repcached-2.2]# gzip -cd ../repcached-2.2-1.2.8.patch.gz | patch -p1
[root@master memcached-1.2.8-repcached-2.2]# ./configure --enable-replication
[root@master memcached-1.2.8-repcached-2.2]# make
[root@master memcached-1.2.8-repcached-2.2]# make install
[root@master memcached-1.2.8-repcached-2.2]# cd ..
以上操作,我们需要针对主节点和备节点都操作!这里我仅仅以主节点的部署为例!切记!
四、启动配置
1、启动master
[root@master ~]# memcached -v -d -p 11211 -l 192.168.0.102 -u root -P /tmp/memcached1.pid
[root@master ~]# replication: listen
[root@master ~]# replication: accept
2、启动salve
[root@slave src]# memcached -v -d -p 11211 -l 192.168.0.103 -u root -x 192.168.0.102 -P /tmp/memcached1.pid
[root@slave src]# replication: connect (peer=192.168.0.102:11212)
replication: marugoto copying
replication: start
[root@slave src]#
3、回到master节点
[root@master ~]# replication: accept # 启动正常后,master 将 accept
五、测试
由于我们主节点和从节点的memcached服务都骑起来了,并且监听也都正常,所以以下的测试操作全部放到master节点进行。
[root@master ~]# telnet 192.168.0.102 11211 # 连接到我们主节点,添加一个记录
Trying 192.168.0.102...
Connected to 192.168.0.102.
Escape character is '^]'.
set key 0 0 6
sunsky
STORED
quit
Connection closed by foreign host.
[root@master ~]# telnet 192.168.0.103 11211 # 连接到我们的从节点,查看主节点的记录是否同步过来
Trying 192.168.0.103...
Connected to 192.168.0.103.
Escape character is '^]'.
get key
VALUE key 0 6
sunsky
END
quit
Connection closed by foreign host.
[root@master ~]# pkill memcached # 现在,杀掉我们主节点的memcached进程
replication: cleanup start
replication: close
replication: cleanup complete
[root@slave src]# replication: close # 备节点此时变为监听状态,即变成了主节点
replication: listen
[root@master ~]# telnet 192.168.0.103 11211 # 查看从节点上面的数据是否还存在
Trying 192.168.0.103...
Connected to 192.168.0.103.
Escape character is '^]'.
get key
VALUE key 0 6
sunsky
END
quit
Connection closed by foreign host.