ECS Consul Cluster Incorrect Cluster IP
Consul Version
1.0.0
Operating system and Environment details
amzn-ami-2017.03.g-amazon-ecs-optimized (ami-40d5672f)
Description of the Issue (and unexpected/desired result).
I have an issue trying to cluster 3 consul servers on AWS EC2 Container Server (ECS) The guide I have been following is here : https://blog.unif.io/deploying-consul-with-ecs-part-2-38de98a56611
However I have swapped out the deprecated -retry-join-ec2-tag-key and -retry-join-ec2-tag-value for the -retry-join "provider=aws tag_key=xxx tag_value=xxx".
Here is my ECS task definition
{
"requiresAttributes": [
{
"value": null,
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs",
"targetId": null,
"targetType": null
},
{
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19",
"targetId": null,
"targetType": null
}
],
"taskDefinitionArn": "arn:aws:ecs:eu-central-1:556264438221:task-definition/consul-server:24",
"networkMode": "bridge",
"status": "ACTIVE",
"revision": 24,
"taskRoleArn": null,
"containerDefinitions": [
{
"volumesFrom": [],
"memory": 100,
"extraHosts": null,
"linuxParameters": null,
"dnsServers": null,
"disableNetworking": null,
"dnsSearchDomains": null,
"portMappings": [
{
"hostPort": 8500,
"containerPort": 8500,
"protocol": "tcp"
},
{
"hostPort": 8300,
"containerPort": 8300,
"protocol": "tcp"
},
{
"hostPort": 8302,
"containerPort": 8302,
"protocol": "tcp"
},
{
"hostPort": 8302,
"containerPort": 8302,
"protocol": "udp"
},
{
"hostPort": 8301,
"containerPort": 8301,
"protocol": "tcp"
},
{
"hostPort": 8301,
"containerPort": 8301,
"protocol": "udp"
}
],
"hostname": null,
"essential": true,
"entryPoint": null,
"mountPoints": [],
"name": "consul-server",
"ulimits": null,
"dockerSecurityOptions": null,
"environment": [
{
**"name": "CONSUL_BIND_INTERFACE",
"value": "eth0"**
}
],
"links": null,
"workingDirectory": null,
"readonlyRootFilesystem": null,
"image": "consul:1.0.0",
"command": [
**"agent",
"-client=0.0.0.0",
"-retry-join",
"provider=aws tag_key=aws:cloudformation:stack-name tag_value=test2",
"-server",
"-bootstrap-expect=3",
"-ui"**
],
"user": null,
"dockerLabels": null,
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "awslogs",
"awslogs-region": "eu-central-1",
"awslogs-stream-prefix": "consul-server"
}
},
"cpu": 0,
"privileged": null,
"memoryReservation": null
}
],
"placementConstraints": [],
"volumes": [],
"family": "consul-server"
}
Here is a log snippet:
==> Found address '172.17.0.2' for interface 'eth0', setting bind option...
bootstrap_expect > 0: expecting 3 servers
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.0.0'
Node ID: 'f3bb355f-7bba-de52-6da0-5da8f64b464f'
Node name: 'd682a0b72e8b'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
**Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, DNS: 8600)**
**Cluster Addr: 172.17.0.2** (LAN: 8301, WAN: 8302)**
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
==> Log data will now stream in as it occurs:
2017/10/27 10:43:30 [INFO] raft: Initial configuration (index=0): []
2017/10/27 10:43:30 [INFO] raft: Node at 172.17.0.2:8300 [Follower] entering Follower state (Leader: "")
2017/10/27 10:43:30 [INFO] serf: EventMemberJoin: d682a0b72e8b.dc1 172.17.0.2
2017/10/27 10:43:30 [INFO] serf: EventMemberJoin: d682a0b72e8b 172.17.0.2
2017/10/27 10:43:30 [INFO] agent: Started DNS server 0.0.0.0:8600 (udp)
2017/10/27 10:43:30 [INFO] consul: Adding LAN server d682a0b72e8b (Addr: tcp/172.17.0.2:8300) (DC: dc1)
2017/10/27 10:43:30 [INFO] consul: Handled member-join event for server "d682a0b72e8b.dc1" in area "wan"
2017/10/27 10:43:30 [INFO] agent: Started DNS server 0.0.0.0:8600 (tcp)
2017/10/27 10:43:30 [INFO] agent: Started HTTP server on [::]:8500 (tcp)
2017/10/27 10:43:30 [INFO] agent: Retry join LAN is supported for: aws azure gce softlayer
2017/10/27 10:43:30 [INFO] agent: Joining LAN cluster...
2017/10/27 10:43:30 [INFO] discover-aws: Address type is not supported. Valid values are {private_v4,public_v4,public_v6}. Falling back to 'private_v4'
2017/10/27 10:43:30 [INFO] discover-aws: Region not provided. Looking up region in metadata...
2017/10/27 10:43:30 [INFO] discover-aws: Region is eu-central-1
2017/10/27 10:43:30 [INFO] discover-aws: Filter instances with aws:cloudformation:stack-name=test2
2017/10/27 10:43:31 [INFO] serf: EventMemberJoin: ee0fcd4d1a16 172.17.0.2
2017/10/27 10:43:31 [INFO] consul: Adding LAN server ee0fcd4d1a16 (Addr: tcp/172.17.0.2:8300) (DC: dc1)
**2017/10/27 10:43:31 [INFO] discover-aws: Instance i-0c9cbfa32942d6c3a has private ip 172.32.50.220
2017/10/27 10:43:31 [INFO] discover-aws: Instance i-03ca0001213c263f9 has private ip 172.32.50.168
2017/10/27 10:43:31 [INFO] discover-aws: Instance i-0311218429eceeb36 has private ip 172.32.49.137
2017/10/27 10:43:31 [INFO] discover-aws: Instance i-0e23cda5fe2521a0f has private ip 172.32.49.97**
2017/10/27 10:43:31 [INFO] agent: Discovered LAN servers: 172.32.50.220 172.32.50.168 172.32.49.137 172.32.49.97
2017/10/27 10:43:31 [INFO] agent: (LAN) joining: [172.32.50.220 172.32.50.168 172.32.49.137 172.32.49.97]
2017/10/27 10:43:31 [INFO] serf: EventMemberJoin: be7497de5592 172.17.0.2
2017/10/27 10:43:31 [INFO] consul: Adding LAN server be7497de5592 (Addr: tcp/172.17.0.2:8300) (DC: dc1)
2017/10/27 10:43:31 [INFO] consul: Found expected number of peers, attempting bootstrap: 172.17.0.2:8300,172.17.0.2:8300,172.17.0.2:8300
2017/10/27 10:43:31 [ERR] consul: Failed to bootstrap cluster: Found duplicate address in configuration: 172.17.0.2:8300
From the logs you can see the cluster address is 172.17.0.2 which is the actual IP address of the container, this should be in fact the IP address of the host which as far as I am aware should have been provided by using the -retry-join provider aws as this should query the AWS metadata API for the instance IPV4 address. The weird thing is the IPV4 address of each host is correct in subsequent logging statements(discover-aws) .
Any help would be greatly appreciated as I'm really unsure of how else to tackle this without using the UserData object directly rather than provisioning via the ECS console which I would lose visibility.
Thanks, J
The cluster address is the advertise address for the LAN. This is the address under which this consul agent can be reached by others. You need to set -advertise on the command line or advertise_addr in the config file to the correct value. If you don't know the address upfront then you can use a https://github.com/hashicorp/go-sockaddr template to detect it.
-retry-join are the addresses of the other consul agents in the cluster which this consul agent will try to join.
Does that make sense?
Since I am provisioning using ECS and TaskDefinitions I don't think I can use the go-sockaddr as I would have to use String interpolation which is not supported in TaskDefinitions. How are other people managing to run this on ECS?
I've got this working now by using Host networking in ECS
@jwarder: any chance you could provide your working config ?
@asquel Here is my task definition in JSON format
{
"requiresAttributes": [
{
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.18",
"targetId": null,
"targetType": null
},
{
"value": null,
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs",
"targetId": null,
"targetType": null
},
{
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19",
"targetId": null,
"targetType": null
}
],
"taskDefinitionArn": "arn:aws:ecs:eu-central-1:556264438221:task-definition/consul-server:30",
"networkMode": "host",
"status": "ACTIVE",
"revision": 30,
"taskRoleArn": null,
"containerDefinitions": [
{
"volumesFrom": [],
"memory": 100,
"extraHosts": null,
"linuxParameters": null,
"dnsServers": null,
"disableNetworking": null,
"dnsSearchDomains": null,
"portMappings": [
{
"hostPort": 8500,
"containerPort": 8500,
"protocol": "tcp"
},
{
"hostPort": 8300,
"containerPort": 8300,
"protocol": "tcp"
},
{
"hostPort": 8302,
"containerPort": 8302,
"protocol": "tcp"
},
{
"hostPort": 8302,
"containerPort": 8302,
"protocol": "udp"
},
{
"hostPort": 8301,
"containerPort": 8301,
"protocol": "tcp"
},
{
"hostPort": 8301,
"containerPort": 8301,
"protocol": "udp"
}
],
"hostname": null,
"essential": true,
"entryPoint": null,
"mountPoints": [],
"name": "consul-server",
"ulimits": null,
"dockerSecurityOptions": null,
"environment": [
{
"name": "CONSUL_BIND_INTERFACE",
"value": "eth0"
},
{
"name": "CONSUL_CLIENT_INTERFACE",
"value": "eth0"
}
],
"links": null,
"workingDirectory": null,
"readonlyRootFilesystem": null,
"image": "consul:1.0.0",
"command": [
"agent",
"-client=0.0.0.0",
"-retry-join",
"provider=aws tag_key=aws:cloudformation:stack-name tag_value=test2",
"-server",
"-bootstrap-expect=3",
"-ui"
],
"user": null,
"dockerLabels": null,
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "awslogs",
"awslogs-region": "eu-central-1",
"awslogs-stream-prefix": "consul-server"
}
},
"cpu": 0,
"privileged": null,
"memoryReservation": null
}
],
"placementConstraints": [],
"volumes": [],
"family": "consul-server"
}
Alternatively, you can have your container discover the 'real' host ip and pass that to Consul. I modified mine to have a startup script that discovers this:
ADVERTISE=
# Get our ipv4 address for use in advertise
INSTANCE_METADATA_HOST=${INSTANCE_METADATA_HOST:-"169.254.169.254"}
IPV4=$(curl -sf "http://$INSTANCE_METADATA_HOST/latest/meta-data/local-ipv4" || true)
if [ -n "$IPV4" ]; then
ADVERTISE="-advertise $IPV4"
fi
$ADVERTISE is later used when invoking the Consul daemon.
This works with Bridge networking.
Looks like this got worked out.