博主昵称:跳楼梯企鹅
博主主页面链接:博主主页传送门博主专栏页面连接:专栏传送门--网路安全技术
创作初心:本博客的初心为与技术朋友们相互交流,每个人的技术都存在短板,博主也是一样,虚心求教,希望各位技术友给予指导。
博主座右铭:发现光,追随光,成为光,散发光;
博主研究方向:渗透测试、机器学习 ;
博主寄语:感谢各位技术友的支持,您的支持就是我前进的动力 ;
目录
一、CRD 扩展 Kubernetes 集群
1.什么是 CRD
2.CRD 能做什么
(1)微服务管理总览
(2)创建 Yaml 配置
(3)自定义 Controller 逻辑
二、CRD 字段校验
1.校验方式
2.举例
3.API的常用方式
(1)配置
(2)查看文件
(3)自定义CRD模型
CRD 本身是一种 Kubernetes 内置的资源类型,是 CustomResourceDefinition 的缩写,可以通过
kubectlget
命令查看集群内定义的 CRD 资源。
$kubectl get crd
NAME CREATED AT
apps.app.my.cn 2022-09-25T07:02:47Z
microservices.app.my.cn 2022-09-25T07:02:47Z
App
负责管理整个应用的生命周期,MicroService
负责管理微服务的生命周期。① 部署方面:App
可以直接管理多个MicroService
,同时MicroService
利用控制器模式,可以为每个版本创建一个Deployment
, 实现多个版本同时部署。
MicroService
为自己创建 1 个的LoadBalance
,也为每个版本创建了Service
。如下图所示,MicroService
下的每个版本(对应每个Deployment
)都有Service
,而本身也有LoadBalance
,即总共拥有n+1
个Service
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
creationTimestamp: null
labels:
controller-tools.k8s.io: "1.0"
# 名称必须与下面的spec字段匹配,格式为: .
name: apps.app.o0w0o.cn
spec:
# 用于REST API的组名称: /apis//
group: app.o0w0o.cn
names:
# kind字段使用驼峰命名规则. 资源清单使用如此
kind: App
# URL中使用的复数名称: /apis///
plural: apps
# 指定crd资源作用范围在命名空间或集群
scope: Namespaced
# 自定义资源的子资源的描述
subresources:
# 启用状态子资源
status: {}
# 验证机制
validation:
# openAPIV3Schema is the schema for validating custom objects.
openAPIV3Schema:
properties:
...
func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, error) {
...
// 状态 App 同步
if err := r.syncAppStatus(instance); err != nil {
log.Info("Sync App error", err)
return reconcile.Result{}, err
}
// 协调资源 MicroService
if err := r.reconcileMicroService(request, instance); err != nil {
log.Info("Creating MicroService error", err)
return reconcile.Result{}, err
}
...
}
func (r *ReconcileMicroService) Reconcile(request reconcile.Request) (reconcile.Result, error) {
...
// 同步 MicroService 状态
if err := r.syncMicroServiceStatus(instance); err != nil {
log.Info("Sync MicroServiceStatus error", err)
return reconcile.Result{}, err
}
// 协调实例
if err := r.reconcileInstance(instance); err != nil {
log.Info("Reconcile Instance Versions error", err)
return reconcile.Result{}, err
}
// 协调负载均衡器
if err := r.reconcileLoadBalance(instance); err != nil {
log.Info("Reconcile LoadBalance error", err)
return reconcile.Result{}, err
}
...
}
kubernetes 目前提供了两种方式来对 CR 的校验
语法校验(
OpenAPI v3 schema
)语义校验(
validatingadmissionwebhook
)
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: .
name: kubernetesclusters.ecs.yun.com
spec:
# group name to use for REST API: /apis//
group: ecs.yun.com
# list of versions supported by this CustomResourceDefinition
versions:
- name: v1
# Each version can be enabled/disabled by Served flag.
served: true
# One and only one version must be marked as the storage version.
storage: true
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis///
plural: kubernetesclusters
# singular name to be used as an alias on the CLI and for display
singular: kubernetescluster
# kind is normally the CamelCased singular type. Your resource manifests use this.
kind: KubernetesCluster
# listKind
listKind: KubernetesClusterList
# shortNames allow shorter string to match your resource on the CLI
shortNames:
- ecs
#CRD 对象
apiVersion: ecs.yun.com/v1
kind: KubernetesCluster
metadata:
name: test-cluster
spec:
clusterType: kubernetes
serviceCIDR: ''
masterList:
- ip: 192.168.1.10
nodeList:
- ip: 192.168.1.11
privateSSHKey: ''
scaleUp: 0
scaleDown: 0
apiVersion: apiextensions.k8s.io/v1 #API群组和版本
kind: CustomResourceDefinition #资源类别
metadata:
-name #资源名称
spec:
conversion
calico的yaml文件
[root@k8s-master plugin]# vim calico.yaml
...
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: ippools.crd.projectcalico.org
spec:
......
...
[root@k8s-master plugin]# kubectl get CustomResourceDefinition
NAME CREATED AT
bgpconfigurations.crd.projectcalico.org 2022-08-25T14:33:24Z
bgppeers.crd.projectcalico.org 2022-08-25T14:33:24Z
blockaffinities.crd.projectcalico.org 2022-08-25T14:33:24Z
clusterinformations.crd.projectcalico.org 2022-08-25T14:33:24Z
felixconfigurations.crd.projectcalico.org 2022-08-25T14:33:24Z
globalnetworkpolicies.crd.projectcalico.org 2022-08-25T14:33:24Z
globalnetworksets.crd.projectcalico.org 2022-08-25T14:33:24Z
hostendpoints.crd.projectcalico.org 2022-08-25T14:33:24Z
ipamblocks.crd.projectcalico.org 2022-08-25T14:33:24Z
ipamconfigs.crd.projectcalico.org 2022-08-25T14:33:24Z
ipamhandles.crd.projectcalico.org 2022-08-25T14:33:24Z
ippools.crd.projectcalico.org 2022-08-25T14:33:24Z
kubecontrollersconfigurations.crd.projectcalico.org 2022-08-25T14:33:24Z
networkpolicies.crd.projectcalico.org 2022-08-25T14:33:24Z
networksets.crd.projectcalico.org 2022-08-25T14:33:24Z
[root@k8s-master crd]# cat user-cr-demo.yaml
apiVersion: auth.ilinux.io/v1alpha1
kind: User
metadata:
name: admin
namespace: default
spec:
userID: 1
email: [email protected]
groups:
- superusers
- adminstrators
password: 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 User
NAME AGE
admin 14s
[root@k8s-master ~]# kubectl describe User admin
Name: admin
Namespace: default
Labels:
Annotations:
API Version: auth.ilinux.io/v1alpha1
Kind: User
Metadata:
Creation Timestamp: 2022-09-25T14:51:53Z
Generation: 1
Managed Fields:
API Version: auth.ilinux.io/v1alpha1
Fields Type: FieldsV1
fieldsV1:
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-apply
Operation: Update
Time: 2022-09-25T14:51:53Z
Resource Version: 2583010
Self Link: /apis/auth.ilinux.io/v1alpha1/namespaces/default/users/admin
UID: 5af89454-e067-4f30-83b7-cc2ad82e3526
Spec:
Email: [email protected]
Groups:
superusers
adminstrators
Password: ikubernetes.io
User ID: 1
Events: