使用Terraform将Web应用程序部署到Azure App Service

Øriginally published in deepu.tech.

Deploying Java web applications to Azure is easy and has been tried, tested and explained many times by many people. My friend Julien dubois has a nice series on it here. Azure makes it really easy to use its App Service as it provides many different ways of deploying a web app.

如果您是现代的全栈Java开发人员,则很有可能会将应用程序部署为Docker映像。 因此,今天让我们看看如何以真正的基础结构代码为精神,使用Docker和Terraform将Java Web应用程序部署到Azure App Service。 对于作为docker映像构建的任何Web应用程序,该方法几乎相同,而不必局限于Java。

To try this out you would need to have Java, NodeJS, Ťerraform, Docker and Azure CLI installed. Follow the links to install them if needed.

As one of the lead developer of JHipster (A handy development platform to generate, develop and deploy Spring Boot + Angular/React/Vue Web applications and Spring microservices), I would use a JHipster web application as the example here. So let's get started.

Let's build a very simple web application using JHipster. We will use the JD大号 feature to scaffold our application.

我们将在我们的应用程序中使用以下JDL。 将其保存到名为app.jdl在要创建应用程序的目录中。

application {
    config {
        baseName helloJHipster,
        applicationType monolith,
        packageName tech.jhipster.demo,
        authenticationType jwt,
        buildTool gradle,
        clientFramework react,
        databaseType sql,
        prodDatabaseType mysql,
        languages [en, nl]
    }
}

现在让我们使用JHipster搭建支架。 打开您喜欢的控制台/终端,并在保存上述JDL文件的目录中运行以下命令,确保该目录为空。

$ npx generator-jhipster import-jdl app.jdl

If you already have JHipster installed you can just run

$ jhipster import-jdl app.jdl

这将搭建应用程序并安装所需的客户端依赖项。 可能要花几分钟(NPM!),所以可能要喝点咖啡了。

您可以通过运行以下命令查看正在运行的应用程序./gradlew一旦完成脚手架,就在同一终端上。 您可以参考生成的自述文件有关该应用程序的更多说明。

现在,让我们继续本文的重点,使用Terraform将其部署到Azure App Service。 让我们首先为应用程序构建和发布docker映像。

JHipster conveniently provides everything that is required to build docker images. Let us use the provided docker integration using ĴIB to build the images. Run the below Gradle command.

$ ./gradlew bootJar -Pprod jibDockerBuild

Now let us tag and push this to our docker registry, make sure you have logged into docker and run these commands. Use your own docker hub account name.

$ docker tag hellojhipster:latest deepu105/hellojhipster:latest
$ docker push deepu105/hellojhipster:latest

如果愿意,还可以推送到Azure Container注册表而不是Docker Hub。

Now that our application and Docker images are ready, let's prepare the Terraform infrastructure for App Service and MySQL database. For other ways of deploying a JHipster web app to Azure check this out.

首先,为我们的Terraform文件创建一个文件夹。 我们命名文件夹地貌。

现在创建三个名为主文件,输出文件,and 变量在此文件夹中。

让我们定义将要使用的变量。 将以下内容保存在变量。

variable "prefix" {
  description = "The prefix used for all resources in this example"
  default     = "xl"
}

variable "location" {
  description = "The Azure location where all resources in this example should be created"
}

variable "subscription_id" {
  description = "Azure Subscription ID to be used for billing"
}

variable "my_sql_master_password" {
  description = "MySql master password"
}

variable "docker_image" {
  description = "Docker image name"
}

variable "docker_image_tag" {
  description = "Docker image tag"
}

现在让我们定义我们的主文件

首先,让我们为Azure资源管理器添加配置,并创建一个Azure资源组来保存我们的资源。

provider "azurerm" {
  version         = "=1.24.0"
  subscription_id = "${var.subscription_id}"
}

resource "azurerm_resource_group" "main" {
  name     = "${var.prefix}-resources"
  location = "${var.location}"
}

Now let us add the configuration to create a MySQL database server along with the required firewall rules to let App Service access the DB. If you want to add local access from your machine add a firewall rule block for your IP as well.

# This creates a MySQL server
resource "azurerm_mysql_server" "main" {
  name                = "${var.prefix}-mysql-server"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"

  sku {
    name     = "B_Gen5_2"
    capacity = 2
    tier     = "Basic"
    family   = "Gen5"
  }

  storage_profile {
    storage_mb            = 5120
    backup_retention_days = 7
    geo_redundant_backup  = "Disabled"
  }

  administrator_login          = "mysqladminun"
  administrator_login_password = "${var.my_sql_master_password}"
  version                      = "5.7"
  ssl_enforcement              = "Disabled"
}

