Cluster API 检索从未如此简单

开发
如何快速部署一套 Cluster API And Clusterpedia 的示例环境?

​Clusterpedia 是一个 CNCF 沙箱项目,用于跨集群复杂的资源检索。

名字源于 Wikipedia,寓意是打造多集群的百科全书,可以与多个集群同步资源,并在与 Kubernetes OpenAPI 兼容的基础上,提供更强大的搜索功能,以帮助您快速、简便、有效地获取任何多集群资源。​

   项目地址:https://github.com/clusterpedia-io/clusterpedia

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 cluster
NAME PHASE AGE VERSION
capi-quickstart Provisioned 10m v1.24.2
capi-quickstart-2 Provisioned 118s v1.24.2


$ kubectl get kubeadmcontrolplane
NAME CLUSTER INITIALIZED API SERVER AVAILABLE REPLICAS READY UPDATED UNAVAILABLE AGE VERSION
capi-quickstart-2-ctm9k capi-quickstart-2 true 1 1 1 10m v1.24.2
capi-quickstart-2xcsz capi-quickstart true 1 1 1 19m v1.24.2


$ # pediacluster 会根据 cluster 资源自动创建,更新和删除
$ kubectl get pediacluster -o wide
NAME READY VERSION APISERVER VALIDATED SYNCHRORUNNING CLUSTERHEALTHY
default-capi-quickstart True v1.24.2 Validated Running Healthy
default-capi-quickstart-2 True v1.24.2 Validated Running Healthy


$ kubectl --cluster clusterpedia get no
CLUSTER NAME STATUS ROLES AGE VERSION
default-capi-quickstart-2 capi-quickstart-2-ctm9k-g2m87 NotReady control-plane 12m v1.24.2
default-capi-quickstart-2 capi-quickstart-2-md-0-s8hbx-7bd44554b5-kzcb6 NotReady <none> 11m v1.24.2
default-capi-quickstart capi-quickstart-2xcsz-fxrrk NotReady control-plane 21m v1.24.2
default-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 <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraMounts:
- hostPath: /var/run/docker.sock
containerPath: /var/run/docker.sock
EOF


$ 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 cluster
NAME PHASE AGE VERSION
capi-quickstart Provisioned 8s v1.24.2


$ kubectl get kubeadmcontrolplane -w
NAME CLUSTER INITIALIZED API SERVER AVAILABLE REPLICAS READY UPDATED UNAVAILABLE AGE VERSION
capi-quickstart-2xcsz capi-quickstart true

当 kubeadmcontrolplane 的 Initialized 为 True 后,clusterpedia 会自动同步该集群内的资源,可以使用 kubectl --cluster clusterpedia get po -A 来查看资源。

$ kubectl get pediacluster
NAME READY VERSION APISERVER
default-capi-quickstart True v1.24.2


$ kubectl --cluster clusterpedia get pod -A
NAMESPACE CLUSTER NAME READY STATUS RESTARTS AGE
kube-system default-capi-quickstart kube-apiserver-capi-quickstart-2xcsz-fxrrk 1/1 Running 0 2m32s
kube-system default-capi-quickstart kube-scheduler-capi-quickstart-2xcsz-fxrrk 1/1 Running 0 2m31s
kube-system default-capi-quickstart coredns-6d4b75cb6d-lrwj4 0/1 Pending 0 2m20s
kube-system default-capi-quickstart kube-proxy-p8v9m 1/1 Running 0 2m20s
kube-system default-capi-quickstart kube-controller-manager-capi-quickstart-2xcsz-fxrrk 1/1 Running 0 2m32s
kube-system default-capi-quickstart etcd-capi-quickstart-2xcsz-fxrrk 1/1 Running 0 2m32s
kube-system default-capi-quickstart kube-proxy-2ln2w 1/1 Running 0 105s
kube-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 cluster
NAME PHASE AGE VERSION
capi-quickstart Provisioned 10m v1.24.2
capi-quickstart-2 Provisioned 118s v1.24.2


