关于阿里云:Helm-Chart-多环境多集群交付实践透视资源拓扑和差异

8次阅读

共计 7483 个字符,预计需要花费 19 分钟才能阅读完成。

Helm Charts [ 1] 如今已是一种十分风行的软件打包形式,在其利用市场中你能够找到靠近一万款实用于云原生环境的软件。而后在现在的混合云多集群环境中,业务越来越依赖部署到不同的集群、不同的环境、同时指定不同的配置。再这样的环境下,单纯依赖 Helm 工具可能无奈做到灵便的部署和交付。

在本文中,咱们将介绍如何通过 KubeVela [ 2] 解决多集群环境下 Helm Chart 的部署问题。如果你手里没有多集群也不要紧,咱们将介绍一种仅依赖于 Docker 或者 Linux 零碎的轻量级部署形式,能够让你轻松的体验多集群性能。当然,KubeVela 也齐全具备单集群的 Helm Chart 交付 [ 3] 能力。

前提条件

  • 装置 Docker v20.10.5+ (runc >= v1.0.0-rc93) 或者你的操作系统是 Linux。
  • VelaD [ 4],一个轻量级的部署 KubeVela 和 Kubernetes 的工具。

筹备集群

本节是做 KubeVela 以及多集群环境的筹备,咱们将基于 Docker 或者 Linux 环境从头开始。如果你曾经具备了 KubeVela 的环境并且实现了集群治理 [ 5],则能够跳过本节。

装置 KubeVela 管制立体

velad install

将新创建的集群导入到环境变量

export KUBECONFIG=$(velad kubeconfig --name default --host)

到这里,祝贺你!咱们曾经实现了 KubeVela 管制立体的装置。你能够通过上面这个形式退出你的 Kubernetes 集群:

vela cluster join <path-to-kubeconfig-of-cluster> --name foo

如果你没有现成的 Kubernetes 集群,VelaD 也能够很不便的为你创立一个:

用 velad 创立一个名为 foo 的集群,并退出到管制立体

velad install --name foo --cluster-only
vela cluster join $(velad kubeconfig --name foo --internal) --name foo

作为一个充沛可扩大的管制立体,KubeVela 的大多数能力都是作为插件提供的。接下来的几步咱们介绍装置 Helm 多集群部署的必要插件。

启用 velaux 插件,取得 UI 控制台

vela addon enable velaux

启用 fluxcd 插件取得 helm chart 交付能力

vela addon enable fluxcd

如果你在退出新集群之前已启用过 fluxcd 插件,则应该通过以下形式来为新退出的集群启用(部署)插件:

vela addon enable fluxcd --clusters foo

至此,咱们实现了所有的筹备工作,能够查看退出的集群了:

$ vela cluster ls
CLUSTER ALIAS   TYPE            ENDPOINT                ACCEPTED    LABELS
local           Internal        -                       true
foo             X509Certificate https://172.20.0.6:6443 true

local 是 KubeVela 管制立体的集群,foo 则是咱们刚刚增加的集群。

多集群部署

咱们能够应用 topology 策略来指定 Helm Chart 交付的环境,指令如下:

cat <<EOF | vela up -f -
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  name: helm-hello
spec:
  components:
    - name: hello
      type: helm
      properties:
        repoType: "helm"
        url: "https://jhidalgo3.github.io/helm-charts/"
        chart: "hello-kubernetes-chart"
        version: "3.0.0"
  policies:
    - name: foo-cluster-only
      type: topology
      properties:
        clusters: ["foo"]
EOF

clusters 字段的 topology 策略是一个切片(slice),此处能够指定多个集群的名称。你还能够应用标签选择器或指定命名空间,详情见参考文档 [ 6]

部署后,你能够通过以下形式查看已部署的应用程序:

vela status helm-hello

部署胜利的预期输入应该如下:

About:

  Name:         helm-hello
  Namespace:    default
  Created at:   2022-06-09 19:14:57 +0800 CST
  Status:       running

Workflow:

  mode: DAG
  finished: true
  Suspend: false
  Terminated: false
  Steps
  - id:vtahj5zrz4
    name:deploy-foo-cluster-only
    type:deploy
    phase:succeeded
    message:

Services:

  - Name: hello
    Cluster: foo  Namespace: default
    Type: helm
    Healthy Fetch repository successfully, Create helm release successfully
    No trait applied

你能够通过以下形式查看已部署的资源:

