kubernetes|kubernetes k8s CRD自定义资源学习笔记
目录
- CustomResourceDefinition简介:
- 目前扩展Kubernetes API的常用方式有3种:
- 配置规范
- 示例1: 创建自定义CRD
- 创造自定义CRD类型
- 示例2: etcd Operator 部署 (该项目已不在维护)
- 部署创建etcd集群
CustomResourceDefinition简介: 在 Kubernetes 中一切都可视为资源,Kubernetes 1.7 之后增加了对 CRD 自定义资源二次开发能力来扩展 Kubernetes API,通过 CRD 我们可以向 Kubernetes API 中增加新资源类型,而不需要修改 Kubernetes 源码来创建自定义的 API server,该功能大大提高了 Kubernetes 的扩展能力。
当你创建一个新的CustomResourceDefinition (CRD)时,Kubernetes API服务器将为你指定的每个版本创建一个新的RESTful资源路径,我们可以根据该api路径来创建一些我们自己定义的类型资源。CRD可以是命名空间的,也可以是集群范围的,由CRD的作用域(scpoe)字段中所指定的,与现有的内置对象一样,删除名称空间将删除该名称空间中的所有自定义对象。customresourcedefinition本身没有名称空间,所有名称空间都可以使用。
目前扩展Kubernetes API的常用方式有3种:
- 使用CRD(CustomResourceDefinitions)自定义资源类型
- 开发自定义的APIServer并聚合至主API Server
- 及定制扩展API Server源码。这其中,CRD最为易用但限制颇多,自定义API Server更富于弹性但代码工作量偏大,而仅在必须添加新的核心类型才能确保专用的Kberneves集群功能正常,才应该定制系统源码
- 其中CRD与CRT一般由开发或服务供应商提供
- CRD只是定义一个类型Kind,但实际把kind运行起来CR需要有Controller来对资源进行控制,所有只有定义CRD定义没有并没有实际意义,当然也可以通过定义现在kind来运行,比如deployment 通过定义 RC来运行
配置规范
apiVersion: apiextensions.k8s.io/v1 #API群组和版本kind: CustomResourceDefinition #资源类别metadata:-name#资源名称spec:conversion
可以查看之前部署Calico创建的自定义CRD
[root@k8s-master ~]# kubectl api-resources#查看所有资源类型NAMESHORTNAMESAPIGROUPNAMESPACEDKIND...bgpconfigurationscrd.projectcalico.orgfalseBGPConfigurationbgppeerscrd.projectcalico.orgfalseBGPPeerblockaffinitiescrd.projectcalico.orgfalseBlockAffinityclusterinformationscrd.projectcalico.orgfalseClusterInformationfelixconfigurationscrd.projectcalico.orgfalseFelixConfigurationglobalnetworkpoliciescrd.projectcalico.orgfalseGlobalNetworkPolicyglobalnetworksetscrd.projectcalico.orgfalseGlobalNetworkSethostendpointscrd.projectcalico.orgfalseHostEndpointipamblockscrd.projectcalico.orgfalseIPAMBlockipamconfigscrd.projectcalico.orgfalseIPAMConfigipamhandlescrd.projectcalico.orgfalseIPAMHandleippoolscrd.projectcalico.orgfalseIPPoolkubecontrollersconfigurationscrd.projectcalico.orgfalseKubeControllersConfigurationnetworkpoliciescrd.projectcalico.orgtrueNetworkPolicynetworksetscrd.projectcalico.orgtrueNetworkSet
查看calico的yaml文件可以看到里面很多CRD的定义
[root@k8s-master plugin]# vim calico.yaml...---apiVersion: apiextensions.k8s.io/v1kind: CustomResourceDefinitionmetadata:name: ippools.crd.projectcalico.orgspec:.........[root@k8s-master plugin]# kubectl get CustomResourceDefinitionNAMECREATED ATbgpconfigurations.crd.projectcalico.org2021-08-29T14:33:24Zbgppeers.crd.projectcalico.org2021-08-29T14:33:24Zblockaffinities.crd.projectcalico.org2021-08-29T14:33:24Zclusterinformations.crd.projectcalico.org2021-08-29T14:33:24Zfelixconfigurations.crd.projectcalico.org2021-08-29T14:33:24Zglobalnetworkpolicies.crd.projectcalico.org2021-08-29T14:33:24Zglobalnetworksets.crd.projectcalico.org2021-08-29T14:33:24Zhostendpoints.crd.projectcalico.org2021-08-29T14:33:24Zipamblocks.crd.projectcalico.org2021-08-29T14:33:24Zipamconfigs.crd.projectcalico.org2021-08-29T14:33:24Zipamhandles.crd.projectcalico.org2021-08-29T14:33:24Zippools.crd.projectcalico.org2021-08-29T14:33:24Zkubecontrollersconfigurations.crd.projectcalico.org2021-08-29T14:33:24Znetworkpolicies.crd.projectcalico.org2021-08-29T14:33:24Znetworksets.crd.projectcalico.org2021-08-29T14:33:25Z
示例1: 创建自定义CRD
[root@k8s-master crd]# cat crd-v1-user.yaml apiVersion: apiextensions.k8s.io/v1kind: CustomResourceDefinitionmetadata:name: users.auth.ilinux.iospec:group: auth.ilinux.ionames:kind: Userplural: userssingular: usershortNames:- uscope: Namespaced#名称空间级别versions:- served: truestorage: truename: v1alpha1#版本号schema:openAPIV3Schema:type: object#对字段做限制 properties:spec:type: objectproperties:userID:type: integer#整形minimum: 1maximum: 65535groups :type: array#列表items:type: stringemail:type: stringpassword:type: stringformat: passwordrequired: ["userID","groups"][root@k8s-master crd]# kubectl apply -f crd-v1-user.yaml [root@k8s-master crd]# kubectl api-resourcesNAMESHORTNAMESAPIGROUPNAMESPACEDKINDbindingstrueBinding...usersuauth.ilinux.iotrueUser
创造自定义CRD类型
[root@k8s-master crd]# cat user-cr-demo.yaml apiVersion: auth.ilinux.io/v1alpha1kind: Usermetadata:name: adminnamespace: defaultspec:userID: 1email: test@test.comgroups:- superusers- adminstratorspassword: ikubernetes.io[root@k8s-master crd]# kubectl apply -f user-cr-demo.yaml user.auth.ilinux.io/admin created[root@k8s-master crd]# kubectl get UserNAMEAGEadmin14s[root@k8s-master ~]# kubectl describe User adminName:adminNamespace:defaultLabels:Annotations: API Version:auth.ilinux.io/v1alpha1Kind:UserMetadata:Creation Timestamp:2021-09-10T14:51:53ZGeneration:1Managed Fields:API Version:auth.ilinux.io/v1alpha1Fields Type:FieldsV1fieldsV1:f:metadata:f:annotations:.:f:kubectl.kubernetes.io/last-applied-configuration:f:spec:.:f:email:f:groups:f:password:f:userID:Manager:kubectl-client-side-applyOperation:UpdateTime:2021-09-10T14:51:53ZResource Version:2583010Self Link:/apis/auth.ilinux.io/v1alpha1/namespaces/default/users/adminUID:5af89454-e067-4f30-83b7-cc2ad82e3526Spec:Email:test@test.comGroups:superusersadminstratorsPassword:ikubernetes.ioUser ID:1Events:
以上定义的kind资源 没Controller并不能运行成实际对象,Controller的开发需要开发来完成
示例2: etcd Operator 部署 (该项目已不在维护)
文章图片
Operator 项目地址:
https://github.com/coreos/etcd-operator/blob/master/doc/user/install_guide.md
https://github.com/coreos/etcd-operator
https://github.com/operator-framework/awesome-operators
先安装RBAC 再安装etcd operator 再部署创建etcd集群
[root@k8s-master etcd-operator]# example/rbac/create_role.shCreating role with ROLE_NAME=etcd-operator, NAMESPACE=defaultWarning: rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleclusterrole.rbac.authorization.k8s.io/etcd-operator createdCreating role binding with ROLE_NAME=etcd-operator, ROLE_BINDING_NAME=etcd-operator, NAMESPACE=defaultWarning: rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBindingclusterrolebinding.rbac.authorization.k8s.io/etcd-operator created[root@k8s-master etcd-operator]# kubectl create -f example/deployment.yamlerror: unable to recognize "example/deployment.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"#deployment版本太老修改example/deployment.yaml[root@k8s-master etcd-operator]# cat example/deployment.yamlapiVersion: apps/v1#版本kind: Deploymentmetadata:name: etcd-operatorspec:replicas: 1selector:#添加字段matchLabels:name: etcd-operatortemplate:metadata:labels:name: etcd-operatorspec:containers:- name: etcd-operatorimage: quay.io/coreos/etcd-operator:v0.9.4command:- etcd-operator# Uncomment to act for resources in all namespaces. More information in doc/user/clusterwide.md#- -cluster-wideenv:- name: MY_POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespace- name: MY_POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name[root@k8s-master etcd-operator]# kubectl create -f example/deployment.yamldeployment.apps/etcd-operator created[root@k8s-master etcd-operator]# [root@k8s-master etcd-operator]# kubectl api-resources...etcdclustersetcdetcd.database.coreos.comtrueEtcdCluster
部署创建etcd集群
[root@k8s-master etcd-operator]# cat example/example-etcd-cluster.yamlapiVersion: "etcd.database.coreos.com/v1beta2"kind: "EtcdCluster"metadata:name: "example-etcd-cluster"## Adding this annotation make this cluster managed by clusterwide operators## namespaced operators ignore it# annotations:#etcd.database.coreos.com/scope: clusterwidespec:size: 3#集群数理version: "3.2.13"[root@k8s-master etcd-operator]# kubectl apply -fexample/example-etcd-cluster.yamletcdcluster.etcd.database.coreos.com/example-etcd-cluster created[root@k8s-master etcd-operator]# kubectl get pod -o wideNAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATESetcd-operator-646cbffdb6-brbn61/1Running012m192.168.51.58k8s-node3example-etcd-cluster-nc8pdgjrjr1/1Running03m3s192.168.51.59k8s-node3 - 后面在加一个SVC就可以使用了
【kubernetes|kubernetes k8s CRD自定义资源学习笔记】以上就是kubernetes k8s CRD自定义资源学习笔记的详细内容,更多关于kubernetes(k8s) CRD的资料请关注脚本之家其它相关文章!
推荐阅读
- Kubernetes 集群中 Ingress 故障的根因诊断
- 编写一个kubernetes|编写一个kubernetes controller
- XMeter Newsletter 2022-05|企业版 v4.0.0 发布,基于 K8s 部署测试机资源
- 认识Kubernetes(k8s)
- k8s|Kubernetes服务质量保证之配置容器资源limits和requests
- Kubernetes 标准化部署文档
- K8s 上轻松搭建百万连接的 MQTT 服务
- #云原生征文#深入Kubernetes(k8s)概念
- k8s使用containerd作为容器运行时
- k8s 调度指定节点