Get Hands-on with Amazon EKS - Workshop Event Series
Whether you're taking your first steps with Kubernetes or you're an experienced practitioner looking to sharpen your skills, our Amazon EKS workshop series delivers practical, real-world experience that moves you forward. Learn directly from AWS solutions architects and EKS specialists through hands-on sessions designed to build your confidence with Kubernetes. Register now and start building with Amazon EKS!
RHEL を実行している EC2 インスタンスのコンソールログにユーザーデータ出力を送信する方法を教えてください。
Amazon Elastic Compute Cloud (Amazon EC2) Linux RHEL 7、RHEL 8、または RHEL 9 インスタンスのブートストラップに関してトラブルシューティングを行いたいです。ユーザーデータの呼び出しをログに記録し、その内容をコンソールログに送信しようとしています。
簡単な説明
EC2 インスタンスのブートストラップの問題をトラブルシューティングするには、ユーザーデータの bash スクリプトを変更してすべての出力をリダイレクトします。出力を /var/log/user-data.log および /dev/console にリダイレクトします。スクリプトを実行すると、ユーザーデータの呼び出しログをコンソールで直接確認できます。
注: 次の解決策は、RHEL 7、RHEL 8、RHEL 9 のみを対象としています。Amazon Linux 2、Amazon Linux 2023 の詳細については、「Amazon Linux 1、Amazon Linux 2、または Amazon Linux 2023 で、EC2 インスタンスのコンソールログにユーザーデータ出力を送信する方法を教えてください」を参照してください。
解決策
RHEL を実行している EC2 インスタンスのコンソールログにユーザーデータ出力を送信するには、次の手順を実行します。
-
Amazon EC2 コンソールを開きます。
-
既存のインスタンスに接続するか、新しいインスタンスを起動します。次に、SSH を使用してインスタンスに接続します。
-
/etc/default/grub で GRUB_CMDLINE_LINUX 行を編集し、console=ttyS0,115200n8 console=tty0 をconsole=tty1 console=ttyS0 に変更します。
RHEL 7# cat /etc/default/grub GRUB_TIMEOUT=1 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0 net.ifnames=0 rd.blacklist=nouveau crashkernel=auto" GRUB_DISABLE_RECOVERY="true"RHEL 8
# cat /etc/default/grub GRUB_TIMEOUT=1 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0 net.ifnames=0 rd.blacklist=nouveau nvme_core.io_timeout=4294967295 crashkernel=auto" GRUB_DISABLE_RECOVERY="true" GRUB_ENABLE_BLSCFG=trueRHEL 9
# cat /etc/default/grub GRUB_CMDLINE_LINUX="console=tty1 console=ttyS0 net.ifnames=0 rd.blacklist=nouveau nvme_core.io_timeout=4294967295" GRUB_TIMEOUT=0 GRUB_ENABLE_BLSCFG=true GRUB_DEFAULT=saved -
/boot/grub2/grub.cfg ファイルを再作成するには、次のスクリプトを実行します。
RHEL 7# grub2-mkconfig -o /boot/grub2/grub.cfgGenerating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-1062.1.2.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-1062.1.2.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-026767dbe06a4910a5ce3bd0def903c0 Found initrd image: /boot/initramfs-0-rescue-026767dbe06a4910a5ce3bd0def903c0.img doneRHEL 8
# grub2-mkconfig -o /boot/grub2/grub.cfg Generating grub configuration file ... doneRHEL 9
# grub2-mkconfig -o /boot/grub2/grub.cfg Generating grub configuration file ... Adding boot menu entry for UEFI Firmware Settings ... done -
インスタンスを停止します。
-
(オプション) インスタンスの再起動ごとにユーザーデータを実行するには、MIME マルチパートファイルを使用してユーザーデータスクリプトと cloud-init ディレクティブを設定します。
注: デフォルトでは、ユーザーデータスクリプトと cloud-init ディレクティブは、EC2 インスタンス起動の最初の起動サイクル中にのみ実行されます。 -
ユーザーデータ出力をコンソールにリダイレクトするには、次のコマンドを実行してユーザーデータフィールドを編集します。
Content-Type: multipart/mixed; boundary="//" MIME-Version: 1.0 --// Content-Type: text/cloud-config; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cloud-config.txt" #cloud-config cloud_final_modules: - [scripts-user, always] --// Content-Type: text/x-shellscript; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="userdata.txt" #!/bin/bash -xe exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1 cat /etc/redhat-release echo "Hello from user-data!" --//--注: ユーザーデータ出力をリダイレクトするスクリプトの最初の行は、#!/bin/bash -xe です。上記のスクリプトは cloud-init 設定データであり、インスタンスが起動するたびにシェルスクリプトが実行されます。上記ののコマンドでは、次の行がユーザーデータ出力をリダイレクトします。
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1 -
インスタンスを起動し、コンソールログ出力を確認します。次の例のようなコンソール出力が表示されます。
RHEL 7<13>May 21 03:11:44 user-data: + cat /etc/redhat-release <13>May 21 03:11:44 user-data: Red Hat Enterprise Linux Server release 7.9 (Maipo) <13>May 21 03:11:44 user-data: + echo 'Hello from user-data!' <13>May 21 03:11:44 user-data: Hello from user-data!RHEL 8
<13>May 21 03:11:21 user-data: + cat /etc/redhat-release <13>May 21 03:11:21 user-data: Red Hat Enterprise Linux release 8.6 (Ootpa) <13>May 21 03:11:21 user-data: + echo 'Hello from user-data!' <13>May 21 03:11:21 user-data: Hello from user-data!RHEL 9
<13>May 21 03:12:34 user-data: + cat /etc/redhat-release <13>May 21 03:12:34 user-data: Red Hat Enterprise Linux release 9.3 (Plow) <13>May 21 03:12:34 user-data: + echo 'Hello from user-data!' <13>May 21 03:12:34 user-data: Hello from user-data!
重要: ユーザーデータがコンソールに表示されます。送信するデータには機密情報を含めないでください。
関連情報
関連するコンテンツ
- 質問済み 6ヶ月前