$ vela status helm-hello --tree
CLUSTER       NAMESPACE     RESOURCE             STATUS
foo       ─── default   ─┬─ HelmRelease/hello    updated
                         └─ HelmRepository/hello updated

你也能够通过 VelaUX 查看已部署的资源。

应用 UI 控制台查看部署状态

通过应用 velaux UI 控制台,则能够很不便的查看多集群信息,并取得对立的体验。你能够参考文档 *[ 7] * 理解 VelaUX 的拜访和应用细节。

通过 UI 界面,咱们能够:

  • 查看来自不同集群的实例状态和事件:
  • 查看来自不同集群的实例日志:
  • 查看资源拓扑关系和状态:

应用 Override 配置进行部署

在某些状况下,咱们会为不同集群的 Helm Chart 设置不同的 Value,这样咱们能够应用 Override 策略 [ 8]

上面是一个简单的示例,咱们将把一个 Helm Chart 部署到两个集群中,并为每个集群指定不同的 Value。让咱们部署它:

cat <<EOF | vela up -f -
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  name: helm-hello
spec:
  components:
    - name: hello
      type: helm
      properties:
        repoType: "helm"
        url: "https://jhidalgo3.github.io/helm-charts/"
        chart: "hello-kubernetes-chart"
        version: "3.0.0"
  policies:
    - name: topology-local
      type: topology
      properties:
        clusters: ["local"]
    - name: topology-foo
      type: topology
      properties:
        clusters: ["foo"]
    - name: override-local
      type: override
      properties:
        components:
          - name: hello
            properties:
              values:
                configs:
                  MESSAGE: Welcome to Control Plane Cluster!
    - name: override-foo
      type: override
      properties:
        components:
          - name: hello
            properties:
              values:
                configs:
                  MESSAGE: Welcome to Your New Foo Cluster!
  workflow:
    steps:
      - name: deploy2local
        type: deploy
        properties:
          policies: ["topology-local", "override-local"]
      - name: manual-approval
        type: suspend
      - name: deploy2foo
        type: deploy
        properties:
          policies: ["topology-foo", "override-foo"]
EOF

留神:如果你感觉策略和工作流程有点简单,你能够将它们作为一个内部对象并仅援用该对象,用法和 器交付 [ 9] 是一样** 的。

部署过程分为三个步骤:

(1)部署到本地集群;

(2)期待人工审批;

(3)部署到 foo 集群。

你会发现它在第一步之后就被暂停了,就像上面这样:

$ vela status helm-hello
About:

  Name:         helm-hello
  Namespace:    default
  Created at:   2022-06-09 19:38:13 +0800 CST
  Status:       workflowSuspending

Workflow:

  mode: StepByStep
  finished: false
  Suspend: true
  Terminated: false
  Steps
  - id:ww4cydlvee
    name:deploy2local
    type:deploy
    phase:succeeded
    message:
  - id:xj6hu97e1e
    name:manual-approval
    type:suspend
    phase:succeeded
    message:

Services:

  - Name: hello
    Cluster: local  Namespace: default
    Type: helm
    Healthy Fetch repository successfully, Create helm release successfully
    No trait applied

你能够查看并应用 Value 为“Welcome to Control Plane Cluster!”的部署在管制立体的 Helm Chart。

vela port-forward helm-hello

浏览器会主动提醒如下页面:

发现部署胜利,让咱们持续。

vela workflow resume helm-hello

而后它会部署到 foo 集群,你能够查看这些资源的详细信息。

$ vela status helm-hello --tree --detail
CLUSTER       NAMESPACE     RESOURCE             STATUS    APPLY_TIME          DETAIL
foo       ─── default   ─┬─ HelmRelease/hello    updated   2022-06-09 19:38:13 Ready: True  Status: Release reconciliation succeeded  Age: 64s
                         └─ HelmRepository/hello updated   2022-06-09 19:38:13 URL: https://jhidalgo3.github.io/helm-charts/  Age: 64s  Ready: True
                                                                               Status: stored artifact for revision 'ab876069f02d779cb4b63587af1266464818ba3790c0ccd50337e3cdead44803'
local     ─── default   ─┬─ HelmRelease/hello    updated   2022-06-09 19:38:13 Ready: True  Status: Release reconciliation succeeded  Age: 7m34s
                         └─ HelmRepository/hello updated   2022-06-09 19:38:13 URL: https://jhidalgo3.github.io/helm-charts/  Age: 7m34s  Ready: True

Status: stored 再次应用端口转发:

vela port-forward helm-hello

而后它会弹出一些选项:

? You have 2 deployed resources in your app. Please choose one:  [Use arrows to move, type to filter]
> Cluster: foo | Namespace: default | Kind: HelmRelease | Name: hello
  Cluster: local | Namespace: default | Kind: HelmRelease | Name: hello

抉择带有 foo 集群的选项,而后你会看到后果曾经被新音讯笼罩。

$ curl http://127.0.0.1:8080/
...snip...
      <div id="message">
  Welcome to Your New Foo Cluster!
</div>
...snip...

为不同环境指定不同的 Value 文件

你能够为不同环境抉择 Helm Chart 中现有的不同 Value 文件。比方:

请确保你的本地集群有两个命名空间“test”和“prod”,它们代表咱们示例中的两个环境。

咱们以 Chart hello-kubernetes-chart 为例。这个 Chart 有两个 Value 文件。你能够拉取此 Chart 并查看其中蕴含的所有文件:

$ tree ./hello-kubernetes-chart
./hello-kubernetes-chart
├── Chart.yaml
├── templates
│ ├── NOTES.txt
│ ├── _helpers.tpl
│ ├── config-map.yaml
│ ├── deployment.yaml
│ ├── hpa.yaml
│ ├── ingress.yaml
│ ├── service.yaml
│ ├── serviceaccount.yaml
│ └── tests
│ └── test-connection.yaml
├── values-production.yaml
└── values.yaml

咱们能够看到此 Chart 中有 values.yaml values-production.yaml 这两个 Value 文件。

cat <<EOF | vela up -f -
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  name: hello-kubernetes
spec:
  components:
    - name: hello-kubernetes
      type: helm
      properties:
        repoType: "helm"
        url: "https://wangyikewxgm.github.io/my-charts/"
        chart: "hello-kubernetes-chart"
        version: "0.1.0"

  policies:
    - name: topology-test
      type: topology
      properties:
        clusters: ["local"]
        namespace: "test"
    - name: topology-prod
      type: topology
      properties:
        clusters: ["local"]
        namespace: "prod"
    - name: override-prod
      type: override
      properties:
        components:
          - name: hello-kubernetes
            properties:
              valuesFiles:
                - "values-production.yaml"
  workflow:
    steps:
      - name: deploy2test
        type: deploy
        properties:
          policies: ["topology-test"]
      - name: deploy2prod
        type: deploy
        properties:
          policies: ["topology-prod", "override-prod"]  
EOF

拜访 Application 的 endpoint:

vela port-forward hello-kubernetes

如果你抉择 Cluster: local | Namespace: test | Kind: HelmRelease | Name: hello-kubernetes 你会看到:

抉择 Cluster: local | Namespace: prod | Kind: HelmRelease | Name: hello-kubernetes 则会看到:

清理

如果你应用 velad 进行此演示,则能够通过以下形式便捷地进行清理:

  • 清理 foo 集群
velad uninstall -n foo
  • 清理默认集群
velad uninstall

不仅如此

KubeVela 提供的能力远不止如此,通过装置其余插件,你还能够取得包含金丝雀公布 *[ 10] * 在内的更多能力,为你的 Helm Chart 交付保驾护航。

快应用 KubeVela 交付 Helm Chart,让现代化的利用交付和治理更简略、轻松、牢靠!

相干链接

[1] Helm Charts:

https://artifacthub.io/packag…

[2] KubeVela:

https://kubevela.io/

[3] 单集群的 Helm Charts 交付

https://kubevela.net/zh/docs/…

*[4] velad:*

https://github.com/kubevela/v…

[5] 集群治理:

https://kubevela.net/zh/docs/…

[6] 参考文档:

https://kubevela.net/zh/docs/…

[7] 文档

https://kubevela.io/docs/inst…

[8] Override 策略

https://kubevela.io/docs/end-…

[9] 容器交付

https://kubevela.io/docs/case…

[10] 金丝雀公布:

https://kubevela.io/docs/tuto…


您能够通过如下资料理解更多对于 KubeVela 以及 OAM 我的项目的细节:

  • 我的项目代码库:github.com/oam-dev/kubevela 欢送 Star/Watch/Fork!
  • 我的项目官方主页与文档:kubevela.io,从 1.1 版本开始,已提供中文、英文文档,更多语言文档欢送开发者进行翻译。
  • 我的项目钉钉群:23310022;Slack:CNCF #kubevela Channel
  • 退出微信群:请先增加以下 maintainer 微信号,表明进入 KubeVela 用户群:

戳此处:查看 KubeVela 我的项目官网!

正文完
 0