$ kubectl get kubeadmcontrolplane
NAME CLUSTER INITIALIZED API SERVER AVAILABLE REPLICAS READY UPDATED UNAVAILABLE AGE VERSION
capi-quickstart-2-ctm9k capi-quickstart-2 true 1 1 1 10m v1.24.2
capi-quickstart-2xcsz capi-quickstart true 1 1 1 19m v1.24.2


$ # pediacluster 会根据 cluster 资源自动创建
$ kubectl get pediacluster -o wide
NAME READY VERSION APISERVER VALIDATED SYNCHRORUNNING CLUSTERHEALTHY
default-capi-quickstart True v1.24.2 Validated Running Healthy
default-capi-quickstart-2 True v1.24.2 Validated Running Healthy


$ kubectl --cluster clusterpedia get no
CLUSTER NAME STATUS ROLES AGE VERSION
default-capi-quickstart-2 capi-quickstart-2-ctm9k-g2m87 NotReady control-plane 12m v1.24.2
default-capi-quickstart-2 capi-quickstart-2-md-0-s8hbx-7bd44554b5-kzcb6 NotReady <none> 11m v1.24.2
default-capi-quickstart capi-quickstart-2xcsz-fxrrk NotReady control-plane 21m v1.24.2
default-capi-quickstart capi-quickstart-md-0-9tw2g-b8b4f46cf-gggvq NotReady <none> 20m v1.24.2

clusterpedia 提供了两种资源检索方式

兼容 Kubernetes OpenAPI 的资源检索[15]

$ kubectl --cluster clusterpedia get cm -A
NAMESPACE CLUSTER NAME DATA AGE
kube-system default-capi-quickstart extension-apiserver-authentication 6 19m
kube-system default-capi-quickstart kubeadm-config 1 19m
kube-public default-capi-quickstart cluster-info 2 19m
kube-system default-capi-quickstart kube-proxy 2 19m
kube-node-lease default-capi-quickstart kube-root-ca.crt 1 19m
kube-system default-capi-quickstart-2 extension-apiserver-authentication 6 10m
kube-system default-capi-quickstart kubelet-config 1 19m
kube-system default-capi-quickstart coredns 1 19m
kube-system default-capi-quickstart kube-root-ca.crt 1 19m
kube-public default-capi-quickstart kube-root-ca.crt 1 19m
kube-system default-capi-quickstart-2 coredns 1 10m
default default-capi-quickstart kube-root-ca.crt 1 19m
kube-system default-capi-quickstart-2 kube-proxy 2 10m
kube-system default-capi-quickstart-2 kubeadm-config 1 10m
kube-system default-capi-quickstart-2 kubelet-config 1 10m
kube-system default-capi-quickstart-2 kube-root-ca.crt 1 10m
kube-node-lease default-capi-quickstart-2 kube-root-ca.crt 1 10m
kube-public default-capi-quickstart-2 cluster-info 3 10m
kube-public default-capi-quickstart-2 kube-root-ca.crt 1 10m
default 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 collectionresources
NAME RESOURCES
any *
workloads apps.deployments,apps.daemonsets,apps.statefulsets
kuberesources .*,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 AGE
kube-system default-capi-quickstart extension-apiserver-authentication 6 23m
kube-system default-capi-quickstart kubeadm-config 1 23m
kube-system default-capi-quickstart kube-proxy 2 23m
kube-system default-capi-quickstart-2 extension-apiserver-authentication 6 14m
kube-system default-capi-quickstart kubelet-config 1 23m
kube-system default-capi-quickstart coredns 1 23m
kube-system default-capi-quickstart kube-root-ca.crt 1 23m
kube-system default-capi-quickstart-2 coredns 1 14m
default default-capi-quickstart kube-root-ca.crt 1 23m
kube-system default-capi-quickstart-2 kube-proxy 2 14m
kube-system default-capi-quickstart-2 kubeadm-config 1 14m
kube-system default-capi-quickstart-2 kubelet-config 1 14m
kube-system default-capi-quickstart-2 kube-root-ca.crt 1 14m
default default-capi-quickstart-2 kube-root-ca.crt 1 14m
  • 模糊搜索[18]
  • 增强的 Field Selector[19]
  • 根据父辈或者祖辈 Owner 检索[20]
  • 分页和排序[21]
  • 自定义条件搜索[22]​

