kubernetes Python API客户端:执行完整的yaml文件 [英] kubernetes Python API Client: execute full yaml file

查看:382
本文介绍了kubernetes Python API客户端:执行完整的yaml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kubernetes有一个非常好的官方Python API客户端. API客户端假定您将创建单个资源(例如pod或服务),并假定您将使用Python对象来编写和创建API请求.

Kubernetes has a very nice official Python API client. The API client assumes that you will be creating individual resources (such as pods, or services) and assumes that you will be using Python objects to compose and create API requests.

但是,我想通过Python接口运行任意kubernetes YAML文件(包含一个或多个k8s资源).我想知道是否可以利用Python kubernetes客户端来应用任意的YAML文件?

However, I'd like to run arbitrary kubernetes YAML files (containing one or more k8s resources) via a Python interface. I was wondering if the Python kubernetes client can be leveraged to apply arbitrary YAML files?

我基本上是在寻找与之等效的Python:

I'm basically looking for the Python equivalent of:

kubectl apply -f some-file-containing-multiple-resources.yaml

我正在寻找一种基本上可以加载kubeconfig并以Python方式通过python应用yaml的东西.

I'm looking for something where I can basically load the kubeconfig and apply the yaml via Python in a fairly Pythonic way.

我知道我可以用一个Python子进程调用包装kubectl命令,但是我希望能提供比Python更高级的功能,并希望核心K8s Python客户端可以做到这一点.或者,如果还有另一个类似的Python包.

I know I can probably wrap the kubectl command with a Python subprocess call, but I was hoping for something more Pythonic than that and hoped that the core K8s Python client could do something like that. Or, if there is another Python package that does something similar.

Python kubernetes客户端可以调用任意k8s yaml文件吗?如果不能,是否可以执行某些操作?

Can the Python kubernetes client call arbitrary k8s yaml files and if not, is there something that can?

感谢您的阅读-感谢您​​提供的任何建议.

Thanks for reading - I appreciate any advice you have to offer.

推荐答案

examples目录中似乎有此示例.特别是 https://github.com/kubernetes-client/python /blob/master/examples/create_deployment.py 可以:

There appears to be examples of this in the examples directory. In particular https://github.com/kubernetes-client/python/blob/master/examples/create_deployment.py which does:

from os import path

import yaml

from kubernetes import client, config


def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.
    config.load_kube_config()

    with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f:
        dep = yaml.safe_load(f)
        k8s_beta = client.ExtensionsV1beta1Api()
        resp = k8s_beta.create_namespaced_deployment(
            body=dep, namespace="default")
        print("Deployment created. status='%s'" % str(resp.status))


if __name__ == '__main__':
    main()

这篇关于kubernetes Python API客户端:执行完整的yaml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