【问题标题】:Get the status of google cloud container cluster provisioning using api python client使用 api python 客户端获取谷歌云容器集群配置状态
【发布时间】:2017-12-03 06:57:12
【问题描述】:

我正在使用用于谷歌云平台的 API python 客户端创建一个容器引擎集群。我已经成功完成了容器创建,现在我需要应用一些 yaml 配置,但在应用任何 kubernetes yaml 配置之前,应该配置集群,否则 kubernetes API 不可用。 我需要在一个请求中完成这两个(容器创建和应用 yaml 配置)的事情。 如何使用 api 获取集群的配置状态?

这是我尝试过的:

集群创建后: 来自views.py:

print('Fetching Cluster configs ....')
cc = subprocess.call(
                'gcloud container clusters get-credentials ' + deployment.deploymentName.lower() + ' --zone ' + deployment.region + ' --project ' + deployment.project,
                shell=True)
print(cc)
while cc == 1:
     cc = subprocess.call(
                    'gcloud container clusters get-credentials ' + deployment.deploymentName.lower() + ' --zone ' + deployment.region + ' --project ' + deployment.project,
                    shell=True)
     print(cc)

请帮帮我!

提前致谢!

【问题讨论】:

    标签: python google-cloud-platform google-kubernetes-engine google-api-python-client


    【解决方案1】:

    这就是我在代码中的做法:

    """
    If you have a credentials issue, run:
    
    gcloud beta auth application-default login
    
    """
    import time
    
    import googleapiclient.discovery
    
    service = googleapiclient.discovery.build('container', 'v1')
    clusters_resource = service.projects().zones().clusters()
    operations_resource = service.projects().zones().operations()
    
    
    def create_cluster(project_id, zone, config, async=False):
        req = clusters_resource.create(projectId=project_id, zone=zone, body=config)
        operation = req.execute()
    
        if async:
            return
    
        while operation['status'] == 'RUNNING':
            time.sleep(1)
            req = operations_resource.get(projectId=project_id, zone=zone, operationId=operation['name'])
            operation = req.execute()
    
        return operation['status'] == 'DONE'
    

    【讨论】:

    • 嗨@Anthony!感谢您的回答,请您帮我获取已创建集群的 IP 地址!
    【解决方案2】:

    您要查找的是从create cluster call 返回ID 的操作的状态。然后您需要获取操作(通过容器 API,而不是计算 API)并检查操作的状态以查看它是否已完成。完成后,您可以通过查看操作中的状态消息来确定是否存在错误。如果为空,则创建集群 API 调用成功。如果它不为空,则呼叫失败,状态消息将告诉您原因。一旦创建集群的操作完成,get-credentials 调用就会成功。

    【讨论】:

    • 如何从响应集群对象中检索状态,我已经尝试过:response['status'] 但它没有返回实际状态,它返回类似{'name': 'newyear', 'initialNodeCount': 8, 'status': 'PROVISIONING', } 的意思是完整的集群对象!
    • cluster status 可以是 PROVISIONING、RUNNING、RECONCILING、STOPPING 或 ERROR。在创建集群的操作完成的同时,集群状态应该从 PROVISIONING 转变为 RUNNING。
    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 2019-02-16
    • 1970-01-01
    • 2018-02-21
    • 2018-03-26
    • 2018-07-15
    • 1970-01-01
    • 2017-11-23
    相关资源
    最近更新 更多