下記の記事のように Pacemaker で systemd のユニットをリソースに設定できるので、
下記の記事のような systemd.timer のユニットもできるかと思って試しました。
なお、pm01 と pm02 という2つのノードであらかじめクラスタにしています。
試した環境は下記の通り。
centos-release-7-2.1511.el7.centos.2.10.x86_6
systemd-219-19.el7_2.11.x86_64
pacemaker-1.1.13-10.el7_2.2.x86_64
corosync-2.3.4-7.el7_2.3.x86_64
resource-agents-3.9.5-54.el7_2.10.x86_64
ダメだった
ダメでした・・・
[root@pm01 ~]# pcs resource create orebatch systemd:orebatch.timer --force
[root@pm01 ~]# crm_mon -1f
...(snip)...
Failed Actions:
* orebatch_start_0 on pm01 'not installed' (5): call=29, status=Not installed, exitreason='none',
last-rc-change='Tue Jul 26 04:18:00 2016', queued=0ms, exec=138ms
[root@pm01 ~]# cat /var/log/cluster/corosync.log | grep orebatch.timer
Jul 26 04:18:00 [21690] pm01 lrmd: info: pcmk_dbus_find_error: start error 'org.freedesktop.systemd1.LoadFailed': Unit orebatch.timer.service failed to load: No such file or directory.
Jul 26 04:18:00 [21690] pm01 lrmd: info: pcmk_dbus_find_error: stop error 'org.freedesktop.systemd1.NoSuchUnit': Unit orebatch.timer.service not loaded.
ユニット名の拡張子が .service
以外だと自動で .service
が付与されて orebatch.timer.service
などとなってしまうようでした。
service から timer を Requires する
service のユニットなら Pacemaker のリソースにできるので service のユニットと連動して timer のユニットを開始/停止するようにしてみました。
service ユニットを作ります。
/etc/systemd/system/oreservice.service
[Unit]
Description = oreservice
# ↓ の timer と連動するために Requires を指定する
Requires = orebatch.timer
[Service]
Type = oneshot
# 実際の処理は必要ないので /bin/true で RemainAfterExit も指定
ExecStart = /bin/true
RemainAfterExit = yes
[Install]
WantedBy = multi-user.target
timer ユニットを作ります。
/etc/systemd/system/orebatch.timer
[Unit]
Description = orebatch timer
# ↑の service と連動するために PartOf を指定する
PartOf = oreservice.service
[Timer]
OnBootSec = 1min
OnUnitActiveSec = 5sec
AccuracySec = 1sec
[Install]
WantedBy = timers.target
timer から起動する service を作ります。
/etc/systemd/system/orebatch.service
[Unit]
Description = orebatch
[Service]
Type = oneshot
ExecStart = /bin/sh -c 'echo hello orebatch'
[Install]
WantedBy = multi-user.target
リロードします。
[root@pm01 ~]# systemctl daemon-reload
[root@pm02 ~]# systemctl daemon-reload
Pacemaker のリソースとしてします・・が、普通に追加するとなぜかエラーになりました(pcs resource list
にはあるんですが・・)。
[root@pm01 ~]# pcs resource create oreservice systemd:oreservice
Error: Unable to create resource 'systemd:oreservice', it is not installed on this system (use --force to override)
--force
を付けて追加します。
[root@pm01 ~]# pcs resource create oreservice systemd:oreservice --force
追加できたようです。pm02 で開始しています。
[root@pm01 ~]# pcs resource show
oreservice (systemd:oreservice): Started pm02
pm02 でバッチが実行されていることを確認します。
[root@pm02 ~]# systemctl is-active orebatch.timer
active
[root@pm02 ~]# journalctl -f -u orebatch.service
Jul 26 04:09:36 pm02 systemd[1]: Starting orebatch...
Jul 26 04:09:36 pm02 sh[16939]: hello orebatch
Jul 26 04:09:36 pm02 systemd[1]: Started orebatch.
Jul 26 04:09:42 pm02 systemd[1]: Starting orebatch...
Jul 26 04:09:42 pm02 sh[16941]: hello orebatch
Jul 26 04:09:42 pm02 systemd[1]: Started orebatch.
Jul 26 04:09:48 pm02 systemd[1]: Starting orebatch...
Jul 26 04:09:48 pm02 sh[16943]: hello orebatch
Jul 26 04:09:48 pm02 systemd[1]: Started orebatch.
pm01 では実行されていません。
[root@pm01 ~]# systemctl is-active orebatch.timer
unknown
pm02 を standby にしてフェイルオーバーさせます。
[root@pm02 ~]# pcs cluster standby
リソースは pm01 に移りました。
[root@pm02 ~]# pcs resource show
oreservice (systemd:oreservice): Started pm01
pm02 では停止しています。
[root@pm02 ~]# systemctl is-active orebatch.timer
unknown
pm01 でバッチが実行されています。
[root@pm01 ~]# systemctl is-active orebatch.timer
active
[root@pm01 ~]# journalctl -f -u orebatch.service
Jul 26 04:10:29 pm01 systemd[1]: Starting orebatch...
Jul 26 04:10:29 pm01 sh[22072]: hello orebatch
Jul 26 04:10:29 pm01 systemd[1]: Started orebatch.
Jul 26 04:10:34 pm01 systemd[1]: Starting orebatch...
Jul 26 04:10:34 pm01 sh[22074]: hello orebatch
Jul 26 04:10:34 pm01 systemd[1]: Started orebatch.
Jul 26 04:10:40 pm01 systemd[1]: Starting orebatch...
Jul 26 04:10:40 pm01 sh[22076]: hello orebatch
Jul 26 04:10:40 pm01 systemd[1]: Started orebatch.