如何缩容AutoScalingGroup中的特定实例?

How to scale down the specific instance in AutoScalingGroup?

我正在使用 Heat 实现自动缩放,下面是我的代码的一小部分:

heat_template_version: 2016-10-14
...

resources:
    corey_server_group:
        type: OS::Heat::AutoScalingGroup
        depends_on: corey-server
        properties:
            min_size: 1
            max_size: 5
            resource:
                type: CoreyLBSServer.yaml
                properties:
......

CoreyLBSServer.yaml

heat_template_version: 2016-10-14
...

resources:
    server:
    type: OS::Nova::Server
    properties:
        flavor:
......

我正在寻找一种缩小特定实例的方法,这里有一些我试过但都没有用,它总是缩小最旧的 .

1.Shutdown 实例,然后信号缩小策略。 (X)
2.According 到 this,从属性 refs_map 中找到堆栈 ID,将资源 server 标记为 unhealthy,然后发出缩减策略信号。 (X)
3.Find 来自属性 refs_map 的堆栈 ID,将堆栈状态设置为 FAILED,然后发出缩减策略信号。 (X)

我试图找出 AutoScalingGroup 在缩减时使用什么策略,从代码 heat/common/grouputils.py 中,它按“created_time”然后按名称对成员进行排序,因此最旧的成员将被删除首先在缩小时。但是有一个例外,如果设置了include_failed,失败的成员将被放在按created_time排序的列表中的第一位,然后按名称

更新

我终于成功将我的目标设置为“失败”,这里是命令:

# firstly, print the physical_resource_id of corey_server_group
openstack stack resource show -c physical_resource_id <parent_stack_id> corey_server_group

# secondly, list resources in the corey_server_group
openstack stack resource list <physical_resource_id>

# thirdly, mark the target as unhealthy  
openstack stack resource mark unhealthy <physical_resource_id> <resource_name>

# after these commands, you will see the resource_status of the target becomes "Check Failed"

但它还有另一个问题,Heat 会在缩容时同时删除“失败”和“最旧”的资源!如何仅缩小“标记为失败”目标?

经过几天的跟踪,我终于找到了缩容AutoScalingGroup中特定实例的方法。

先看一下源码:heat/common/grouputils.py#L114

Sort the list of instances first by created_time then by name. If include_failed is set, failed members will be put first in the list sorted by created_time then by name.

如您所见,include_failed默认设置为False,因此不健康的成员不会被包含在列表中,这就是为什么我的问题中描述的程序不起作用的原因.

如果你想启用缩小特定实例的功能,你必须在调用函数时显式定义include_failed=True,下面是我的部分代码:

heat/engine/resources/aws/autoscaling/autoscaling_group.py

因为我正在使用AutoScalingGroup,我需要修改两个文件:
heat/engine/resources/aws/autoscaling/autoscaling_group.py
heat/engine/resources/openstack/heat/autoscaling_group.py

重新启动 Heat 服务,然后您可以将目标标记为不健康并向策略发出信号以缩减特定实例:

openstack stack resource mark unhealthy <physical_resource_id> <resource_name>
openstack stack resource signal <parent_stack_id> your_scaledown_policy

仅供参考,table 显示了 FalseTrue 之间的不同行为 (scaling_adjustment=1)。

                   | include_failed=False (default) | include_failed=True  
                   |                                |
Scale Up           | Add one instance               | Add one instance
                   |                                |
Scale down         | Remove the oldest              | Remove the oldest
                   |                                |
Stack Update       | Nothing changed                | Nothing changed
                   |                                |
Unhealthy + Up     | Add one & remove unhealthy     | Add one & fix unhealthy
                   |                                |
Unhealthy + Down   | Remove one & remove unhealthy  | Remove the unhealthy one
                   |                                |
Unhealthy + Update | Fix unhealthy                  | Fix unhealthy