简体   繁体   English

用于在 EC2 实例中启动服务的用户数据 bash 脚本未执行

[英]User data bash script to start services in EC2 instance not executing

I'm having difficulty getting certain services running within my EC2 instance.我很难在我的 EC2 实例中运行某些服务。 I'm using an Amazon Linux AMI, and would like to start neo4j , the graphical database, as a service when my instance first launches.我正在使用 Amazon Linux AMI,并希望在我的实例首次启动时将图形数据库neo4j作为服务启动。

However, I've been having trouble getting anything start, let alone neo4j .但是,我一直无法启动任何东西,更不用说neo4j I can't even get the basic tutorial user data script provided by Amazon working.我什至无法让亚马逊提供的基本教程用户数据脚本正常工作。 I copied and pasted the below code:我复制并粘贴了以下代码:

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

into the User Data section of my EC2 instance, and then started up my EC2 instance.进入我的 EC2 实例的用户数据部分,然后启动我的 EC2 实例。 Theoretically, since httpd is started, I should be able to navigate to the IP address of my instance in a browser and see the default Apache web page (I have allowed inbound HTTP at port 80 for security group settings).从理论上讲,自从httpd启动后,我应该能够在浏览器中导航到我的实例的 IP 地址并查看默认的 Apache 网页(我已允许端口 80 的入站 HTTP 用于安全组设置)。 However, I get a Connection refused error.但是,我收到Connection refused错误。

This is confirmed when I issue the following commands after SSH-ing into my instance:当我在 SSH-ing 到我的实例后发出以下命令时,这一点得到了证实:

sudo su service httpd status

The response is httpd is stopped , when clearly the user data script is supposed to start it.响应是httpd is stopped ,显然用户数据脚本应该启动它。 Amazon recommends to debug that I check /var/log/cloud-init-output.log for error messages, but I don't see any when I inspect the log:亚马逊建议调试我检查/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

I looked on StackOverflow for similar posts, and found this: user data script doesn't launch with ec2 instance .我在 StackOverflow 上查看了类似的帖子,发现了这一点: user data script doesn't launch with ec2 instance The accepted answer says that user data scripts only execute on the first boot cycle- does that mean I need to terminate my instance and launch a completely fresh instance in order to execute my script?公认的答案是用户数据脚本仅在第一个启动周期执行 - 这是否意味着我需要终止我的实例并启动一个全新的实例才能执行我的脚本? In which case, I feel like that defeats the whole purpose of a startup script to begin with!在这种情况下,我觉得这违背了启动脚本的全部目的!

EDIT: I added in a few echo test statements to see if they were being printed out in the logs- nope!编辑:我添加了一些echo测试语句,看看它们是否被打印在日志中——不!

However, I did try curl http://169.254.169.254/latest/meta-data from my EC2 instance, and got back this output:但是,我确实从我的 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

So it's definitely loading my user data... just not executing it.所以它肯定会加载我的用户数据......只是没有执行它。

From Configuring Instances with User Data使用用户数据配置实例

When you specify user data, note the following:指定用户数据时,请注意以下事项:

  • User data is treated as opaque data: what you give is what you get back.用户数据被视为不透明数据:您给予的就是您得到的。 It is up to the instance to be able to interpret it.由实例来解释它。
  • User data is limited to 16 KB.用户数据限制为 16 KB。 This limit applies to the data in raw form, not base64-encoded form.此限制适用于原始形式的数据,而不是 base64 编码形式的数据。
  • User data must be base64-encoded.用户数据必须采用 base64 编码。 The Amazon EC2 console can perform the base64 encoding for you or accept base64-encoded input. Amazon EC2 控制台可以为您执行 base64 编码或接受 base64 编码的输入。
  • User data must be decoded when you retrieve it.检索用户数据时必须对其进行解码。 The data is decoded when you retrieve it using instance metadata and the console.当您使用实例元数据和控制台检索数据时,数据会被解码。
  • User data is executed only at launch.用户数据仅在启动时执行。 If you stop an instance, modify the user data, and start the instance, the new user data is not executed automatically如果停止实例,修改用户数据,再启动实例,新的用户数据不会自动执行

Userdata only runs on the instance launch. Userdata 仅在实例启动时运行。 Its purpose is to run once, to allow for one-time server configuration tasks before the instance is put into service.其目的是运行一次,以允许在实例投入使用之前执行一次性服务器配置任务。

Do you want the software installation process to run each time on reboot?您是否希望每次重新启动时都运行软件安装过程? If you want to start a process on each reboot, then look into /etc/rc.local.如果您想在每次重新启动时启动一个进程,请查看 /etc/rc.local。

I've just encountered a similar problem and the solution that worked for me is described in this article: How can I utilize user data to automatically run a script with every restart of my Amazon EC2 Linux instance?我刚刚遇到了类似的问题,本文描述了对我有用的解决方案: 如何利用用户数据在每次重启我的 Amazon EC2 Linux 实例时自动运行脚本?

So, just add your own script after the #!/bin/bash line and put the whole block to the Edit user data of your EC2 instance.因此,只需在#!/bin/bash行之后添加您自己的脚本,并将整个块放入您的 EC2 实例的Edit user data中。 Basically, it will look something like this:基本上,它看起来像这样:

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
--//--

Here's an example of what that page looks like, and notice the unchecked box for " Input is already base64-encoded ":这是该页面的示例,请注意“ Input is already base64-encoded ”的未选中框:

在 EC2 上编辑用户数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM