Version: v1.0
使用 Sidecar
在本章节中,我们会展示如何使用 sidecar trait 来收集日志。
查看 Sidecar 的使用手册
$ kubectl vela show sidecar# Properties+---------+-----------------------------------------+-----------------------+----------+---------+| NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |+---------+-----------------------------------------+-----------------------+----------+---------+| name | Specify the name of sidecar container | string | true | || cmd | Specify the commands run in the sidecar | []string | false | || image | Specify the image of sidecar container | string | true | || volumes | Specify the shared volume path | [[]volumes](#volumes) | false | |+---------+-----------------------------------------+-----------------------+----------+---------+## volumes+-----------+-------------+--------+----------+---------+| NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |+-----------+-------------+--------+----------+---------+| name | | string | true | || path | | string | true | |+-----------+-------------+--------+----------+---------+
安装应用
应用的组件 log-gen-worker 和 sidecar 共享相同的日志数据目录。sidecar 会重新输出日志到标准输出中。
# app.yamlapiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: vela-app-with-sidecarspec:components:- name: log-gen-workertype: workerproperties:image: busyboxcmd:- /bin/sh- -c- >i=0;while true;doecho "$i: $(date)" >> /var/log/date.log;i=$((i+1));sleep 1;donevolumes:- name: varlogmountPath: /var/logtype: emptyDirtraits:- type: sidecarproperties:name: count-logimage: busyboxcmd: [ /bin/sh, -c, 'tail -n+1 -f /var/log/date.log']volumes:- name: varlogpath: /var/log
安装这个应用
kubectl apply -f app.yaml
检查应用生成的 workload
$ kubectl get podNAME READY STATUS RESTARTS AGElog-gen-worker-76945f458b-k7n9k 2/2 Running 0 90s
查看 sidecar 的输出
$ kubectl logs -f log-gen-worker-76945f458b-k7n9k count-log0: Fri Apr 16 11:08:45 UTC 20211: Fri Apr 16 11:08:46 UTC 20212: Fri Apr 16 11:08:47 UTC 20213: Fri Apr 16 11:08:48 UTC 20214: Fri Apr 16 11:08:49 UTC 20215: Fri Apr 16 11:08:50 UTC 20216: Fri Apr 16 11:08:51 UTC 20217: Fri Apr 16 11:08:52 UTC 20218: Fri Apr 16 11:08:53 UTC 20219: Fri Apr 16 11:08:54 UTC 2021