From fe194dcc264b5160754194c5937bd5f1098e58f7 Mon Sep 17 00:00:00 2001 From: vp15591 Date: Fri, 14 Jun 2019 17:16:16 +0530 Subject: [PATCH 1/6] Terraform module for GCP --- main.tf | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ outputs.tf | 8 ++++ utilities | 1 + variables.tf | 70 ++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 main.tf create mode 100644 outputs.tf create mode 160000 utilities create mode 100644 variables.tf diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..fecdabf --- /dev/null +++ b/main.tf @@ -0,0 +1,120 @@ + +data "google_compute_image" "YugaByte_DB_Image" { + family = "centos-6" + project = "centos-cloud" +} +data "google_compute_zones" "available" { + region = "${var.region_name}" +} + +resource "google_compute_firewall" "YugaByte-Firewall" { + name = "${var.vpc_firewall}-yugabyte-firewall" + network = "${var.vpc_network}" + allow { + protocol = "tcp" + ports = ["9000","7000","6379","9042","5433","22"] + } + target_tags = ["yugabyte"] +} +resource "google_compute_firewall" "YugaByte-Intra-Firewall" { + name = "${var.vpc_firewall}-yugabyte-intra-firewall" + network = "${var.vpc_network}" + allow { + protocol = "tcp" + ports = ["7100", "9100"] + } + target_tags = ["yugabyte"] +} + +resource "google_compute_instance" "yugabyte_node" { + count = "${var.node_count}" + name = "yugabute-node-${count.index}" + machine_type = "${var.node_type}" + zone = "${data.google_compute_zones.available.names[count.index]}" + tags=["yugabyte"] + + boot_disk{ + initialize_params { + image = "${data.google_compute_image.YugaByte_DB_Image.self_link}" + size = "${var.disk_size}" + } + } + metadata { + sshKeys = "${var.ssh_user}:${file(var.ssh_pub_key)}" + } + + network_interface{ + network = "${var.vpc_network}" + access_config { + // external ip to instance + } + } + + provisioner "file" { + source = "${path.module}/scripts/install_software.sh" + destination = "/home/${var.ssh_user}/install_software.sh" + connection { + type = "ssh" + user = "${var.ssh_user}" + private_key = "${file(var.ssh_key_path)}" + } + } + + provisioner "file" { + source = "${path.module}/scripts/create_universe.sh" + destination ="/home/${var.ssh_user}/create_universe.sh" + connection { + type = "ssh" + user = "${var.ssh_user}" + private_key = "${file(var.ssh_key_path)}" + } + } + provisioner "file" { + source = "${path.module}/utilities/scripts/start_master.sh" + destination ="/home/${var.ssh_user}/start_master.sh" + connection { + type = "ssh" + user = "${var.ssh_user}" + private_key = "${file(var.ssh_key_path)}" + } + } + provisioner "file" { + source = "${path.module}/utilities/scripts/start_tserver.sh" + destination ="/home/${var.ssh_user}/start_tserver.sh" + connection { + type = "ssh" + user = "${var.ssh_user}" + private_key = "${file(var.ssh_key_path)}" + } + } + provisioner "remote-exec" { + inline = [ + "chmod +x /home/${var.ssh_user}/install_software.sh", + "chmod +x /home/${var.ssh_user}/create_universe.sh", + "chmod +x /home/${var.ssh_user}/start_tserver.sh", + "chmod +x /home/${var.ssh_user}/start_master.sh", + "/home/${var.ssh_user}/install_software.sh '${var.yb_edition}' '${var.yb_version}' '${var.yb_download_url}'" + ] + connection { + type = "ssh" + user = "${var.ssh_user}" + private_key = "${file(var.ssh_key_path)}" + } + } +} + +locals { + depends_on = ["google_compute_instance.yugabyte_node"] + ssh_ip_list = "${var.use_public_ip_for_ssh == "true" ? join(" ",google_compute_instance.yugabyte_node.*.network_interface.0.access_config.0.nat_ip) : join(" ",google_compute_instance.yugabyte_node.*.network_interface.0.network_ip)}" + config_ip_list = "${join(" ",google_compute_instance.yugabyte_node.*.network_interface.0.network_ip)}" + zone = "${join(" ", google_compute_instance.yugabyte_node.*.zone)}" +} + +resource "null_resource" "create_yugabyte_universe" { + depends_on = ["google_compute_instance.yugabyte_node"] + + provisioner "local-exec" { + command = "${path.module}/utilities/scripts/create_universe.sh 'GCP' '${var.region_name}' ${var.replication_factor} '${local.config_ip_list}' '${local.ssh_ip_list}' '${local.zone}' '${var.ssh_user}' ${var.ssh_key_path}" + } +} + diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..5e9659e --- /dev/null +++ b/outputs.tf @@ -0,0 +1,8 @@ +output "ui" { + sensitive = false + value = "http://${google_compute_instance.yugabyte_node.0.network_interface.0.access_config.0.nat_ip}:7000" +} +output "ssh_key" { + sensitive = false + value = "${var.ssh_key_path}" +} \ No newline at end of file diff --git a/utilities b/utilities new file mode 160000 index 0000000..caacc8a --- /dev/null +++ b/utilities @@ -0,0 +1 @@ +Subproject commit caacc8a0d161c1f72293f0385fc28500b11c0c3c diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..6923ab8 --- /dev/null +++ b/variables.tf @@ -0,0 +1,70 @@ +variable "use_public_ip_for_ssh" { + description = "Flag to control use of public or private ips for ssh." + default = "true" + type = "string" +} +variable "replication_factor" { + description = "The replication factor for the universe." + default = 3 + type = "string" +} +variable "node_count" { + description = "The number of nodes to create YugaByte Db Cluter" + default = 3 + type = "string" +} +variable "vpc_network" { + description = "VPC network to deploy YugaByte DB" + default = "default" + type = "string" +} +variable "vpc_firewall" { + description = "Firewall used by the YugaByte Node" + default = "default" + type = "string" +} +variable "ssh_key_path" { + description = "The public key to use when connecting to the instances." + type = "string" +} +variable "ssh_pub_key" { + description = "SSH public key to be used by nodes" + type = "string" +} +variable "ssh_user" { + description = "User name to ssh YugaByte Node to configure cluster" + type = "string" +} +variable "node_type" { + description = "Type of Node to be used for YugaByte DB node " + default = "n1-standard-4" + type = "string" +} +variable "yb_edition" { + description = "The edition of YugaByteDB to install" + default = "ce" + type = "string" +} + +variable "yb_download_url" { + description = "The download location of the YugaByteDB edition" + default = "https://downloads.yugabyte.com" + type = "string" +} + +variable "yb_version" { + description = "The version number of YugaByteDB to install" + default = "1.2.8.0" + type = "string" +} + +variable "region_name" { + description = "Region name for GCP" + default = "us-west1" + type = "string" +} +variable "disk_size" { + description = "Disk size for YugaByte DB nodes" + default = "50" + type = "string" +} From 139c13a22f27a8394099e5c3e17388fe964d4716 Mon Sep 17 00:00:00 2001 From: vp15591 Date: Thu, 20 Jun 2019 10:32:09 +0530 Subject: [PATCH 2/6] submodule updated --- .gitmodules | 3 +++ utilities | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c055890 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "utilities"] + path = utilities + url = https://github.com/YugaByte/utilities.git diff --git a/utilities b/utilities index caacc8a..77fe6f1 160000 --- a/utilities +++ b/utilities @@ -1 +1 @@ -Subproject commit caacc8a0d161c1f72293f0385fc28500b11c0c3c +Subproject commit 77fe6f13563e5eb0b7b6ff881e2f0ec54644cd71 From 0d86bac044652e78baa4c542085d60bcf3b30e0e Mon Sep 17 00:00:00 2001 From: vp15591 Date: Thu, 20 Jun 2019 13:03:32 +0530 Subject: [PATCH 3/6] Updated the README file and made some chages in variables file --- README.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.tf | 16 +++++++------- variables.tf | 9 ++++++++ 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 50bca09..670a9e9 100644 --- a/README.md +++ b/README.md @@ -1 +1,62 @@ # terraform-gcp-yugabyte +A Terraform module to deploy and run YugaByte on Google Cloud. + +## Config + +``` +module "yugabyte-db-cluster" { + source = "./terraform-gcp-yugabyte" + + # The name of the cluster to be created. + cluster_name = "tf-test" + + # key pair. + ssh_private_key = "SSH_PRIVATE_KEY_HERE" + ssh_public_key = "SSH_PUBLIC_KEY_HERE" + ssh_user = "SSH_USER_NAME_HERE" + + # The region name where the nodes should be spawned. + region_name = "YOUR VPC REGION" + + # Replication factor. + replication_factor = "3" + + # The number of nodes in the cluster, this cannot be lower than the replication factor. + node_count = "3" +} +``` + + +## Usage + +Init terraform first if you have not already done so. + +``` +$ terraform init +``` + +Now run the following to create the instances and bring up the cluster. + +``` +$ terraform apply +``` + +Once the cluster is created, you can go to the URL `http://:7000` to view the UI. You can find the node's ip or dns by running the following: + +``` +terraform state show google_compute_instance.yugabyte_node[0] +``` + +You can access the cluster UI by going to any of the following URLs. + +You can check the state of the nodes at any point by running the following command. + +``` +$ terraform show +``` + +To destroy what we just created, you can run the following command. + +``` +$ terraform destroy +``` diff --git a/main.tf b/main.tf index fecdabf..0065ddf 100644 --- a/main.tf +++ b/main.tf @@ -8,30 +8,30 @@ data "google_compute_zones" "available" { } resource "google_compute_firewall" "YugaByte-Firewall" { - name = "${var.vpc_firewall}-yugabyte-firewall" + name = "${var.vpc_firewall}-${var.prefix}${var.cluster_name}-firewall" network = "${var.vpc_network}" allow { protocol = "tcp" ports = ["9000","7000","6379","9042","5433","22"] } - target_tags = ["yugabyte"] + target_tags = ["${var.prefix}${var.cluster_name}"] } resource "google_compute_firewall" "YugaByte-Intra-Firewall" { - name = "${var.vpc_firewall}-yugabyte-intra-firewall" + name = "${var.vpc_firewall}-${var.prefix}${var.cluster_name}-intra-firewall" network = "${var.vpc_network}" allow { protocol = "tcp" ports = ["7100", "9100"] } - target_tags = ["yugabyte"] + target_tags = ["${var.prefix}${var.cluster_name}"] } resource "google_compute_instance" "yugabyte_node" { count = "${var.node_count}" - name = "yugabute-node-${count.index}" + name = "${var.prefix}${var.cluster_name}-n${format("%d", count.index + 1)}" machine_type = "${var.node_type}" zone = "${data.google_compute_zones.available.names[count.index]}" - tags=["yugabyte"] + tags=["${var.prefix}${var.cluster_name}"] boot_disk{ initialize_params { @@ -51,7 +51,7 @@ resource "google_compute_instance" "yugabyte_node" { } provisioner "file" { - source = "${path.module}/scripts/install_software.sh" + source = "${path.module}/utilities/scripts/install_software.sh" destination = "/home/${var.ssh_user}/install_software.sh" connection { type = "ssh" @@ -61,7 +61,7 @@ resource "google_compute_instance" "yugabyte_node" { } provisioner "file" { - source = "${path.module}/scripts/create_universe.sh" + source = "${path.module}/utilities/scripts/create_universe.sh" destination ="/home/${var.ssh_user}/create_universe.sh" connection { type = "ssh" diff --git a/variables.tf b/variables.tf index 6923ab8..c023422 100644 --- a/variables.tf +++ b/variables.tf @@ -1,3 +1,7 @@ +variable "cluster_name" { + description = "The name for the cluster (universe) being created." + type = "string" +} variable "use_public_ip_for_ssh" { description = "Flag to control use of public or private ips for ssh." default = "true" @@ -68,3 +72,8 @@ variable "disk_size" { default = "50" type = "string" } +variable "prefix" { + description = "Prefix prepended to all resources created." + default = "yugabyte-" + type = "string" +} From cc053db51873fa2c61382e8e7b922cc03b002751 Mon Sep 17 00:00:00 2001 From: vp15591 Date: Thu, 20 Jun 2019 16:13:42 +0530 Subject: [PATCH 4/6] fixed the issue related to missing variables --- README.md | 35 ++++++++++++++++++++++++++++------- main.tf | 14 +++++++------- outputs.tf | 2 +- variables.tf | 8 ++++---- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 670a9e9..075f4a4 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,26 @@ A Terraform module to deploy and run YugaByte on Google Cloud. ## Config - -``` -module "yugabyte-db-cluster" { - source = "./terraform-gcp-yugabyte" +* First create a terraform file with provider details + ``` + provider "google" + { + # Provide your GCP Creadentilals + credentials = "${file("yugabyte-pcf-bc8114281026.json")}" + + # The name of your GCP project + project = "yugabyte-pcf" + } + ``` + Note :- You can get credentials file by following steps given [here](https://cloud.google.com/docs/authentication/getting-started#auth-cloud-implicit-python) + +* Now add the yugabyte terraform module to your file + ``` + module "yugabyte-db-cluster" { + source = "github.com/YugaByte/terraform-gcp-yugabyte.git" # The name of the cluster to be created. - cluster_name = "tf-test" + cluster_name = "test-yugabyte" # key pair. ssh_private_key = "SSH_PRIVATE_KEY_HERE" @@ -23,8 +36,8 @@ module "yugabyte-db-cluster" { # The number of nodes in the cluster, this cannot be lower than the replication factor. node_count = "3" -} -``` + } + ``` ## Usage @@ -35,6 +48,13 @@ Init terraform first if you have not already done so. $ terraform init ``` +To check what changes are going to happen in the environment run the following + +``` +$ terraform plan +``` + + Now run the following to create the instances and bring up the cluster. ``` @@ -60,3 +80,4 @@ To destroy what we just created, you can run the following command. ``` $ terraform destroy ``` +`Note:- To make any changes in the created cluster you will need the terraform state files. So don't delete state files of Terraform.` \ No newline at end of file diff --git a/main.tf b/main.tf index 0065ddf..616ba2c 100644 --- a/main.tf +++ b/main.tf @@ -40,7 +40,7 @@ resource "google_compute_instance" "yugabyte_node" { } } metadata { - sshKeys = "${var.ssh_user}:${file(var.ssh_pub_key)}" + sshKeys = "${var.ssh_user}:${file(var.ssh_public_key)}" } network_interface{ @@ -56,7 +56,7 @@ resource "google_compute_instance" "yugabyte_node" { connection { type = "ssh" user = "${var.ssh_user}" - private_key = "${file(var.ssh_key_path)}" + private_key = "${file(var.ssh_private_key)}" } } @@ -66,7 +66,7 @@ resource "google_compute_instance" "yugabyte_node" { connection { type = "ssh" user = "${var.ssh_user}" - private_key = "${file(var.ssh_key_path)}" + private_key = "${file(var.ssh_private_key)}" } } provisioner "file" { @@ -75,7 +75,7 @@ resource "google_compute_instance" "yugabyte_node" { connection { type = "ssh" user = "${var.ssh_user}" - private_key = "${file(var.ssh_key_path)}" + private_key = "${file(var.ssh_private_key)}" } } provisioner "file" { @@ -84,7 +84,7 @@ resource "google_compute_instance" "yugabyte_node" { connection { type = "ssh" user = "${var.ssh_user}" - private_key = "${file(var.ssh_key_path)}" + private_key = "${file(var.ssh_private_key)}" } } provisioner "remote-exec" { @@ -98,7 +98,7 @@ resource "google_compute_instance" "yugabyte_node" { connection { type = "ssh" user = "${var.ssh_user}" - private_key = "${file(var.ssh_key_path)}" + private_key = "${file(var.ssh_private_key)}" } } } @@ -114,7 +114,7 @@ resource "null_resource" "create_yugabyte_universe" { depends_on = ["google_compute_instance.yugabyte_node"] provisioner "local-exec" { - command = "${path.module}/utilities/scripts/create_universe.sh 'GCP' '${var.region_name}' ${var.replication_factor} '${local.config_ip_list}' '${local.ssh_ip_list}' '${local.zone}' '${var.ssh_user}' ${var.ssh_key_path}" + command = "${path.module}/utilities/scripts/create_universe.sh 'GCP' '${var.region_name}' ${var.replication_factor} '${local.config_ip_list}' '${local.ssh_ip_list}' '${local.zone}' '${var.ssh_user}' ${var.ssh_private_key}" } } diff --git a/outputs.tf b/outputs.tf index 5e9659e..59bf234 100644 --- a/outputs.tf +++ b/outputs.tf @@ -4,5 +4,5 @@ output "ui" { } output "ssh_key" { sensitive = false - value = "${var.ssh_key_path}" + value = "${var.ssh_private_key}" } \ No newline at end of file diff --git a/variables.tf b/variables.tf index c023422..2792e5e 100644 --- a/variables.tf +++ b/variables.tf @@ -27,12 +27,12 @@ variable "vpc_firewall" { default = "default" type = "string" } -variable "ssh_key_path" { - description = "The public key to use when connecting to the instances." +variable "ssh_private_key" { + description = "The private key to use when connecting to the instances." type = "string" } -variable "ssh_pub_key" { - description = "SSH public key to be used by nodes" +variable "ssh_public_key" { + description = "SSH public key to be use when creating the instances." type = "string" } variable "ssh_user" { From 4db4ce1220e1a341ed10126e85ba37365589d45f Mon Sep 17 00:00:00 2001 From: Vaibhav Pathak Date: Thu, 20 Jun 2019 17:35:25 +0530 Subject: [PATCH 5/6] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 075f4a4..3473dea 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ A Terraform module to deploy and run YugaByte on Google Cloud. project = "yugabyte-pcf" } ``` - Note :- You can get credentials file by following steps given [here](https://cloud.google.com/docs/authentication/getting-started#auth-cloud-implicit-python) + Note :- You can get credentials file by following steps given [here](https://cloud.google.com/docs/authentication/getting-started) * Now add the yugabyte terraform module to your file ``` @@ -80,4 +80,4 @@ To destroy what we just created, you can run the following command. ``` $ terraform destroy ``` -`Note:- To make any changes in the created cluster you will need the terraform state files. So don't delete state files of Terraform.` \ No newline at end of file +`Note:- To make any changes in the created cluster you will need the terraform state files. So don't delete state files of Terraform.` From d2ce69a688935c9e3465586ced67d1ecef22d52a Mon Sep 17 00:00:00 2001 From: vp15591 Date: Fri, 5 Jul 2019 15:49:46 +0530 Subject: [PATCH 6/6] Fix for the issue while creating the universe for replication factor greater the 3 --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 616ba2c..051150d 100644 --- a/main.tf +++ b/main.tf @@ -30,7 +30,7 @@ resource "google_compute_instance" "yugabyte_node" { count = "${var.node_count}" name = "${var.prefix}${var.cluster_name}-n${format("%d", count.index + 1)}" machine_type = "${var.node_type}" - zone = "${data.google_compute_zones.available.names[count.index]}" + zone = "${element(data.google_compute_zones.available.names, count.index)}" tags=["${var.prefix}${var.cluster_name}"] boot_disk{