[新機能] Amazon EC2でハイバネーションが可能になりました #reinvent

2017.12.02

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

はじめに

AWSチームのすずきです。

re:Invent2017期間中、スポットインスタンスのアップデートとして、インスタンスハイバネーション、 稼働中のOSのメモリ情報をEBSに退避させ、メモリ状態を維持したままEC2インスタンスを停止、再開する機能のリリースがありました。

メモリに多くの状態を保持するワークロードで、インスタンスの一時中断させる際に有効な手段として紹介されているハイバネーション。 今回は、CLI(pm-hibernate)を利用したハイバネーション動作を試す機会がありましたので、紹介させて頂きます。

環境

  • リージョン : 東京(ap-northeast-1)
  • AMI : Amazon Linux AMI 2017.09.1 (HVM), SSD Volume Type (ami-da9e2cbc)
  • インスタンスタイプ : c4.large
  • ハイバネーションが利用できるインスタンスは、2017年12月現在「C3」「C4」「M4」「R3」「R4」、122GiB以下のメモリである必要があります。

  • EBS: 12GiB

  • ハイバネーションで利用される退避先として、デフォルトではSwap(/swap)が利用されます。
  • メモリ4GBの「c4.large」に併せ、EBS AMI標準の8GiBから4GiBを増量しました。
  • 上位(xlarge、2xlarge)インスタンスでハイバネーション利用する場合、EBSは適宜拡張してご利用ください。

設定

  • 2017.09.1 以降のAmazon Linux AMIでは、ハイバネーションに必要なエージェント(hibagent)はプリインストールされています。
  • 今回はサービスの有効化、起動のみで利用できました。
$ cat /etc/system-release
Amazon Linux AMI release 2017.09

$ chkconfig --list| grep hibagent
hibagent 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  • 「hibagent」エージェントの自動起動設定と、手動起動を行います。
sudo /sbin/chkconfig hibagent on
sudo service hibagent start
  • 「hibagent」エージェントの初回起動時、ハイバネーションの退避先となるスワップ領域が「/swap」に生成されます。
$ ls -lah /swap
-rw------- 1 root root 4.0G 12月 2 07:12 /swap

動作確認

  • AmazonLinuxに付属する「pm-utils」パッケージに含まれる「pm-hibernate」コマンドで、ハイバネーションが可能です。
sudo pm-hibernate
  • ハイバネーション実施後、EC2インスタンスのステータスは「Stopped」となります。

  • AWSコンソール上などから「開始」操作を行うと、ハイバネーションからの復帰が開始されます。

  • ハイバネーションしたEC2インスタンス「07:40:00」に再開させた後に出力されたシスログ(/var/log/messages)は以下の通り、約20秒で再開した事が確認できました。
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.335717] PM: Syncing filesystems ...
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.337432] done.
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.338789] Freezing user space processes ... (elapsed 0.001 seconds) done.
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.343766] PM: Preallocating image memory... done (allocated 76032 pages)
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.405683] PM: Allocated 304128 kbytes in 0.05 seconds (6082.56 MB/s)
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.408701] Freezing remaining freezable tasks ... (elapsed 0.001 seconds) done.
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.413902] Suspending console(s) (use no_console_suspend to debug)
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.530140] PM: freeze of devices complete after 113.381 msecs
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.530378] PM: late freeze of devices complete after 0.236 msecs
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.532767] PM: noirq freeze of devices complete after 2.387 msecs
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.532770] ACPI: Preparing to enter system sleep state S4
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.532835] PM: Saving platform NVS memory
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.532836] Disabling non-boot CPUs ...
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.533064] Broke affinity for irq 1
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.534158] smpboot: CPU 1 is now offline
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.534999] PM: Creating hibernation image:
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] PM: Need to copy 74738 pages
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] PM: Restoring platform NVS memory
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] xen:grant_table: Grant tables using version 1 layout
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] Suspended for 135.105 seconds
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] Enabling non-boot CPUs ...
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] installing Xen timer for CPU 1
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] x86: Booting SMP configuration:
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] smpboot: Booting Node 0 Processor 1 APIC 0x1
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] cache: parent cpu1 should not be sleeping
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] cpu 1 spinlock event irq 59
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] CPU1 is up
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.538154] ACPI: Waking up from system sleep state S4
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.540093] PM: noirq restore of devices complete after 2.804 msecs
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.540244] PM: early restore of devices complete after 0.103 msecs
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.645931] ixgbevf 0000:00:03.0: Multiqueue Enabled: Rx Queue count = 2, Tx Queue count = 2
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.673411] PM: restore of devices complete after 120.676 msecs
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.676574] ixgbevf 0000:00:03.0: NIC Link is Up 10 Gbps
Dec 2 07:40:16 ip-172-31-9-130 kernel: [ 1421.725689] Restarting tasks ... done.

まとめ

インスタンスハイバネーションを利用する事で、メモリ状態を保持したまま、EC2インスタンスの一時中止させる事が 可能になりました。

スポットインスタンスの需要高騰に伴う一時停止は許容できるが、 コールドスタート、OS、ミドルウェアの初期化処理に多くの時間を必要とするシステムでは、 特に嬉しいアップデートと思われます。

また、スポットインスタンス用として提供開始されたEC2のハイバネーション。 2017年10月以降、秒単位で計算されるようになったEC2費用を抑制する手段として利用できる可能性もあると思われます。 今後の適用範囲のアップデートや追加検証などの機会があれば、改めて紹介させて頂きたいと思います。