带有 docker 应用程序的 AWS Elastic Beanstalk 返回 502

Posted

技术标签:

【中文标题】带有 docker 应用程序的 AWS Elastic Beanstalk 返回 502【英文标题】:AWS Elastic Beanstalk with docker application returns 502 【发布时间】:2021-01-17 12:25:19 【问题描述】:

我用 docker 创建了一个 SPRING-BOOT 应用程序,我正在将此映像部署到 Elastic-beanstalk,一切顺利,通过日志,应用程序启动,但随后访问 ulr,我得到一个 503,我试了好几样,谁能给个意见?

我的 Dockerrun 文件:


    "AWSEBDockerrunVersion": "2",
    "containerDefinitions": [
        
            "essential": true,
            "image": "187190575110.dkr.ecr.us-east-1.amazonaws.com/kroton-app:$TAG",
            "memory": 512,
            "memoryReservation": 256,
            "mountPoints": [],
            "name": "kroton-app",
            "portMappings": [
                
                    "containerPort": "8080",
                    "hostPort": "80"
                
            ]
        
    ]

我的 Docker 文件:

FROM node:12-alpine

#TEST

# JAVA DEPS
RUN apk --no-cache add openjdk11 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
RUN apk --no-cache add maven

# NPM DEPS
RUN mkdir -p /eventos/src/main/resources/node
WORKDIR /eventos
COPY ./src/main/resources/node/package.json src/main/resources/node

RUN cd src/main/resources/node && \
    npm install

# NODE APP
COPY ./src/main/resources/node src/main/resources/node

# NODE BUILD
RUN cd src/main/resources/node && \
    npm run build:prod

# MAVEN DEPS
COPY ./pom.xml /eventos
RUN mvn dependency:go-offline

# MAVEN BUILD
COPY . /eventos
run mvn clean package -Dspring.profiles.active=prod,storage-s3
RUN mvn install -DskipTests

# CLEANUP
RUN rm -rf src/main/resources/node

EXPOSE 8080
CMD ["java","-jar","-Dspring.profiles.active=prod,storage-s3","/eventos/target/eventos-1.0.0-RELEASE.jar"]

我在容器上的应用程序的 aws 日志 (var/log/containers/app.log)

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/eventos/target/eventos-1.0.0-RELEASE.jar!/BOOT-INF/lib/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/eventos/target/eventos-1.0.0-RELEASE.jar!/BOOT-INF/lib/slf4j-simple-1.7.28.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2020-10-01 18:07:36.768  INFO 1 --- [           main] b.c.p.eventos.EventosApplication         : Starting EventosApplication v1.0.0-RELEASE on 4e2622a12739 with PID 1 (/eventos/target/eventos-1.0.0-RELEASE.jar started by root in /eventos)
2020-10-01 18:07:36.779  INFO 1 --- [           main] b.c.p.eventos.EventosApplication         : The following profiles are active: prod,storage-s3
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (jar:file:/eventos/target/eventos-1.0.0-RELEASE.jar!/BOOT-INF/lib/groovy-2.5.6.jar!/) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2020-10-01 18:07:39.229  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-10-01 18:07:39.394  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 145ms. Found 9 repository interfaces.
2020-10-01 18:07:40.934  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$65e536b6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-10-01 18:07:40.974  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$e56583e8] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-10-01 18:07:41.968  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-10-01 18:07:42.045  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-01 18:07:42.046  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2020-10-01 18:07:42.217  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-01 18:07:42.217  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4768 ms
2020-10-01 18:07:43.643  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-10-01 18:07:44.648  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-10-01 18:07:44.781  INFO 1 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2020-10-01 18:07:45.017  INFO 1 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core 5.3.9.Final
2020-10-01 18:07:45.023  INFO 1 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2020-10-01 18:07:45.560  INFO 1 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.0.4.Final
2020-10-01 18:07:46.462  INFO 1 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.mysql57Dialect
2020-10-01 18:07:47.990  INFO 1 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-10-01 18:07:50.035  INFO 1 --- [           main] b.c.p.e.service.impl.S3StorageService    : S3 Service succesfully connected!
2020-10-01 18:07:50.127  INFO 1 --- [           main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2020-10-01 18:07:52.377  INFO 1 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@2a16d393, org.springframework.security.web.context.SecurityContextPersistenceFilter@1edac3b4, org.springframework.security.web.header.HeaderWriterFilter@437c1a87, org.springframework.security.web.csrf.CsrfFilter@7294a684, org.springframework.security.web.authentication.logout.LogoutFilter@24842b8e, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@5d6b5d3d, org.springframework.security.web.session.ConcurrentSessionFilter@4a2653a0, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@11069eac, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@3d9c8c3c, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@14d1032a, org.springframework.security.web.session.SessionManagementFilter@6c8e5ac4, org.springframework.security.web.access.ExceptionTranslationFilter@75663443, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@d08f85a]
2020-10-01 18:07:52.662  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-01 18:07:52.700  WARN 1 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-10-01 18:07:53.811  INFO 1 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-10-01 18:07:54.089  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-01 18:07:54.093  INFO 1 --- [           main] b.c.p.eventos.EventosApplication         : Started EventosApplication in 18.837 seconds (JVM running for 20.264)

