简体   繁体   English

如何使用 google-cloud-container 模块在 python 中的 google 云平台中创建 kubernetes 集群

[英]How to create kubernetes cluster in google cloud platform in python using google-cloud-container module

I'm trying to create kubernetes cluster on google cloud platform through python (3.7) using google-cloud-container module.我正在尝试使用 google-cloud-container 模块通过 python (3.7) 在 google 云平台上创建 kubernetes 集群。

Created kubernetes cluster through google cloud platform and was able to successfully retrieve details for that cluster using google-cloud container (python module).通过谷歌云平台创建了 kubernetes 集群,并能够使用 google-cloud 容器(python 模块)成功检索该集群的详细信息。

I'm trying now to create kubernetes cluster through this module.我现在正在尝试通过这个模块创建 kubernetes 集群。 I created a JSON file with required key values and passed it as parameter, but i'm getting errors.我创建了一个具有所需键值的 JSON 文件并将其作为参数传递,但出现错误。 Would appreciate if provided a sample code for creating kubernetes cluster in google cloud platform.如果提供在谷歌云平台中创建 kubernetes 集群的示例代码,将不胜感激。 Thank you in advance.先感谢您。

from google.oauth2 import service_account
from google.cloud import container_v1



class GoogleCloudKubernetesClient(object):

    def __init__(self, file, project_id, project_name, zone, cluster_id):
        credentials = service_account.Credentials.from_service_account_file(
            filename=file)
        self.client = container_v1.ClusterManagerClient(credentials=credentials)
        self.project_id = project_id
        self.zone = zone

    def create_cluster(self, cluster):
        print(cluster)
        response = self.client.create_cluster(self.project_id, self.zone, cluster=cluster)
        print(f"response for cluster creation: {response}")


def main():
    cluster_data = {
            "name": "test_cluster",
            "masterAuth": {
                "username": "admin",
                "clientCertificateConfig": {
                    "issueClientCertificate": True
                }
            },
            "loggingService": "logging.googleapis.com",
            "monitoringService": "monitoring.googleapis.com",
            "network": "projects/abhinav-215/global/networks/default",
            "addonsConfig": {
                "httpLoadBalancing": {},
                "horizontalPodAutoscaling": {},
                "kubernetesDashboard": {
                    "disabled": True
                },
                "istioConfig": {
                    "disabled": True
                }
            },
            "subnetwork": "projects/abhinav-215/regions/us-west1/subnetworks/default",
            "nodePools": [
                {
                    "name": "test-pool",
                    "config": {
                        "machineType": "n1-standard-1",
                        "diskSizeGb": 100,
                        "oauthScopes": [
                            "https://www.googleapis.com/auth/cloud-platform"
                        ],
                        "imageType": "COS",
                        "labels": {
                            "App": "web"
                        },
                        "serviceAccount": "abhinav@abhinav-215.iam.gserviceaccount.com",
                        "diskType": "pd-standard"
                    },
                    "initialNodeCount": 3,
                    "autoscaling": {},
                    "management": {
                        "autoUpgrade": True,
                        "autoRepair": True
                    },
                    "version": "1.11.8-gke.6"
                }
            ],
            "locations": [
                "us-west1-a",
                "us-west1-b",
                "us-west1-c"
            ],
            "resourceLabels": {
                "stage": "dev"
            },
            "networkPolicy": {},
            "ipAllocationPolicy": {},
            "masterAuthorizedNetworksConfig": {},
            "maintenancePolicy": {
                "window": {
                    "dailyMaintenanceWindow": {
                        "startTime": "02:00"
                    }
                }
            },
            "privateClusterConfig": {},
            "databaseEncryption": {
                "state": "DECRYPTED"
            },
            "initialClusterVersion": "1.11.8-gke.6",
            "location": "us-west1-a"
        }


    kube = GoogleCloudKubernetesClient(file='/opt/key.json', project_id='abhinav-215', zone='us-west1-a')

    kube.create_cluster(cluster_data)


