Elastic Beanstalk允许在AWS云中部署和管理应用程序,而不必了解运行这些应用程序的基础设施。
通过Elastic Beanstalk,你可以运行一个网站、网络应用程序或提供HTTP请求的网络API,但你也可以运行一个工作程序来运行长任务。Elastic Beanstalk支持几个预配置的平台,包括Go 、.NET 或Java (仅适用于Java 8),但也支持通用的Docker 平台。
你只需用AWS CLI 、AWS EB CLI ,或用Elastic Beanstack console ,上传你的应用程序,Elastic Beanstalk会自动处理剩下的事情。
在这篇博文中,你将学习如何在Elastic Beanstalk上用基于Quarkus的应用程序启动单容器Docker 环境:
注意:这篇博客并没有描述从头开始创建应用程序。相反,它是基于Quarkus宠物诊所的REST API应用,该应用是我为开始使用Quarkus博文而创建的。源代码可以在Github上找到: https://github.com/kolorobot/quarkus-petclinic-api
TL;DR:创建包并上传到Elastic Beanstalk
在Elastic Beanstalk控制台创建新的应用程序
如果你还不是AWS客户,你需要创建一个AWS账户。注册后,你就可以访问Elastic Beanstalk和其他你需要的AWS服务:
- 使用此链接打开Elastic Beanstalk控制台:https://us-west-2.console.aws.amazon.com/elasticbeanstalk/home?region=us-west-2#/gettingStarted?applicationName=Pet Clinic API
- 对于
Platform,选择Docker - 对于
Application Code,选择Sample Application - 选择
Configure more options- 在列表中找到
Database,然后点击Modify - 对于
Engine,选择postgres - 对于
Engine version,选择11.6 - 设置你选择的
username和password - 对于
Retention,如果你不创建snaphost,就选择Delete。 - 点击
Save。
- 在列表中找到
- 点击
Create app
Elastic Beanstalk将为你创建具有所有必要资源(包括RDS)的示例应用程序。
一旦应用程序创建完成,你就可以看到该应用程序的链接:
注意:以上步骤是基于官方文档:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/GettingStarted.CreateApp.html
预备应用程序包
- 克隆资源库
git clone https://github.com/kolorobot/quarkus-petclinic-api
- 导航到应用程序目录并执行
./mvnw clean package assembly:single -Dquarkus.package.uber-jar=true
上述命令创建了具有以下内容的包
$ unzip -l target/quarkus-petclinic-api-1.0.1-eb.zip
Archive: target/quarkus-petclinic-api-1.0.1-eb.zip
Length Date Time Name
--------- ---------- ----- ----
0 03-15-2020 13:35 config/
2059 03-15-2020 13:34 Dockerfile
369 03-15-2020 13:34 config/application.properties
38604205 03-15-2020 13:35 quarkus-petclinic-api-1.0.1-runner.jar
--------- -------
38606633 4 files
上传应用程序到Elastic Beanstalk
- 使用Elastic Beanstalk控制台上传软件包
- 导航到console.aws.amazon.com/elasticbean…
- 导航到应用程序仪表板
- 点击
Upload and Deploy - 选择上一步创建的包,然后点击
Deploy - 等待应用程序被部署
就这样了。在下一段中,你将学习如何使用Maven准备软件包。
一步一步来:为Elastic Beanstalk配置应用程序
运行时配置
让我们从Elastic Beanstalk环境的特定应用配置开始。
Quarkus提供了几个选项,可以在运行时覆盖属性。我决定使用将配置文件放在config/application.properties 文件中的方法。这个文件会被Quarkus自动读取,并且这个文件的所有属性都优先于默认值。
创建src/main/resources/application-eb.properties 文件,并将quarkus.http.port 设为5000 ,因为这是Elastic Beanstalk网络应用的默认端口。
接下来的属性与数据源配置有关,因为应用程序将连接到RDS(PostgreSQL)。在Elastic Beanstalk上运行的应用程序可以通过RDS_* 环境属性获得RDS实例的连接信息,该属性对运行中的容器可用。要使用它,请设置以下属性:
quarkus.datasource.url=jdbc:postgresql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
quarkus.datasource.username=${RDS_USERNAME}
quarkus.datasource.password=${RDS_PASSWORD}
阅读更多关于连接你的应用程序到RDS的信息:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.db.html
Dockerfile
Elastic Beanstalk使用Dockerfile 来构建和运行镜像。该文件必须位于应用程序目录的root 。我使用了原始的src/main/docker/Dockerfile.jvm ,并做了以下调整:
- 将
config/application.properties复制到容器中 - 暴露端口
5000,而不是8080
完整的src/main/docker/Dockerfile.eb
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
ARG JAVA_PACKAGE=java-11-openjdk-headless
ARG RUN_JAVA_VERSION=1.3.5
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'
# Install java and the run-java script
# Also set up permissions for user `1001`
RUN microdnf install openssl curl ca-certificates ${JAVA_PACKAGE} \
&& microdnf update \
&& microdnf clean all \
&& mkdir /deployments \
&& chown 1001 /deployments \
&& chmod "g+rwX" /deployments \
&& chown 1001:root /deployments \
&& curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \
&& chown 1001 /deployments/run-java.sh \
&& chmod 540 /deployments/run-java.sh \
&& echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security
ENV JAVA_OPTIONS="-Djava.util.logging.manager=org.jboss.logmanager.LogManager"
COPY *-runner.jar /deployments/app.jar
COPY config /deployments/config
EXPOSE 5000
USER 1001
ENTRYPOINT [ "/deployments/run-java.sh" ]
用Maven创建应用程序包
到目前为止,已经创建了以下两个文件:
src/main/resources/application-eb.properties含有Elastic Beanstalk环境的特定属性src/main/docker/Dockerfile.eb带有Elastic Beanstack环境的容器配置。
为了完成配置和配置包装配,我们将使用Copy Rename Maven Plugin 和Maven Assembly Plugin 。
准备好组装的文件
修改pom.xml ,并添加目标来复制和重命名将存储在最终应用程序包zip 文件中的文件:
<build>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-file</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<sourceFile>src/main/resources/application-eb.properties</sourceFile>
<destinationFile>target/eb/application.properties</destinationFile>
</fileSet>
<fileSet>
<sourceFile>src/main/docker/Dockerfile.eb</sourceFile>
<destinationFile>target/eb/Dockerfile</destinationFile>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
</build>
copy-file 目标将在package 阶段运行,并将先前创建的文件复制到target/eb ,并调整其名称。
配置装配插件
Maven Assembly Plugin 将被用来创建应用程序包。将下面的配置添加到 pom.xml
<build>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/eb.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</build>
现在,创建src/assembly/eb.xml 描述符,指示汇编插件创建一个包含Dockerfile 、config/application.properties 和 Quarkusuber-jar 的zip 。所有这三个文件都将位于存档的root
<assembly>
<id>eb</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>target/eb/Dockerfile</source>
<outputDirectory></outputDirectory>
<filtered>false</filtered>
</file>
<file>
<source>target/eb/application.properties</source>
<outputDirectory>config</outputDirectory>
<filtered>false</filtered>
</file>
<file>
<source>target/${project.build.finalName}-runner.jar</source>
<outputDirectory></outputDirectory>
<filtered>false</filtered>
</file>
</files>
</assembly>
这样就完成了配置,现在你可以通过运行创建包(assembly)。
有了上述所有的变化,我们就可以创建包了:
./mvnw clean package assembly:single -Dquarkus.package.uber-jar=true
在本地测试该软件包
要在本地测试该包,请运行:
unzip target/quarkus-petclinic-api-1.0.1-eb.zip -d target/eb-dist && cd target/eb-dist
docker build -t quarkus/petclinic-api-jvm-eb .
在运行容器之前,启动数据库:
docker run -it --name petclinic-db -p 5432:5432 -e POSTGRES_DB=petclinic -e POSTGRES_USER=petclinic -e POSTGRES_PASSWORD=petclinic -d postgres:11.6-alpine
运行应用程序,通过RDS环境变量并链接到数据库容器:
docker run -i --rm -p 8080:5000 --link petclinic-db -e RDS_HOSTNAME=petclinic-db -e RDS_PORT=5432 -e RDS_DB_NAME=petclinic -e RDS_USERNAME=petclinic -e RDS_PASSWORD=petclinic quarkus/petclinic-api-jvm-eb
在浏览器中打开http://localhost:8080 ,你应该看到主页。
源代码
这篇文章的源代码可以在Github上找到:https://github.com/kolorobot/quarkus-petclinic-api