kubernetes - 用于自定义或运算符的kubectl vs json API

标签 kubernetes

是否可以使用json api而不是所有kubectl命令。

在这种情况下,还将如何更新资源或自定义定义。

最佳答案

你可以看看API client libraries.

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的两条路径是:
  • CustomResourceDefinition用于基本的CRUD需求。
  • aggregator提供全套Kubernetes 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/

    相关文章:

    kubernetes - 自定义 cloudwatch 指标 EKS CloudWatch 代理

    azure - AKS 的 key 保管库问题

    docker - 我如何列出推送到 microk8s 内置注册表的图像

    kubernetes - Istio上消息的模式匹配

    kubernetes - 如何在 kubernetes 中使用字符串值设置 targetPort?

    kubernetes - 无法通过 kubernetes 就绪探测访问容器

    kubernetes - 如何在 kubernetes 中启用 api 标志

    docker - 如何配置 kubernetes (microk8s) 使用本地 docker 镜像?

    nginx - 仅使用 nginx Ingress 重写特定路由

    docker - Ballerina:构建镜像并通过 k8s 插件推送到 gcr.io