if __name__ == '__main__':
    main()

Actual Output:
Traceback (most recent call last):
  File "/opt/matilda_linux/matilda_linux_logtest/matilda_discovery/matilda_discovery/test/google_auth.py", line 118, in <module>
    main()
  File "/opt/matilda_linux/matilda_linux_logtest/matilda_discovery/matilda_discovery/test/google_auth.py", line 113, in main
    kube.create_cluster(cluster_data)
  File "/opt/matilda_linux/matilda_linux_logtest/matilda_discovery/matilda_discovery/test/google_auth.py", line 31, in create_cluster
    response = self.client.create_cluster(self.project_id, self.zone, cluster=cluster)
  File "/opt/matilda_discovery/venv/lib/python3.6/site-packages/google/cloud/container_v1/gapic/cluster_manager_client.py", line 407, in create_cluster
    project_id=project_id, zone=zone, cluster=cluster, parent=parent
ValueError: Protocol message Cluster has no "masterAuth" field.

Kind of late answer, but I had the same problem and figured it out.有点晚的答案,但我遇到了同样的问题并想通了。 Worth writing for future viewers.值得为未来的观众写作。

You should not write the field names in the cluster_data as they appear in the REST API .您不应在 cluster_data 中写入出现在REST API 中的字段名称。 Instead you should translate them to how they would look by python convention, with words separated by underline instead of camelcase.相反,您应该将它们转换为 Python 约定的外观,用下划线分隔单词而不是驼峰命名。 Thus, instead of writing masterAuth, you should write master_auth.因此,您应该编写 master_auth,而不是编写 masterAuth。 You should make similar changes to the rest of your fields, and then the script should work.您应该对其余字段进行类似的更改,然后脚本应该可以工作。

PS you aren't using the project_name and cluster_id params in GoogleCloudKubernetesClient. PS 您没有在 GoogleCloudKubernetesClient 中使用 project_name 和 cluster_id 参数。 init .初始化 Not sure what they are, but you should probably remove them.不确定它们是什么,但您可能应该删除它们。

The module is still using the basic REST API format to create the cluster.该模块仍然使用基本的REST API格式来创建集群。 You can also use the GUI to choose all the options you want to use for your cluster, then press on the REST hyperlink at the bottom of the page, this will provide you with the REST format required to build the cluster you want.您还可以使用 GUI 选择要用于集群的所有选项,然后按页面底部的 REST 超链接,这将为您提供构建所需集群所需的 REST 格式。

The error you are getting is because you have a blank (or unspecified) field that must be specified.您收到的错误是因为您必须指定一个空白(或未指定)字段。 Some of the fields listed on the API have default values that you don't need, others are required. API 上列出的某些字段具有您不需要的默认值,其他字段则是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将带有textract模块的Python应用程序部署到Google Cloud Platform - Deploy Python app with textract module to Google Cloud Platform 在Google云端上创建集群 - creation of a cluster on google cloud 如何在Google Cloud Platform中为Node应用程序安装Python3 - How to install Python3 in Google Cloud Platform for a Node app 如何将辅助python文件上传到Google云平台? - How do I upload a secondary python file to google cloud platform? 如何使用python下载谷歌云平台上文件夹内的文件? - How can i download the files inside a folder on google cloud platform using python? 如何使用 python 从 google-cloud-platform 下载我的数据? - how can i download my data from google-cloud-platform using python? 如何使用python API列出Google Cloud Platform中的所有应用程序服务 - How to list all app services in google cloud platform using python API 如何在 App Engine 中使用 Python3 连接到 Google Cloud Platform 中的 SQL 实例 - How to connect to SQL instance in Google Cloud Platform using Python3 in App Engine 如何在 Google Cloud Platform 中安装 cmake - how to install cmake in Google Cloud Platform 如何使用 python 删除 GKE(Google Kubernetes 引擎)集群? - How to delete GKE (Google Kubernetes Engine) cluster using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM