无法使用 HTTP API 设置 Azure 自动缩放公式

问题描述

这是我在 SO 中的第一个问题。它与 Azure 以及我们可以使用 HTTP API 设置自动缩放公式的方式有关。

我遵循了 Authenticate Requests to Azure Batch 文档并创建了 StringToSing。它看起来像这样:

POST\n
\n
\n
\n
\n
application/json;odata=minimalMetadata\n
\n
\n
\n
\n
\n
\n
ocp-date:Wed,30 Dec 2020 13:02:22 GMT\n
/myaccountname/pools/scalingpool/enableautoscale\n
api-version:2020-09-01.12.0\n
timeout:30\n

当然,我的批处理帐户名称不是 myaccountname。我只是在这文章中使用了这个值。在控制台上,我使用的是真实帐户名。

我上面粘贴的整个字符串与相应的 Secret Key 一起散列,结果是 base64 编码。

因此,考虑到我的密钥存储在变量 secretKey 中。我使用以下 Python 代码创建 base64 字符串(如您所见,它在屏幕上打印 base64 字符串):

signature = hmac.new(secretKey.encode('utf-8'),data.encode('utf-8'),hashlib.sha256) 
byteSignature = signature.digest()
b64Signature = base64.b64encode(byteSignature)
print (b64Signature.decode('ascii'))

生成 base64 后,我使用它来创建 Authorization 标头。我把所有东西都这样组合在一起:

$ curl -v -X POST -H "Authorization: SharedKey myaccountname:BASE64_STRING" -H "Content-Type: application/json;odata=minimalMetadata" -H "ocp-date:Wed,30 Dec 2020 13:20:12 GMT" --data @autoscaling_pool_scalingpool.json "https://myaccountname.germanywestcentral.batch.azure.com/pools/scalingpool/enableautoscale?timeout=30&api-version=2020-09-01.12.0"

地点:

  1. BASE64_STRING 是之前生成的 base64 字符串
  2. autoscaling_pool_scalingpool.json 是存储我的自动缩放公式的文件
  3. scalingpool 是我的 Kubernetes 集群中节点池的名称,我想启用自动缩放。

不幸的是,结果如下:

*  subject: C=US; ST=WA; L=Redmond; O=Microsoft Corporation; CN=*.germanywestcentral.batch.azure.com
*  start date: Nov 14 04:33:26 2020 GMT
*  expire date: Nov  9 04:33:26 2021 GMT
*  subjectAltName: host "myaccountname.germanywestcentral.batch.azure.com" matched cert's "*.germanywestcentral.batch.azure.com"
*  issuer: C=US; O=Microsoft Corporation; CN=Microsoft Azure TLS Issuing CA 05
*  SSL certificate verify ok.
* Using HTTP2,server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x55b584497920)
> POST /pools/scalingpool/enableautoscale?timeout=30&api-version=2020-09-01.12.0 HTTP/2
> Host: myaccountname.germanywestcentral.batch.azure.com
> user-agent: curl/7.72.0
> accept: */*
> authorization: SharedKey myaccountname:BASE64_STRING
> content-type: application/json;odata=minimalMetadata
> ocp-date:Wed,30 Dec 2020 13:20:12 GMT
> content-length: 237
> 
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
* We are completely uploaded and fine
< HTTP/2 403 
< content-length: 906
< content-type: application/json;odata=minimalMetadata
< server: Microsoft-HTTPAPI/2.0
< request-id: a9b609a2-4b15-453d-904c-b0fc588527a0
< strict-transport-security: max-age=31536000; includeSubDomains
< x-content-type-options: nosniff
< dataserviceversion: 3.0
< date: Wed,30 Dec 2020 13:22:44 GMT
< 
{
  "odata.Metadata":"https://myaccountname.germanywestcentral.batch.azure.com/$Metadata#Microsoft.Azure.Batch.Protocol.Entities.Container.errors/@Element","code":"AuthenticationFailed","message":{
    "lang":"en-US","value":"Server Failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:a9b609a2-4b15-453d-904c-b0fc588527a0\nTime:2020-12-30T13:22:44.1054663Z"
  },"values":[
    {
      "key":"AuthenticationErrorDetail","value":"The MAC signature found in the HTTP request 'BASE64_STRING' is not the same as any computed signature. Server used following string to sign: 'POST\n\n\n237\n\napplication/json;odata=minimalMetadata\n\n\n\n\n\n\nocp-date:Wed,30 Dec 2020 13:20:12 GMT\n/myaccountname/pools/scalingpool/enableautoscale\napi-version:2020-09-01.12.0\ntimeout:30'."
    }
  ]
* Connection #0 to host myaccountname.germanywestcentral.batch.azure.com left intact
}

如您所见,我无法进行身份验证。真正吸引我的是响应中键 AuthenticationErrorDetail 的值。它包括我用来唱我的请求的字符串,但它看起来有点不同:

POST\n\n\n237\n\napplication/json;odata=minimalMetadata\n\n\n\n\n\n\nocp-date:Wed,30 Dec 2020 13:20:12 GMT\n/myaccountname/pools/scalingpool/enableautoscale\napi-version:2020-09-01.12.0\ntimeout:30

在 POST 方法之后,我们应该只有 5 次“\n”。但是,有一个 237 数字!我绝对确定我没有在要签名的字符串中包含这个数字。另外,我不明白这是什么意思。

我在 C# 和 .Net 中找到了一些示例,但我不想使用它。据我所知,这些示例以相同的格式创建要签名的字符串。

有什么建议吗?

更新 1

我可以得到 Stanley Gong 下面建议的代码。然而,整体解决方案并不是我所期望的。

我知道 Horizo​​ntal Pod Autoscaler 和 Cluster Autoscaler。尽管如此,并且不想因为部署/Pod 未运行或因为系统指标(cpu、内存等)如此说明而扩展/缩减节点。

鉴于我在 Internet 上找到的示例,批处理帐户以及缩放公式适合我的需求。正如我在这文章中发布的那样,我希望在工作日/小时内保持机器运行。

现在我有集群、一个资源组 (rg1) 和一个批处理帐户。批处理帐户属于资源组 rg2。但集群似乎属于资源组 rg2

我会继续深入研究这个问题。

解决方法

如果您使用 Python 并且想要设置池自动缩放公式,使用 Azure 批处理 Python SDK 将比生成授权标头和发出 HTTP 请求容易得多。

试试下面的代码:

import azure.batch._batch_service_client as batch
import azure.batch.batch_auth as batchauth

batch_url= '<batch service url>'
batch_account = '<account name>'
pool_id = '<id of pool you want to set formula>'
key = '<account key>'

creds = batchauth.SharedKeyCredentials(batch_account,key)
batch_client = batch.BatchServiceClient(
        creds,batch_url=batch_url)

myAutoScaleFormula = "$TargetDedicatedNodes = (time().weekday == 1 ? 5:1);"

batch_client.pool.enable_auto_scale(pool_id=pool_id,auto_scale_formula=myAutoScaleFormula)

结果:

enter image description here

您可以找到有关适用于 Python here 的 Azure 批处理 SDK 的更多示例代码。