垃圾回收

    在垃圾回收策略中,目前主要有两种能力你可以使用。

    如果你想要保留应用在先前版本中创建出来的资源,你可以配置垃圾回收策略中的选项来实现。比如如下应用:

    1. 首先创建这个应用
    1. vela up -f app.yaml
    1. $ vela ls
    2. APP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIME
    3. first-vela-app express-server webservice ingress-1-20 running healthy Ready:1/1 2022-04-06 16:20:25 +0800 CST
    1. 然后更新它
    1. # app1.yaml
    2. apiVersion: core.oam.dev/v1beta1
    3. kind: Application
    4. metadata:
    5. name: first-vela-app
    6. spec:
    7. components:
    8. - name: express-server-1
    9. type: webservice
    10. properties:
    11. image: oamdev/hello-world
    12. port: 8000
    13. traits:
    14. - type: ingress-1-20
    15. properties:
    16. domain: testsvc.example.com
    17. http:
    18. "/": 8000
    19. policies:
    20. - name: keep-legacy-resource
    21. type: garbage-collect
    22. properties:
    23. keepLegacyResource: true
    1. vela up -f app1.yaml

    可以发现旧版本的应用资源和新版本的应用资源同时存在于集群中。

    1. $ kubectl get deploy
    2. NAME READY UP-TO-DATE AVAILABLE AGE
    3. express-server 1/1 1 1 10m
    4. express-server-1 1/1 1 1 40s
    1. $ kubectl get svc
    2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    3. express-server ClusterIP 10.96.102.249 <none> 8000/TCP 10m
    4. express-server-1 ClusterIP 10.96.146.10 <none> 8000/TCP 46s
    1. $ kubectl get ingress
    2. NAME CLASS HOSTS ADDRESS PORTS AGE
    3. express-server-1 <none> testsvc.example.com 80 50s
    1. $ kubectl get resourcetracker
    2. first-vela-app-default 12m
    3. first-vela-app-v1-default 12m
    4. first-vela-app-v2-default 2m56s

    在删除该应用时所有的版本资源会被一并回收。

    如果你希望删除某个应用版本下的过时资源,你可以运行kubectl delete resourcetracker first-vela-app-v1-default

    持久化资源

    除了在整个应用维度上保留历史版本资源外,你还可以通过配置部分资源的持久化策略来跳过常规的垃圾回收过程。

    1. $ cat <<EOF | vela up -f -
    2. apiVersion: core.oam.dev/v1beta1
    3. kind: Application
    4. metadata:
    5. name: garbage-collect-app
    6. spec:
    7. components:
    8. - name: hello-world
    9. type: webservice
    10. properties:
    11. image: oamdev/hello-world
    12. traits:
    13. - type: expose
    14. properties:
    15. port: [8000]
    16. policies:
    17. - name: garbage-collect
    18. type: garbage-collect
    19. properties:
    20. rules:
    21. - selector:
    22. traitTypes:
    23. - expose
    24. strategy: onAppDelete
    25. EOF
    1. $ kubectl get deployment
    2. NAME READY UP-TO-DATE AVAILABLE AGE
    3. hello-world 1/1 1 1 74s
    4. $ kubectl get service
    5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    6. hello-world ClusterIP 10.96.160.208 <none> 8000/TCP 78s

    如果你升级该应用并使用一个新的组件,你会发现旧版本应用中通过 webservice 组件创建的 Deployment 被删除掉了,但是通过 expose 这一运维特征创建的 Service 资源仍然保留下来。

    1. $ cat <<EOF | vela up -f -
    2. apiVersion: core.oam.dev/v1beta1
    3. kind: Application
    4. metadata:
    5. name: garbage-collect-app
    6. - name: hello-world-new
    7. type: webservice
    8. properties:
    9. image: oamdev/hello-world
    10. traits:
    11. - type: expose
    12. properties:
    13. port: [8000]
    14. policies:
    15. - name: garbage-collect
    16. type: garbage-collect
    17. properties:
    18. rules:
    19. - selector:
    20. traitTypes:
    21. - expose
    22. strategy: onAppDelete
    23. EOF
    24. $ kubectl get deployment
    25. NAME READY UP-TO-DATE AVAILABLE AGE
    26. hello-world-new 1/1 1 1 10s
    27. $ kubectl get service
    28. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    29. hello-world ClusterIP 10.96.160.208 <none> 8000/TCP 5m56s
    30. hello-world-new ClusterIP 10.96.20.4 <none> 8000/TCP 13s

    如果你想要部署类 Job 组件,希望部署的资源不会被应用回收, 那么你可以使用组件组件类型选择器,同时将回收策略设置为 never

    1. apiVersion: core.oam.dev/v1beta1
    2. kind: Application
    3. metadata:
    4. name: garbage-collect-app
    5. spec:
    6. components:
    7. - name: hello-world-new
    8. type: job-like-component
    9. policies:
    10. - name: garbage-collect
    11. type: garbage-collect
    12. properties:
    13. rules:
    14. - selector:
    15. componentTypes:

    另一种对于组件资源的选择方式是使用组件名称选择器。