如何安装Repcached(Memcached复制)用于2个节点的高可用性在Ubuntu 11.04上

如何在Ubuntu 11.04上为高可用性2个节点安装Repcached(Memcached Replication)

这是一个基于MarcusSpiegel 在这里发现的简短教程。 这将如何覆盖在Ubuntu服务器11.04上安装Repcached的缺少部件。 这是一个复制/粘贴友好的教程,所以使用油灰将使这更容易。

这将介绍如何安装和构建memcached与复制,创建一个启动脚本,并配置PHP使用memcache进行会话,而不是将它们存储为文件。 本教程假设您已经设置了2个服务器来复制信息。

我的设置

在这个演示中,我将在2台主机上设置repcache,他们将同时安装lighttpd和repcache。

  • Web服务器1: server1.example.com ,IP地址: 10.10.20.10 ; 这将被称为server1
  • Web Server 2: server2.example.com ,IP地址: 10.10.20.11 ; 这将被称为server2

2.安装repcache

首先,您需要访问http://repcached.lab.klab.org/并下载最新版本(最新版本:2.2-1.2.8)。 下载tar文件后,我们需要安装一些额外的包。 在两个节点上执行此操作。

apt-get install libevent-dev g++ make

从这里我们可以继续安装。

tar xvf memcached-1.2.8-repcached-2.2.tar
cd memcached-1.2.8-repcached-2.2/
./configure --enable-replication
make
make install

3.配置repcache

Repcache现在安装,二进制位置是/ usr / local / bin / memcached 。 从这里我们将创建配置文件和启动脚本。 配置文件和init脚本是在MarcusSpiegel的howto中找到 。 在两个节点上执行此操作。

首先用vi打开文件:

vi /etc/memcachedrep

然后输入以下内容,请记住更改IP地址。 此IP将是您要复制到的其他服务器。

## 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 10.10.20.11"

保存文件后,我们需要创建init脚本。 再次,这是MarcusSpiegel的howto的配置位置的一些细微变化。

vi /etc/init.d/memcachedrep
#! /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: Michael 
  
   
#
# 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/$DESC ] && . /etc/$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
:
  

保存,然后键入以下内容。

chmod +x /etc/init.d/memcacherep
update-rc.d memcachedrep defaults

这将确保每次重新启动服务器时都会启动重新缓存。

4.测试

在我们继续之前,我们将测试2个节点正在复制。

server1:

telnet 127.0.0.1 11211

然后输入

set foo 0 0 3
bar

你应该看到STORED一词出现在它下面。 输入

quit

它将返回到控制台。 转到其他服务器并输入以下内容。

server2:

telnet 127.0.0.1 11211

然后输入

get foo

它应该返回在第一个服务器上输入的值。 如果有复制正在工作。

5.设置PHP

这里我将向您展示如何设置PHP以使用memcache。 因为我使用lighttpd目录可能不同于那些使用apache2或nginx。 在控制台中键入以下内容,然后重复使用辅助服务器。

vi /etc/php5/cgi/php.ini

搜索文件,直到找到以下内容:

[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = files

你将需要改变这些:

[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
extension = memcache.so
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"

一旦这些保存简单地输入(记住将其更改为您用于托管的软件):

/etc/init.d/lighttpd force-reload

或者只需重新启动两个服务器。 现在,您可以通过在其中创建一个会话并从另一个读取该会话来测试该PHP正确使用它们。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