Amazon Linux 2はNTPにChronyとAmazon Time Sync Serviceを使う

2017.12.26

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

2017年の re:invent ではマネージドの時刻同期サービス Amazon Time Sync Service が発表されました。

【速報】マネージドNTP Amazon Time Sync Serviceが登場 #reinvent

最新のAmazon Linuxでは、デフォルトで起動している ntp(パッケージ) のかわりに chrony(パッケージ)をインストールすると、Amazon Time Sync Service を参照する設定ファイルがもれなくついてきます。

その後発表された Amazon Linux 2の時刻同期サービスを確認すると、デフォルトで chrony(パッケージ) が起動しており、Amazon Time Sync Service を参照しているのを確認できました。

確認ログを共有します。

NTP 系サービスの確認

ntpd のプロセスは見当たらず、かわりに chronyd が起動しています。

ps から

$ pgrep -a ntp
$ 
$ pgrep -a chrony
2630 /usr/sbin/chronyd

systemctl から

$ systemctl status ntpd
Unit ntpd.service could not be found.
$ systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2017-12-25 09:45:02 UTC; 3h 33min ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 2640 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 2615 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 2630 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─2630 /usr/sbin/chronyd

Dec 25 09:45:02 localhost systemd[1]: Starting NTP client/server...
Dec 25 09:45:02 localhost chronyd[2630]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SECHASH +SIGND +ASYNCDNS +IPV6 +DEBUG)
Dec 25 09:45:02 localhost systemd[1]: Started NTP client/server.
Dec 25 09:45:08 ip-172-31-16-149.eu-central-1.compute.internal chronyd[2630]: Selected source 169.254.169.123
Dec 25 09:45:35 ip-172-31-16-149.eu-central-1.compute.internal chronyd[2630]: Selected source 178.63.9.110
Dec 25 09:45:37 ip-172-31-16-149.eu-central-1.compute.internal chronyd[2630]: Selected source 178.63.9.110
Dec 25 09:46:15 ip-172-31-16-149.eu-central-1.compute.internal chronyd[2630]: Selected source 169.254.169.123

yum から

$ yum info chrony
Loaded plugins: langpacks, update-motd
Installed Packages
Name        : chrony
Arch        : x86_64
Version     : 3.2
Release     : 1.amzn2.0.1
Size        : 472 k
Repo        : installed
Summary     : An NTP client/server
URL         : https://chrony.tuxfamily.org
License     : GPLv2
Description : A client/server for the Network Time Protocol, this program keeps your
            : computer's clock accurate. It was specially designed to support
            : systems with intermittent internet connections, but it also works well
            : in permanently connected environments. It can use also hardware reference
            : clocks, system real-time clock or manual input as time references.

時刻同期状況の確認

$ chronyc sources -v で chronyd がアクセスしている時刻ソースの状況を確認出来ます。

$ chronyc sources -v
210 Number of sources = 5

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 169.254.169.123               3   7   377    14    +12us[  +25us] +/-  867us
^- static.25.37.46.78.clien>     2  10   377    31    +59us[  +71us] +/-   41ms
^- ntp2.ig-haase.de              2  10   377   212   -505us[ -507us] +/- 8924us
^- ntp01.cc-netzwerk.de          3  10   377   690  -1301us[-1305us] +/-   46ms
^- ntp2.m-online.net             2  10   377   505    +49us[  +44us] +/-   36ms

Amazon Time Sync Service の 169.254.169.123 には current synced を表す * が接頭しています。

テーブル右端にある +12us[ +25us] +/- 867us の3つの数字は順に

  • adjusted offset
  • measured offset
  • estimated error

を表します。

これらの値が小さいほど、タイムソースとよりうまく時刻同期できていることになります。

169.254.169.123 は他のタイムソースに比べて、とりわけ estimated error の値が小さいことを確認できます。

chronyd の設定ファイルの確認

設定ファイルは /etc/chrony.conf にあります。

/etc/chrony.conf

# use the local instance NTP service, if available
server 169.254.169.123 prefer iburst

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool 2.amazon.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

# save data between restarts for fast re-load
dumponexit
dumpdir /var/run/chrony

# use a key to secure communication between chronyc and the daemon
#commandkey key1

一部の設定を解説します。

タイムソース

server 169.254.169.123 prefer iburst
pool 2.amazon.pool.ntp.org iburst

タイムソースに利用するサーバーの指定です

server と pool は個別サーバーかサーバーのプールを指定するかの違いです。

prefer オプションのあるサーバーが優先されます。

iburst オプションがあると、 chronyd デーモン起動後の最初4回の同期が通常よりも短い2秒間隔で実施されます。

slew/step の違い

NTP プロトコルの時刻同期は、大きく分けて以下の2方式があります。

  • slew : 時間の速度を調整してタイムソースの時刻に徐々にに合わせる。過去には遡らない
  • step : タイムソースの時刻に一気に合わせる。過去に遡ることもある。

通常は slew モードで同期を行いますが、時刻が大きくずれている場合は step モードで同期します。その閾値を決めるのが次の設定です。

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

この設定の場合 chronyd を起動後の最初の3回の時刻同期で、オフセットは 1.0 秒よりも大きければ、step モードで同期されます。

ログ出力

chronyd の

  • logdir : ログ出力先
  • log : 出力するログの種類

を制御します。

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

デフォルトでは log がコメントアウトされているため、ログ出力されません。

ログの種類は以下のものがあります。

  • rawmeasurements
  • measurements
  • statistics
  • tracking
  • rtc
  • refclocks
  • tempcomp

詳細はマニュアルを確認してください。

https://chrony.tuxfamily.org/doc/3.2/chrony.conf.html

/etc/chrony.conf

logdir /var/log/chrony
log measurements statistics tracking

としたあと chronyd を再起動($ sudo systemctl restart chronyd.service)すれば、ログ出力は有効になります。

$ ls -l /var/log/chrony/
total 56
-rw-r--r-- 1 chrony chrony 26852 Dec 25 14:11 measurements.log
-rw-r--r-- 1 chrony chrony 18018 Dec 25 14:11 statistics.log
-rw-r--r-- 1 chrony chrony  5280 Dec 25 14:11 tracking.log

まとめ

Amazon Linux 2 では初期状態から Amazon Time Sync Service を利用した高精度な時刻同期がされます。 同サービスのリンクローカルアドレス(169.254.169.123)の恩恵により、インターネット通信が不要なのは大きなメリットですね。

参考

  • https://chrony.tuxfamily.org/doc/3.2/chrony.conf.html
  • http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html
  • https://dev.classmethod.jp/cloud/aws/aws-reinvent-time-sync-service/