Version: v1.1

容器注入

本小节会介绍,如何为应用部署计划的一个组件,添加 sidecar 运维特征来收集日志。

字段说明

  1. $ vela show sidecar
  2. # Properties
  3. +---------+-----------------------------------------+-----------------------+----------+---------+
  4. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  5. +---------+-----------------------------------------+-----------------------+----------+---------+
  6. | name | Specify the name of sidecar container | string | true | |
  7. | cmd | Specify the commands run in the sidecar | []string | false | |
  8. | image | Specify the image of sidecar container | string | true | |
  9. | volumes | Specify the shared volume path | [[]volumes](#volumes) | false | |
  10. +---------+-----------------------------------------+-----------------------+----------+---------+
  11. ## volumes
  12. +-----------+-------------+--------+----------+---------+
  13. | NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
  14. +-----------+-------------+--------+----------+---------+
  15. | name | | string | true | |
  16. | path | | string | true | |
  17. +-----------+-------------+--------+----------+---------+

如何使用

我们来编写一个应用部署计划里的组件 log-gen-worker。 同时,我们将 sidecar 所记录的日志数据目录和组件,指向同一个数据源 varlog

  1. # app.yaml
  2. apiVersion: core.oam.dev/v1beta1
  3. kind: Application
  4. metadata:
  5. name: vela-app-with-sidecar
  6. spec:
  7. components:
  8. - name: log-gen-worker
  9. type: worker
  10. properties:
  11. image: busybox
  12. cmd:
  13. - /bin/sh
  14. - -c
  15. - >
  16. i=0;
  17. while true;
  18. do
  19. echo "$i: $(date)" >> /var/log/date.log;
  20. i=$((i+1));
  21. sleep 1;
  22. done
  23. volumes:
  24. - name: varlog
  25. mountPath: /var/log
  26. type: emptyDir
  27. traits:
  28. - type: sidecar
  29. properties:
  30. name: count-log
  31. image: busybox
  32. cmd: [ /bin/sh, -c, 'tail -n+1 -f /var/log/date.log']
  33. volumes:
  34. - name: varlog
  35. path: /var/log

编写完毕,在 YAML 文件所在路径下,部署这个应用:

  1. kubectl apply -f app.yaml

成功后,先检查应用生成的工作负载情况:

  1. $ kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. log-gen-worker-76945f458b-k7n9k 2/2 Running 0 90s

然后,查看 sidecar 的输出,日志显示正常。

  1. $ kubectl logs -f log-gen-worker-76945f458b-k7n9k count-log
  2. 0: Fri Apr 16 11:08:45 UTC 2021
  3. 1: Fri Apr 16 11:08:46 UTC 2021
  4. 2: Fri Apr 16 11:08:47 UTC 2021
  5. 3: Fri Apr 16 11:08:48 UTC 2021
  6. 4: Fri Apr 16 11:08:49 UTC 2021
  7. 5: Fri Apr 16 11:08:50 UTC 2021
  8. 6: Fri Apr 16 11:08:51 UTC 2021
  9. 7: Fri Apr 16 11:08:52 UTC 2021
  10. 8: Fri Apr 16 11:08:53 UTC 2021
  11. 9: Fri Apr 16 11:08:54 UTC 2021