Skip to content

Commit e38f7d7

Browse files
author
Simran Bajaj
committed
Added Fargate usage with Firelens example
1 parent 11369ac commit e38f7d7

File tree

5 files changed

+127
-7
lines changed

5 files changed

+127
-7
lines changed

examples/README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Run the example:
2929
./examples/agent/bin/run.sh
3030
```
3131

32-
## FireLens on ECS
32+
## FireLens on ECS EC2
3333

3434
You can deploy the example by running the following:
3535

@@ -45,7 +45,7 @@ aws s3api create-bucket --bucket <bucket-name> --region <region>
4545
# create ECS service
4646

4747
# deploy
48-
./examples/ecs-firelens/publish.sh \
48+
./examples/ecs-firelens/bin/publish.sh \
4949
<account-id> \
5050
<region> \
5151
<image-name> \
@@ -54,3 +54,48 @@ aws s3api create-bucket --bucket <bucket-name> --region <region>
5454
<ecs-task-family> \
5555
<ecs-service-name>
5656
```
57+
58+
## FireLens on ECS Fargate
59+
60+
For running on Fargate, s3 file option is not supported for Fluent-bit config. Hence, we need to build the fluent-bit custom config image and then use its reference in our Firelens container definition.
61+
62+
For building the custom fluent-bit image, clone the [amazon-ecs-firelens-examples](https://github.com/aws-samples/amazon-ecs-firelens-examples) and modify the contents of [extra.conf](https://github.com/aws-samples/amazon-ecs-firelens-examples/blob/mainline/examples/fluent-bit/config-file-type-file/extra.conf) in the amazon-ecs-firelens-examples repository. Post this run the following commands to build the custom fluent-bit image:-
63+
64+
```sh
65+
# create an ECR repository for the Fluentbit-config image
66+
aws ecr create-repository --repository-name <config-image-name> --region <region>
67+
68+
# Navigate to the config file directory
69+
cd examples/fluent-bit/config-file-type-file
70+
71+
# Build the docker image from Dockerfile. Replace config-image-name with your image name
72+
docker build -t <config-image-name> .
73+
74+
# Tag the recently built docker image . Replace the config-image-name, account-id and region with your values.
75+
docker tag <config-image-name>:latest <account-id>.dkr.ecr.<region>.amazonaws.com/<config-image-name>:latest
76+
77+
# Push the docker image to ECR
78+
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/<config-image-name>:latest
79+
```
80+
81+
For executing EMF application on Fargate, you need to execute the following commands :-
82+
83+
```sh
84+
# create an ECR repository for the example image
85+
aws ecr create-repository --repository-name <image-name> --region <region>
86+
87+
# create ECS cluster
88+
# create ECS task definition
89+
# create ECS service
90+
91+
# deploy
92+
./examples/ecs-firelens/bin/publish-fargate.sh
93+
<account-id> \
94+
<region> \
95+
<example-image> \
96+
<fargate-config-image> \
97+
<ecs-cluster> \
98+
<ecs-task> \
99+
<ecs-service>
100+
101+
```

examples/ecs-firelens/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM openjdk:8-jdk-slim
1+
FROM openjdk:11
22
RUN mkdir -p /app
33

44
# copy the source files over
55
COPY build/libs/*.jar /app/app.jar
66

77
ENV JAVA_OPTS=""
8-
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app/app.jar" ]
8+
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app/app.jar" ]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[
2+
{
3+
"name": "example",
4+
"image": "<account-id>.dkr.ecr.<region>.amazonaws.com/<image-name>:latest",
5+
"environment": [{
6+
"name": "AWS_EMF_AGENT_ENDPOINT",
7+
"value": "tcp://127.0.0.1:25888"
8+
}
9+
],
10+
"essential": true,
11+
"logConfiguration": {
12+
"logDriver": "awsfirelens",
13+
"options": {
14+
"Name": "cloudwatch",
15+
"region": "<region>",
16+
"log_key": "log",
17+
"log_group_name": "aws-emf-ecs-firelens-example-metrics",
18+
"auto_create_group": "true",
19+
"log_stream_prefix": "emf-",
20+
"retry_limit": "2",
21+
"log_format": "json/emf"
22+
}
23+
}
24+
},
25+
{
26+
"name": "fluent-bit",
27+
"image": "<account-id>.dkr.ecr.<region>.amazonaws.com/<Firelens-custom-conf-image>:latest",
28+
"essential": true,
29+
"firelensConfiguration": {
30+
"type": "fluentbit",
31+
"options": {
32+
"config-file-type": "file",
33+
"config-file-value": "/extra.conf",
34+
"enable-ecs-log-metadata": "false"
35+
}
36+
},
37+
"logConfiguration": {
38+
"logDriver": "awslogs",
39+
"options": {
40+
"awslogs-group": "firelens-container",
41+
"awslogs-region": "<region>",
42+
"awslogs-create-group": "true",
43+
"awslogs-stream-prefix": "firelens"
44+
}
45+
}
46+
}
47+
]

examples/ecs-firelens/extra.conf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# TCP input used for EMF payloads
2+
[INPUT]
3+
Name tcp
4+
Listen 0.0.0.0
5+
Port 25888
6+
Chunk_Size 32
7+
Buffer_Size 64
8+
Format none
9+
Tag emf-${HOSTNAME}
10+
# This tag is used by the output plugin to determine the LogStream
11+
# including the HOSTNAME is a way to increase the number of LogStreams.
12+
# The maximum throughput on a
13+
# single LogStream is 5 MB/s (max 1 MB at max 5 TPS).
14+
# In AWSVPC mode, the HOSTNAME is the ENI private IP
15+
# in bridge mode, the HOSTNAME is the Docker container ID
16+
17+
# Output for EMF over TCP -> CloudWatch
18+
[OUTPUT]
19+
Name cloudwatch
20+
Match emf-*
21+
region us-east-1
22+
log_key log
23+
log_group_name aws-emf-ecs-firelens-example-metrics
24+
log_stream_prefix from-fluent-bit-
25+
auto_create_group true
26+
log_format json/emf

examples/ecs-firelens/src/main/java/App.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
2525
import software.amazon.cloudwatchlogs.emf.model.Unit;
2626
import sun.misc.Signal;
27-
2827
import java.io.IOException;
2928
import java.io.OutputStream;
3029
import java.net.InetSocketAddress;
@@ -36,9 +35,12 @@ public class App {
3635

3736
public static void main(String[] args) throws Exception {
3837
registerShutdownHook();
39-
40-
int portNumber = 8000;
38+
MetricsLogger logger = new MetricsLogger();
39+
logger.setNamespace("FargateEMF");
40+
logger.putMetric("Latency", 63, Unit.MILLISECONDS);
41+
logger.flush();
4142
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
43+
int portNumber = 8000;
4244
System.out.println("Server started. Listening on " + portNumber);
4345
server.createContext("/", new SimpleHandler());
4446
server.setExecutor(null);

0 commit comments

Comments
 (0)