在 EC2 中的 Amazon Linux 实例上运行 beanstalkd 的最佳方法是什么?Beanstalkd 不存在于 yum 存储库中,并且 beanstalkd 附带的 upstart 脚本似乎无法立即使用。我想知道是否有人已经解决了这个问题,或者我是否需要去尝试使用自定义 EC2 用户数据脚本 + beanstalkd upstart 脚本。
答案1
我最终编写了一个在创建 ec2 实例时运行的用户数据脚本。该脚本下载 beanstalkd 的最新源代码,编译并安装二进制文件。该脚本使用经过修改的 upstart 脚本来控制 beanstalkd 进程的生命周期。
这是用户数据脚本:
#!/bin/bash -ex
set -e -x
# Log output
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
# Update yum first
yum groupinstall "Development Tools" -y
yum update -y
# Download /etc/default/beanstalkd
curl -J -O https://gist.github.com/phaitour/6482469/download
tar -zxvf *.tar.gz
/bin/cp -f gist*/beanstalkd /etc/default/
ln -s /etc/default/beanstalkd /etc/beanstalkd.conf
rm -rf gist*
# Download /etc/init/beanstalkd.conf
curl -J -O https://gist.github.com/phaitour/6482467/download
tar -zxvf *.tar.gz
/bin/cp -f gist*/beanstalkd.conf /etc/init/
rm -rf gist*
# Create beanstalkd user
/usr/sbin/useradd -c "beanstalk user" -s /bin/false -r -m -d /var/lib/beanstalkd/ beanstalkd
# Compile latest version of beanstalkd
wget https://github.com/kr/beanstalkd/archive/v1.9.tar.gz
tar -zxvf v1.9.tar.gz
cd beanstalkd-1.9
make
mv beanstalkd /usr/bin/beanstalkd-1.9
cd /usr/bin
rm -rf beanstalkd
ln -s beanstalkd-1.9 beanstalkd
# Start beanstalks with upstart
initctl reload-configuration
initctl start beanstalkd
它从 gist 下载的两个脚本是 upstart beanstalkd 脚本和一个设置运行程序时使用的选项的配置文件。
这是 upstart beanstalkd.conf 脚本:
description "simple, fast work queue"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 5 2
expect fork
script
. /etc/default/beanstalkd
exec su -c "exec /usr/bin/beanstalkd $BEANSTALKD_OPTIONS"
end script
这是复制到 /etc/default/beanstalkd 的配置文件:
# Set some constants to be used to construct BEANSTALKD_OPTIONS
BEANSTALKD_ADDR=0.0.0.0
BEANSTALKD_PORT=11300
BEANSTALKD_USER=beanstalkd
BEANSTALKD_BINLOG_DIR=/var/lib/beanstalkd
BEANSTALKD_BINLOG_FSYNC_PERIOD=0
# Create the actual BEANSTALKD_OPTIONS string
# Copied from https://gist.github.com/shiki/515422
BEANSTALKD_OPTIONS="-l ${BEANSTALKD_ADDR} -p ${BEANSTALKD_PORT} -u ${BEANSTALKD_USER}"
if [ "${BEANSTALKD_MAX_JOB_SIZE}" != "" ]; then
BEANSTALKD_OPTIONS="${BEANSTALKD_OPTIONS} -z ${BEANSTALKD_MAX_JOB_SIZE}"
fi
if [ "${BEANSTALKD_BINLOG_DIR}" != "" ]; then
if [ ! -d "${BEANSTALKD_BINLOG_DIR}" ]; then
echo "Creating binlog directory (${BEANSTALKD_BINLOG_DIR})"
mkdir -p ${BEANSTALKD_BINLOG_DIR} && chown ${BEANSTALKD_USER}:${BEANSTALKD_USER} ${BEANSTALKD_BINLOG_DIR}
fi
BEANSTALKD_OPTIONS="${BEANSTALKD_OPTIONS} -b ${BEANSTALKD_BINLOG_DIR}"
if [ "${BEANSTALKD_BINLOG_FSYNC_PERIOD}" != "" ]; then
BEANSTALKD_OPTIONS="${BEANSTALKD_OPTIONS} -f ${BEANSTALKD_BINLOG_FSYNC_PERIOD}"
else
BEANSTALKD_OPTIONS="${BEANSTALKD_OPTIONS} -F"
fi
if [ "${BEANSTALKD_BINLOG_SIZE}" != "" ]; then
BEANSTALKD_OPTIONS="${BEANSTALKD_OPTIONS} -s ${BEANSTALKD_BINLOG_SIZE}"
fi
fi