SSH 响应:

$ sudo docker ps
CONTAINER ID        IMAGE                                                            COMMAND                  CREATED             STATUS                   PORTS                    NAMES
60208e7c7429        187190575110.dkr.ecr.us-east-1.amazonaws.com/kroton-app:master   "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes             0.0.0.0:8080->8080/tcp   ecs-awseb-kroton-prod-ywvf9pvxrr-19-kroton-app-bc9591de9be2fd9f3000
3cee31b0f74d        amazon/amazon-ecs-agent:latest                                   "/agent"                 6 minutes ago       Up 6 minutes (healthy)                            ecs-agent



$ curl http://localhost:8080
<!doctype html> <html lang="pt"> <head> <title>Eventos Kroton - Home</title> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/> <link rel="stylesheet" href="/css/eventos-977e18c37ae6ce996d5f9871a231481b.css"> </head> <body> <input name="_csrf" value="a03be7d3-b991-4f48-a596-b0c4b6f82dce" id="csrf-token" type="hidden"/> <div> <script src="/js/eventos-82e6dbb5b75296a131489f409be47723.js"></script> </div> <header> <div class="border-bottom"> <div class="mobile-header container my-2 d-md-none"> <span class="mobile-menu-btn fa fa-3x fa-bars"></span> </div> <div class="mini-header container py-2 d-md-block d-none"> <div class="row"> <div class="col"> <a class="text-uppercase" href="https://pgsskroton.com.br/" target="_blank">Portal Strictu Sensu</a> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="main-header container-fluid d-flex py-2 px-xl-5"> <div class="row m-0 w-100"> <div class="search-box form-inline col-4 d-md-flex d-none"> <div class="form-group w-100"> <label class="mr-2" for="search"><span class="fa fa-2x fa-search"></span></label> <select class="flex-grow-1 form-control form-control-sm border-0" id="search" placeholder="Pesquisar" type="text"> </select> </div> </div> <div class="logo-container align-self-center col-12 col-md-4"> <a class="text-decoration-none" href="/" title="Eventos Kroton"> <img  class="mx-auto d-block" src="/img/logo.svg"> </a> </div> <ul class="header-menu list-inline text-right align-self-center col-4 d-md-block d-none m-0" id="desktop-menu">   <li class="list-inline-item"><a href="/login">Login</a></li> <li class="list-inline-item"><a href="/registrar">Criar Nova Conta</a></li>  </ul> </div> </div> </header> <div> <section id="events-banner" class="py-5"> <div class="events-featured container"> <div id="events-carousel" class="carousel slide carousel-fade" data-ride="carousel"> <ol class="carousel-indicators">  </ol> <div class="carousel-inner">  </div> </div> </div> </section>   </div> <div> <div class="footer navbar-fixed-bottom text-center" style="background-color: #046eb1; color: white;padding : 5px; margin: 5px;"> <address> Kroton Educacional 2020. Todos os Direitos Reservados - Sistema de Eventos<br> Contato: <a style="color: white;hover: red;" href="mailto:editora@kroton.com.br">editora@kroton.com.br</a> </address> </div> </div> <script>EventosJs.init();function searchSelectize(a)a("#search").selectize(valueField:"id",labelField:"titulo",searchField:"titulo",preload:true,options:[],create:false,render:option:function(c,b)return'<div><div class="row px-1 mb-2 cursor-pointer"><div class="col-4"><img class="img-fluid" src="/img/eventos/'+c.id+"/thumb/"+c.imgMiniaturaName+'.jpg" ></div><div class="col-8">'+b(c.titulo)+"</div></div></div>",load:function(b,c)a.ajax(url:"/api/evento/find",type:"GET",dataType:"json",data:titulo:b,error:function()c(),success:function(d)c(d.content)),onChange:function(b)this.clearOptions();window.location="/eventos/"+b)EventosJs.addEvent(searchSelectize);</script> </body> </html>

