【问题标题】:Monitor services in azure在 Azure 中监控服务
【发布时间】:2021-08-21 21:44:16
【问题描述】:

错误信息是:

配置存在一些问题,如下所述。 Terraform 配置必须在初始化之前有效,以便 Terraform 可以确定需要安装哪些模块和提供程序。

这就是我正在做的事情:

  • 在 azvm.tf 中编写 terraform 脚本以创建 VM。

  • 在 variables.tf 文件中定义变量 resourcegroup、location 和 pub_key,并在 azvm.tf 文件中使用字符串插值语法调用这些变量。

  • 创建了具有以下功能的虚拟机:

    a) Ubuntu 18.04 服务器 b) VM 名称:任何自定义名称
    c) Admin_username:任何自定义名称
    d) 禁用密码验证
    e) 尺寸:标准 DS1_v2
    f) 允许 ssh、http、https
    的流量 g) 使用上述步骤中生成的公共 ssh 密钥

在 vi 命令的帮助下,我们创建 variables.tf 文件为:

variable "resourcegroup"
{
default = "user-pbtwiiiuofyu"
}
variable "location"
{ 
default = ["East US"]
}
variable "pub_key"
{ 
default = ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDH7+uZHdf3SXSfz3Z80FCahr1Bt2t2u+RBlgE/QPN5Qy/CdrpcNH/8oncuDLKW5r+Ie+J5yNVwJz96gdLAQPNMmAYEPGjNct63kQp8hZPlWFdQIEyvHZwwP4YWf5vyRjozQ1PZN/0WyBjqTYmJh3z6haFxqnmIf6a9G8JldjtTPuF2oAnHaUDpqD4qBy/+OSN7CX1Lowny83NLNFoTgmm2npU+RIS2FDYpf1z30tejZSO0V3Z+iT3Xddlzh2NwunzOPv9avzwrLbSSvdbugcXTSPURMLgXwCapH9oHt4RCB/2mgsrRcLQb14FKG9UVBwzh07v2w8fDAHLnv2g4bpnhIyZMpX3zLO9mtQ4ix9DpyqPMqLV7b1uTSyj9t3UEcfprGWRPDJgkpGXEzP0iQgrGlfpctL/6/Hy009123uzb4ugXwndPbuQnPtj45PX2D4zEOFPrlvFBofazuGp65wxdzlzZpGsgqxiZQbdFCU4eQV2Y2kBPj/uv/3DOI2x27ML6i9D/shiVz/sSpryjKRHIl9bU7rU4vClgED/1NuhhTGLk5qPFOjsvvHpIORu5zFKNmeGFHW6e0TJnJMsckszseQuyjqUMkBoFV0NIOyS/mGLrxxzOTpzQyM9duat4T3kGLjjy+ozpHRaahXO79I91pDP66enitmj861kS6G5chQ== root@6badb6ae71d1"]
}

而 vi 命令我们创建了 azvm.tf 文件为

az vm create -n Myuyuuy5Vm -g var.resourcegroup --ssh-key-values var.pub_key

我们还有另一项类似的任务,但相对来说不太容易,即-

  • 在 azmonitor.tf 中编写 terraform 脚本以创建存储帐户, 存储容器,用于监控和发送日志报告的存储 blob 每天。

  • 在 variables.tf 中定义变量资源组和位置 文件,并使用字符串在 azmonitor.tf 文件中调用这些变量 插值语法

variables.tf as-

variable "resourcegroup"
{
default = "user-pbtwiiiuofyu"
}
variable "location"
{ 
default = ["East US"]
}

azmonitor.tf 我们创建为-

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = var.resourcegroup
  location = var.location
}

resource "azurerm_storage_account" "example" {
  name                = "sa123321123"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.location
  sku                 = "Standard_LRS"
}

resource "azurerm_storage_container" "example" {
  name                = "sc123321123"
  resource_group_name = azurerm_resource_group.example.name
  account_key         = azurerm_storage_account.eample.name
  public_access       = "blob"
}

