misterli's Blog.

kubevela之appfile详解(上)

字数统计: 1.6k阅读时长: 8 min
2020/12/08

上一篇我们使用vela部署了一个简单的服务,下面我们部署一个完整的服务并学习一下appfile该如何去写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
vela.yaml
name: testapp

services:
test-web-service:
# this image will be used in both build and deploy steps
image: christianhxc/gorandom:1.0

type: webservice

port: 8080
cpu: "0.1"
env:
- name: FOO
value: bar
scaler:
replicas: 1

route:
domain: kubevela.lishuai.fun
rules:
- path: /

metrics:
enabled: true
format: prometheus
path: /metrics
port: 0
scheme: http

autoscale:
min: 1
max: 4
cron:
startAt: "15:40"
duration: "3m"
days: "Monday, Thursday"
replicas: 2
timezone: "Asia/Shanghai"


test-worker:
type: worker
image: oamdev/testapp:v1
cmd: ["node", "server.js"]

test-task:
type: task
image: perl
count: 10
restart: Never
cmd: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]

部署上面的vela.yaml之后vela根据文件的内容为我们创建了相关的pod,service,job,ingress。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
[root@master-01 testapp]# vela up 
Parsing vela.yaml ...
Loading templates ...

Rendering configs for service (test-task)...

Rendering configs for service (test-web-service)...

Rendering configs for service (test-worker)...
Writing deploy config to (.vela/deploy.yaml)

Applying deploy configs ...
Checking if app has been deployed...
App exists, updating existing deployment...
✅ App has been deployed 🚀🚀🚀
Port forward: vela port-forward testapp
SSH: vela exec testapp
Logging: vela logs testapp
App status: vela status testapp
Service status: vela status testapp --svc test-task
Service status: vela status testapp --svc test-web-service
Service status: vela status testapp --svc test-worker
[root@master-01 testapp]# kubectl get pod,svc,job,ingress
NAME READY STATUS RESTARTS AGE
pod/check-ecs-price-7cdc97b997-j9w9q 1/1 Running 0 11d
pod/test-task-59hqm 1/1 Running 0 17s
pod/test-task-bp8f4 0/1 ContainerCreating 0 17s
pod/test-task-fh7zf 0/1 ContainerCreating 0 17s
pod/test-task-gv44j 0/1 ContainerCreating 0 17s
pod/test-task-lqrds 0/1 ContainerCreating 0 17s
pod/test-task-mn8z9 0/1 ContainerCreating 0 17s
pod/test-task-swzg5 0/1 ContainerCreating 0 17s
pod/test-task-xg2nq 0/1 ContainerCreating 0 17s
pod/test-task-xz5h2 0/1 ContainerCreating 0 17s
pod/test-task-zfjm8 0/1 ContainerCreating 0 17s
pod/test-web-service-5dff6fd5d-gw7bd 1/1 Running 0 17s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13d
service/oam-metricapp ClusterIP 10.104.9.200 <none> 4848/TCP 16h
service/oam-test-web-service ClusterIP 10.104.240.83 <none> 4848/TCP 4m56s
service/test-web-service-route-5897fbd549 ClusterIP 10.103.62.97 <none> 8080/TCP 16s
service/traefik-forward-auth ClusterIP 10.96.227.183 <none> 4181/TCP 13d

NAME COMPLETIONS DURATION AGE
job.batch/test-task 0/10 17s 17s

NAME CLASS HOSTS ADDRESS PORTS AGE
ingress.extensions/test-web-service-route-5897fbd549-0 <none> kubevela.lishuai.fun 80 16s
[root@master-01 testapp]# curl kubevela.lishuai.fun/metrics
# HELP go_build_info Build information about the main Go module.
# TYPE go_build_info gauge
go_build_info{checksum="unknown",path="unknown",version="unknown"} 1
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 2.8372e-05
go_gc_duration_seconds{quantile="0.25"} 2.8372e-05
go_gc_duration_seconds{quantile="0.5"} 6.7686e-05
go_gc_duration_seconds{quantile="0.75"} 7.9579e-05
go_gc_duration_seconds{quantile="1"} 7.9579e-05
go_gc_duration_seconds_sum 0.000175637

接下来我们看一下appfile文件的结构,Appfile所有可用部分的列表如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
name: _app-name_   #定义这个app的名称

services:
_service-name_: #_service
# 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.
image: oamdev/testapp:v1

build:
docker:
file: _Dockerfile_path_ # relative path is supported, e.g. "./Dockerfile"
context: _build_context_path_ # relative path is supported, e.g. "."

push:
local: kind # optionally push to local KinD cluster instead of remote registry

type: webservice (default) | worker | task

