Logs Collected in Sidecar Method

Although it is not recommended to use the sidecar method to collect container logs by default, in some limited scenarios, only the sidecar method can be used to collect container logs. Here we give an example for reference.

General Idea

As shown below: Sidecar Mode - 图1

Loggie and the business container are deployed in the same Pod, and the same log file volume needs to be mounted. In addition, the configuration of Loggie can be mounted to the container through configMap. Loggie collects the logs of the container according to the provided configuration file in configMap, and send them to the backend.

Injecte Loggie Sidecar

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: tomcat
  6. name: tomcat
  7. namespace: default
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: tomcat
  13. template:
  14. metadata:
  15. labels:
  16. app: tomcat
  17. spec:
  18. containers:
  19. - name: tomcat
  20. image: tomcat
  21. volumeMounts:
  22. - mountPath: /usr/local/tomcat/logs
  23. name: log
  24. - name: loggie
  25. args:
  26. - -config.system=/opt/loggie/loggie.yml
  27. - -config.pipeline=/opt/loggie/pipeline.yml
  28. image: loggieio/loggie:main
  29. volumeMounts:
  30. # same log volume
  31. - mountPath: /usr/local/tomcat/logs
  32. name: log
  33. # loggie configuration configMap
  34. - mountPath: /opt/loggie
  35. name: loggie-config
  36. # loggie data
  37. - mountPath: /data
  38. name: registry
  39. volumes:
  40. - emptyDir: {}
  41. name: log
  42. - emptyDir: {}
  43. name: registry
  44. - name: loggie-config
  45. configMap:
  46. name: tomcat-loggie-config
  47. ---
  48. apiVersion: v1
  49. kind: ConfigMap
  50. metadata:
  51. name: tomcat-loggie-config
  52. namespace: default
  53. data:
  54. loggie.yml: |
  55. loggie:
  56. reload:
  57. enabled: true
  58. period: 10s
  59. monitor:
  60. logger:
  61. period: 30s
  62. enabled: true
  63. listeners:
  64. filesource: ~
  65. filewatcher: ~
  66. reload: ~
  67. sink: ~
  68. http:
  69. enabled: true
  70. port: 9196
  71. pipeline.yml: |
  72. pipelines:
  73. - name: "tomcat"
  74. sources:
  75. - type: "file"
  76. name: "tomcatlog"
  77. paths:
  78. - "/usr/local/tomcat/logs/*.log"
  79. fields:
  80. namespace: default
  81. deployment: tomcat
  82. fieldsFromEnv:
  83. podname: HOSTNAME
  84. sink:
  85. type: "dev"
  86. printEvents: true
  87. codec:
  88. pretty: true

You can use the loggie sidecar to collect container logs by referring to the above exapmle. Node:

  • At present, it is not recommended to enable kubernetes discovery in the configuration file. Since Kubernetes will be requested after opening, when there are many Pods, it will cause certain pressure on Kubernetes. Therefore, LogConfig CRD cannot be used, and configMap needs to be used to mount the configuration file.
  • Since Kubernetes discovery is not used, the fields here will not be automatically added with the Pod’s meta information, and need to be obtained from the Pod’s environment variables using the fieldsFromEnv.

Tips

  • Fixed information such as namespace can be configured in fields, or referenced to env by using the downward API.
  • The Env environment variable obtained by fieldsFromEnv is not limited to the env field configured in the Pod yaml, but any environment variable in the Loggie container. We can execute the env command in the container to view it.

  • Modifying the parameters in the configMap will take a period of time to be refreshed to the Pod. If you want it to take effect immediately, you need to rebuild the Pod. Please pay attention to whether it will affect the business.

Info

In the future, Loggie will support automatic Sidecar injection and automatic generation of ConfigMap mounts through LogConfig, so as to achieve the same experience as using DaemonSet.