资料链接

[1]    Clusterpedia 仓库: https://github.com/clusterpedia-io/clusterpedia/tree/main/deploy/clusterimportpolicy

[2]    ClusterImportPolicy: https://clusterpedia.io/zh-cn/docs/concepts/cluster-import-policy/

[3]    创建新的 ClusterImportPolicy: https://clusterpedia.io/docs/usage/interfacing-to-multi-cloud-platforms/#new-clusterimportpolicy

[4]    clusterpedia#288: https://github.com/clusterpedia-io/clusterpedia/pull/288

[5]    kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/

[6]    Kind: https://kind.sigs.k8s.io/

[7]    Docker: https://www.docker.com/

[8]    clusterctl: https://cluster-api.sigs.k8s.io/user/quick-start.html#install-clusterctl

[9]    详见: https://github.com/clusterpedia-io/clusterpedia/tree/main/charts

[10]  集群自动导入策略: https://clusterpedia.io/docs/concepts/cluster-import-policy/

[11]  Interfacing to Multi-Cloud Platforms: https://clusterpedia.io/docs/usage/interfacing-to-multi-cloud-platforms/

[12]  生成 kubectl cluster shortcut: https://clusterpedia.io/docs/usage/access-clusterpedia/#configure-the-cluster-shortcut-for-kubectl

[13]  clusterimportpolicy 中设置: https://clusterpedia.io/docs/concepts/cluster-import-policy/#pediacluster-template

[14]  Synchronize Cluster Resources: https://clusterpedia.io/docs/usage/sync-resources/

[15]  兼容 Kubernetes OpenAPI 的资源检索: https://clusterpedia.io/zh-cn/docs/usage/access-clusterpedia/#%E8%AE%BF%E9%97%AE-clusterpedia-%E8%B5%84%E6%BA%90

[16]  Collection Resource: https://clusterpedia.io/zh-cn/docs/concepts/collection-resource/

[17]  元信息过滤 (资源名称,命名空间,集群,创建时间区间): https://clusterpedia.io/docs/usage/search/#search-by-metadata

[18]  模糊搜索: https://clusterpedia.io/docs/usage/search/multi-cluster/#fuzzy-search

[19]  增强的 Field Selector: https://clusterpedia.io/docs/usage/search/multi-cluster/#field-selector

[20]  根据父辈或者祖辈 Owner 检索: https://clusterpedia.io/docs/usage/search/multi-cluster/#search-by-parent-or-ancestor-owner

[21]  分页和排序: https://clusterpedia.io/docs/usage/search/multi-cluster/#paging-and-sorting

[22]  自定义条件搜索: https://clusterpedia.io/docs/us​age/search/#advanced-searchcustom-conditional-search

责任编辑:未丽燕 来源: 云原生社区动态
相关推荐

2021-10-18 13:31:28

Web应用交互式

2017-04-21 14:57:57

AndroidSuperTextViTextView

2024-12-03 08:43:49

2020-03-03 15:40:51

开发技能代码

2020-03-17 07:41:50

ApacheKafka系统

2022-06-13 06:33:04

浏览器浏览器插件

2013-06-14 10:36:00

2019-05-23 10:55:22

Istio灰度发布ServiceMesh

2020-06-17 10:42:54

shellshell脚本Linux

2009-08-26 20:12:41

BMCBSMITIL

2013-11-28 13:47:37

Android4.4ART模式

2013-10-25 13:35:02

云计算

2024-06-04 10:45:07

OraclePostgreSQLMERGE

2024-02-27 18:06:35

2020-02-20 16:07:45

IT需求

2021-11-09 13:59:47

物联网安全设备

2021-02-06 17:49:49

人工智能智能医疗智慧安防

2020-12-28 07:47:35

动态代理AOP

2022-02-23 20:42:40

HTMLmarkdownturndown

2023-10-08 08:16:58

点赞
收藏

51CTO技术栈公众号