resource "azurerm_storage_blob" "example" {
  name                = "sb123321123"
  resource_group_name = azurerm_resource_group.example.name
  account_key         = azurerm_storage_account.eample.name
  source              = azurerm_storage_container.name
}

【问题讨论】:

  • 我认为您的概念有误。因此,您似乎正在混合使用两种不同的方式来创建 VM,第一种是使用 terraform - 这是您的任务,而另一种创建 VM 的方式是使用 Az CLI

标签: azure terraform


【解决方案1】:

如上所述,您在这里混合使用了 Terraform 和 Az CLI - 这是不对的。您应该使用其中一个。

您的任务似乎是使用 Terraform 创建一个 Linux VM。您需要一个用于主要代码/terraform 对象的“主”Terraform 文件,然后需要一个用于变量的“变量”Terraform 文件。从技术上讲,当您执行 Terraform 初始化/计划时,Terraform 将展平同一目录中的每个 Terraform 文件 - 这意味着您可以将所有内容放在一个 .tf 文件中。

但是,就本教程而言,最好将两者分开,以便轻松管理它们。 This 是您想要使用的 Terraform 代码 - 它将帮助您创建 Linux VM。

为简单起见,我在此处放置了一个示例,该示例链接回您的 variables.tf 中的变量

terraform {
  required_version = "= 0.14.10" //change this accordingly
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.55.0"
    }
  }
}
resource "azurerm_resource_group" "example" {
  name     = var.resourcegroup
  location = var.location
}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location 
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_linux_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = "Standard_DS1_v2"
  admin_username      = "Admin_username"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

  admin_ssh_key {
    username   = "adminuser"
    public_key = file("~/.ssh/id_rsa.pub") //put your public key here
  }

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }
}

验证完上面的代码后,您还需要两个对象,

  1. azurerm_network_security_group 将帮助您创建规则以允许与 VM 的入站连接
  2. azurerm_subnet_network_security_group_association 将帮助您将子网附加到 NSG。

获得完整代码后,您可以运行 terraform init 来初始化模块,然后运行 ​​terraform plan 来验证您的计划,最后运行 terraform apply 来部署 VM。

对于 blob 部分:

variable.tf


variable "resourcegroup" {
default = "user-pbtwiiiuofyu"
}

variable "location" { 
default = "East US"
}

azmonitor.tf

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = var.resourcegroup
  location = var.location
}

resource "azurerm_storage_account" "example" {
  name                     = "examplestoracc"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_container" "example" {
  name                  = "content"
  storage_account_name  = azurerm_storage_account.example.name
  container_access_type = "private"
}

resource "azurerm_storage_blob" "example" {
  name                   = "my-awesome-content.zip"
  storage_account_name   = azurerm_storage_account.example.name
  storage_container_name = azurerm_storage_container.example.name
  type                   = "Block"
  source                 = "./some-local-file.zip"
}

【讨论】:

  • 对不起,你是什么意思? iI 文件名可以吗?
  • 在 variables.tf 文件中定义变量资源组、位置和 pub_key,并使用字符串插值语法在 azvm.tf 文件中调用这些变量。我们需要创建 2 个 .tf 文件这是主要部分
  • 你的 variable.tf 是正确的 - 你的 azvm.tf 是错误的 - 你应该用我提供的代码替换 az vm create -n Myuyuuy5Vm -g var.resourcegroup --ssh-key-values var.pub_key 然后改进它。 :)
  • 是的,这是正确的!但是发送日志会有所不同,我不确定如何实现。顺便说一句,对于变量 "location" { default = ["East US"] },将其更改为 `variable "location" { default = "East US" }。如果您对答案感到满意,请投票/标记为答案,以帮助其他也有类似问题的人! :)
  • $ terraform init 配置有一些问题,如下所述。 Terraform 配置必须在初始化之前有效,以便 Terraform 可以确定需要安装哪些模块和提供程序。
猜你喜欢
  • 2014-04-16
  • 1970-01-01
  • 2013-11-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
相关资源
最近更新 更多