我有一个关于如何使用打包程序在 Windows 虚拟机中运行 .NET 应用程序(一个简单的服务器)的问题。
这是我的配置: 这是为虚拟机安装所有必需依赖项的脚本:
# Install .NET Core Runtime
Invoke-WebRequest -Uri https://dot.net/v1/dotnet-install.ps1 -OutFile dotnet-install.ps1
.\dotnet-install.ps1 -Channel 8.0 -Runtime aspNetCore
Invoke-WebRequest -Uri https://dot.net/v1/dotnet-install.ps1 -OutFile dotnet-install.ps1
.\dotnet-install.ps1 -Channel 8.0
Remove-Item dotnet-install.ps1
# Check .NET is installed
(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full').Version
# Set PATH environment variable to include dotnet directory
[System.Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Users\Administrator\AppData\Local\Microsoft\dotnet\", [EnvironmentVariableTarget]::Machine)
# Install Git
Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.35.1.windows.1/Git-2.35.1-64-bit.exe -OutFile Git-2.35.1-64-bit.exe
Start-Process -FilePath Git-2.35.1-64-bit.exe -ArgumentList "/SILENT" -Wait
Remove-Item Git-2.35.1-64-bit.exe
# Set PATH environment variable to include Git directory
[System.Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Program Files\Git\bin\;C:\Program Files\Git\cmd\", [EnvironmentVariableTarget]::Machine)
# Install IIS (Internet Information Services) if not already installed
Install-WindowsFeature -Name Web-Server -IncludeManagementTools
# Install Web Management Service
Install-WindowsFeature -Name Web-Mgmt-Service
这是在虚拟机内构建 .NET 应用程序的第二个脚本
# Define the directory where you want to clone the application
$cloneDirectory = "C:\DotNetApp"
$iisDirectory = "C:\WebApiDeployed"
# Create the directory if it doesn't exist
if (-not (Test-Path -Path $cloneDirectory -PathType Container)) {
New-Item -Path $cloneDirectory -ItemType Directory
}
# Create the directory if it doesn't exist
if (-not (Test-Path -Path $iisDirectory -PathType Container)) {
New-Item -Path $iisDirectory -ItemType Directory
}
# Clone the application from GitHub
git clone https://github.com/biancapistea/webApi.git $cloneDirectory
# Navigate to the cloned directory
Set-Location $cloneDirectory
# Build the application
dotnet build
Copy-Item -Path "$cloneDirectory\bin\Debug\net8.0\*" -Destination $iisDirectory -Recurse
New-WebSite -Name "webApi" -PhysicalPath $iisDirectory -Port 80 -HostHeader "localhost"
# Run the application
# dotnet run
这是我的打包文件: 此版本在 Windows Server 之上构建映像并仅安装必要的依赖项:
packer {
required_plugins {
amazon = {
source = "github.com/hashicorp/amazon"
version = "~> 1"
}
}
}
variable "aws_access_key" {
type = string
default = "${env("AWS_ACCESS_KEY_ID")}"
}
variable "aws_secret_access_key" {
type = string
default = "${env("AWS_SECRET_ACCESS_KEY")}"
}
variable "aws_region" {
type = string
default = "${env("AWS_REGION")}"
}
# "timestamp" template function replacement
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
source "amazon-ebs" "autogenerated_1" {
ami_name = "packer prerequisites ${local.timestamp}"
instance_type = "t2.micro"
source_ami = "ami-0381840963c35cb1f"
access_key = var.aws_access_key
secret_key = var.aws_secret_access_key
region = var.aws_region
communicator = "winrm"
user_data_file = "./bootstrap_win.txt"
winrm_password = "SuperS3cr3t!!!!"
winrm_username = "Administrator"
}
build {
sources = ["source.amazon-ebs.autogenerated_1"]
provisioner "powershell" {
script = "./prerequisites.ps1"
}
post-processor "manifest" {
output = "prerequisites_ami.json"
strip_path = true
}
}
第二个在 AWS 上基于之前创建的已安装依赖项的映像构建映像。
packer {
required_plugins {
// amazon = {
// source = "github.com/hashicorp/amazon"
// version = "~> 1"
// }
}
}
variable "aws_access_key_app" {
type = string
default = "${env("AWS_ACCESS_KEY_ID")}"
}
variable "aws_secret_access_key_app" {
type = string
default = "${env("AWS_SECRET_ACCESS_KEY")}"
}
variable "aws_region_app" {
type = string
default = "${env("AWS_REGION")}"
}
variable "prerequisites_ami" {
type = string
default = "ami-0d0f70de1aeb0ce6"
}
# "timestamp" template function replacement
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
source "amazon-ebs" "autogenerated_1" {
ami_name = "packer application ${local.timestamp}"
instance_type = "t2.micro"
source_ami = var.prerequisites_ami #id of the windows-prerequisites image generated by me
access_key = var.aws_access_key_app
secret_key = var.aws_secret_access_key_app
region = var.aws_region_app
communicator = "winrm"
user_data_file = "./bootstrap_win.txt"
winrm_password = "SuperS3cr3t!!!!"
winrm_username = "Administrator"
}
build {
sources = ["source.amazon-ebs.autogenerated_1"]
provisioner "powershell" {
script = "./application.ps1"
}
}
现在,当我使用 terraform 在 AWS 上创建负载均衡器时(我使用之前创建的映像 AMI,其中包含安装和我的 .NET 应用程序),我尝试访问 DNS,但我只看到 Windows 服务器默认网站,而不是我的.NET 应用程序。这是链接:aws-alb-1335693886.eu-central-1.elb.amazonaws.com。
可能是什么问题?如何在图像内运行 .NET 应用程序,以便当我单击此链接时:aws-alb-1335693886.eu-central-1.elb.amazonaws.com 它将显示我的 .NET 应用程序?
PS:我对 DevOps 很陌生。谢谢您的帮助!
如果你先用docker打包你的应用程序会更好。使用虚拟机中的映像来启动应用程序。
应该可以。