|
| 1 | +# Customizing CloudWatch Log Group or Stream with Kubernetes Metadata |
| 2 | + |
| 3 | +### Option 1: Use the high performance cloudwatch_logs plugin |
| 4 | + |
| 5 | +To use the [recommended AWS CloudWatch Plugin](https://docs.fluentbit.io/manual/pipeline/outputs/cloudwatch/), please see the main [Customize the tag based on Kubernetes metadata](k8s-metadata-customize-tag/) example, which demonstrates the technique with the cloudwatch_logs plugin. |
| 6 | + |
| 7 | +### Option 2: Use templating in the older Golang cloudwatch plugin |
| 8 | + |
| 9 | +The [lower performance CloudWatch plugin](https://github.com/aws/amazon-cloudwatch-logs-for-fluent-bit#new-higher-performance-core-fluent-bit-plugin) has a [templating feature](https://github.com/aws/amazon-cloudwatch-logs-for-fluent-bit#templating-log-group-and-stream-names) which can be used to customize the log group and stream names. |
| 10 | + |
| 11 | +First, you must enable the [kubernetes filter](https://docs.fluentbit.io/manual/pipeline/filters/kubernetes), which can add metadata like this: |
| 12 | +``` |
| 13 | +kubernetes: { |
| 14 | + annotations: { |
| 15 | + "kubernetes.io/psp": "eks.privileged" |
| 16 | + }, |
| 17 | + container_hash: "<some hash>", |
| 18 | + container_name: "myapp", |
| 19 | + docker_id: "<some id>", |
| 20 | + host: "ip-10-1-128-166.us-east-2.compute.internal", |
| 21 | + labels: { |
| 22 | + app: "myapp", |
| 23 | + "pod-template-hash": "<some hash>" |
| 24 | + }, |
| 25 | + namespace_name: "default", |
| 26 | + pod_id: "198f7dd2-2270-11ea-be47-0a5d932f5920", |
| 27 | + pod_name: "myapp-5468c5d4d7-n2swr" |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +The kubernetes metadata can be referenced just like any other keys using the templating feature, for example, the following will result in a log group name which is /eks/{namespace_name}/{pod_name}. |
| 32 | + |
| 33 | +``` |
| 34 | + [OUTPUT] |
| 35 | + Name cloudwatch |
| 36 | + Match application.* |
| 37 | + region us-east-1 |
| 38 | + log_group_name /eks/$(kubernetes['namespace_name'])/$(kubernetes['pod_name']) |
| 39 | + log_stream_name $(kubernetes['host'])/$(kubernetes['namespace_name'])/$(kubernetes['pod_name'])/$(kubernetes['container_name']) |
| 40 | + auto_create_group on |
| 41 | +``` |
| 42 | + |
| 43 | +If you want to deploy this example yourself, included is an altered version of the [Amazon CloudWatch Container Insights Daemonset](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Container-Insights-setup-logs-FluentBit.html) that uses this technique the customize the log stream and group names. Follow the steps 1 & 2 to create a namespace and config map. Then, instead of step 3, apply the file in this example with: |
| 44 | + |
| 45 | +``` |
| 46 | +kubectl apply -f fluent-bit.yaml |
| 47 | +``` |
0 commit comments