垃圾回收
默认情况下,KubeVela 应用会在新的版本发布并健康运行后回收掉过时的资源。但是在一些场景下,你可能希望对于过时资源的回收有其他的控制方式,这种情况下你可以使用垃圾回收策略来配置相应的行为。
在垃圾回收策略中,目前主要有两种能力你可以使用。
保留过时资源
如果你想要保留应用在先前版本中创建出来的资源,你可以配置垃圾回收策略中的keepLegacyResource选项来实现。比如如下应用:
# app.yamlapiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: first-vela-appspec:components:- name: express-servertype: webserviceproperties:image: oamdev/hello-worldport: 8000traits:- type: ingress-1-20properties:domain: testsvc.example.comhttp:"/": 8000policies:- name: keep-legacy-resourcetype: garbage-collectproperties:keepLegacyResource: true
- 首先创建这个应用
vela up -f app.yaml
$ vela lsAPP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIMEfirst-vela-app express-server webservice ingress-1-20 running healthy Ready:1/1 2022-04-06 16:20:25 +0800 CST
- 然后更新它
# app1.yamlapiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: first-vela-appspec:components:- name: express-server-1type: webserviceproperties:image: oamdev/hello-worldport: 8000traits:- type: ingress-1-20properties:domain: testsvc.example.comhttp:"/": 8000policies:- name: keep-legacy-resourcetype: garbage-collectproperties:keepLegacyResource: true
vela up -f app1.yaml
$ vela lsAPP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIMEfirst-vela-app express-server-1 webservice ingress-1-20 running healthy Ready:1/1 2022-04-06 16:20:25 +0800 CST
可以发现旧版本的应用资源和新版本的应用资源同时存在于集群中。
在下述步骤中,我们将使用
kubectl命令来做一些验证。你也可以使用vela status first-vela-app来检查应用及其组件的健康状态。
$ kubectl get deployNAME READY UP-TO-DATE AVAILABLE AGEexpress-server 1/1 1 1 10mexpress-server-1 1/1 1 1 40s
$ kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEexpress-server ClusterIP 10.96.102.249 <none> 8000/TCP 10mexpress-server-1 ClusterIP 10.96.146.10 <none> 8000/TCP 46s
$ kubectl get ingressNAME CLASS HOSTS ADDRESS PORTS AGEexpress-server <none> testsvc.example.com 80 10mexpress-server-1 <none> testsvc.example.com 80 50s
$ kubectl get resourcetrackerNAME AGEfirst-vela-app-default 12mfirst-vela-app-v1-default 12mfirst-vela-app-v2-default 2m56s
在删除该应用时所有的版本资源会被一并回收。
$ vela delete first-vela-app
如果你希望删除某个应用版本下的过时资源,你可以运行
kubectl delete resourcetracker first-vela-app-v1-default。
持久化资源
除了在整个应用维度上保留历史版本资源外,你还可以通过配置部分资源的持久化策略来跳过常规的垃圾回收过程。
在如下所示的样例中,添加了一条规则(rule)。该规则指定所有由expose这一运维特征创建出来的资源使用onAppDelete策略,即这些资源只有在应用删除时才会被回收。
$ cat <<EOF | vela up -f -apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: garbage-collect-appspec:components:- name: hello-worldtype: webserviceproperties:image: oamdev/hello-worldtraits:- type: exposeproperties:port: [8000]policies:- name: garbage-collecttype: garbage-collectproperties:rules:- selector:traitTypes:- exposestrategy: onAppDeleteEOF
$ kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGEhello-world 1/1 1 1 74s$ kubectl get serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEhello-world ClusterIP 10.96.160.208 <none> 8000/TCP 78s
如果你升级该应用并使用一个新的组件,你会发现旧版本应用中通过 webservice 组件创建的 Deployment 被删除掉了,但是通过 expose 这一运维特征创建的 Service 资源仍然保留下来。
$ cat <<EOF | vela up -f -apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: garbage-collect-appspec:components:- name: hello-world-newtype: webserviceproperties:image: oamdev/hello-worldtraits:- type: exposeproperties:port: [8000]policies:- name: garbage-collecttype: garbage-collectproperties:rules:- selector:traitTypes:- exposestrategy: onAppDeleteEOF$ kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGEhello-world-new 1/1 1 1 10s$ kubectl get serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEhello-world ClusterIP 10.96.160.208 <none> 8000/TCP 5m56shello-world-new ClusterIP 10.96.20.4 <none> 8000/TCP 13s
如果你想要部署类 Job 组件,希望部署的资源不会被应用回收, 那么你可以使用组件组件类型选择器,同时将回收策略设置为 never。
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: garbage-collect-appspec:components:- name: hello-world-newtype: job-like-componentpolicies:- name: garbage-collecttype: garbage-collectproperties:rules:- selector:componentTypes:- webservicestrategy: never
另一种对于组件资源的选择方式是使用组件名称选择器。
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: create-ns-appspec:components:- name: example-addon-namespacetype: k8s-objectsproperties:objects:- apiVersion: v1kind: Namespacepolicies:- name: garbage-collecttype: garbage-collectproperties:rules:- selector:componentNames:- example-addon-namespacestrategy: never