【问题讨论】:

如果你 ssh 进入实例,容器会运行吗?你可以运行sudo docker ps。检查暴露的端口,直接 curl 容器看应用是否在 localhost 上运行。 好的,我将它插入实例并在端口上运行 docker ps 和 Curl,将上面的结果添加为 SSH 响应 【参考方案1】:

您的新输出显示您正在使用端口 8080:

0.0.0.0:8080->8080/tcp

您的Dockerrun 与这些端口不匹配:

            "portMappings": [
                
                    "containerPort": "8000",
                    "hostPort": "80"
                

所以不知何故,您在 Dockerrun 和实际 docker 运行时使用了不同的端口。

也许不需要在您的Dokerfile 中使用EXPOSE 8080

【讨论】:

对不起,我在我的 Dockerrun.aws.json 中编辑了这个端口,但不工作。【参考方案2】:

您将在 Dockerfile 上公开端口 8080,并在 Dockerrun.aws.json 上映射 8000。我相信这是真正的问题。为了解决,你可以在镜像上暴露8000端口,部署后尝试访问the endpoint with the port。

你能尝试用这个Dockerfile 构建一个镜像吗?

FROM node:12-alpine

#TEST

# JAVA DEPS
RUN apk --no-cache add openjdk11 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
RUN apk --no-cache add maven

# NPM DEPS
RUN mkdir -p /eventos/src/main/resources/node
WORKDIR /eventos
COPY ./src/main/resources/node/package.json src/main/resources/node

RUN cd src/main/resources/node && \
    npm install

# NODE APP
COPY ./src/main/resources/node src/main/resources/node

# NODE BUILD
RUN cd src/main/resources/node && \
    npm run build:prod

# MAVEN DEPS
COPY ./pom.xml /eventos
RUN mvn dependency:go-offline

# MAVEN BUILD
COPY . /eventos
run mvn clean package -Dspring.profiles.active=prod,storage-s3
RUN mvn install -DskipTests

# CLEANUP
RUN rm -rf src/main/resources/node

EXPOSE 8000
CMD ["java","-jar","-Dspring.profiles.active=prod,storage-s3","/eventos/target/eventos-1.0.0-RELEASE.jar"]

【讨论】:

对不起,我在我的 Dockerrun.aws.json 中编辑了这个端口,但不工作 您是否公开了负载均衡器上关联的安全组上的端口? 我没有做任何关于负载平衡的事情,你有我如何验证这一点的例子吗? 任何想法@Cauê Alves Braz

以上是关于带有 docker 应用程序的 AWS Elastic Beanstalk 返回 502的主要内容,如果未能解决你的问题,请参考以下文章

带有 docker 应用程序的 AWS Elastic Beanstalk 返回 502

在 AWS ECS 中将带有卷参数的 docker 容器作为任务定义或服务运行

带有 Django 的 AWS copilot 从未完成部署

text 带有docker post命令的AWS EC2简单AMI

AWS - ECS - 如何在现有 ECS(带有 1 个 EC2)实例上重新部署更新的 Docker 映像?

如何在 Elasticbeanstalk 环境中运行的 docker 容器中承担 AWS 角色?