Skip to content

Instantly share code, notes, and snippets.

@swade1987
Created May 23, 2016 14:41
Show Gist options
  • Save swade1987/d253f1139ffda0b6a21d6eaf89bb511c to your computer and use it in GitHub Desktop.
Save swade1987/d253f1139ffda0b6a21d6eaf89bb511c to your computer and use it in GitHub Desktop.
consul cluster
resource "aws_security_group" "consul_server" {
name = "consul-server-security-group"
description = "Consul internal traffic + maintenance."
// These are for internal traffic
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
self = true
}
ingress {
from_port = 0
to_port = 65535
protocol = "udp"
self = true
}
// These are for maintenance
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
// This is for outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "template_file" "user_data" {
template = "${file("${path.module}/user_data.sh")}"
vars {
consul_server_1 = "${var.consul_domain_name_one}"
consul_server_2 = "${var.consul_domain_name_two}"
consul_server_3 = "${var.consul_domain_name_three}"
}
}
resource "aws_instance" "consul_server_one" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
tags { Name = "consul-server-one" }
security_groups = ["${aws_security_group.consul_server.name}"]
user_data = "${template_file.user_data.rendered}"
}
resource "aws_instance" "consul_server_two" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
tags { Name = "consul-server-two" }
security_groups = ["${aws_security_group.consul_server.name}"]
user_data = "${template_file.user_data.rendered}"
}
resource "aws_instance" "consul_server_three" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
tags { Name = "consul-server-three" }
security_groups = ["${aws_security_group.consul_server.name}"]
user_data = "${template_file.user_data.rendered}"
}
resource "aws_route53_record" "consul_server_one" {
zone_id = "Z3820KW3201KHJ"
name = "${var.consul_domain_name_one}"
type = "A"
ttl = "300"
records = ["${aws_instance.consul_server_one.private_ip}"]
}
resource "aws_route53_record" "consul_server_two" {
zone_id = "Z3820KW3201KHJ"
name = "${var.consul_domain_name_two}"
type = "A"
ttl = "300"
records = ["${aws_instance.consul_server_two.private_ip}"]
}
resource "aws_route53_record" "consul_server_three" {
zone_id = "Z3820KW3201KHJ"
name = "${var.consul_domain_name_three}"
type = "A"
ttl = "300"
records = ["${aws_instance.consul_server_three.private_ip}"]
}
# Set necessary constants
bootstrap_expect=3
# Obtain the ip address for this host.
host_ip=$(ifconfig eth0 | grep "inet addr" | awk '{split($2,a,":"); print a[2]}')
# Pull the consul image from Docker Hub (https://hub.docker.com/_/consul/)
sudo docker pull consul
# Create a consul server container binding port 8500
sudo docker run -d --name consul \
-p $host_ip:8500:8500 \
-e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt": true}' \
consul agent \
-server \
-ui \
-client=0.0.0.0 \
-bootstrap-expect $bootstrap_expect
-advertise $host_ip \
-join ${consul_server_1} \
-join ${consul_server_2} \
-join ${consul_server_3} \
-retry-join ${consul_server_1} \
-retry-join ${consul_server_2} \
-retry-join ${consul_server_3} \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment