使用Supervisord管理mongos进程

时间:2018-05-01 23:18:16

标签: mongodb sharding supervisord autostart

背景

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

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

supervisord config

[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

结果

supervisord log

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 log

  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).

正在运行流程。但是如果杀死不会自动重启。

案例2 :使用init脚本

这里的情况略有变化,一些ulimit命令,pid文件的创建将以root身份完成,然后实际流程应以mongod用户身份启动。

mongos script

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下的守护进程不合逻辑,所以使用命令在前台运行进程(?)

supervisord config

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

结果

supervisord log

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 log

Nothing indicating error, normal logs

正在运行流程。但是如果杀死不会自动重启。

问题

如何在supervisord下正确配置脚本/无脚本选项以运行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

1 个答案:

答案 0 :(得分:0)

要让mongos在前台运行,请设置以下选项

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

使用上述supervisord.conf设置,mongos将在supervisord控件

下启动