首页 > 解决方案 > 无法使用 HTTP API 设置 Azure 自动缩放公式

问题描述

这是我在SO中的第一个问题。它与Azure我们可以使用HTTP API.

我已按照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响应中 key 的值。它包括我用来发出请求的字符串,但看起来有点不同:

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

我会继续深入研究这一点。

标签: pythonazureazure-aksazure-batch

解决方案


如果您使用 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)

结果:

在此处输入图像描述

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


推荐阅读