# This is the database that our application will use
resource "azurerm_mysql_database" "main" {
  name                = "${var.prefix}_mysql_db"
  resource_group_name = "${azurerm_resource_group.main.name}"
  server_name         = "${azurerm_mysql_server.main.name}"
  charset             = "utf8"
  collation           = "utf8_unicode_ci"
}

# This rule is to enable the 'Allow access to Azure services' checkbox
resource "azurerm_mysql_firewall_rule" "main" {
  name                = "${var.prefix}-mysql-firewall"
  resource_group_name = "${azurerm_resource_group.main.name}"
  server_name         = "${azurerm_mysql_server.main.name}"
  start_ip_address    = "0.0.0.0"
  end_ip_address      = "0.0.0.0"
}

这将创建一个MySQL服务器,该服务器上是我们应用程序的数据库,并允许通过App Service进行访问。

Now let us configure the App Service itself along with a service plan.

# This creates the plan that the service use
resource "azurerm_app_service_plan" "main" {
  name                = "${var.prefix}-asp"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"
  kind                = "Linux"
  reserved            = true

  sku {
    tier = "Standard"
    size = "S1"
  }
}

# This creates the service definition
resource "azurerm_app_service" "main" {
  name                = "${var.prefix}-appservice"
  location            = "${azurerm_resource_group.main.location}"
  resource_group_name = "${azurerm_resource_group.main.name}"
  app_service_plan_id = "${azurerm_app_service_plan.main.id}"

  site_config {
    app_command_line = ""
    linux_fx_version = "DOCKER|${var.docker_image}:${var.docker_image_tag}"
    always_on        = true
  }

  app_settings = {
    "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
    "DOCKER_REGISTRY_SERVER_URL"          = "https://index.docker.io"

    # These are app specific environment variables
    "SPRING_PROFILES_ACTIVE"     = "prod,swagger"
    "SPRING_DATASOURCE_URL"      = "jdbc:mysql://${azurerm_mysql_server.main.fqdn}:3306/${azurerm_mysql_database.main.name}?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC"
    "SPRING_DATASOURCE_USERNAME" = "${azurerm_mysql_server.main.administrator_login}@${azurerm_mysql_server.main.name}"
    "SPRING_DATASOURCE_PASSWORD" = "${var.my_sql_master_password}"
  }
}

在此配置下site_config我们用linux_fx_version声明我们的docker镜像并设置always_on为true,以便在一段时间不活动时不会关闭应用程序。

在里面app_settings部分,我们需要使用标志禁用存储WEBSITES_ENABLE_APP_SERVICE_STORAGE并指定DOCKER_REGISTRY_SERVER_URL。 其他所有内容都特定于我们的应用程序。 传递给MySQL连接URL的标志很重要。

现在我们的主文件准备好了,让我们定义一些方便的输出属性。 在里面输出文件文件添加以下

output "app_service_name" {
  value = "${azurerm_app_service.main.name}"
}

output "app_service_default_hostname" {
  value = "https://${azurerm_app_service.main.default_site_hostname}"
}

Now we are ready to rock and roll! let us deploy the app. Make sure you have set up your Azure CLI and have logged in using az login. Now in a terminal/console navigate to the terraform folder we created and execute these commands. Please change the values for prefix, location & docker_image accordingly.

$ terraform init

$ terraform apply \
-var prefix=myAwesomeApp \
-var location=northeurope \
-var docker_image=deepu105/hellojhipster \
-var docker_image_tag=latest

这将提示您输入MySQL服务器的主密码和Azure订阅ID(可以从Azure门户或通过运行以下命令找到AZ帐户列表-的ID field is the subscription ID). Once you provIDe the values and confirm, Terraform will get to work and will start creating the resources. this could take a while since we are provisioning a Database server. Wait for it or go have that second coffee ;)

部署完成后,Terraform将打印输出,其中包括app_service_default_hostname。 复制URL并在您喜欢的浏览器中打开它。 第一次可能要花一些时间,因为仅在第一个请求期间才会启动(冷启动)应用程序。

I hope you found this useful. This is my first post in dev.to, I hope to migrate my blogs from Medium to dev.to soon.

如果您喜欢这篇文章,请留下喜欢或评论。

You can follow me on Twitter and LinkedIn.

我的其他相关文章:

from: https://dev.to//deepu105/deploy-a-web-app-to-azure-app-service-using-terraform-40e1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值