垃圾回收
在垃圾回收策略中,目前主要有两种能力你可以使用。
如果你想要保留应用在先前版本中创建出来的资源,你可以配置垃圾回收策略中的选项来实现。比如如下应用:
- 首先创建这个应用
vela up -f app.yaml
$ vela ls
APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
first-vela-app express-server webservice ingress-1-20 running healthy Ready:1/1 2022-04-06 16:20:25 +0800 CST
- 然后更新它
# app1.yaml
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: first-vela-app
spec:
components:
- name: express-server-1
type: webservice
properties:
image: oamdev/hello-world
port: 8000
traits:
- type: ingress-1-20
properties:
domain: testsvc.example.com
http:
"/": 8000
policies:
- name: keep-legacy-resource
type: garbage-collect
properties:
keepLegacyResource: true
vela up -f app1.yaml
可以发现旧版本的应用资源和新版本的应用资源同时存在于集群中。
$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
express-server 1/1 1 1 10m
express-server-1 1/1 1 1 40s
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
express-server ClusterIP 10.96.102.249 <none> 8000/TCP 10m
express-server-1 ClusterIP 10.96.146.10 <none> 8000/TCP 46s
$ kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
express-server-1 <none> testsvc.example.com 80 50s
$ kubectl get resourcetracker
first-vela-app-default 12m
first-vela-app-v1-default 12m
first-vela-app-v2-default 2m56s
在删除该应用时所有的版本资源会被一并回收。
如果你希望删除某个应用版本下的过时资源,你可以运行
kubectl delete resourcetracker first-vela-app-v1-default
。
持久化资源
除了在整个应用维度上保留历史版本资源外,你还可以通过配置部分资源的持久化策略来跳过常规的垃圾回收过程。
$ cat <<EOF | vela up -f -
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: garbage-collect-app
spec:
components:
- name: hello-world
type: webservice
properties:
image: oamdev/hello-world
traits:
- type: expose
properties:
port: [8000]
policies:
- name: garbage-collect
type: garbage-collect
properties:
rules:
- selector:
traitTypes:
- expose
strategy: onAppDelete
EOF
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
hello-world 1/1 1 1 74s
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world ClusterIP 10.96.160.208 <none> 8000/TCP 78s
如果你升级该应用并使用一个新的组件,你会发现旧版本应用中通过 webservice 组件创建的 Deployment 被删除掉了,但是通过 expose 这一运维特征创建的 Service 资源仍然保留下来。
$ cat <<EOF | vela up -f -
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: garbage-collect-app
- name: hello-world-new
type: webservice
properties:
image: oamdev/hello-world
traits:
- type: expose
properties:
port: [8000]
policies:
- name: garbage-collect
type: garbage-collect
properties:
rules:
- selector:
traitTypes:
- expose
strategy: onAppDelete
EOF
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
hello-world-new 1/1 1 1 10s
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world ClusterIP 10.96.160.208 <none> 8000/TCP 5m56s
hello-world-new ClusterIP 10.96.20.4 <none> 8000/TCP 13s
如果你想要部署类 Job 组件,希望部署的资源不会被应用回收, 那么你可以使用组件组件类型选择器,同时将回收策略设置为 never
。
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: garbage-collect-app
spec:
components:
- name: hello-world-new
type: job-like-component
policies:
- name: garbage-collect
type: garbage-collect
properties:
rules:
- selector:
componentTypes:
另一种对于组件资源的选择方式是使用组件名称选择器。