使用 Supervisord 管理 mongos 进程

Using Supervisord to manage mongos process

背景

我正在尝试在崩溃或重启的情况下为 mongodb 分片设置中使用的 mongos 进程自动重启。

案例 1:使用直接命令,使用 mongod user

supervisord 配置

[program:mongos_router]
command=/usr/bin/mongos -f /etc/mongos.conf --pidfilepath=/var/run/mongodb/mongos.pid
user=mongod
autostart=true
autorestart=true
startretries=10

结果

监督日志

INFO spawned: 'mongos_router' with pid 19535
INFO exited: mongos_router (exit status 0; not expected)
INFO gave up: mongos_router entered FATAL state, too many start retries too quickly    

mongodb 日志

  2018-05-01T21:08:23.745+0000 I SHARDING [Balancer] balancer id: ip-address:27017 started
  2018-05-01T21:08:23.745+0000 E NETWORK  [mongosMain] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
  2018-05-01T21:08:23.745+0000 E NETWORK  [mongosMain]   addr already in use
  2018-05-01T21:08:23.745+0000 I -        [mongosMain] Invariant failure inShutdown() src/mongo/db/auth/user_cache_invalidator_job.cpp 114
  2018-05-01T21:08:23.745+0000 I -        [mongosMain] 

  ***aborting after invariant() failure


  2018-05-01T21:08:23.748+0000 F -        [mongosMain] Got signal: 6 (Aborted).

进程可见 运行ning。但是如果被杀死不会自动重启。

案例 2:使用初始化脚本

此处场景的细微变化是某些 ulimit 命令,pid 文件的创建将以 root 身份完成,然后实际进程应以 mongod 用户身份启动。

mongos 脚本

start()
{
  # Make sure the default pidfile directory exists
  if [ ! -d $PID_PATH ]; then
    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR

  fi

  # Make sure the pidfile does not exist
  if [ -f $PID_FILE ]; then
    echo "Error starting mongos. $PID_FILE exists."
    RETVAL=1
    return
  fi

  ulimit -f unlimited
  ulimit -t unlimited
  ulimit -v unlimited
  ulimit -n 64000
  ulimit -m unlimited
  ulimit -u 64000
  ulimit -l unlimited

  echo -n $"Starting mongos: "
  #daemon --user "$MONGO_USER" --pidfile $PID_FILE $MONGO_BIN $OPTIONS --pidfilepath=$PID_FILE
  #su $MONGO_USER -c "$MONGO_BIN -f $CONFIGFILE --pidfilepath=$PID_FILE  >> /home/mav/startup_log"
  su - mongod -c "/usr/bin/mongos -f /etc/mongos.conf --pidfilepath=/var/run/mongodb/mongos.pid"

  RETVAL=$?
  echo -n  "Return value : "$RETVAL
  echo
  [ $RETVAL -eq 0 ] && touch $MONGO_LOCK_FILE
}

daemon comman 代表原始脚本,但是在 supervisord 下守护进程是不合逻辑的,所以使用 command 来 运行 前台的进程(?)

supervisord 配置

[program:mongos_router_script]
command=/etc/init.d/mongos start
user=root
autostart=true
autorestart=true
startretries=10

结果

监督日志

INFO spawned: 'mongos_router_script' with pid 20367     
INFO exited:  mongos_router_script (exit status 1; not expected)
INFO gave up: mongos_router_script entered FATAL state, too many start retries too quickly

mongodb 日志

Nothing indicating error, normal logs

进程可见 运行ning。但是如果被杀死不会自动重启。

问题

如何在 supervisord 下为 运行ning mongos 正确配置脚本/无脚本选项?

编辑 1

修改命令

sudo su -c "/usr/bin/mongos -f /etc/mongos.conf --pidfilepath=/var/run/mongodb/mongos.pid" -s /bin/bash mongod`

如果 运行 单独在命令行以及脚本的一部分,但不适用于 supervisord

编辑 2

mongos 的配置文件中添加了以下选项,以强制其在前台 运行

processManagement:
    fork: false # fork and run in background

现在命令行和脚本正确 运行 它在前台,但 supervisord 无法启动它。同时从命令行或脚本

运行 时会出现 3 个进程
root   sudo su -c /usr/bin/mongos -f /etc/mongos.conf --pidfilepath=/var/run/mongodb/mongos.pid -s /bin/bash mongod
root   su -c /usr/bin/mongos -f /etc/mongos.conf --pidfilepath=/var/run/mongodb/mongos.pid -s /bin/bash mongod
mongod /usr/bin/mongos -f /etc/mongos.conf --pidfilepath=/var/run/mongodb/mongos.pid

编辑 3

使用以下 supervisord 配置一切正常。但是我想尽可能地尝试执行脚本来设置 ulimit

[program:mongos_router]
 command=/usr/bin/mongos -f /etc/mongos.conf --pidfilepath=/var/run/mongodb/mongos.pid
 user=mongod
 autostart=true
 autorestart=true
 startretries=10
 numprocs=1

对于前台的 mongos 运行 设置以下选项

#how the process runs
processManagement:
    fork: false  # fork and run in background

使用 supervisord.conf 设置和以上 supervisord.conf 设置,mongos 将启动并在 supervisord 控制下