如何在 Elastic Beanstalk 中自定义主机文件?

How to customize hosts file in Elastic Beanstalk?

我们有一个通过 EBS 部署的应用程序(在 tomcat),我们时不时会收到错误消息:

unable to resolve host ip-172-30-0-167

这会导致其他问题。 据我所知,解决它的方法之一是更新主机文件并使用指向 ip-172-30-0-167 指向服务器真实 IP 地址的条目。

我的问题是,如何在部署时向主机文件添加条目?我没有通过 .ebsextensions 找到任何与之相关的内容。

也许还有其他方法可以解决。

更新

我遇到的问题之一是发送电子邮件:

ERROR g.plugins.mail.MailMessageBuilder: Failed to send email
Jan 27 15:40:30 unknown staging:  org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: 501 Syntax: EHLO hostname
Jan 27 15:40:30 unknown staging:  . Failed messages: javax.mail.MessagingException: 501 Syntax: EHLO hostname
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:417) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSender$send[=11=].call(Unknown Source) ~[na:na]
Jan 27 15:40:30 unknown staging:    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) ~[groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) ~[groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) ~[groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at grails.plugins.mail.MailMessageBuilder$_sendMessage_closure1.doCall(MailMessageBuilder.groovy:124) [mail-2.0.0.RC6.jar:na]
Jan 27 15:40:30 unknown staging:    at grails.plugins.mail.MailMessageBuilder$_sendMessage_closure1.doCall(MailMessageBuilder.groovy) [mail-2.0.0.RC6.jar:na]
Jan 27 15:40:30 unknown staging:    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.Closure.call(Closure.java:426) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.Closure.call(Closure.java:420) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.Closure.run(Closure.java:507) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Jan 27 15:40:30 unknown staging:  Caused by: javax.mail.MessagingException: 501 Syntax: EHLO hostname
Jan 27 15:40:30 unknown staging:    at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481) ~[mail-1.4.1.jar:1.4.1]
Jan 27 15:40:30 unknown staging:    at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:917) ~[mail-1.4.1.jar:1.4.1]
Jan 27 15:40:30 unknown staging:    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:417) ~[mail-1.4.1.jar:1.4.1]
Jan 27 15:40:30 unknown staging:    at javax.mail.Service.connect(Service.java:345) ~[javax.mail-api-1.5.1.jar:1.5.1]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:486) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:406) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    ... 22 common frames omitted
Jan 27 15:40:30 unknown staging:  ERROR g.plugins.mail.MailMessageBuilder: Failed to send email
Jan 27 15:40:30 unknown staging:  org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: 501 Syntax: EHLO hostname
Jan 27 15:40:30 unknown staging:  . Failed messages: javax.mail.MessagingException: 501 Syntax: EHLO hostname
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:417) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSender$send[=11=].call(Unknown Source) ~[na:na]
Jan 27 15:40:30 unknown staging:    at grails.plugins.mail.MailMessageBuilder$_sendMessage_closure1.doCall(MailMessageBuilder.groovy:124) [mail-2.0.0.RC6.jar:na]
Jan 27 15:40:30 unknown staging:    at grails.plugins.mail.MailMessageBuilder$_sendMessage_closure1.doCall(MailMessageBuilder.groovy) [mail-2.0.0.RC6.jar:na]
Jan 27 15:40:30 unknown staging:    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.Closure.call(Closure.java:426) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.Closure.call(Closure.java:420) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at groovy.lang.Closure.run(Closure.java:507) [groovy-2.4.5.jar:2.4.5]
Jan 27 15:40:30 unknown staging:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
Jan 27 15:40:30 unknown staging:    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Jan 27 15:40:30 unknown staging:  Caused by: javax.mail.MessagingException: 501 Syntax: EHLO hostname
Jan 27 15:40:30 unknown staging:    at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481) ~[mail-1.4.1.jar:1.4.1]
Jan 27 15:40:30 unknown staging:    at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:917) ~[mail-1.4.1.jar:1.4.1]
Jan 27 15:40:30 unknown staging:    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:417) ~[mail-1.4.1.jar:1.4.1]
Jan 27 15:40:30 unknown staging:    at javax.mail.Service.connect(Service.java:345) ~[javax.mail-api-1.5.1.jar:1.5.1]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:486) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:406) ~[spring-context-support-4.1.8.RELEASE.jar:4.1.8.RELEASE]
Jan 27 15:40:30 unknown staging:    ... 19 common frames omitted

看起来在部署时提议的方法没有帮助:

ERROR net.sf.ehcache.Cache: Unable to set localhost. This prevents creation of a GUID. Cause was: ip-172-35-11-183: ip-172-35-11-183: Name or service not known
Jan 27 15:35:25 unknown staging:  java.net.UnknownHostException: ip-172-35-11-183: ip-172-35-11-183: Name or service not known
Jan 27 15:35:25 unknown staging:    at java.net.InetAddress.getLocalHost(InetAddress.java:1505) ~[na:1.8.0_111]
Jan 27 15:35:25 unknown staging:    at net.sf.ehcache.Cache.<clinit>(Cache.java:200) ~[ehcache-core-2.4.3.jar:na]
Jan 27 15:35:25 unknown staging:    at net.sf.ehcache.config.ConfigurationHelper.createCache(ConfigurationHelper.java:302) [ehcache-core-2.4.3.jar:na]
Jan 27 15:35:25 unknown staging:    at net.sf.ehcache.config.ConfigurationHelper.createDefaultCache(ConfigurationHelper.java:218) [ehcache-core-2.4.3.jar:na]
Jan 27 15:35:25 unknown staging:    at net.sf.ehcache.CacheManager.configure(CacheManager.java:599) [ehcache-core-2.4.3.jar:na]
Jan 27 15:35:25 unknown staging:    at net.sf.ehcache.CacheManager.init(CacheManager.java:358) [ehcache-core-2.4.3.jar:na]

好吧,我说过这很常见,然后查看了一些其他代码,发现我在 .ebextensions 中设置了主机名。我有类似的东西:

01_command:
    echo 127.0.0.1 `hostname` >> /etc/hosts"

添加主机名。请注意,我不是在发送电子邮件,所以我不确定您是否需要真实的主机名。如果你这样做可以更改为:

01_command:
    echo `curl -s http://169.254.169.254/latest/meta-data/local-ipv4` `hostname` >> /etc/hosts"

最终的问题是 Elastic Beanstalk 将主机名设置为不在 /etc/hosts 中的内容。

我认为这是由您的 VPC 配置中的 enableDnsHostnames 参数引起的。

看看

谢谢!