kubernetes Python API 客户端:执行完整的 yaml 文件
kubernetes Python API Client: execute full yaml file
Kubernetes 有一个非常好的官方 Python API 客户端。 API 客户端假定您将创建单独的资源(例如 pods 或服务)并假定您将使用 Python 对象来编写和创建 API 请求.
但是,我想通过 Python 接口 运行 任意 kubernetes YAML 文件(包含一个或多个 k8s 资源)。我想知道是否可以利用 Python kubernetes 客户端来应用任意 YAML 文件?
我基本上是在寻找 Python 相当于:
kubectl apply -f some-file-containing-multiple-resources.yaml
我正在寻找基本上可以加载 kubeconfig 并通过 Python 以相当 Pythonic 的方式应用 yaml 的东西。
我知道我可能可以用 Python 子进程调用来包装 kubectl 命令,但我希望得到比这更多 Pythonic 的东西,并希望核心 K8s Python客户可以做类似的事情。或者,如果有另一个 Python 包做类似的事情。
Python kubernetes 客户端可以调用任意 k8s yaml 文件吗?如果不能,有什么可以的吗?
感谢阅读 - 感谢您提供的任何建议。
examples
目录中似乎有这方面的示例。特别是 https://github.com/kubernetes-client/python/blob/6709b753b4ad2e09aa472b6452bbad9f96e264e3/examples/create_deployment_from_yaml.py 这样做:
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.utils提供的create_from_yaml
。
这是多资源定义文件
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: ngnx-container
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 8080
targetPort: 80
现在您可以尝试 运行 下面的代码并检查它是否适合您。
from kubernetes import client, config, utils
config.load_kube_config()
k8s_client = client.ApiClient()
yaml_file = '<location to your multi-resource file>'
utils.create_from_yaml(k8s_client, yaml_file)
除了其他人推荐的之外,您还可以使用 Hikaru's load_full_yaml
。
Hikaru 的优点在于:
- 它是面向对象的 Pythonic,具有
Pod.create()
. 等功能
- 它是根据 OpenAPI 规范自动生成的,因此没有遗漏任何内容
我在构建自己的开源 Python Kubernetes platform 时评估了至少五个不同的 Kubernetes 库,而 Hikaru 是迄今为止最容易使用的。
Kubernetes 有一个非常好的官方 Python API 客户端。 API 客户端假定您将创建单独的资源(例如 pods 或服务)并假定您将使用 Python 对象来编写和创建 API 请求.
但是,我想通过 Python 接口 运行 任意 kubernetes YAML 文件(包含一个或多个 k8s 资源)。我想知道是否可以利用 Python kubernetes 客户端来应用任意 YAML 文件?
我基本上是在寻找 Python 相当于:
kubectl apply -f some-file-containing-multiple-resources.yaml
我正在寻找基本上可以加载 kubeconfig 并通过 Python 以相当 Pythonic 的方式应用 yaml 的东西。
我知道我可能可以用 Python 子进程调用来包装 kubectl 命令,但我希望得到比这更多 Pythonic 的东西,并希望核心 K8s Python客户可以做类似的事情。或者,如果有另一个 Python 包做类似的事情。
Python kubernetes 客户端可以调用任意 k8s yaml 文件吗?如果不能,有什么可以的吗?
感谢阅读 - 感谢您提供的任何建议。
examples
目录中似乎有这方面的示例。特别是 https://github.com/kubernetes-client/python/blob/6709b753b4ad2e09aa472b6452bbad9f96e264e3/examples/create_deployment_from_yaml.py 这样做:
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.utils提供的create_from_yaml
。
这是多资源定义文件
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: ngnx-container
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 8080
targetPort: 80
现在您可以尝试 运行 下面的代码并检查它是否适合您。
from kubernetes import client, config, utils
config.load_kube_config()
k8s_client = client.ApiClient()
yaml_file = '<location to your multi-resource file>'
utils.create_from_yaml(k8s_client, yaml_file)
除了其他人推荐的之外,您还可以使用 Hikaru's load_full_yaml
。
Hikaru 的优点在于:
- 它是面向对象的 Pythonic,具有
Pod.create()
. 等功能
- 它是根据 OpenAPI 规范自动生成的,因此没有遗漏任何内容
我在构建自己的开源 Python Kubernetes platform 时评估了至少五个不同的 Kubernetes 库,而 Hikaru 是迄今为止最容易使用的。