stop GKE worker nodes while ASG is turned off

I have created python script which basically turn off the autoscale of GKE cluster , then stop the underlying nodes in each MIG(Managed Instance Group) present zone wise. Now the problem is while stopping the instances , instances being recreated and status is getting changed from UNKNOWN to READY state immediate after stopping the instance. Though I turned off autoscale group before instance being stopped. on the other hand this is not the case while I manually stop the instance from console itself in each MIG per ZONE. why its happening ?can anyone suggest what needs to be checked or what more I need to apply on the code.

I have checked autoscale group is getting turned off without any issues. For your kind info autoscaling at MIG level is off and also health check also disabled at MIG level. Autoscale is there only at Node Pool level.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import subprocess
import json
import argparse
import time
def get_node_pool_name(cluster_name, project_name, region_name):
try:
cmd = [
'gcloud', 'container', 'clusters', 'describe', cluster_name,
'--project', project_name,
'--region', region_name,
'--format', 'json'
]
...
...
return node_pool_name
except Exception as e:
print(f"Error occurred while getting node pool name: {str(e)}")
raise
def disable_autoscaler(cluster_name, project_name, region_name, node_pool_name):
try:
cmd = [
'gcloud', 'container', 'node-pools', 'update', node_pool_name,
'--cluster', cluster_name,
'--project', project_name,
'--region', region_name,
'--no-enable-autoscaling'
]
...
...
except Exception as e:
print(f"Error occurred while disabling node pool autoscaler: {str(e)}")
raise
def get_instance_groups(cluster_name, project_name, region_name):
try:
cmd = [
'gcloud', 'container', 'clusters', 'describe', cluster_name,
'--project', project_name,
'--region', region_name,
'--format', 'json'
]
...
...
return instance_groups
except Exception as e:
print(f"Error occurred while getting instance-groups: {str(e)}")
raise
def get_instances(instance_group_name, project_name, zone):
try:
cmd = [
'gcloud', 'compute', 'instance-groups', 'list-instances', instance_group_name,
'--project', project_name,
'--zone', zone,
'--format', 'json'
]
...
...
return instance_names
except Exception as e:
print(f"Error occurred while getting instances: {str(e)}")
raise
def stop_instances(instance_names, project_name, zone):
try:
for instance in instance_names:
cmd = [
'gcloud', 'compute', 'instances', 'stop', instance,
'--project', project_name,
'--zone', zone
]
subprocess.run(cmd, check=True)
print(f"Instance stopped: {instance} in zone {zone}")
except Exception as e:
print(f"Error occurred while stopping instances: {str(e)}")
raise
def main(cluster_name, project_name, region_name):
node_pool_name = get_node_pool_name(cluster_name, project_name, region_name)
disable_autoscaler(cluster_name, project_name, region_name, node_pool_name)
time.sleep(60)
instance_groups = get_instance_groups(cluster_name, project_name, region_name)
for instance_group, zone in instance_groups:
instance_names = get_instances(instance_group, project_name, zone)
stop_instances(instance_names, project_name, zone)
time.sleep(30)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Disable GKE autoscaler and stop instances.")
parser.add_argument('cluster_name', type=str, help='Name of the GKE cluster')
parser.add_argument('project_name', type=str, help='Google Cloud project name')
parser.add_argument('region_name', type=str, help='Region name of the GKE cluster')
args = parser.parse_args()
main(args.cluster_name, args.project_name, args.region_name)
</code>
<code>import subprocess import json import argparse import time def get_node_pool_name(cluster_name, project_name, region_name): try: cmd = [ 'gcloud', 'container', 'clusters', 'describe', cluster_name, '--project', project_name, '--region', region_name, '--format', 'json' ] ... ... return node_pool_name except Exception as e: print(f"Error occurred while getting node pool name: {str(e)}") raise def disable_autoscaler(cluster_name, project_name, region_name, node_pool_name): try: cmd = [ 'gcloud', 'container', 'node-pools', 'update', node_pool_name, '--cluster', cluster_name, '--project', project_name, '--region', region_name, '--no-enable-autoscaling' ] ... ... except Exception as e: print(f"Error occurred while disabling node pool autoscaler: {str(e)}") raise def get_instance_groups(cluster_name, project_name, region_name): try: cmd = [ 'gcloud', 'container', 'clusters', 'describe', cluster_name, '--project', project_name, '--region', region_name, '--format', 'json' ] ... ... return instance_groups except Exception as e: print(f"Error occurred while getting instance-groups: {str(e)}") raise def get_instances(instance_group_name, project_name, zone): try: cmd = [ 'gcloud', 'compute', 'instance-groups', 'list-instances', instance_group_name, '--project', project_name, '--zone', zone, '--format', 'json' ] ... ... return instance_names except Exception as e: print(f"Error occurred while getting instances: {str(e)}") raise def stop_instances(instance_names, project_name, zone): try: for instance in instance_names: cmd = [ 'gcloud', 'compute', 'instances', 'stop', instance, '--project', project_name, '--zone', zone ] subprocess.run(cmd, check=True) print(f"Instance stopped: {instance} in zone {zone}") except Exception as e: print(f"Error occurred while stopping instances: {str(e)}") raise def main(cluster_name, project_name, region_name): node_pool_name = get_node_pool_name(cluster_name, project_name, region_name) disable_autoscaler(cluster_name, project_name, region_name, node_pool_name) time.sleep(60) instance_groups = get_instance_groups(cluster_name, project_name, region_name) for instance_group, zone in instance_groups: instance_names = get_instances(instance_group, project_name, zone) stop_instances(instance_names, project_name, zone) time.sleep(30) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Disable GKE autoscaler and stop instances.") parser.add_argument('cluster_name', type=str, help='Name of the GKE cluster') parser.add_argument('project_name', type=str, help='Google Cloud project name') parser.add_argument('region_name', type=str, help='Region name of the GKE cluster') args = parser.parse_args() main(args.cluster_name, args.project_name, args.region_name) </code>
import subprocess
import json
import argparse
import time

