LoginSignup
53

More than 5 years have passed since last update.

consulのinitスクリプトとsystemdユニットファイルを書いてみた

Last updated at Posted at 2015-04-16

consulをサービスとして動かすべく、CentOS 6用のinitスクリプトとCentOS 7用のsystemdのユニットファイルを書いてみた。

前提条件

consulの実行ファイルはrootユーザでも一般ユーザでもパスが通っている /usr/local/sbin に配置することにした。
(初期設定でCentOS 6だと共通でパスが通っているディレクトリがもう少しあるがCentOS 7ではここくらいしかない)

# wget https://dl.bintray.com/mitchellh/consul/0.5.0_linux_amd64.zip
# unzip 0.5.0_linux_amd64.zip -d /usr/local/sbin

設定ファイルを配置するディレクトリを作っておく。

# mkdir /etc/consul.d

任意で次のような /etc/sysconfig/consul を作っておくとOPTIONS内に /etc/consul.d 配下の設定ファイルで指定したもの以外の起動オプションを書くことができる。
GOMAXPROCSはプログラムが利用可能な最大スレッド数だが、GOMAXPROCS=1とするとconsul infoとかで警告が出る(1はデフォルト値なので未定義の場合も警告される)。

/etc/sysconfig/consul
GOMAXPROCS=2
OPTIONS=""

initスクリプト

次のものを使用しようと思ったのだけど、restartすると完全にstopする前にstartが始まって起動に失敗するのでウェイトを入れるなどの修正を行った。
https://gist.github.com/blalor/c325d500818361e28daf

CentOS 6では /etc/init.d/consul に以下を配置する。

/etc/init.d/consul
#!/bin/sh
#
# consul - this script manages the consul agent
#
# chkconfig:   345 95 05
# processname: consul

### BEGIN INIT INFO
# Provides:       consul
# Required-Start: $local_fs $network
# Required-Stop:  $local_fs $network
# Default-Start: 3 4 5
# Default-Stop:  0 1 2 6
# Short-Description: Manage the consul agent
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

exec="/usr/local/sbin/consul"
prog=${exec##*/}

lockfile="/var/lock/subsys/$prog"
pidfile="/var/run/${prog}.pid"
logfile="/var/log/${prog}.log"
sysconfig="/etc/sysconfig/$prog"
confdir="/etc/${prog}.d"

[ -f $sysconfig ] && . $sysconfig

export GOMAXPROCS=${GOMAXPROCS:-2}

start() {
    [ -x $exec ] || exit 5
    [ -d $confdir ] || exit 6

    echo -n $"Starting $prog: "
    touch $logfile $pidfile
    daemon "{ $exec agent $OPTIONS -config-dir=$confdir &>> $logfile & }; echo \$! >| $pidfile"

    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $lockfile
    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p $pidfile $exec -INT 2>> $logfile
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        while :
        do
            ss -pl | grep $prog > /dev/null
            if [ $? -ne 0 ]; then
                break
            fi
            sleep 1
        done
        rm -f $pidfile $lockfile
    fi
    echo
    return $RETVAL
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    killproc -p $pidfile $exec -HUP
    echo
}

force_reload() {
    restart
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload|force-reload)
        rh_status_q || exit 7
        $1
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 7
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac

exit $?

/etc/init.d/consul を配置したらchmod 755 /etc/init.d/consulでアクセス権を755にし、chkconfig --add consulでOS起動時に起動するようにする。
service consul startで起動できる。

なお、service consul stopはSIGINTを発行しているのでconsul leaveと同等、service consul reloadはSIGHUPを発行しているのでconsul reloadと同等である。

GOMAXPROCSは /etc/sysconfig/consul に定義していないなら自動的に2になるようにしている。

systemdのユニットファイル

CentOS 7では次を /etc/systemd/system/consul.service に配置する。
CentOS 7でなくてもsystemdを使っているディストリビューションなら大抵は使えるんじゃないかな。
(Debian系だとEnvironmentFileは /etc/sysconfig/consul ではなく /etc/default/consul の方が良い、とかはあるだろうが)

/etc/systemd/system/consul.service
[Unit]
Description=consul agent
Requires=network-online.target
After=network-online.target

[Service]
EnvironmentFile=-/etc/sysconfig/consul
Environment=GOMAXPROCS=2
Restart=on-failure
ExecStart=/usr/local/sbin/consul agent $OPTIONS -config-dir=/etc/consul.d
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

/etc/systemd/system/consul.service を配置したらsystemctl daemon-reloadで反映し、systemctl enable consulでOS起動時に起動するようにする。
systemctl start consulで起動できる。systemctl stop consulでSIGINT、systemctl reload consulでSIGHUPが発行されるのはinitスクリプトと同じ。

network-online.targetをRequiresとAfterに指定し、サービス起動が行われる前にはすでにネットワークに接続されていることを要求している。

Environmentに定義した変数の値よりEnvironmentFileで指定したファイルに定義した変数の値の方が優先される。よってinitスクリプトと同様、GOMAXPROCSは /etc/sysconfig/consul に定義していないなら自動的に2になる、という動きになる。

initスクリプトの方は /var/log/consul.log に出力がリダイレクトされるようにしてあるが、こちらはjournalctl -u consul.serviceで出力が取得できるので、特に別途ファイルに出力するようにはしていない。

Gistからのダウンロード

consul, consul-template, vaultのinitスクリプトとsystemdユニットファイルをGistに上げておいた。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53