本文介绍了通过Python或Node访问GKE kubectl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GKE管理几个(目前,但将会增加)集群,到目前为止,可以根据需要手动启动事物.我已经开始使用自己的API,该API可以接收请求以按需为特定集群增加新资源,但是为了使其具有可扩展性,我需要做一些比在每个请求之间切换集群更具动态性的事情.我找到了一个Google API python客户端的链接,该链接据说可以访问GKE:

I manage a couple (presently, but will increase) clusters at GKE and up till now have been ok launching things manually as needed. I've started working my own API that can take in requests to spin up new resources on-demand for a specific cluster but in order to make it scalable I need to do something more dynamic than switching between clusters with each request. I have found a link for a Google API python client that supposedly can access GKE:

https://developers.google .com/api-client-library/python/apis/container/v1#system-requirements

我还发现了其他几个可以访问Kubernetes的客户端(特别是我一直在密切关注的一个客户端是godaddy的nodejs客户端):

I've also found several other clients (specifically one I was looking closely at was the nodejs client from godaddy) that can access Kubernetes:

https://github.com/godaddy/kubernetes-client

Google API客户端似乎没有被记录为可与GKE/kubectl命令一起使用,并且godaddy kubernetes-client必须访问一个集群主服务器,但无法在GKE上访问一个集群主服务器(未首先启用kubectl代理) ).所以我的问题是,一个人如何通过编程方式在GKE上管理kubernetes,而不必在nodejs或python中使用命令行实用程序?

The Google API Client doesn't appear to be documented for use with GKE/kubectl commands, and the godaddy kubernetes-client has to access a single cluster master but can't reach one at GKE (without a kubectl proxy enabled first). So my question is, how does one manage kubernetes on GKE programmatically without having to use the command-line utilities in either nodejs or python?

推荐答案

我刚刚在Medium上发表了一篇文章,其中包含如何执行此操作的示例

本文的第一部分概述了如何设置服务帐户,角色和凭据并将其作为环境变量加载.完成后,您可以运行以下python:

The first part of the article outlines how to setup the service account, roles and credentials and load them as Environmental variables. Once done, you could then run the following python:

from kubernetes import client
import base64
from tempfile import NamedTemporaryFile
import os
import yaml
from os import path


def main():
    try:
        host_url = os.environ["HOST_URL"]
        cacert = os.environ["CACERT"]
        token = os.environ["TOKEN"]

        # Set the configuration
        configuration = client.Configuration()
        with NamedTemporaryFile(delete=False) as cert:
            cert.write(base64.b64decode(cacert))
            configuration.ssl_ca_cert = cert.name
        configuration.host = host_url
        configuration.verify_ssl = True
        configuration.debug = False
        configuration.api_key = {"authorization": "Bearer " + token}
        client.Configuration.set_default(configuration)

        # Prepare all the required properties in order to run the create_namespaced_job method
        # https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/BatchV1Api.md#create_namespaced_job
        v1 = client.BatchV1Api()
        with open(path.join(path.dirname(__file__), "job.yaml")) as f:
            body = yaml.safe_load(f)

        v1.create_namespaced_job(namespace="default", body=body, pretty=True)

        return f'Job created successfully', 200

    except Exception as e:
        return str(e), 500


if __name__ == '__main__':
    main()

这篇关于通过Python或Node访问GKE kubectl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 17:57