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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!