Docs

Documentation versions (currently viewingVaadin 24)
Documentation translations (currently viewingChinese)

本页面由官方文档 http://vaadin.com/docs 机器翻译而来,可能存在错误、不准确或不恰当之处。Vaadin 不保证或声明翻译的准确性、可靠性或时效性。

使用 Azure Cloud Kit 快速入门

分步指南,展示如何使用 Azure Cloud Kit 将应用程序部署到 Azure Cloud。

Azure Cloud Kit 可以让您将 Vaadin 应用程序快速部署到 Microsoft 的 Azure Cloud。该套件包含一组 Terraform 蓝图,用于将您的应用部署到 Azure Cloud,特别是部署到 Azure Kubernetes Service(AKS)。

Terraform 是一款开源的基础设施即代码(IaC)软件工具,提供一致的 CLI 工作流,使您能够安全、可预测地创建、更改和完善云基础设施。

先决条件

要使用 Azure Cloud Kit,您需要具备以下条件:

下载 Azure Cloud Kit

Azure Cloud Kit 提供于一个私有 GitHub 仓库中。如果您的商业订阅包含使用 Azure Cloud Kit 的权限,或您希望获取试用许可证,请 联系 Vaadin,您将获得访问该私有 GitHub 仓库的权限。

获得权限后,可从 GitHub 下载 Azure Cloud Kit。随后将其解压到本地目录。

配置 Azure CLI

首先,需通过终端使用 Azure CLI 登录您的 Azure 帐户。然后选择您希望用于部署的 Azure 订阅。

在终端执行以下命令登录 Azure:

Source code
terminal
az login

现在,选择用于部署的 Azure 订阅:

Source code
terminal
az account set --subscription <SUBSCRIPTION_ID>

请务必验证您已登录,并选择了正确的订阅。您可以执行以下命令进行验证:

Source code
terminal
az account show

为 Terraform 状态准备环境

要使用 Azure Cloud Kit,您需要创建一个用于 Terraform 状态的存储账户和容器。Terraform 状态用于跟踪已创建的资源。Terraform 根据这些信息,在您运行 terraform apply(用于将应用部署到 Azure)时判断需要做出的更改。

Azure Cloud Kit 中包含一个名为 create_terraform_storage.sh 的脚本。运行该脚本后,它将在所选 Azure 订阅下创建用于 Terraform 状态的存储账户和容器。

运行脚本的方法是,打开终端窗口,导航到您解压 Azure Cloud Kit 的文件夹,然后运行以下命令:

Source code
Terminal
create_terraform_storage.sh

脚本的输出会包含存储账户的名称(即 storageaccount)。该名称是随机生成的,在您的 Azure 订阅中是唯一的。您需要在下一步中使用该名称,请确保记录 storageaccount 的值。

初始化 Terraform

接下来,需将解压 Azure Cloud Kit 的目录初始化为 Terraform 工作目录。为此,打开终端,切换至该目录,并执行如下命令:

Source code
terminal
terraform init

Terraform 随后会下载 Azure provider 及其它必需插件。初始化过程中,Terraform 会要求您输入在上一步创建的存储账户名称。

Source code
terminal
    Initializing modules...
    - acr in modules/acr
    - aks in modules/aks
    - keyvault in modules/keyvault
    - network in modules/network

    Initializing the backend...
    storage_account_name
    The name of the storage account.

    Enter a value: <enter storageaccount name>

    Successfully configured the backend "azurerm"! Terraform will automatically
    use this backend unless the backend configuration changes.

    Initializing provider plugins...
    ...

定制化

Azure Cloud Kit 将定义部署的配置存储在两个文件中:variables.tfsettings.tf。您可以在 Azure Cloud Kit 根目录下找到这些文件。

variables.tf 文件包含描述部署的大多数变量,包括:

  • 应用程序名称和环境名称;

  • 希望部署应用的 Azure 区域;

  • Kubernetes 集群中的节点数目;

  • 节点的规格。

该列表并非完全,还可能有其他变量。

settings.tf 文件包含部署的网络设置。

variables.tfsettings.tf 文件都已填充有默认值,您可根据需要编辑它们。

检查计划

在部署应用到 Azure 之前,应使用 terraform 命令加 plan 选项,查看将要创建哪些资源。以该选项运行时,仅会显示若执行 apply 选项(您将在下一步操作)时会创建的资源。这有助于验证配置是否正确,并确保您对将要创建的资源感到满意。

Source code
terminal
terraform plan -out=plan.out

执行后,请检查屏幕上的输出内容——如果拟建环境符合需求,请进入下一步。

应用 Terraform

若对 terraform plan 的输出结果满意,可使用 apply 选项运行 terraform 命令。它将把应用部署到 Azure,并创建 variables.tfsettings.tf 文件中定义的全部资源。

Source code
terminal
$ terraform apply "plan.out"
azurerm_resource_group.resource_group: Creating...
...
Apply complete! Resources: 8 added, 0 changed, 0 destroyed.

您只需在第一行(美元符号后)输入命令,其余部分为示例输出,实际结果可能有所不同。

连接到 Kubernetes 集群

要连接由 Azure Cloud Kit 创建的 Kubernetes 集群,请使用 kubectl 命令配合 kubeconfig 文件。此配置文件是在前述 terraform apply 步骤自动生成的。

要具体连接到 Kubernetes 集群,您可在命令行执行如下:

Source code
terminal
$ export KUBECONFIG="./kubeconfig"
$ kubectl get nodes
NAME                               STATUS   ROLES   AGE     VERSION
aks-nodepool-31060480-vmss000000   Ready    agent   3m      v1.23.12
aks-nodepool-31060480-vmss000001   Ready    agent   3m      v1.23.12

如需获取 ingress 的 IP 地址,可通过命令行执行如下命令:

Source code
terminal
$ kubectl -n kube-system get service ingress-nginx-controller
NAME                                 TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)                      AGE
ingress-nginx-controller             LoadBalancer   10.240.97.40    a.b.c.d        80:30799/TCP,443:31928/TCP   106m

清理 Terraform 部署的项目

如果希望移除由 Azure Cloud Kit 创建的所有资源,可在命令行执行以下命令:

Source code
terminal
terraform destroy

此外,还需在 Azure portal 上手动删除已创建的存储账户(即 storageaccount),以及名为 Terraform-ResourceGroup 的资源组,这是上述脚本所创建的。

3EFFB1E4-FEF7-4836-90A4-30B9B6CB455E