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

时间:2018-09-30 13:05:17

标签: azure-service-fabric azure-keyvault azure-vm-scale-set azure-msi

我想利用MSI,例如可以访问托管在由Azure Service Fabric VMSS托管的容器中的应用程序(尤其是对我来说:Azure Functions运行时)中的KeyVault。

我需要做什么来实现这一目标?

1 个答案:

答案 0 :(得分:3)

基于对此issue的提示:

步骤1-向VMSS添加身份

为群集Microsoft.Compute/virtualMachineScaleSets资源扩展ARM模板。在资源的根级别上添加identity元素,就像使用properties

...
  "identity": {
    "type": "SystemAssigned"
  },      
...

(重新)部署群集。

第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

并修改了Dockerfile /容器ENTRY:

...
ENTRYPOINT ["powershell.exe","C:\\entry.PS1"]
  

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

第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

我仍然遇到此错误

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

要解决此问题,我必须re-image the VMSS nodes