Amazon-Ec2

執行 beanstalkd 的 Amazon Linux 實例

  • September 9, 2013

在 EC2 中的 Amazon Linux 實例上執行 beanstalkd 的最佳方式是什麼?yum 儲存庫中不存在 Beanstalkd,並且 beanstalkd 中包含的 upstart 腳本似乎無法開箱即用。我想知道是否有人已經解決了這個問題,或者我是否需要使用自定義 EC2 使用者數據腳本 + beanstalkd upstart 腳本來解決問題。

我最終編寫了一個在創建 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 下載的兩個腳本是新貴 beanstalkd 腳本和一個配置文件,該文件設置執行程序時使用的選項。

這是新貴 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

引用自:https://serverfault.com/questions/537342