def get_node_pool_name(cluster_name, project_name, region_name):
    try:
        cmd = [
            'gcloud', 'container', 'clusters', 'describe', cluster_name,
            '--project', project_name,
            '--region', region_name,
            '--format', 'json'
        ]
        ...
        ...
        return node_pool_name
    except Exception as e:
        print(f"Error occurred while getting node pool name: {str(e)}")
        raise

def disable_autoscaler(cluster_name, project_name, region_name, node_pool_name):
    try:
        cmd = [
            'gcloud', 'container', 'node-pools', 'update', node_pool_name,
            '--cluster', cluster_name,
            '--project', project_name,
            '--region', region_name,
            '--no-enable-autoscaling'
        ]
         ...
        ...

        
    except Exception as e:
        print(f"Error occurred while disabling node pool autoscaler: {str(e)}")
        raise


def get_instance_groups(cluster_name, project_name, region_name):
    try:
        cmd = [
            'gcloud', 'container', 'clusters', 'describe', cluster_name,
            '--project', project_name,
            '--region', region_name,
            '--format', 'json'
        ]
        ...
        ...

    
        return instance_groups
    except Exception as e:
        print(f"Error occurred while getting instance-groups: {str(e)}")
        raise

def get_instances(instance_group_name, project_name, zone):
    try:
        cmd = [
            'gcloud', 'compute', 'instance-groups', 'list-instances', instance_group_name,
            '--project', project_name,
            '--zone', zone,
            '--format', 'json'
        ]
        ...
        ...

        return instance_names
    except Exception as e:
        print(f"Error occurred while getting instances: {str(e)}")
        raise

def stop_instances(instance_names, project_name, zone):
    try:
        for instance in instance_names:
            cmd = [
                'gcloud', 'compute', 'instances', 'stop', instance,
                '--project', project_name,
                '--zone', zone
            ]
            subprocess.run(cmd, check=True)
            print(f"Instance stopped: {instance} in zone {zone}")
    except Exception as e:
        print(f"Error occurred while stopping instances: {str(e)}")
        raise

def main(cluster_name, project_name, region_name):
    node_pool_name = get_node_pool_name(cluster_name, project_name, region_name)
    disable_autoscaler(cluster_name, project_name, region_name, node_pool_name)
    
    time.sleep(60)
    
    instance_groups = get_instance_groups(cluster_name, project_name, region_name)
    for instance_group, zone in instance_groups:
        instance_names = get_instances(instance_group, project_name, zone)
        stop_instances(instance_names, project_name, zone)
        
        time.sleep(30)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Disable GKE autoscaler and stop instances.")
    parser.add_argument('cluster_name', type=str, help='Name of the GKE cluster')
    parser.add_argument('project_name', type=str, help='Google Cloud project name')
    parser.add_argument('region_name', type=str, help='Region name of the GKE cluster')

    args = parser.parse_args()
    main(args.cluster_name, args.project_name, args.region_name)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật