Skip to content

Commit

Permalink
Merge pull request #12 from mcapuccini/feature/aws_bootstrap
Browse files Browse the repository at this point in the history
add AWS support. Thanks @andersla !
  • Loading branch information
mcapuccini authored Oct 31, 2016
2 parents 5994ecf + 7c8e37c commit 43db3ff
Show file tree
Hide file tree
Showing 10 changed files with 532 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Want to try KubeNow? You can get started following the tutorials in the document
### Cloud Providers
- [x] OpenStack
- [x] Google Cloud Platform
- [ ] Amazon Web Services
- [x] Amazon Web Services
- [ ] Local


Expand Down
64 changes: 64 additions & 0 deletions aws/edge/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
variable name_prefix {}
variable kubenow_image_id {}
variable instance_type {}
variable kubeadm_token {}
variable disk_size {}
variable availability_zone {}
variable ssh_user {}
variable ssh_keypair_name {}
variable master_ip {}
variable subnet_id {}
variable count {}
variable security_group_id {}

# create bootstrap script file from template
resource "template_file" "edge_bootstrap" {
template = "${file("${path.root}/../bootstrap/node.sh")}"
vars {
kubeadm_token = "${var.kubeadm_token}"
master_ip = "${var.master_ip}"
}
}

resource "aws_instance" "edge" {
count = "${var.count}"
ami = "${var.kubenow_image_id}"
availability_zone = "${var.availability_zone}"
instance_type = "${var.instance_type}"
associate_public_ip_address = true
key_name = "${var.ssh_keypair_name}"
vpc_security_group_ids = ["${var.security_group_id}"]
subnet_id = "${var.subnet_id}"
user_data = "${template_file.edge_bootstrap.rendered}"

root_block_device {
delete_on_termination = true
volume_size = "${var.disk_size}"
}

tags {
Name = "${var.name_prefix}-edge-${format("%03d", count.index)}"
sshUser = "${var.ssh_user}"
}
}

# Generate ansible inventory
resource "null_resource" "generate-inventory" {

provisioner "local-exec" {
command = "echo \"[edge]\" >> inventory"
}

provisioner "local-exec" {
command = "echo \"${join("\n",formatlist("%s ansible_ssh_host=%s ansible_ssh_user=ubuntu", aws_instance.edge.*.tags.Name, aws_instance.edge.*.public_ip))}\" >> inventory"
}

provisioner "local-exec" {
command = "echo \"[master:vars]\" >> inventory"
}

provisioner "local-exec" {
# generates aws hostnames (ip-000-111-222-333) from ip-numbers
command = "echo 'edge_names=\"${replace(join(" ",formatlist("ip-%s", aws_instance.edge.*.private_ip)),".","-")}\"' >> inventory"
}
}
86 changes: 86 additions & 0 deletions aws/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Cluster settings
variable cluster_prefix {}
variable kubenow_image_id {}
variable kubeadm_token {}

variable aws_access_key_id {}
variable aws_secret_access_key {}
variable aws_region {}
variable availability_zone {}

variable ssh_user { default = "ubuntu" }
variable ssh_keypair_name {}

# Master settings
variable master_instance_type {}
variable master_disk_size {}

# Nodes settings
variable node_count {}
variable node_instance_type {}
variable node_disk_size {}

# Edges settings
variable edge_count {}
variable edge_instance_type {}
variable edge_disk_size {}

# Provider
provider "aws" {
access_key = "${var.aws_access_key_id}"
secret_key = "${var.aws_secret_access_key}"
region = "${var.aws_region}"
}

# VPC Virtual Private Cloud - Networking
module "vpc" {
source = "./vpc"
name_prefix = "${var.cluster_prefix}"
availability_zone = "${var.availability_zone}"
}

module "master" {
source = "./master"
name_prefix = "${var.cluster_prefix}"
kubenow_image_id = "${var.kubenow_image_id}"
instance_type = "${var.master_instance_type}"
subnet_id = "${module.vpc.subnet_id}"
security_group_id = "${module.vpc.security_group_id}"
kubeadm_token = "${var.kubeadm_token}"
availability_zone = "${var.availability_zone}"
ssh_user = "${var.ssh_user}"
ssh_keypair_name = "${var.ssh_keypair_name}"
disk_size = "${var.master_disk_size}"
}

module "node" {
source = "./node"
name_prefix = "${var.cluster_prefix}"
kubenow_image_id = "${var.kubenow_image_id}"
instance_type = "${var.node_instance_type}"
subnet_id = "${module.vpc.subnet_id}"
security_group_id = "${module.vpc.security_group_id}"
kubeadm_token = "${var.kubeadm_token}"
master_ip = "${module.master.ip_address_internal}"
count = "${var.node_count}"
availability_zone = "${var.availability_zone}"
ssh_user = "${var.ssh_user}"
ssh_keypair_name = "${var.ssh_keypair_name}"
disk_size = "${var.node_disk_size}"
}

