美文网首页
Kubernetes Python Client 初体验之安装授

Kubernetes Python Client 初体验之安装授

作者: lippon | 来源:发表于2019-10-18 13:31 被阅读0次

    最近想做一个基于flask的云平台管理服务器,利用python调用kubenetes提供的API来实现云平台的操作。笔者使用的是Windows,kubernetes集群安装在Ubuntu和Respbian等操作系统中,将所有节点和服务器主机连接在一个局域网里,都可以实现授权操作。
    在这篇文章中说一下基本的安装和登陆授权。
    安装有两种方式,可以从GitHub上直接clone下来,用python安装在本地:

    git clone --recursive https://github.com/kubernetes-client/python.git
    cd python
    python setup.py install
    

    或者用pip直接安装更为方便:

    pip install kubernetes
    

    首先在master节点中获取kubernetes的认证文件,在root用户的根目录下执行:

    cp .kube/config    kubeconfig.yaml
    

    python通过kubernetes config和client实现授权和获取kubernetes的API,代码片如下:

    config.kube_config.load_kube_config(config_file="kubeconfig.yaml")
    coreApi = client.CoreV1Api()
    appApi = client.AppsV1Api()
    

    client提供多种API方法,需要在之后的运用中加以区分。
    如果报错:

    TypeError: argument of type 'NoneType' is not iterable
    

    说明yaml文件的路径有问题。
    之后通过调用API检测是否授权成功。

    for i in coreApi.list_namespace().items:
        print(i.metadata.name)
    

    结果如下:

    输出结果
    参考文档:https://github.com/kubernetes-client/python

    相关文章

      网友评论

          本文标题:Kubernetes Python Client 初体验之安装授

          本文链接:https://www.haomeiwen.com/subject/ehuimctx.html