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

2024-06-16 09:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一个问题。它与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。我刚刚在这篇文章中使用了这个值。在控制台上,我使用的是真实的帐户名

我在上面粘贴的整个字符串与相应的密钥一起散列,结果是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,我就用它来创建授权头。我把所有的东西都放在一起:

$ 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建议的代码。然而,总体解决方案并不是我所期望的

我知道水平吊舱自动缩放和集群自动缩放。尽管如此,并且不想放大/缩小节点,因为部署/吊舱没有运行,或者因为系统指标(CPU、内存等)表明了这一点

考虑到我在互联网上找到的例子,批量帐户的事情,连同一个缩放公式是什么适合我的需要。正如我在这篇文章中所说,我希望在工作日/小时内保持机器运行

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

我会在这方面继续挖掘


Tags: 字符串comjsonhttpdatebatchazurepost
1条回答
网友
1楼 · 发布于 2024-06-16 09:16:32

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

您可以找到更多关于Azure batch SDK for Pythonhere的示例代码

相关问题 更多 >