module "edge" {
source = "./edge"
name_prefix = "${var.cluster_prefix}"
kubenow_image_id = "${var.kubenow_image_id}"
instance_type = "${var.edge_instance_type}"
subnet_id = "${module.vpc.subnet_id}"
security_group_id = "${module.vpc.security_group_id}"
kubeadm_token = "${var.kubeadm_token}"
master_ip = "${module.master.ip_address_internal}"
count = "${var.edge_count}"
availability_zone = "${var.availability_zone}"
ssh_user = "${var.ssh_user}"
ssh_keypair_name = "${var.ssh_keypair_name}"
disk_size = "${var.edge_disk_size}"
}
66 changes: 66 additions & 0 deletions aws/master/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
variable name_prefix {}
variable kubenow_image_id {}
variable instance_type {}
variable kubeadm_token {}
variable disk_size {}
variable availability_zone {}
variable ssh_user {}
variable ssh_keypair_name {}
variable subnet_id {}
variable security_group_id {}


# create bootstrap script file from template
resource "template_file" "master_bootstrap" {
template = "${file("${path.root}/../bootstrap/master.sh")}"
vars {
kubeadm_token = "${var.kubeadm_token}"
}
}

resource "aws_instance" "master" {
ami = "${var.kubenow_image_id}"
availability_zone = "${var.availability_zone}"
instance_type = "${var.instance_type}"
associate_public_ip_address = true
key_name = "${var.ssh_keypair_name}"
vpc_security_group_ids = ["${var.security_group_id}"]
subnet_id = "${var.subnet_id}"
user_data = "${template_file.master_bootstrap.rendered}"

root_block_device {
delete_on_termination = true
volume_size = "${var.disk_size}"
}

tags {
Name = "${var.name_prefix}-master"
sshUser = "${var.ssh_user}"
}
}

# Generate ansible inventory
resource "null_resource" "generate-inventory" {

provisioner "local-exec" {
command = "echo \"[master]\" > inventory"
}

provisioner "local-exec" {
command = "echo \"${aws_instance.master.0.tags.Name} ansible_ssh_host=${aws_instance.master.0.public_ip} ansible_ssh_user=ubuntu\" >> inventory"
}

}

output "ip_address" {
value = "${aws_instance.master.0.public_ip}"
}

output "ip_address_internal" {
value = "${aws_instance.master.0.private_ip}"
}





44 changes: 44 additions & 0 deletions aws/node/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
variable name_prefix {}
variable kubenow_image_id {}
variable instance_type {}
variable kubeadm_token {}
variable disk_size {}
variable availability_zone {}
variable ssh_user {}
variable ssh_keypair_name {}
variable master_ip {}
variable subnet_id {}
variable count {}
variable security_group_id {}


# create bootstrap script file from template
resource "template_file" "node_bootstrap" {
template = "${file("${path.root}/../bootstrap/node.sh")}"
vars {
kubeadm_token = "${var.kubeadm_token}"
master_ip = "${var.master_ip}"
}
}

resource "aws_instance" "node" {
count = "${var.count}"
ami = "${var.kubenow_image_id}"
availability_zone = "${var.availability_zone}"
instance_type = "${var.instance_type}"
associate_public_ip_address = true
key_name = "${var.ssh_keypair_name}"
vpc_security_group_ids = ["${var.security_group_id}"]
subnet_id = "${var.subnet_id}"
user_data = "${template_file.node_bootstrap.rendered}"

root_block_device {
delete_on_termination = true
volume_size = "${var.disk_size}"
}

tags {
Name = "${var.name_prefix}-node-${format("%03d", count.index)}"
sshUser = "${var.ssh_user}"
}
}
102 changes: 102 additions & 0 deletions aws/vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
variable name_prefix {}
variable availability_zone {}


resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags {
Name = "${var.name_prefix}"
}
}

resource "aws_subnet" "main" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "${aws_vpc.main.cidr_block}"
availability_zone = "${var.availability_zone}"
tags {
Name = "${var.name_prefix}"
}
}

resource "aws_internet_gateway" "main" {
vpc_id = "${aws_vpc.main.id}"
tags {
Name = "${var.name_prefix}"
}
}

resource "aws_route_table" "main" {
vpc_id = "${aws_vpc.main.id}"

route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main.id}"
}

tags {
Name = "${name_prefix}"
}
}

resource "aws_main_route_table_association" "main" {
vpc_id = "${aws_vpc.main.id}"
route_table_id = "${aws_route_table.main.id}"
}

resource "aws_route_table_association" "main" {
subnet_id = "${aws_subnet.main.id}"
route_table_id = "${aws_route_table.main.id}"
}

resource "aws_security_group" "main" {
name = "${var.name_prefix}"
description = "kubenow default security group"
vpc_id = "${aws_vpc.main.id}"


ingress { # SSH
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress { # HTTP
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress { # HTTPS
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress { # Allow ALL internal (self)
from_port = 0
to_port = 0
protocol = -1
self = true
}

egress { # Allow ALL outbound
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

}

output "subnet_id" {
value = "${aws_subnet.main.id}"
}

output "security_group_id" {
value = "${aws_security_group.main.id}"
}
Loading

0 comments on commit 43db3ff

Please sign in to comment.