DevOps - IaC 基础架构即代码(一)Azure Resource Manager及GitHub部署实例

Infrastructure-as-code直译就是将基础架构作为代码,是自动化部署重要的一环,具有易迁移,易伸缩,相对风险低等优势。随着生产和交付周期数量和频率增加,IaC作为DevOps的最佳实践之一,使构建和配置基础架构更加高效。

这一篇我们将介绍Azure Resource Manager,并且以GitHub为例,如何在DevOps管道中使用ARM。



Azure Resource Manager

我们先来看一下Azure提供的IaC工具。Azure资源管理器(ARM)是为部署和管理Azure资源提供的服务,是Azure特定的IaC工具,允许对资源进行部署,维护和追踪。

ARM通过声明性模板而非脚本来管理基础结构。Azure Resource Manager templates (ARM templates)是定义项目基础结构和配置的JSON文件,包含六个顶级元素schemacontentVersionparametersvariablesresourcesoutput

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "",
  "apiProfile": "",
  "parameters": {  },
  "variables": {  },
  "functions": [  ],
  "resources": [  ],
  "outputs": {  }
}

我们可以使用 Visual Studio Code和模板工具扩展来创作模板;如果不熟悉 Azure 部署和 JSON 格式,也可以通过在Azure门户中导出来获取现有资源组的模板。

创建资源前可以下载模板


有了模板以后,我们可以通过Azure门户/Azure CLI/Powershell进行模板的部署。以下使用Azure CLI部署本地模板`azuredeploy.json`为例。若要部署外部模板,将`template-file`改为`template-uri` 参数即可。此外根据部署范围不同使用不同的命令,以下`group`为部署到资源组。还有更多参数以满足不同部署需要,例如mode参数,可以指定部署为增量更新还是完整更新。 ``` az deployment group create \ --name ExampleDeployment \ --resource-group ExampleGroup \ --template-file azuredeploy.json \ --parameters storageAccountType=Standard_GRS ```

声明性模板的好处在于你不必了解后面到底是怎么调用完成资源的配置,ARM对请求进行身份验证和授权后,将模板转换为 REST API 操作,会发送到相关资源提供程序,配置资源。
ARM处理Azure请求


使用ARM在GitHub中进行持续部署

基本了解ARM之后,我们来看一下如何在DevOps过程中加入ARM的使用,作为IaC的方式,提升软件开发的效率。以下以GitHub为例

  1. 首先将部署存储账号的ARM模板上传到Repository。
    ARM模板

  2. 我们需要通过Service Principal 来连接Azure与GitHub。这里的范围为某一资源组。

az ad sp create-for-rbac --name {myApp} --role contributor --scopes /subscriptions/{subscription-id}/resourceGroups/{MyResourceGroup} --sdk-auth
  1. 创建成功后,记录clientId, clientSecret, subscriptionIdtenantId,并在GitHub里新建Secret。
    新建Secret

  2. 以同样的方式新建AZURE_RGAZURE_SUBSCRIPTION,分别放入Service Principal的范围中,Azure资源组的名字和Azure的订阅ID,我们会在部署ARM模板时用到这些。
    Secrets

  3. 在GitHub的Action选项下,新建workflow。在右侧的marketplace,可以搜索相关模块。首先我们需要添加Azure Login,使用我们刚刚在Secret中添加的AZURE_CREDENTIALS进行认证。
    添加Azure Login

  4. 搜索并复制Deploy Azure Resource Manager (ARM) Template的内容到.yml文件中,填入相关的变量值后commit。注意根据模板的schema,这里scope选择resourcegroup。

ARMDeploy

  1. yaml文件如下。
name: CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Azure Login
        uses: Azure/login@v1
        with:
          # Paste output of `az ad sp create-for-rbac` as value of secret variable: AZURE_CREDENTIALS
          creds: '${{ secrets.AZURE_CREDENTIALS }}'
          # Set this value to true to enable Azure PowerShell Login in addition to Az CLI login
      
      - name: Deploy Azure Resource Manager (ARM) Template
        uses: Azure/arm-deploy@v1
        with:
          # Provide the scope of the deployment. Valid values are: 'resourcegroup', 'managementgroup', 'subscription'
          scope: 'resourcegroup'
          # Provide the Id of the subscription which should be used, only required for resource Group or Subscription deployments.
          subscriptionId: ${{ secrets.AZURE_SUBSCRIPTION }}
          # Provide the name of a resource group, only required for resource Group deployments.
          resourceGroupName: ${{ secrets.AZURE_RG }}
          # Specify the path or URL to the Azure Resource Manager template.
          template: ./azuredeploy.json
          # Incremental (only add resources to resource group) or Complete (remove extra resources from resource group) or Validate (only validates the template).
          deploymentMode: Incremental
          # Supply deployment parameter values.
          parameters: storageAccountType=Standard_LRS
  1. 回到action中查看情况,可以看到我们的Jobs已运行成功。
    Workflow Jobs

  2. 到Azure门户中,确认已部署成功
    部署成功

相关阅读

ARM 模板文档 | Microsoft Docs
使用 GitHub Actions 部署资源管理器模板 | Azure Docs


下篇预告 - Terraform

ARM是针对Azure的IaC工具,Terraform作为最主流的IaC工具之一,可以管理众多云服务提供商(如Azure,Google Cloud,AWS)的资源,以及定制的内部解决方案。
我们可以通过配置文件定义需要的组件,Terraform会生成执行计划,列出达到预期状态所需执行的操作,然后执行该计划以构建所需的基础结构。如果需要对配置进行更改,Terraform能够确定更改的内容并创建可以应用的增量执行计划。

posted @ 2021-03-09 15:21  懒人奶昔  阅读(450)  评论(0编辑  收藏  举报