【问题标题】:User data bash script to start services in EC2 instance not executing用于在 EC2 实例中启动服务的用户数据 bash 脚本未执行
【发布时间】:2022-01-14 16:33:45
【问题描述】:

我无法在我的 EC2 实例中运行某些服务。我正在使用 Amazon Linux AMI,并希望在我的实例首次启动时将图形数据库 neo4j 作为服务启动。

但是,我一直无法启动任何东西,更不用说neo4j。我什至无法让亚马逊提供的基本tutorial user data script 工作。我复制并粘贴了以下代码:

#!/bin/bash
yum update -y
yum install -y httpd24 php56 mysql55-server php56-mysqlnd
service httpd start
chkconfig httpd on

进入我的 EC2 实例的用户数据部分,然后启动我的 EC2 实例。从理论上讲,由于httpd 已启动,我应该能够在浏览器中导航到我的实例的 IP 地址并查看默认的 Apache 网页(我已允许端口 80 的入站 HTTP 用于安全组设置)。但是,我收到 Connection refused 错误。

当我在通过 SSH 连接到我的实例后发出以下命令时,这一点得到了证实:

sudo su service httpd status

响应是httpd is stopped,显然用户数据脚本应该启动它。亚马逊建议调试我检查/var/log/cloud-init-output.log 是否有错误消息,但我在检查日志时看不到任何错误消息:

Cloud-init v. 0.7.6 running 'init-local' at Sun, 12 Nov 2017 16:41:03 +0000. Up 5.73 seconds.
Cloud-init v. 0.7.6 running 'init' at Sun, 12 Nov 2017 16:41:03 +0000. Up 5.91 seconds.
ci-info: +++++++++++++++++++++++Net device info+++++++++++++++++++++++
ci-info:  Device   Up     Address         Mask          Hw-Address    
ci-info:    lo    True   127.0.0.1     255.0.0.0            .         
ci-info:   eth0   True  172.31.7.27  255.255.240.0  0a:8c:a2:67:1f:46 
ci-info: ++++++++++++++++++++++++++++++Route info+++++++++++++++++++++++++++++++
ci-info:  Route    Destination     Gateway        Genmask      Interface  Flags 
ci-info:    0        0.0.0.0      172.31.0.1      0.0.0.0         eth0      UG  
ci-info:    1    169.254.169.254   0.0.0.0    255.255.255.255     eth0      UH  
ci-info:    2       172.31.0.0     0.0.0.0     255.255.240.0      eth0      U   
Generating public/private rsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
...
The key's randomart image is:
+---[RSA 2048]----+
[RANDOM ART]
+----[SHA256]-----+
Generating public/private dsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
...
The key's randomart image is:
+---[DSA 1024]----+
+----[SHA256]-----+
Generating public/private ecdsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key.
Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub.
The key fingerprint is:
...
The key's randomart image is:
... [RANDOMART IMAGE]
Cloud-init v. 0.7.6 running 'modules:config' at Sun, 12 Nov 2017 16:41:04 +0000. Up 6.70 seconds.
Loaded plugins: priorities, update-motd, upgrade-helper
No packages needed for security; 0 packages available
No packages marked for update
Cloud-init v. 0.7.6 running 'modules:final' at Sun, 12 Nov 2017 16:41:08 +0000. Up 10.93 seconds.
Cloud-init v. 0.7.6 finished at Sun, 12 Nov 2017 16:41:08 +0000. Datasource DataSourceEc2.  Up 11.04 seconds
Cloud-init v. 0.7.6 running 'init-local' at Sun, 12 Nov 2017 17:10:45 +0000. Up 5.85 seconds.
Cloud-init v. 0.7.6 running 'init' at Sun, 12 Nov 2017 17:10:46 +0000. Up 6.04 seconds.
ci-info: +++++++++++++++++++++++Net device info+++++++++++++++++++++++
ci-info:  Device   Up     Address         Mask          Hw-Address    
ci-info:    lo    True   127.0.0.1     255.0.0.0            .         
ci-info:   eth0   True  172.31.7.27  255.255.240.0  0a:8c:a2:67:1f:46 
ci-info: ++++++++++++++++++++++++++++++Route info+++++++++++++++++++++++++++++++
ci-info:  Route    Destination     Gateway        Genmask      Interface  Flags 
ci-info:    0        0.0.0.0      172.31.0.1      0.0.0.0         eth0      UG  
ci-info:    1    169.254.169.254   0.0.0.0    255.255.255.255     eth0      UH  
ci-info:    2       172.31.0.0     0.0.0.0     255.255.240.0      eth0      U   
Cloud-init v. 0.7.6 running 'modules:config' at Sun, 12 Nov 2017 17:10:46 +0000. Up 6.61 seconds.
Cloud-init v. 0.7.6 running 'modules:final' at Sun, 12 Nov 2017 17:10:47 +0000. Up 7.03 seconds.
Cloud-init v. 0.7.6 finished at Sun, 12 Nov 2017 17:10:47 +0000. Datasource DataSourceEc2.  Up 7.11 seconds
Cloud-init v. 0.7.6 running 'init-local' at Sun, 12 Nov 2017 17:18:25 +0000. Up 5.60 seconds.
Cloud-init v. 0.7.6 running 'init' at Sun, 12 Nov 2017 17:18:25 +0000. Up 5.79 seconds.
ci-info: +++++++++++++++++++++++Net device info+++++++++++++++++++++++
ci-info:  Device   Up     Address         Mask          Hw-Address    
ci-info:    lo    True   127.0.0.1     255.0.0.0            .         
ci-info:   eth0   True  172.31.7.27  255.255.240.0  0a:8c:a2:67:1f:46 
ci-info: ++++++++++++++++++++++++++++++Route info+++++++++++++++++++++++++++++++
ci-info:  Route    Destination     Gateway        Genmask      Interface  Flags 
ci-info:    0        0.0.0.0      172.31.0.1      0.0.0.0         eth0      UG  
ci-info:    1    169.254.169.254   0.0.0.0    255.255.255.255     eth0      UH  
ci-info:    2       172.31.0.0     0.0.0.0     255.255.240.0      eth0      U   
Cloud-init v. 0.7.6 running 'modules:config' at Sun, 12 Nov 2017 17:18:26 +0000. Up 6.41 seconds.
Cloud-init v. 0.7.6 running 'modules:final' at Sun, 12 Nov 2017 17:18:26 +0000. Up 6.84 seconds.
Cloud-init v. 0.7.6 finished at Sun, 12 Nov 2017 17:18:26 +0000. Datasource DataSourceEc2.  Up 6.93 seconds

