Version: v1.0

学习使用 Appfile

appfile 的示例如下:

  1. name: testapp
  2. services:
  3. frontend: # 1st service
  4. image: oamdev/testapp:v1
  5. build:
  6. docker:
  7. file: Dockerfile
  8. context: .
  9. cmd: ["node", "server.js"]
  10. port: 8080
  11. route: # trait
  12. domain: example.com
  13. rules:
  14. - path: /testapp
  15. rewriteTarget: /
  16. backend: # 2nd service
  17. type: task # workload type
  18. image: perl
  19. cmd: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]

在底层,Appfile 会从源码构建镜像,然后用镜像名称创建 application 资源

Schema

在深入学习 Appfile 的详细 schema 之前,我们建议你先熟悉 KubeVela 的核心概念

  1. name: _app-name_
  2. services:
  3. _service-name_:
  4. # If `build` section exists, this field will be used as the name to build image. Otherwise, KubeVela will try to pull the image with given name directly.
  5. image: oamdev/testapp:v1
  6. build:
  7. docker:
  8. file: _Dockerfile_path_ # relative path is supported, e.g. "./Dockerfile"
  9. context: _build_context_path_ # relative path is supported, e.g. "."
  10. push:
  11. local: kind # optionally push to local KinD cluster instead of remote registry
  12. type: webservice (default) | worker | task
  13. # detailed configurations of workload
  14. ... properties of the specified workload ...
  15. _trait_1_:
  16. # properties of trait 1
  17. _trait_2_:
  18. # properties of trait 2
  19. ... more traits and their properties ...
  20. _another_service_name_: # more services can be defined
  21. ...

想了解怎样设置特定类型的 workload 或者 trait,请阅读参考文档手册

示例流程

在以下的流程中,我们会构建并部署一个 NodeJs 的示例 app。该 app 的源文件在这里

环境要求

  • Docker 需要在主机上安装 docker
  • KubeVela 需要安装 KubeVela 并配置

1. 下载测试的 app 的源码

git clone 然后进入 testapp 目录:

  1. $ git clone https://github.com/oam-dev/kubevela.git
  2. $ cd kubevela/docs/examples/testapp

这个示例包含 NodeJs app 的源码和用于构建 app 镜像的Dockerfile

2. 使用命令部署 app

我们将会使用目录中的 vela.yaml 文件来构建和部署 app

注意:请修改 oamdev 为你自己注册的账号。或者你可以尝试 本地测试方式

  1. image: oamdev/testapp:v1 # change this to your image

执行如下命令:

  1. $ vela up
  2. Parsing vela.yaml ...
  3. Loading templates ...
  4. Building service (express-server)...
  5. Sending build context to Docker daemon 71.68kB
  6. Step 1/10 : FROM mhart/alpine-node:12
  7. ---> 9d88359808c3
  8. ...
  9. pushing image (oamdev/testapp:v1)...
  10. ...
  11. Rendering configs for service (express-server)...
  12. Writing deploy config to (.vela/deploy.yaml)
  13. Applying deploy configs ...
  14. Checking if app has been deployed...
  15. App has not been deployed, creating a new deployment...
  16. App has been deployed 🚀🚀🚀
  17. Port forward: vela port-forward testapp
  18. SSH: vela exec testapp
  19. Logging: vela logs testapp
  20. App status: vela status testapp
  21. Service status: vela status testapp --svc express-server

检查服务状态:

  1. $ vela status testapp
  2. About:
  3. Name: testapp
  4. Namespace: default
  5. Created at: 2020-11-02 11:08:32.138484 +0800 CST
  6. Updated at: 2020-11-02 11:08:32.138485 +0800 CST
  7. Services:
  8. - Name: express-server
  9. Type: webservice
  10. HEALTHY Ready: 1/1
  11. Last Deployment:
  12. Created at: 2020-11-02 11:08:33 +0800 CST
  13. Updated at: 2020-11-02T11:08:32+08:00
  14. Routes:

本地测试方式

如果你本地有运行的 kind 集群,你可以尝试推送到本地。这种方法无需注册远程容器仓库。

build 中添加 local 的选项值:

  1. build:
  2. # push image into local kind cluster without remote transfer
  3. push:
  4. local: kind
  5. docker:
  6. file: Dockerfile
  7. context: .

然后部署到 kind:

  1. $ vela up

(进阶) 检查渲染后的 manifests 文件

默认情况下,Vela 通过 ./vela/deploy.yaml 渲染最后的 manifests 文件:

  1. apiVersion: core.oam.dev/v1alpha2
  2. kind: ApplicationConfiguration
  3. metadata:
  4. name: testapp
  5. namespace: default
  6. spec:
  7. components:
  8. - componentName: express-server
  9. ---
  10. apiVersion: core.oam.dev/v1alpha2
  11. kind: Component
  12. metadata:
  13. name: express-server
  14. namespace: default
  15. spec:
  16. workload:
  17. apiVersion: apps/v1
  18. kind: Deployment
  19. metadata:
  20. name: express-server
  21. ...
  22. ---
  23. apiVersion: core.oam.dev/v1alpha2
  24. kind: HealthScope
  25. metadata:
  26. name: testapp-default-health
  27. namespace: default
  28. spec:
  29. ...

[可选] 配置其他类型的 workload

至此,我们成功地部署一个默认类型的 workload 的 web 服务。我们也可以添加 Task 类型的服务到同一个 app 中。

  1. services:
  2. pi:
  3. type: task
  4. image: perl
  5. cmd: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
  6. express-server:
  7. ...

然后再次部署 Applfile 来升级应用:

  1. $ vela up

恭喜!你已经学会了使用 Appfile 来部署应用了。

下一步?

更多关于 app 的操作: