自定义资源是对k8s api的扩展,并且能够通过api动态注册和删除。自定义资源被注册后,用户就可以使用kubectl来访问它们。
自定义资源只是让你能够存储和获取结构化的数据。仅当结合一个控制器时,它们才能成为一个真正的声明式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.
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.
(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://www.163yun.com/gift
本文来自网易实践者社区,经作者李岚清授权发布。