猪小花1号

个人签名

282篇博客

Custom Resource Definition 简介

猪小花1号2018-08-29 09:09

1 什么是自定义资源 Custom resources

自定义资源是对k8s api的扩展,并且能够通过api动态注册和删除。自定义资源被注册后,用户就可以使用kubectl来访问它们。

2 自定义控制器 Custom controllers

自定义资源只是让你能够存储和获取结构化的数据。仅当结合一个控制器时,它们才能成为一个真正的声明式API。

A custom controller is a controller that users can deploy and update on a running cluster, independently of the cluster’s own lifecycle. Custom controllers can work with any kind of resource, but they are especially effective when combined with custom resources. The Operator pattern is one example of such a combination. It allows developers to encode domain knowledge for specific applications into an extension of the Kubernetes API.

3 自定义资源的定义 CustomResourceDefinitions

CustomResourceDefinition是一个内建的API,用来创建自定义资源。部署一个CRD到集群中,kube-apiserver会帮助你安装好路由和提供自定义资源的一般服务端实现。

也就是说你不再需要编写服务端代码,只需要创建一个CRD,然后根据CRD创建自定义资源的具体实例即可。但同时,这意味着缺少灵活性。

Note: CRD is the successor to the deprecated ThirdPartyResource (TPR) API, and is available as of Kubernetes 1.7.

4 如何使用CRD扩展k8s api

(1) 首先创建一个CRD:

kubectl create -f crd.json

crd.json:

{
    "apiVersion": "apiextensions.k8s.io/v1beta1",
    "kind": "CustomResourceDefinition",
    "metadata": {
        # name must match the spec fields below, and be in the form: <plural>.<group>
        "name": "tasks.stable.netease"

    },
    "spec": {
        # group name to use for REST API: /apis/<group>/<version>
        "group": "stable.netease",
        # version name to use for REST API: /apis/<group>/<version>
        "version": "v1",
        # either Namespaced or Cluster
        "scope": "Namespaced",
        "names": {
            # plural name to be used in the URL: /apis/<group>/<version>/<plural>
            "plural": "tasks",
            # singular name to be used as an alias on the CLI and for display
            "singular": "task",
            # kind is normally the CamelCased singular type. Your resource manifests use this.
            "kind": "Task",
            # shortNames allow shorter string to match your resource on the CLI
            "shortNames": [
                "task"
            ]
        }
    }
}

(2) 根据CRD的定义,创建自定义资源的实例:

kubectl create -f task.json

task.json:

{
    "kind": "Task",
    "apiVersion": "stable.netease/v1",
    "metadata": {
        "name": "mytask",
        "namespace": "default",
        "labels": {
            "env": "dev"
        }
    },
    "spec": {
        "requestId": "5f80fd5550d34067908bcd8ace5f8469",
        "action": "SaveImage",
        "nodeName": "10.174.59.66",
        "podName": "test-108892",
        "object": "[{\"containerName\":\"test\",\"image\":\"hub.c.163.com/hzlilanqing/test:latest\"}]"
    },
    "status": {
        "phase": "Pending"
    }
}

(3) 验证:

root@master:~/json# kubectl get tasks
NAME      AGE
mytask    19h

root@master:~/json# kubectl get tasks mytask -o json
{
    "apiVersion": "stable.netease/v1",
    "kind": "Task",
    "metadata": {
        "clusterName": "",
        "creationTimestamp": "2017-11-07T02:12:57Z",
        "deletionGracePeriodSeconds": null,
        "deletionTimestamp": null,
        "labels": {
            "env": "dev"
        },
        "name": "mytask",
        "namespace": "default",
        "resourceVersion": "586179",
        "selfLink": "/apis/stable.netease/v1/namespaces/default/tasks/mytask",
        "uid": "2c217d84-c361-11e7-a161-fa163e48df23"
    },
    "spec": {
        "action": "SaveImage",
        "nodeName": "10.174.59.66",
        "object": "[{\"containerName\":\"test\",\"image\":\"hub.c.163.com/hzlilanqing/test:latest\"}]",
        "podName": "test-108892",
        "requestId": "5f80fd5550d34067908bcd8ace5f8469"
    },
    "status": {
        "phase": "Pending"
    }
}

编程实例可以参考:https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/apiextensions-apiserver/examples/client-go

5 CRD目前存在的问题

  • 没有updateStatus接口
  • watch condition不支持fieldSelector
  • 不支持TTL

参考文献

  1. https://kubernetes.io/docs/concepts/api-extension/custom-resources/
  2. https://kubernetes.io/docs/tasks/access-kubernetes-api/extend-api-custom-resource-definitions/
  3. https://github.com/kubernetes/kubernetes/tree/master/staging/src/k8s.io/apiextensions-apiserver/examples/client-go
  4. https://github.com/kubernetes/kube-aggregator

网易云新用户大礼包:https://www.163yun.com/gift

本文来自网易实践者社区,经作者李岚清授权发布。