“
Clusterpedia 是一个 CNCF 沙箱项目,用于跨集群复杂的资源检索。
名字源于 Wikipedia,寓意是打造多集群的百科全书,可以与多个集群同步资源,并在与 Kubernetes OpenAPI 兼容的基础上,提供更强大的搜索功能,以帮助您快速、简便、有效地获取任何多集群资源。
0.4.0 后,Clusterpedia 提供了更加友好的接入多云平台的方式,用户在多云平台创建或者纳管集群后,便可以直接使用 kubectl 来检索这些集群内的资源。
我们在Clusterpedia 仓库[1]中维护了各个多云平台的 ClusterImportPolicy[2]。非常欢迎大家提交用于对接其他多云平台的 ClusterImportPolicy。
用户在安装 Clusterpedia 后,创建合适的 ClusterImportPolicy 即可,用户也可以根据自己的需求来创建新的 ClusterImportPolicy[3]。
Cluster API 的 ClusterImportPolicy 已经在 clusterpedia#288[4]中提交,在 Cluster API 中创建集群后,可以直接使用 Clusterpedia 来对这些集群内的资源进行复杂检索。
$ kubectl get clusterNAME PHASE AGE VERSIONcapi-quickstart Provisioned 10m v1.24.2capi-quickstart-2 Provisioned 118s v1.24.2$ kubectl get kubeadmcontrolplaneNAME CLUSTER INITIALIZED API SERVER AVAILABLE REPLICAS READY UPDATED UNAVAILABLE AGE VERSIONcapi-quickstart-2-ctm9k capi-quickstart-2 true 1 1 1 10m v1.24.2capi-quickstart-2xcsz capi-quickstart true 1 1 1 19m v1.24.2$ # pediacluster 会根据 cluster 资源自动创建,更新和删除$ kubectl get pediacluster -o wideNAME READY VERSION APISERVER VALIDATED SYNCHRORUNNING CLUSTERHEALTHYdefault-capi-quickstart True v1.24.2 Validated Running Healthydefault-capi-quickstart-2 True v1.24.2 Validated Running Healthy$ kubectl --cluster clusterpedia get noCLUSTER NAME STATUS ROLES AGE VERSIONdefault-capi-quickstart-2 capi-quickstart-2-ctm9k-g2m87 NotReady control-plane 12m v1.24.2default-capi-quickstart-2 capi-quickstart-2-md-0-s8hbx-7bd44554b5-kzcb6 NotReady <none> 11m v1.24.2default-capi-quickstart capi-quickstart-2xcsz-fxrrk NotReady control-plane 21m v1.24.2default-capi-quickstart capi-quickstart-md-0-9tw2g-b8b4f46cf-gggvq NotReady <none> 20m v1.24.2
01 示例环境
如何快速部署一套 Cluster API And Clusterpedia 的示例环境?
预备条件
- 安装 kubectl[5] 到本地环境
- 安装 Kind[6] and Docker[7]
- 安装 clusterctl[8]
Minimum kind supported version: v0.14.0
创键管理集群并部署 Cluster API
部署 Cluster API 也可以参考 https://cluster-api.sigs.k8s.io/user/quick-start.html
$ cat > kind-cluster-with-extramounts.yaml <<EOFkind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes:- role: control-plane extraMounts: - hostPath: /var/run/docker.sock containerPath: /var/run/docker.sockEOF$ kind create cluster --name capi-sample --config kind-cluster-with-extramounts.yaml$ export CLUSTER_TOPOLOGY=true$ clusterctl init --infrastructure docker
部署 Clusterpedia
$ git clone https://github.com/clusterpedia-io/clusterpedia.git && cd clusterpedia/charts$ helm install clusterpedia . \ --namespace clusterpedia-system \ --create-namespace \ --set installCRDs=true \ # --set persistenceMatchNode={{ LOCAL_PV_NODE }} --set persistenceMatchNode=capi-sample-control-plane
clusterpedia charts 提供了 Local PV,需要创建 LOCAL PV 绑定的节点。如果不需要 charts 来创建 LOCAL PV,可以使用 --set persistenceMatchNode=None.详见[9]
创建用于接入 Cluster API 的集群自动导入策略[10]
$ kubectl apply -f https://raw.githubusercontent.com/Iceber/clusterpedia/add_cluster_api_clusterimportpolicy/deploy/clusterimportpolicy/cluster_api.yaml
Clusterpedia 可以接入任何的多云管理平台,接入方式可以参考 Interfacing to Multi-Cloud Platforms[11]
生成 kubectl cluster shortcut[12],如果使用 client-go 或者 OpenAPI 来访问,可以省略该步骤。
$ curl -sfL https://raw.githubusercontent.com/clusterpedia-io/clusterpedia/main/hack/gen-clusterconfigs.sh | sh -$ # 使用 kubectl 检索多集群资源,当前 Cluster API 未创建集群,所以返回空$ kubectl --cluster clusterpedia api-resources
02 使用 Cluster API创建集群
使用示例环境的 Docker Provider 来创建集群时,需要添加 --flavor development。
$ clusterctl generate cluster capi-quickstart --flavor development \ --kubernetes-version v1.24.2 \ --control-plane-machine-count=1 \ --worker-machine-count=1 \ > capi-quickstart.yaml$ kubectl apply -f ./capi-quickstart.yaml
观察集群创建情况
$ kubectl get clusterNAME PHASE AGE VERSIONcapi-quickstart Provisioned 8s v1.24.2$ kubectl get kubeadmcontrolplane -wNAME CLUSTER INITIALIZED API SERVER AVAILABLE REPLICAS READY UPDATED UNAVAILABLE AGE VERSIONcapi-quickstart-2xcsz capi-quickstart true
当 kubeadmcontrolplane 的 Initialized 为 True 后,clusterpedia 会自动同步该集群内的资源,可以使用 kubectl --cluster clusterpedia get po -A 来查看资源。
$ kubectl get pediaclusterNAME READY VERSION APISERVERdefault-capi-quickstart True v1.24.2$ kubectl --cluster clusterpedia get pod -ANAMESPACE CLUSTER NAME READY STATUS RESTARTS AGEkube-system default-capi-quickstart kube-apiserver-capi-quickstart-2xcsz-fxrrk 1/1 Running 0 2m32skube-system default-capi-quickstart kube-scheduler-capi-quickstart-2xcsz-fxrrk 1/1 Running 0 2m31skube-system default-capi-quickstart coredns-6d4b75cb6d-lrwj4 0/1 Pending 0 2m20skube-system default-capi-quickstart kube-proxy-p8v9m 1/1 Running 0 2m20skube-system default-capi-quickstart kube-controller-manager-capi-quickstart-2xcsz-fxrrk 1/1 Running 0 2m32skube-system default-capi-quickstart etcd-capi-quickstart-2xcsz-fxrrk 1/1 Running 0 2m32skube-system default-capi-quickstart kube-proxy-2ln2w 1/1 Running 0 105skube-system default-capi-quickstart coredns-6d4b75cb6d-2hcmz 0/1 Pending 0 2m20s
自动创建的 pediacluster 默认的同步资源在 cluster-api clusterimportpolicy 中设置[13],用户也可以手动修改 pediacluster 中同步的配置, Synchronize Cluster Resources[14]。
在 Cluster API 中删除集群时,Clusterpedia 也同步删除 PeidaCluster,不会继续同步该集群。
03 对多个集群的资源检索
使用上述步骤创建多个集群。
$ kubectl get clusterNAME PHASE AGE VERSIONcapi-quickstart Provisioned 10m v1.24.2capi-quickstart-2 Provisioned 118s v1.24.2$ kubectl get kubeadmcontrolplaneNAME CLUSTER INITIALIZED API SERVER AVAILABLE REPLICAS READY UPDATED UNAVAILABLE AGE VERSIONcapi-quickstart-2-ctm9k capi-quickstart-2 true 1 1 1 10m v1.24.2capi-quickstart-2xcsz capi-quickstart true 1 1 1 19m v1.24.2$ # pediacluster 会根据 cluster 资源自动创建$ kubectl get pediacluster -o wideNAME READY VERSION APISERVER VALIDATED SYNCHRORUNNING CLUSTERHEALTHYdefault-capi-quickstart True v1.24.2 Validated Running Healthydefault-capi-quickstart-2 True v1.24.2 Validated Running Healthy$ kubectl --cluster clusterpedia get noCLUSTER NAME STATUS ROLES AGE VERSIONdefault-capi-quickstart-2 capi-quickstart-2-ctm9k-g2m87 NotReady control-plane 12m v1.24.2default-capi-quickstart-2 capi-quickstart-2-md-0-s8hbx-7bd44554b5-kzcb6 NotReady <none> 11m v1.24.2default-capi-quickstart capi-quickstart-2xcsz-fxrrk NotReady control-plane 21m v1.24.2default-capi-quickstart capi-quickstart-md-0-9tw2g-b8b4f46cf-gggvq NotReady <none> 20m v1.24.2
clusterpedia 提供了两种资源检索方式
- 兼容 Kubernetes OpenAPI 的资源检索[15]
$ kubectl --cluster clusterpedia get cm -ANAMESPACE CLUSTER NAME DATA AGEkube-system default-capi-quickstart extension-apiserver-authentication 6 19mkube-system default-capi-quickstart kubeadm-config 1 19mkube-public default-capi-quickstart cluster-info 2 19mkube-system default-capi-quickstart kube-proxy 2 19mkube-node-lease default-capi-quickstart kube-root-ca.crt 1 19mkube-system default-capi-quickstart-2 extension-apiserver-authentication 6 10mkube-system default-capi-quickstart kubelet-config 1 19mkube-system default-capi-quickstart coredns 1 19mkube-system default-capi-quickstart kube-root-ca.crt 1 19mkube-public default-capi-quickstart kube-root-ca.crt 1 19mkube-system default-capi-quickstart-2 coredns 1 10mdefault default-capi-quickstart kube-root-ca.crt 1 19mkube-system default-capi-quickstart-2 kube-proxy 2 10mkube-system default-capi-quickstart-2 kubeadm-config 1 10mkube-system default-capi-quickstart-2 kubelet-config 1 10mkube-system default-capi-quickstart-2 kube-root-ca.crt 1 10mkube-node-lease default-capi-quickstart-2 kube-root-ca.crt 1 10mkube-public default-capi-quickstart-2 cluster-info 3 10mkube-public default-capi-quickstart-2 kube-root-ca.crt 1 10mdefault default-capi-quickstart-2 kube-root-ca.crt 1 10m$ # gen cluster shortcuts$ curl -sfL https://raw.githubusercontent.com/clusterpedia-io/clusterpedia/main/hack/gen-clusterconfigs.sh | sh -$ kubectl --cluster default-capi-quickstart get cm -n kube-system
- Collection Resource[16]
$ kubectl get collectionresourcesNAME RESOURCESany *workloads apps.deployments,apps.daemonsets,apps.statefulsetskuberesources .*,admission.k8s.io.*,admissionregistration.k8s.io.*,apiextensions.k8s.io.*,apps.*,authentication.k8s.io.*,authorization.k8s.io.*,autoscaling.*,batch.*,certificates.k8s.io.*,coordination.k8s.io.*,discovery.k8s.io.*,events.k8s.io.*,extensions.*,flowcontrol.apiserver.k8s.io.*,imagepolicy.k8s.io.*,internal.apiserver.k8s.io.*,networking.k8s.io.*,node.k8s.io.*,policy.*,rbac.authorization.k8s.io.*,scheduling.k8s.io.*,storage.k8s.io.*$ kubectl get collectionresources workloads
检索条件
- 元信息过滤 (资源名称,命名空间,集群,创建时间区间)[17]
$ kubectl --cluster clusterpedia get cm -A -l \ "search.clusterpedia.io/clusters in (default-capi-quickstart,default-capi-quickstart-2),\ search.clusterpedia.io/namespaces in (kube-system,default)"NAMESPACE CLUSTER NAME DATA AGEkube-system default-capi-quickstart extension-apiserver-authentication 6 23mkube-system default-capi-quickstart kubeadm-config 1 23mkube-system default-capi-quickstart kube-proxy 2 23mkube-system default-capi-quickstart-2 extension-apiserver-authentication 6 14mkube-system default-capi-quickstart kubelet-config 1 23mkube-system default-capi-quickstart coredns 1 23mkube-system default-capi-quickstart kube-root-ca.crt 1 23mkube-system default-capi-quickstart-2 coredns 1 14mdefault default-capi-quickstart kube-root-ca.crt 1 23mkube-system default-capi-quickstart-2 kube-proxy 2 14mkube-system default-capi-quickstart-2 kubeadm-config 1 14mkube-system default-capi-quickstart-2 kubelet-config 1 14mkube-system default-capi-quickstart-2 kube-root-ca.crt 1 14mdefault default-capi-quickstart-2 kube-root-ca.crt 1 14m
- 模糊搜索[18]
- 增强的 Field Selector[19]
- 根据父辈或者祖辈 Owner 检索[20]
- 分页和排序[21]
- 自定义条件搜索[22]
欢迎大家下载使用和参与讨论,并在 issue 提出自己的意见和想法。
项目地址:
https://github.com/clusterpedia-io/clusterpedia
技术交流:点我