如何:安装Memcached与repcached“内置服务器端复制”在Debian Lenny

如何:使用repcached安装Memcached

如何在Debian Lenny上安装Memcached与repcached“内置服务器端复制”

人们可能知道memcached( http://memcached.org/ )及其基于高性能名称的内存对象缓存界面。 其主要目的是在多节点环境中提供易于使用的分布式缓存引擎。 你有没有想让memcached处理复制?

如果您想添加高可用性功能,建议让客户端(-library)在所有节点上处理数据的复制。 假设使用memcached作为php会话的存储后端配置如下:

# configuration for php memcache module
extension=memcache.so
session.save_handler = memcache
session.save_path = "tcp://192.168.168.61:11211?persistent=1,tcp://192.168.168.62:11211?persistent=1"

现在会话通过两个节点分配(不复制)。 一旦节点关闭,您将会丢失所有数据。 只要您只使用memcached作为性能增强器和经销商的一些“可缓存”数据,不应该受伤:丢失的数据像创建缓存条目一样填满。

要复制会话数据,您需要使用自定义会话处理程序,连接到其自身的每个节点...以进行复制。 相同的,如果你想编写一些自己的缓存对象到你心爱的memcached。 您的应用程序仍然需要知道要写入的所有节点。

通过让memcached将其作为repcached进行工作,可以轻松复制

这些问题可能会混淆开发人员只想关注其应用程序,并且不希望关心HA,负载均衡和可伸缩性。 使用repcached至少可以轻松,透明地安装2节点HA-Solution。

一个简单的补丁为您的memcached添加了精湛的master-master复制。 现在你只需要连接到本地的memcached,而不是自己处理复制。

因此,您的应用程序可能会保留转储和简单,使用您本地的POSIX文件系统(可能由DRBD,GlusterFS等复制)您的本地数据库(您猜测它:“它自己处理复制”)并连接到lokal您的“memcached-cluster”的实例。 该设置为简单易用的HA可扩展性打开了一些更简单和透明的选项。

安装,配置和测试2节点集群

首先抓住repcached补丁的副本,甚至从http://repcached.lab.klab.org/下载预修补的memcached-source。 对libevent有依赖性,在安装之前可能需要解决这个问题。

root@ha-01 ~ # apt-get install libevent-dev

现在解压缩并cd进入目录:

root@ha-01 ~ # tar xvf memcached-1.2.8-repcached-2.2.tar
root@ha-01 ~ # cd memcached-1.2.8-repcached-2.2/

配置--enable-repcached选项。

root@ha-01 ~/memcached-1.2.8-repcached-2.2 # ./configure --enable-replication
root@ha-01 ~/memcached-1.2.8-repcached-2.2 # make
root@ha-01 ~/memcached-1.2.8-repcached-2.2 # make install

不要担心以前安装的memcached.dep包。 这个手动安装将仅在/ usr / local / bin / memcached中安装一个二进制文件,让您的“原始”memcached不会触及/ usr / bin / memcached

与debian软件包相反,您可能希望通过在/ etc / default / memcachedrep (rep =>'replication')中使用简单的默认配置来提供命令行选项,即:

默认配置

## extra commandline options to start memcached in replicated mode
# -x < ip_addr > hostname or IP address of the master replication server
# -X < num > TCP port number of the master (default: 11212)
DAEMON_ARGS="-m 64 -p 11211 -u root -P /var/run/memcachedrep.pid -d -x 192.168.168.2"

通过使用和修改debian骨架,可以很容易地设置init脚本:

init-skript

#! /bin/sh
### BEGIN INIT INFO
# Provides:             memcached
# Required-Start:       $syslog
# Required-Stop:        $syslog
# Should-Start:         $local_fs
# Should-Stop:          $local_fs
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    memcached - Memory caching daemon replicated
# Description:          memcached - Memory caching daemon replicated
### END INIT INFO
# Author: Marcus Spiegel <marcus.spiegel@gmail.com>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="memcachedrep"
NAME=memcached
DAEMON=/usr/local/bin/$NAME
DAEMON_ARGS="--options args"
PIDFILE=/var/run/memcachedrep.pid
SCRIPTNAME=/etc/init.d/$DESC
VERBOSE="yes"
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$DESC ] && . /etc/default/$DESC
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
		|| return 1
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
		$DAEMON_ARGS \
		|| return 2
	# Add code here, if necessary, that waits for the process to be ready
	# to handle requests from services started subsequently which depend
	# on this one.  As a last resort, sleep for some time.
}
#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
	# Wait for children to finish too if this is a daemon that forks
	# and if the daemon is only ever run from this initscript.
	# If the above conditions are not satisfied then add some other code
	# that waits for the process to drop all resources that could be
	# needed by services started subsequently.  A last resort is to
	# sleep for some time.
	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
	[ "$?" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	rm -f $PIDFILE
	return "$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
	start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
	return 0
}
case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  #reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	#log_daemon_msg "Reloading $DESC" "$NAME"
	#do_reload
	#log_end_msg $?
	#;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac
:

测试

在两个节点上设置repcached之后,你应该可以测试出来。 您可以连接每个telnet的每个节点上的memcached:

root@ha-01 ~ # telnet 127.0.0.1 11211

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

现在发出一个set命令来在一个节点上写入一些测试值:

set foo 0 0 3
bar
STORED

移动到另一个节点,连接并尝试阅读:

root@ha-02 ~ # telnet 127.0.0.1 11211

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get foo
VALUE foo 0 3
bar
END

尝试反之亦然,玩得开心! 最后,在将更多的值推入内存之后,最后将其中一个节点拆下来。 运行节点仍然保持响应_all_数据 - 优秀。

真正有趣的部分是,接下来,一旦您再次启动不可用节点,它将从其他主机获取所有“丢失”数据。 继续尝试自己。 如果您现在拆除另一个节点并将其重新备份,同样会发生。 即使在另一个节点关闭时设置为一个节点的数据也将在启动后添加到即将到来的节点。

此设置仅能够进行2节点复制设置。 想想建立一个4节点集群,其中有一种raid10安装,其中复制和分发被组合在一起。 那么至少在这个2节点的HA-Setup repcached表现就像一个魅力。

我个人想尝试下一个3节点的圈子:)(可能与心跳一起关闭那个圈子中的任何缺陷)。