如何将托管服务标识添加到Azure VM Scaleset或Service Fabric中托管的容器中?

Kai*_*ter 1 azure-service-fabric azure-keyvault azure-vm-scale-set azure-managed-identity

我想利用MSI来访问托管在Azure Service Fabric VMSS中的容器内的应用程序(特别是对我来说:Azure Functions运行时)中的KeyVault。

我需要怎么做才能做到这一点?

Kai*_*ter 5

根据有关此问题的提示:

第1步-向VMSS添加身份

扩展用于群集Microsoft.Compute/virtualMachineScaleSets资源的ARM模板。identity在资源的根级别添加元素,例如properties

...
  "identity": {
    "type": "SystemAssigned"
  },      
...
Run Code Online (Sandbox Code Playgroud)

(重新)部署群集。

第2步-将路由添加到容器

在Windows容器中,默认情况下,到MSI终结点的路由不起作用。为此,我添加了一个入口脚本,例如Entry.PS1(不要忘记添加容器的原始ENTRYPOINT- ServiceMonitor.exe在我的情况下,因为我有IIS容器):

Write-Host "adding route for Managed Service Identity"
$gateway = (Get-NetRoute | Where-Object {$_.DestinationPrefix -eq '0.0.0.0/0'}).NextHop
$arguments = 'add','169.254.169.0','mask','255.255.255.0',$gateway
&'route' $arguments

$response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net%2F' -Method GET -Headers @{Metadata="true"} -UseBasicParsing
Write-Host "MSI StatusCode :" $response.StatusCode

C:\ServiceMonitor.exe w3svc
Run Code Online (Sandbox Code Playgroud)

并修改了Dockerfile /容器ENTRY:

...
ENTRYPOINT ["powershell.exe","C:\\entry.PS1"]
Run Code Online (Sandbox Code Playgroud)

背景:route add在入口点级别添加not将在构建时执行该语句并将路由添加到构建主机/容器

步骤3-可选的重新映像VMSS节点

但是,我仍然遇到现有群集的问题。在访问令牌端点时

Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net%2F' -Method GET -Headers @{Metadata="true"} -UseBasicParsing
Run Code Online (Sandbox Code Playgroud)

我仍然遇到这个错误

Invoke-WebRequest : Unable to connect to the remote server
At line:1 char:1
+ Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oaut ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Run Code Online (Sandbox Code Playgroud)

要解决此问题,我必须重新映像VMSS节点