Version: v1.2
多集群应用交付
本章节会介绍如何使用 KubeVela 完成应用的多集群应用交付。
简介
如今,越来越多的企业及开发者出于不同的原因,开始在多集群环境中进行应用交付:
- 由于 Kubernetes 集群存在着部署规模的局限性(单一集群最多容纳 5k 节点),需要应用多集群技术来部署、管理海量的应用。
- 考虑到稳定性及高可用性,同一个应用可以部署在多个集群中,以实现容灾、异地多活等需求。
- 应用可能需要部署在不同的区域来满足不同政府对于数据安全性的政策需求。
下文将会介绍如何在 KubeVela 中使用多集群技术帮助你快速将应用部署在多集群环境中。
准备工作
在使用多集群应用部署之前,你需要将子集群通过 KubeConfig 加入到 KubeVela 的管控中来。Vela CLI 可以帮你实现这一点。
vela cluster join <your kubeconfig path>
该命令会自动使用 KubeConfig 中的 context.cluster 字段作为集群名称,你也可以使用 --name 参数来指定,如
vela cluster join stage-cluster.kubeconfig --name cluster-stagingvela cluster join prod-cluster.kubeconfig --name cluster-prod
在子集群加入 KubeVela 中后,你同样可以使用 CLI 命令来查看当前正在被 KubeVela 管控的所有集群。
$ vela cluster listCLUSTER TYPE ENDPOINTcluster-prod tls https://47.88.4.97:6443cluster-staging tls https://47.88.7.230:6443
如果你不需要某个子集群了,还可以将子集群从 KubeVela 管控中移除。
$ vela cluster detach cluster-prod
当然,如果现在有应用正跑在该集群中,这条命令会被 KubeVela 拒绝。
部署多集群应用
KubeVela 将一个 Kubernetes 集群看作是一个环境,对于一个应用,你可以将其部署在多个环境中。
下面的这个例子将会把应用先部署在预发环境中,待确认应用正常运行后,再将其部署在生产环境中。
对于不同的环境,KubeVela 支持进行差异化部署。比如在本文的例子中,预发环境只使用 webservice 组件而不是用 worker 组件,同时 webservice 也只部署了一份。而在生产环境中,两个组件都会使用,而且 webservice 还会部署三副本。
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: example-appnamespace: defaultspec:components:- name: hello-world-servertype: webserviceproperties:image: crccheck/hello-worldport: 8000traits:- type: scalerproperties:replicas: 1- name: data-workertype: workerproperties:image: busyboxcmd:- sleep- '1000000'policies:- name: example-multi-env-policytype: env-bindingproperties:envs:- name: stagingplacement: # 选择要部署的集群clusterSelector:name: cluster-stagingselector: # 选择要使用的组件components:- hello-world-server- name: prodplacement:clusterSelector:name: cluster-prodpatch: # 对组件进行差异化配置components:- name: hello-world-servertype: webservicetraits:- type: scalerproperties:replicas: 3- name: health-policy-demotype: healthproperties:probeInterval: 5probeTimeout: 10workflow:steps:# 部署到预发环境中- name: deploy-stagingtype: deploy2envproperties:policy: example-multi-env-policyenv: staging# 手动确认- name: manual-approvaltype: suspend# 部署到生产环境中- name: deploy-prodtype: deploy2envproperties:policy: example-multi-env-policyenv: prod
在应用创建后,它会通过 KubeVela 工作流完成部署。
首先,它会将应用部署到预发环境中,你可以运行下面的命令来查看应用的状态。
> kubectl get application example-appNAME COMPONENT TYPE PHASE HEALTHY STATUS AGEexample-app hello-world-server webservice workflowSuspending true Ready:1/1 10s
可以看到,当前的部署工作流在 manual-approval 步骤中暂停。
...status:workflow:appRevision: example-app-v1:44a6447e3653bcc2contextBackend:apiVersion: v1kind: ConfigMapname: workflow-example-app-contextuid: 56ddcde6-8a83-4ac3-bf94-d19f8f55eb3dmode: StepByStepsteps:- id: wek2b31nainame: deploy-stagingphase: succeededtype: deploy2env- id: 7j5eb764mkname: manual-approvalphase: succeededtype: suspendsuspend: trueterminated: falsewaitCount: 0
你也可以检查 status.service 字段来查看应用的健康状态。
...status:services:- env: staginghealthy: truemessage: 'Ready:1/1 'name: hello-world-serverscopes:- apiVersion: core.oam.dev/v1alpha2kind: HealthScopename: health-policy-demonamespace: testuid: 6e6230a3-93f3-4dba-ba09-dd863b6c4a88traits:- healthy: truetype: scalerworkloadDefinition:apiVersion: apps/v1kind: Deployment
通过工作流的 resume 指令,你可以在确认当前部署正常后,继续将应用部署至生产环境中。
> vela workflow resume example-appSuccessfully resume workflow: example-app
再次确认应用的状态:
> kubectl get application example-appNAME COMPONENT TYPE PHASE HEALTHY STATUS AGEexample-app hello-world-server webservice running true Ready:1/1 62s
status:services:- env: staginghealthy: truemessage: 'Ready:1/1 'name: hello-world-serverscopes:- apiVersion: core.oam.dev/v1alpha2kind: HealthScopename: health-policy-demonamespace: defaultuid: 9174ac61-d262-444b-bb6c-e5f0caee706atraits:- healthy: truetype: scalerworkloadDefinition:apiVersion: apps/v1kind: Deployment- env: prodhealthy: truemessage: 'Ready:3/3 'name: hello-world-serverscopes:- apiVersion: core.oam.dev/v1alpha2kind: HealthScopename: health-policy-demonamespace: defaultuid: 9174ac61-d262-444b-bb6c-e5f0caee706atraits:- healthy: truetype: scalerworkloadDefinition:apiVersion: apps/v1kind: Deployment- env: prodhealthy: truemessage: 'Ready:1/1 'name: data-workerscopes:- apiVersion: core.oam.dev/v1alpha2kind: HealthScopename: health-policy-demonamespace: defaultuid: 9174ac61-d262-444b-bb6c-e5f0caee706aworkloadDefinition:apiVersion: apps/v1kind: Deployment
现在,工作流中的所有步骤都已完成。
...status:workflow:appRevision: example-app-v1:44a6447e3653bcc2contextBackend:apiVersion: v1kind: ConfigMapname: workflow-example-app-contextuid: e1e7bd2d-8743-4239-9de7-55a0dd76e5d3mode: StepByStepsteps:- id: q8yx7pr8wbname: deploy-stagingphase: succeededtype: deploy2env- id: 6oxrtvki9oname: manual-approvalphase: succeededtype: suspend- id: uk287p8c31name: deploy-prodphase: succeededtype: deploy2envsuspend: falseterminated: falsewaitCount: 0
更多使用案例
KubeVela 可以提供更多的应用多集群部署策略,如将单一应用的不同组件部署在不同环境中,或在管控集群及子集群中混合部署。
对于工作流与多集群部署的使用,你可以通过下图简单了解其整体流程。

更多的多集群环境下应用部署的使用案例将在不久后加入文档中。