是否可以使用json api而不是所有kubectl命令。
在这种情况下,还将如何更新资源或自定义定义。
最佳答案
To call the Kubernetes API from a programming language, you can use client libraries. Officially supported client libraries:
- Kubernetes Go client library
- Kubernetes Python client library
- Kubernetes Java client library
- Kubernetes JavaScript client library
To write applications using the Kubernetes REST API, you do not need to implement the API calls and request/response types yourself. You can use a client library for the programming language you are using.
Client libraries often handle common tasks such as authentication for you. Most client libraries can discover and use the Kubernetes Service Account to authenticate if the API client is running inside the Kubernetes cluster, or can understand the kubeconfig file format to read the credentials and the API Server address.
支持使用custom resources扩展API的两条路径是:
实现自己的apiserver。
例如there是一种使用python和一些代码示例创建crd资源的方法。
list all pods
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
watch on namespace object:
from kubernetes import client, config, watch
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()
count = 10
w = watch.Watch()
for event in w.stream(v1.list_namespace, _request_timeout=60):
print("Event: %s %s" % (event['type'], event['object'].metadata.name))
count -= 1
if not count:
w.stop()
print("Ended.")
More examples can be found in examples folder.
关于kubernetes - 用于自定义或运算符的kubectl vs json API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61743976/