|
| 1 | +# How to run the Elastic Agent with access to ECS Task Metadata endpoint |
| 2 | + |
| 3 | +Thanks to Marcin, I discovered a nice [blog post from AWS](https://aws.amazon.com/blogs/compute/a-guide-to-locally-testing-containers-with-amazon-ecs-local-endpoints-and-docker-compose/) about how to locally testing ECS Local Endpoints. |
| 4 | + |
| 5 | +## Solution A: Toss containers in |
| 6 | + |
| 7 | +Simple solution where we `stack up` the dev environment and toss a couple of carefully crafted containers using `docker run`. |
| 8 | + |
| 9 | +### Start you dev stack |
| 10 | + |
| 11 | +```shell |
| 12 | +$ elastic-package stack up -v -d |
| 13 | +... |
| 14 | +``` |
| 15 | + |
| 16 | +### Find the network |
| 17 | + |
| 18 | +The first think is to locate the network used by the stack containers: |
| 19 | + |
| 20 | +```shell |
| 21 | +$ docker network ls |
| 22 | +NETWORK ID NAME DRIVER SCOPE |
| 23 | +38ee458eed0a bridge bridge local |
| 24 | +4f60e5ced33a elastic-package-stack_default bridge local <——— 👀 |
| 25 | +6e9d2aa908d4 host host local |
| 26 | +32ea7f983e52 none null local |
| 27 | +``` |
| 28 | + |
| 29 | +Let's pick the `elastic-package-stack_default` network. |
| 30 | + |
| 31 | +### Add the ECS Task Metadata endpoint |
| 32 | + |
| 33 | +Next, we need to set up an ECS Metadata endpoint in the same way Fargate provides one. We are so lucky today because AWS build one for us, in the form of a Docker image. |
| 34 | + |
| 35 | +Run the `amazon-ecs-local-container-endpoints` image and attach it to the stack network: |
| 36 | + |
| 37 | +```shell |
| 38 | +docker run --rm \ |
| 39 | + --name amazon-ecs-local-container-endpoints \ |
| 40 | + --network elastic-package-stack_default \ |
| 41 | + --volume /var/run:/var/run \ |
| 42 | + -i amazon/amazon-ecs-local-container-endpoints |
| 43 | +``` |
| 44 | + |
| 45 | +Let's take a look at this container's detail and take not of the IP address: |
| 46 | + |
| 47 | +```shell |
| 48 | +$ docker inspect amazon-ecs-local-container-endpoints | jq '.[].NetworkSettings.Networks' |
| 49 | +{ |
| 50 | + "elastic-package-stack_default": { |
| 51 | + "IPAMConfig": null, |
| 52 | + "Links": null, |
| 53 | + "Aliases": [ |
| 54 | + "8c250f699e8a" |
| 55 | + ], |
| 56 | + "NetworkID": "4f60e5ced33a15babe89ff7620f93715a1c2128a593bac59b279e779371855d3", |
| 57 | + "EndpointID": "b0e321c7129de8378ffb6c42ae7ec9c6773a51f54b3075eeeb79445842dc0f56", |
| 58 | + "Gateway": "172.20.0.1", |
| 59 | + "IPAddress": "172.20.0.4", |
| 60 | + "IPPrefixLen": 16, |
| 61 | + "IPv6Gateway": "", |
| 62 | + "GlobalIPv6Address": "", |
| 63 | + "GlobalIPv6PrefixLen": 0, |
| 64 | + "MacAddress": "02:42:ac:14:00:04", |
| 65 | + "DriverOpts": null |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Nice, the IP address is `172.20.0.4`, let's test if task metadata endpoint is working as expected: |
| 71 | + |
| 72 | +```shell |
| 73 | +$ docker exec -it amazon-ecs-local-container-endpoints /bin/bash |
| 74 | +bash-4.2# curl -i http://172.20.0.4/v3 |
| 75 | +HTTP/1.1 200 OK |
| 76 | +Content-Type: application/json |
| 77 | +Date: Wed, 30 Mar 2022 13:27:22 GMT |
| 78 | +Content-Length: 663 |
| 79 | + |
| 80 | +{ |
| 81 | + "DockerId": "8c250f699e8a8bc3d692f6c00d00402244859c0de14ef3036f418d3013fffa7f", |
| 82 | + "Name": "amazon-ecs-local-container-endpoints", |
| 83 | + "DockerName": "amazon-ecs-local-container-endpoints", |
| 84 | + "Image": "amazon/amazon-ecs-local-container-endpoints", |
| 85 | + "ImageID": "sha256:b005329f50b1ae4e79c04aee4f1044ccad484d0c5fa2d3c85e8679729e61e1c1", |
| 86 | + "Ports": [ |
| 87 | + { |
| 88 | + "ContainerPort": 80, |
| 89 | + "Protocol": "tcp" |
| 90 | + } |
| 91 | + ], |
| 92 | + "DesiredStatus": "RUNNING", |
| 93 | + "KnownStatus": "RUNNING", |
| 94 | + "Limits": {}, |
| 95 | + "CreatedAt": "2022-03-30T13:24:35Z", |
| 96 | + "StartedAt": "2022-03-30T13:24:35Z", |
| 97 | + "Type": "NORMAL", |
| 98 | + "Networks": [ |
| 99 | + { |
| 100 | + "NetworkMode": "elastic-package-stack_default", |
| 101 | + "IPv4Addresses": [ |
| 102 | + "172.20.0.4" |
| 103 | + ] |
| 104 | + } |
| 105 | + ], |
| 106 | + "Volumes": [ |
| 107 | + { |
| 108 | + "Source": "/var/run", |
| 109 | + "Destination": "/var/run" |
| 110 | + } |
| 111 | + ] |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +This is a good thing, the endpoint is able to get a response from the metadata endpoint. |
| 116 | + |
| 117 | +### Enrol an Elastic Agent |
| 118 | + |
| 119 | +Time to run and enrol an Elastic Agent attaching it to the same network the dev stack and the amazon-ecs-local-container-endpoints container are, so it can start collecting and sending data to the dev stack: |
| 120 | + |
| 121 | +```shell |
| 122 | +$ docker run \ |
| 123 | + --rm \ |
| 124 | + --network elastic-package-stack_default \ |
| 125 | + -e FLEET_URL=http://fleet-server:8220 \ |
| 126 | + -e FLEET_ENROLL=true \ |
| 127 | + -e FLEET_ENROLLMENT_TOKEN=YlgyazJuOEJzOVVRTHViRExpUlI6SzhQdDM5QXRSbGlrc0N3Nkg5bkE1Zw== \ |
| 128 | + -e FLEET_INSECURE=true \ |
| 129 | + -e ECS_CONTAINER_METADATA_URI_V4="http://172.20.0.4/v3" \ |
| 130 | + -i docker.elastic.co/beats/elastic-agent:8.1.0 |
| 131 | +``` |
| 132 | + |
| 133 | +## Solution B: Spin up a docker compose |
| 134 | + |
| 135 | +TBA |
0 commit comments