# detailed configurations of workload
... properties of the specified workload ...

_trait_1_:
# properties of trait 1

_trait_2_:
# properties of trait 2

... more traits and their properties ...

_another_service_name_: # more services can be defined
...

appfile文件主要分为两个部分

描述
name 定义了我们要定义的app的名称
services 定义了我们要创建的application的具体,services段下可以定义多个服务。

Workload type

services可以配置多个服务,每个服务根据workload type不同会被创建为不同的k8s中的资源对象,vela暂时具有以下三种workload type

web service

Web Service 是一种工作负载类型,用于描述长期运行的,可伸缩的,容器化的服务,这些服务具有稳定的网络终结点,可以接收来自客户的外部网络流量。

如果针对Appfile中定义的任何服务跳过了工作负载类型,则默认情况下将为Web Servicetype。

Web Service工作负载类型的所有配置选项的列表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
name: my-app-name

services:
my-service-name:
type: webservice # could be skipped
image: oamdev/testapp:v1
cmd: ["node", "server.js"]
port: 8080
cpu: "0.1"
env:
- name: FOO
value: bar
- name: FOO
valueFrom:
secretKeyRef:
name: bar
key: bar

参数

名称 类型 描述 笔记
Cmd []string [可选的]
Env [] WebserviceEnv (参考下面的表格) [可选的]
image string 服务使用哪个镜像
port int32 客户流量发送到哪个端口 [默认为80]
Cpu string 服务的使用CPU数,例如0.5(0.5 CPU内核),1(1 CPU内核)。会将requests和limits同时设置为该值 [可选的]

WebserviceEnv

名称 类型 描述 笔记
nama string
value string [可选的]
ValueFrom WebserviceValueFrom (参考下面的表格) [可选的]

WebserviceValueFrom

名称 类型 描述 笔记
SecretKeyRef WebserviceValueFromSecretKeyRef (参考下面的表格)

WebserviceValueFromSecretKeyRef

名称 类型 描述 笔记
name string
key string

task

Task 是一种工作负载类型,用于描述运行代码或脚本以完成工作。实际上创建的资源对象上kubernetes中的job.

Task工作负载类型的所有配置选项的列表。

1
2
3
4
5
6
7
8
9
name: my-app-name

services:
my-service-name:
type: task
image: perl
count: 10
restart: Never
cmd: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]

参数

名称 类型 描述 笔记
Cmd []string [可选的]
count int32 指定要并行运行的任务数 [默认为1]
image string 您想为服务使用哪个镜像
restart string 服务的重启策略 默认为never

示例:

1
2
3
4
5
6
7
8
name: testapp

services:
my-service-name:
type: task
image: perl
count: 10
cmd: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]

效果如下:

启动了十个job。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@master-01 testapp]# kubectl get pod 
NAME READY STATUS RESTARTS AGE
check-ecs-price-7cdc97b997-j9w9q 1/1 Running 0 11d
my-service-name-7zpl6 0/1 ContainerCreating 0 4s
my-service-name-9f54d 0/1 ContainerCreating 0 4s
my-service-name-bkmdh 0/1 ContainerCreating 0 4s
my-service-name-frj2p 0/1 ContainerCreating 0 4s
my-service-name-l4cc7 0/1 ContainerCreating 0 4s
my-service-name-l4rrz 0/1 ContainerCreating 0 4s
my-service-name-mbx4r 0/1 ContainerCreating 0 4s
my-service-name-n65xr 0/1 ContainerCreating 0 4s
my-service-name-qkrbk 0/1 ContainerCreating 0 4s
my-service-name-qq4c5 0/1 ContainerCreating 0 4s
[root@master-01 testapp]# kubectl get job
NAME COMPLETIONS DURATION AGE
my-service-name 0/10 12s 12s

worker

Worker是一种工作负载类型,用于描述在后端运行的长期运行的,可扩展的容器化服务。他们没有网络端点来接收外部网络流量。

Worker工作负载类型的所有配置选项的列表。

1
2
3
4
5
6
7
name: my-app-name

services:
my-service-name:
type: worker
image: oamdev/testapp:v1
cmd: ["node", "server.js"]

参数

名称 类型 描述 笔记
Cmd []string [可选的]
image string 指定应用的镜像
CATALOG
  1. 1. Workload type
    1. 1.1. web service
      1. 1.1.1. 参数
      2. 1.1.2. WebserviceEnv
      3. 1.1.3. WebserviceValueFrom
      4. 1.1.4. WebserviceValueFromSecretKeyRef
    2. 1.2. task
      1. 1.2.1. 参数
    3. 1.3. worker
      1. 1.3.1. 参数