多集群应用交付

    本章节会介绍如何使用 KubeVela 完成应用的多集群应用交付。

    如今,越来越多的企业及开发者出于不同的原因,开始在多集群环境中进行应用交付:

    • 由于 Kubernetes 集群存在着部署规模的局限性(单一集群最多容纳 5k 节点),需要应用多集群技术来部署、管理海量的应用。
    • 考虑到稳定性及高可用性,同一个应用可以部署在多个集群中,以实现容灾、异地多活等需求。
    • 应用可能需要部署在不同的区域来满足不同政府对于数据安全性的政策需求。

    下文将会介绍如何在 KubeVela 中使用多集群技术帮助你快速将应用部署在多集群环境中。

    在使用多集群应用部署之前,你需要将子集群通过 KubeConfig 加入到 KubeVela 的管控中来。Vela CLI 可以帮你实现这一点。

    该命令会自动使用 KubeConfig 中的 字段作为集群名称,你也可以使用 --name 参数来指定,如

    1. vela cluster join stage-cluster.kubeconfig --name cluster-staging
    2. vela cluster join prod-cluster.kubeconfig --name cluster-prod

    在子集群加入 KubeVela 中后,你同样可以使用 CLI 命令来查看当前正在被 KubeVela 管控的所有集群。

    1. $ vela cluster list
    2. CLUSTER TYPE ENDPOINT
    3. cluster-prod tls https://47.88.4.97:6443
    4. cluster-staging tls https://47.88.7.230:6443

    如果你不需要某个子集群了,还可以将子集群从 KubeVela 管控中移除。

    1. $ vela cluster detach cluster-prod

    KubeVela 将一个 Kubernetes 集群看作是一个环境,对于一个应用,你可以将其部署在多个环境中。

    下面的这个例子将会把应用先部署在预发环境中,待确认应用正常运行后,再将其部署在生产环境中。

    对于不同的环境,KubeVela 支持进行差异化部署。比如在本文的例子中,预发环境只使用 webservice 组件而不是用 worker 组件,同时 webservice 也只部署了一份。而在生产环境中,两个组件都会使用,而且 webservice 还会部署三副本。

    在应用创建后,它会通过 KubeVela 工作流完成部署。

    你可以参考多环境部署和的用户手册来查看更多参数细节。

    首先,它会将应用部署到预发环境中,你可以运行下面的命令来查看应用的状态。

    1. > kubectl get application example-app
    2. NAME COMPONENT TYPE PHASE HEALTHY STATUS AGE
    3. example-app hello-world-server webservice workflowSuspending true Ready:1/1 10s

    可以看到,当前的部署工作流在 manual-approval 步骤中暂停。

    1. ...
    2. status:
    3. workflow:
    4. appRevision: example-app-v1:44a6447e3653bcc2
    5. contextBackend:
    6. apiVersion: v1
    7. kind: ConfigMap
    8. name: workflow-example-app-context
    9. uid: 56ddcde6-8a83-4ac3-bf94-d19f8f55eb3d
    10. mode: StepByStep
    11. steps:
    12. - id: wek2b31nai
    13. name: deploy-staging
    14. phase: succeeded
    15. type: deploy2env
    16. - id: 7j5eb764mk
    17. name: manual-approval
    18. phase: succeeded
    19. type: suspend
    20. suspend: true
    21. terminated: false
    22. waitCount: 0
    1. ...
    2. status:
    3. services:
    4. - env: staging
    5. healthy: true
    6. name: hello-world-server
    7. - apiVersion: core.oam.dev/v1alpha2
    8. kind: HealthScope
    9. name: health-policy-demo
    10. namespace: test
    11. uid: 6e6230a3-93f3-4dba-ba09-dd863b6c4a88
    12. traits:
    13. - healthy: true
    14. type: scaler
    15. workloadDefinition:
    16. apiVersion: apps/v1
    17. kind: Deployment

    通过工作流的 resume 指令,你可以在确认当前部署正常后,继续将应用部署至生产环境中。

    再次确认应用的状态:

    1. > kubectl get application example-app
    2. NAME COMPONENT TYPE PHASE HEALTHY STATUS AGE
    3. example-app hello-world-server webservice running true Ready:1/1 62s
    1. status:
    2. services:
    3. - env: staging
    4. healthy: true
    5. message: 'Ready:1/1 '
    6. name: hello-world-server
    7. scopes:
    8. - apiVersion: core.oam.dev/v1alpha2
    9. kind: HealthScope
    10. name: health-policy-demo
    11. namespace: default
    12. uid: 9174ac61-d262-444b-bb6c-e5f0caee706a
    13. traits:
    14. - healthy: true
    15. type: scaler
    16. workloadDefinition:
    17. apiVersion: apps/v1
    18. kind: Deployment
    19. - env: prod
    20. healthy: true
    21. message: 'Ready:3/3 '
    22. name: hello-world-server
    23. scopes:
    24. - apiVersion: core.oam.dev/v1alpha2
    25. kind: HealthScope
    26. name: health-policy-demo
    27. namespace: default
    28. uid: 9174ac61-d262-444b-bb6c-e5f0caee706a
    29. - healthy: true
    30. type: scaler
    31. workloadDefinition:
    32. apiVersion: apps/v1
    33. kind: Deployment
    34. - env: prod
    35. healthy: true
    36. message: 'Ready:1/1 '
    37. name: data-worker
    38. scopes:
    39. - apiVersion: core.oam.dev/v1alpha2
    40. kind: HealthScope
    41. name: health-policy-demo
    42. namespace: default
    43. uid: 9174ac61-d262-444b-bb6c-e5f0caee706a
    44. workloadDefinition:
    45. apiVersion: apps/v1
    46. kind: Deployment

    现在,工作流中的所有步骤都已完成。

    1. ...
    2. status:
    3. workflow:
    4. appRevision: example-app-v1:44a6447e3653bcc2
    5. contextBackend:
    6. apiVersion: v1
    7. kind: ConfigMap
    8. name: workflow-example-app-context
    9. uid: e1e7bd2d-8743-4239-9de7-55a0dd76e5d3
    10. mode: StepByStep
    11. steps:
    12. - id: q8yx7pr8wb
    13. name: deploy-staging
    14. phase: succeeded
    15. type: deploy2env
    16. - id: 6oxrtvki9o
    17. name: manual-approval
    18. phase: succeeded
    19. type: suspend
    20. - id: uk287p8c31
    21. name: deploy-prod
    22. phase: succeeded
    23. type: deploy2env
    24. suspend: false
    25. waitCount: 0

    KubeVela 可以提供更多的应用多集群部署策略,如将单一应用的不同组件部署在不同环境中,或在管控集群及子集群中混合部署。

    对于工作流与多集群部署的使用,你可以通过下图简单了解其整体流程。

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