【问题标题】:Unable to set Azure autoscale formula using HTTP API无法使用 HTTP API 设置 Azure 自动缩放公式
【发布时间】:2020-12-30 13:52:18
【问题描述】:

这是我在 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 azure-aks azure-batch


    【解决方案1】:

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

    结果:

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

    【讨论】:

    • 您好,非常感谢您的回答。老实说,AKS 并不是第一个可以让签名请求正常工作的云提供商。最后,它们都向您解释了如何完成工作,但只有在您依赖某些 SDK 时,事情才能“真正”完成。我应用了您的代码,但现在遇到了一个新错误:Message: {'additional_properties': {}, 'lang': 'en-US', 'value': 'The specified pool does not exist.\nRequestId:&lt;REQUEST_ID&gt;\nTime:&lt;TIMESTAMP&gt;'
    • 这条评论是为了澄清最后一条。原来我的批处理账户试图控制的节点池,并不是我想要控制的节点池。这篇文章的初始消息有更新,提供了更多详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2019-08-16
    相关资源
    最近更新 更多