Version: v1.2
交付第一个应用
欢迎来到 KubeVela! 在本小节中,我们会向你介绍一些例子来帮助你理解如何使用 KubeVela 解决应用交付中的实际问题。
在实践之前,请确保你已经按照 快速安装 文档,在你的控制平面集群中安装了 KubeVela。
一个最简单的示例
KubeVela 中一个简单的应用部署定义,大致如下所示:
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: first-vela-appspec:components:- name: express-servertype: webserviceproperties:image: crccheck/hello-worldport: 8000traits:- type: ingress-1-20properties:domain: testsvc.example.comhttp:"/": 8000
我们可以直接使用 kubectl 把它提交给 KubeVela:
vela up -f https://raw.githubusercontent.com/oam-dev/kubevela/master/docs/examples/vela-app.yaml
上述命令一旦执行,KubeVela 就会帮助你在目标集群中交付一个 Web 服务类型的组件,且该组件的容器镜像是crccheck/hello-world。在本示例中,我们并没有特别指明目标集群是哪个,所以 KubeVela 会默认把应用部署在它所在的集群也就是控制平面集群当中。
而由于我们已经在上述 YAML 文件中为这个组件绑定了一个 ingress 类型的运维特征,KubeVela 就会指导 Kubernetes 自动为这个组件背后的工作负载配置 Service、端口映射和 HTTP 路由规则。所以只要目标集群具备 Ingress 能力,上述 YAML 一部署成功,你就可以立刻通过外域名来问这个应用了。
$ curl -H "Host:testsvc.example.com" http://<your ip address>/<xmp>Hello World## .## ## ## ==## ## ## ## ## ===/""""""""""""""""\___/ ===~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~\______ o _,/\ \ _,'`'--.._\..--''</xmp>
交付更多的组件
KubeVela 允许你部署的组件类型是非常丰富的。在上面的例子中,Web Service组件实际上就是一个预先编写好的CUE 文件。
你还可以选择其它很多类型,比如:
Helm 组件
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: app-delivering-chartspec:components:- name: redis-comptype: helmproperties:chart: redis-clusterversion: 6.2.7url: https://charts.bitnami.com/bitnamirepoType: helm
Terraform 云资源组件
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: rds-cloud-sourcespec:components:- name: sample-dbtype: alibaba-rdsproperties:instance_name: sample-dbaccount_name: oamtestpassword: U34rfwefwefffakedwriteConnectionSecretToRef:name: db-conn
来自 Git 仓库的组件(基于 Kustomize)
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: git-appspec:components:- name: git-comptype: kustomizeproperties:repoType: giturl: https://github.com/<path>/<to>/<repo>git:branch: masterpath: ./app/dev/
当然,还有更多。欢迎查看边栏中的选择待交付组件 章节来阅读关于部署各种类型的详细文档。如果你需要的话,你还可以在 KubeVela 中添加自己的组件类型。
绑定运维特征
KubeVela 能做的远不止部署,还包括运维。它允许你为待交付组件绑定预先定义好的各种运维行为(叫做运维特征),并且这个绑定会立刻生效。接下来,我们就为 Web 服务组件绑定一个“分批发布”运维特征:
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: rollout-trait-testspec:components:- name: express-servertype: webserviceexternalRevision: express-server-v1properties:image: stefanprodan/podinfo:4.0.3traits:- type: rolloutproperties:targetSize: 5rolloutBatches:- replicas: 2- replicas: 3
好了,接下来,只要上述 YAML 中的镜像版本发生变化,KubeVela 就会按照你所定义的分批策略来更新对应的应用实例了。
如果你想了解 KubeVela 所支持的所有运维特征,欢迎查看边栏中的 绑定运维特征 章节,同样的,KubeVela 允许你非常容易的在平台中添加自己的运维特征。
定义交付策略与交付工作流
组件与运维特征只是 KubeVela 非常基本的功能。KubeVela 本身实际上是一个全功能的持续交付(CD)系统,并且原生支持面向混合环境(比如混合云/多云/多集群)应用交付。
举个例子:
我要交付一个包括了两个组件的微服务应用,要求先需要在预发集群当中部署 1 个副本,然后暂停交付过程,请主管进行人工审核。当审核通过后,再继续部署到生产集群中,并且需要部署为3个副本。
想象一下,你需要在 CI/CD 流水线里编写多少“脏乱差”的一次性脚本或者胶水代码才能让上述流程自动化的、保证一定正确性的执行起来?
而在 KubeVela 中,上述流程可以非常容易的通过如下所示的声明式工作流建模出来:
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: # selecting the cluster to deploy toclusterSelector:name: cluster-stagingselector: # selecting which component to usecomponents:- hello-world-server- name: prodplacement:clusterSelector:name: cluster-prodpatch: # overlay patch on above componentscomponents:- name: hello-world-servertype: webservicetraits:- type: scalerproperties:replicas: 3- name: health-policy-demotype: healthproperties:probeInterval: 5probeTimeout: 10workflow:steps:# deploy to staging env- name: deploy-stagingtype: deploy2envproperties:policy: example-multi-env-policyenv: staging# manual check- name: manual-approvaltype: suspend# deploy to prod env- name: deploy-prodtype: deploy2envproperties:policy: example-multi-env-policyenv: prod
不需要任何“脏乱差”脚本,KubeVela 就能够以完全自动化、高确定性的声明式工作流完成所有的应用交付动作。更为重要的是,KubeVela 希望你继续使用你现有的任何 CI 方案,而 KubeVela 则负责帮助你更好的完成 CD 流程。