我之前有一个可用的 terraform 设置,它在 Azure 中创建一个 Windows VM,并通过自定义脚本扩展运行引导脚本来安装和配置 SQL Server。这已经工作了几个月,但突然(我假设在特定的 Windows 映像更新之后)它决定现在安装故障转移群集功能需要重新启动,这会破坏脚本,因为在此功能之后需要运行一些步骤已安装,即运行 PowerShell Get-Cluster 命令会返回以下错误:
ERROR: Unhandled exception caught: The term 'Get-Cluster' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
如果我手动重新启动虚拟机,然后再次运行命令,现在就可以识别了。
所以,我想知道的是,除了创建安装了故障转移集群的中间映像,然后将其用作基础映像之外,还有什么方法可以处理这个问题吗?我知道我无法添加第二个自定义脚本扩展(为什么是 Microsoft,只是,为什么?)并且我正在努力想出一种更简单的方法来做到这一点。
预计
Install-WindowsFeature Failover-Clustering -IncludeAllSubFeature -IncludeManagementTools
不需要像以前那样重新启动。
预计
不需要重新启动。Install-WindowsFeature Failover-Clustering -IncludeAllSubFeature -IncludeManagementTools
Failover-Clustering
功能在计算机上安装后需要强制重新启动。不幸的是,这种重启要求是无法避免的。
一种可能的解决方法是在完成安装后重新启动虚拟机,并使用 Terraform 运行剩余的脚本,而无需手动重新启动。该脚本包括安装故障转移群集功能后的重新启动步骤。您可以使用 Terraform 中的
azurerm_virtual_machine_extension
资源添加自定义脚本扩展,该扩展可在安装该功能后重新启动虚拟机。
Terraform 代码
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "VM-resources1"
location = "West Europe"
}
resource "azurerm_virtual_network" "example" {
name = "vm-network1"
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 = "internal1"
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 = "vm-nic1"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "internal1"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.example.id
}
}
resource "azurerm_public_ip" "example" {
name = "vm-public-ip1"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
allocation_method = "Dynamic"
}
# Create Network Security Group and rules
resource "azurerm_network_security_group" "my_terraform_nsg" {
name = "vm-nsg1"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
security_rule {
name = "Allow_All"
priority = 101
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_network_interface_security_group_association" "example" {
network_interface_id = azurerm_network_interface.example.id
network_security_group_id = azurerm_network_security_group.my_terraform_nsg.id
}
resource "azurerm_windows_virtual_machine" "example" {
name = "cluster-vm"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_F2"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"
network_interface_ids = [
azurerm_network_interface.example.id
]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
}
resource "azurerm_virtual_machine_extension" "test" {
name = "CustomScripts"
virtual_machine_id = azurerm_windows_virtual_machine.example.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
settings = <<SETTINGS
{
"commandToExecute": "powershell Add-WindowsFeature Failover-Clustering -IncludeAllSubFeature -IncludeManagementTools"
}
SETTINGS
}
resource "null_resource" "powershell" {
triggers = {
# Add triggers if necessary
}
provisioner "local-exec" {
command = <<-EOT
pwsh -Command "Invoke-AzVMRunCommand -ResourceGroupName 'VM-resources1' -VMName 'cluster-vm' -CommandId 'RunPowerShellScript' -ScriptPath '/home/venkat/VM/additional_script.ps1'"
EOT
}
}
additional_script.ps1
shutdown /r
Write-Host 'Waiting for VM to restart...'
Start-Sleep -Seconds 150
Write-Host 'VM has restarted. Resuming script execution...'
Get-Cluster
Terraform 应用
代码执行故障转移集群功能安装并从脚本运行其他命令。