kubernetes Python API 客户端:执行完整的 yaml 文件

kubernetes Python API Client: execute full yaml file(kubernetes Python API 客户端:执行完整的 yaml 文件)
本文介绍了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 以相当 Pythonic 的方式应用 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 命令,但我希望有比这更 Pythonic 的东西,并希望核心 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/6709b753b4ad2e09aa472b6452bbad9f96e264e3/examples/create_deployment_from_yaml.py 这样做:

There appears to be examples of this in the examples directory. In particular https://github.com/kubernetes-client/python/blob/6709b753b4ad2e09aa472b6452bbad9f96e264e3/examples/create_deployment_from_yaml.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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
how to change discord.py bot activity(如何更改 discord.py 机器人活动)
Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
on_message() and @bot.command issue(on_message() 和@bot.command 问题)
How to edit a message in discord.py(如何在 discord.py 中编辑消息)