我在 StackOverflow 上查找了类似的帖子,发现:user data script doesn't launch with ec2 instance。公认的答案是用户数据脚本仅在第一个启动周期执行 - 这是否意味着我需要终止我的实例并启动一个全新的实例才能执行我的脚本?在这种情况下,我觉得这违背了启动脚本的全部目的!

编辑:我添加了一些 echo 测试语句,以查看它们是否被打印在日志中 - 不!

但是,我确实在我的 EC2 实例中尝试了curl http://169.254.169.254/latest/meta-data,并得到了这个输出:

#!/bin/bash
echo "Testing 1"
yum update -y
echo "Testing 2"
yum install -y httpd24 php56 mysql55-server php56-mysqlnd
echo "Testing 3"
service httpd start
chkconfig httpd on

所以它肯定是在加载我的用户数据......只是没有执行它。

【问题讨论】:

  • 这是您的完整日志吗?尝试添加更多echo 进行调试。
  • 您是否也为 yum install 启用了端口?您可以在用户数据部分添加 echo "Test" 以查看是否正在调用用户数据部分。 #!/bin/bash yum update -y echo "test" yum install -y httpd24 php56 mysql55-server php56-mysqlnd echo "starting httpd service" service httpd start chkconfig httpd on
  • 如果您的日志中没有echo 输出,这意味着您的脚本在您的实例启动期间没有运行。请记住,user-data script 在第一次启动时只启动一次。您可以运行具有较小实例类型的其他实例来测试您的脚本。
  • 不!第一次启动新实例。下次您停止然后重新启动实例时,它不会再次运行user-data。这就是为什么你必须启动新实例来测试user-data 脚本
  • @Yu Chen - 正确。您是否希望每次重新启动时都运行安装过程?如果您想在每次重新启动时启动一个进程,请查看 /etc/rc.local。 Userdata 仅在实例启动时运行。其目的是运行一次,以便在实例投入使用之前进行一次性服务器配置。

标签: linux bash apache amazon-web-services amazon-ec2


【解决方案1】:

来自Configuring Instances with User Data

当您指定用户数据时,请注意以下几点:

  • 用户数据被视为不透明数据:你给的就是你得到的 背部。由实例来解释它。
  • 用户数据 限制为 16 KB。此限制适用于原始形式的数据,不适用于 base64 编码形式。
  • 用户数据必须是 base64 编码的。亚马逊 EC2 控制台可以为您执行 base64 编码或接受 base64 编码的输入。
  • 在检索用户数据时必须对其进行解码。 当您使用实例元数据检索数据时,数据会被解码,并且 控制台。
  • 用户数据仅在启动时执行。如果你停止一个 实例,修改用户数据,并启动实例,新用户 数据不会自动执行

Userdata 仅在实例启动时运行。其目的是运行一次,以便在实例投入使用之前进行一次性服务器配置任务。

您希望每次重新启动时都运行软件安装过程吗?如果您想在每次重新启动时启动一个进程,请查看 /etc/rc.local。

【讨论】:

    【解决方案2】:

    我刚刚遇到了类似的问题,这篇文章中描述了对我有用的解决方案:How can I utilize user data to automatically run a script with every restart of my Amazon EC2 Linux instance?

    因此,只需在 #!/bin/bash 行之后添加您自己的脚本,并将整个块放入您的 EC2 实例的 Edit user data。基本上,它看起来像这样:

    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
    
    # This script is run after the system reboots!
    
    # 1. Go to the project directory.
    cd /home/ubuntu/my-webapp
    
    # 2. Run docker compose to launch the app.
    docker-compose -f docker-compose.prod.yml up -d
    --//--
    

    这是该页面的示例,请注意“Input is already base64-encoded”的未选中框:

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 2018-08-02
      • 1970-01-01
      • 2013-07-21
      相关资源
      最近更新 更多