k8s系统中controller扮演着重要角色,开发自定义controller是深入学习和理解controller的有效途径,《k8s自定义controller三部曲》系列会逐步完成一次完整的自定义controller实战;
整个三部曲的目标如下:
实战环境的版本信息如下,请确保以下软件都已运行正常(Etcd只是用来查看数据的,可以选择不装),并且kubectl工具可以在k8s环境正常操作:
本篇是三部曲的第一篇,我们先自定义一个API对象Student,然后让k8s接收Student的定义,这样我们在k8s创建Student对象时,k8s就能接收并保存了,就像先有了pod定义,才能创建pod一样;
接下来详细讲述应用的编码过程,如果您不想自己写代码,也可以在GitHub下载完整的应用源码,地址和链接信息如下表所示:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | [email protected]:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
这个git项目中有多个文件夹,本章源码在k8s_customize_controller这个文件夹下,如下图红框所示:
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
# metadata.name的内容是由"复数名.分组名"构成,如下,students是复数名,bolingcavalry.k8s.io是分组名
name: students.bolingcavalry.k8s.io
spec:
# 分组名,在REST API中也会用到的,格式是: /apis/分组名/CRD版本
group: bolingcavalry.k8s.io
# list of versions supported by this CustomResourceDefinition
versions:
- name: v1
# 是否有效的开关.
served: true
# 只有一个版本能被标注为storage
storage: true
# 范围是属于namespace的
scope: Namespaced
names:
# 复数名
plural: students
# 单数名
singular: student
# 类型名
kind: Student
# 简称,就像service的简称是svc
shortNames:
- stu
[root@master custom_controller]# kubectl apply -f student.yaml
customresourcedefinition.apiextensions.k8s.io/students.bolingcavalry.k8s.io created
[root@master custom_controller]# kubectl get crd
NAME CREATED AT
students.bolingcavalry.k8s.io 2019-03-30T13:33:13Z
[root@master custom_controller]# kubectl describe crd stu
Name: students.bolingcavalry.k8s.io
Namespace:
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apiextensions.k8s.io/v1beta1","kind":"CustomResourceDefinition","metadata":{"annotations":{},"name":"students.bolingcavalry...
API Version: apiextensions.k8s.io/v1beta1
Kind: CustomResourceDefinition
Metadata:
Creation Timestamp: 2019-03-30T13:33:13Z
Generation: 1
Resource Version: 292010
Self Link: /apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/students.bolingcavalry.k8s.io
UID: 5e4ceb6e-52f0-11e9-96e1-000c29f1f9c9
...
..
.
ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key get /registry/apiextensions.k8s.io/customresourcedefinitions/ --prefix
上述命令用来查看路径/registry/apiextensions.k8s.io/customresourcedefinitions/之下的所有键值对,可以见到刚才创建的CRD信息,如下:
/registry/apiextensions.k8s.io/customresourcedefinitions/students.bolingcavalry.k8s.io{
"kind": "CustomResourceDefinition",
"apiVersion": "apiextensions.k8s.io/v1beta1",
"metadata": {
"name": "students.bolingcavalry.k8s.io",
"uid": "5e4ceb6e-52f0-11e9-96e1-000c29f1f9c9",
"generation": 1,
"creationTimestamp": "2019-03-30T13:33:13Z",
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apiextensions.k8s.io/v1beta1\",\"kind\":\"CustomResourceDefinition\",\"metadata\":{\"annotations\":{},\"name\":\"students.bolingcavalry.k8s.io\"},\"spec\":{\"group\":\"bolingcavalry.k8s.io\",\"names\":{\"kind\":\"Student\",\"plural\":\"students\",\"shortNames\":[\"stu\"],\"singular\":\"student\"},\"scope\":\"Namespaced\",\"versions\":[{\"name\":\"v1\",\"served\":true,\"storage\":true}]}}\n"
}
},
"spec": {
"group": "bolingcavalry.k8s.io",
"version": "v1",
"names": {
"plural": "students",
"singular": "student",
"shortNames": [
"stu"
],
"kind": "Student",
"listKind": "StudentList"
},
"scope": "Namespaced",
"versions": [
{
"name": "v1",
"served": true,
"storage": true
}
],
"conversion": {
"strategy": "None"
}
},
"status": {
"conditions": [
{
"type": "NamesAccepted",
"status": "True",
"lastTransitionTime": "2019-03-30T13:33:13Z",
"reason": "NoConflicts",
"message": "no conflicts found"
},
{
"type": "Established",
"status": "True",
"lastTransitionTime": null,
"reason": "InitialNamesAccepted",
"message": "the initial names have been accepted"
}
],
"acceptedNames": {
"plural": "students",
"singular": "student",
"shortNames": [
"stu"
],
"kind": "Student",
"listKind": "StudentList"
},
"storedVersions": [
"v1"
]
}
}
前面的步骤使得k8s能识别Student类型了,接下来创建个Student对象试试;
apiVersion: bolingcavalry.k8s.io/v1
kind: Student
metadata:
name: object-student
spec:
name: "张三"
school: "深圳中学"
[root@master custom_controller]# kubectl apply -f object-student.yaml
student.bolingcavalry.k8s.io/object-student created
[root@master custom_controller]# kubectl get stu
NAME AGE
object-student 15s
ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key get /registry/bolingcavalry.k8s.io/students/default/object-student --print-value-only
控制台输出的就是该Student对象存储在etcd中的内容,如下:
{
"apiVersion": "bolingcavalry.k8s.io/v1",
"kind": "Student",
"metadata": {
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"bolingcavalry.k8s.io/v1\",\"kind\":\"Student\",\"metadata\":{\"annotations\":{},\"name\":\"object-student\",\"namespace\":\"default\"},\"spec\":{\"name\":\"张三\",\"school\":\"深圳中学\"}}\n"
},
"creationTimestamp": "2019-03-31T02:56:25Z",
"generation": 1,
"name": "object-student",
"namespace": "default",
"uid": "92927d0d-5360-11e9-9d2a-000c29f1f9c9"
},
"spec": {
"name": "张三",
"school": "深圳中学"
}
}
至此,自定义API对象(也就是CRD)就创建成功了,此刻我们只是让k8s能识别到Student这个对象的身份,但是当我们创建Student对象的时候,还没有触发任何业务(相对于创建Pod对象的时候,会触发kubelet在node节点创建docker容器),这也是后面的章节要完成的任务,点击链接进入下一段实战: 《k8s自定义controller三部曲之二:自动生成代码》