Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Added load balancing application in terraform #150

Merged
merged 9 commits into from
Jun 25, 2024
25 changes: 25 additions & 0 deletions terraform_provisioning/README.md
max-amb marked this conversation as resolved.
Show resolved Hide resolved
max-amb marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@
* This folder contains terraform scripts to manage openstack configurations
* These require a `clouds.yaml` in `~/.config/openstack/` to provide authentication

## Running the scripts
* The scripts require the terraform package to be installed
* To install terraform on ubuntu or any ubuntu derivative with snap installed:
```bash
snap install terraform --classic
```
* If you are confused please find further info [here](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli)
* To actually run the script, start by entering the directory of the script you want to run, e.g.:
```bash
cd load_balancing_application
```
* Then generate a plan, this will also tell you what actions terraform will perform:
* Note this will generate a plan file in the current directory
```bash
terraform plan -out plan --var-file=vars.tfvars
```
* After that, to actually "apply" the changes planned in the plan:
```bash
terraform apply plan
```
* To revert the changes, e.g. to change the number of instances, use:
```bash
terraform destroy --var-file=vars.tfvars
```

## private_network
* This terraform script creates a private network with a subnet domain of `10.0.0.x`
* It also adds a router to connect the private network to the external network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ provider "openstack" {

# Creates a private network
resource "openstack_networking_network_v2" "private_network" {
name = "private_network"
name = "${var.deployment_name}-private_network"
admin_state_up = "true"
}

# Creates a subnet within our private network
resource "openstack_networking_subnet_v2" "subnet" {
name = "subnet"
name = "${var.deployment_name}-subnet"
network_id = openstack_networking_network_v2.private_network.id
cidr = "192.168.3.0/24"
cidr = "192.168.1.0/24"
ip_version = 4
}

# Creates a router within our network
resource "openstack_networking_router_v2" "router" {
name = "${var.deployment_name}-router"
external_network_id = var.external
external_network_id = var.external_network_id
}

# Connects the router to our subnet
Expand All @@ -42,17 +42,17 @@ resource "openstack_networking_router_interface_v2" "router_interface" {

# Create a load balancer and places it in the subnet we made earlier
resource "openstack_lb_loadbalancer_v2" "loadbalancer" {
name = "loadbalancer"
name = "${var.deployment_name}-loadbalancer"
vip_subnet_id = openstack_networking_subnet_v2.subnet.id
vip_address = var.floating_ip
}

# Creating the bastion vm to access the other vm's
resource "openstack_compute_instance_v2" "bastion" {
name = "${var.deployment_name}-bastion"
image_name = var.image_name
flavor_name = var.flavor_name
key_pair = var.key_pair
name = "bastion"

network {
uuid = openstack_networking_network_v2.private_network.id
Expand All @@ -61,7 +61,7 @@ resource "openstack_compute_instance_v2" "bastion" {

# Create a ssh listener for the bastion vm
resource "openstack_lb_listener_v2" "ssh_listener" {
name = "bastion-ssh"
name = "${var.deployment_name}-bastion-ssh"
protocol = "TCP"
protocol_port = 22
loadbalancer_id = openstack_lb_loadbalancer_v2.loadbalancer.id
Expand All @@ -72,23 +72,23 @@ resource "openstack_lb_listener_v2" "ssh_listener" {

# Create an ssh listener for the web servers
resource "openstack_lb_listener_v2" "web_server_listener" {
name = "web_servers"
name = "${var.deployment_name}-web_servers"
protocol = "TCP"
protocol_port = 80
loadbalancer_id = openstack_lb_loadbalancer_v2.loadbalancer.id
}

# Creating the ssh pool
resource "openstack_lb_pool_v2" "ssh_pool" {
name = "ssh-pool"
name = "${var.deployment_name}-ssh-pool"
protocol = "TCP"
lb_method = "SOURCE_IP"
listener_id = openstack_lb_listener_v2.ssh_listener.id
}

# Creating a web_servers pool
resource "openstack_lb_pool_v2" "web_servers_pool" {
name = "web_servers_pool"
name = "${var.deployment_name}-web_servers_pool"
protocol = "TCP"
lb_method = "SOURCE_IP"
listener_id = openstack_lb_listener_v2.web_server_listener.id
Expand All @@ -112,11 +112,11 @@ resource "openstack_lb_member_v2" "web_server_member" {

# Create multiple vm's for web serving
resource "openstack_compute_instance_v2" "vm" {
name = format("${var.deployment_name}-vm-%d", count.index)
count = var.instances
image_name = var.image_name
flavor_name = var.flavor_name
key_pair = var.key_pair
name = format("vm-%d", count.index)

network {
uuid = openstack_networking_network_v2.private_network.id
Expand Down
12 changes: 6 additions & 6 deletions terraform_provisioning/load_balancing_application/variables.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
variable "external" {
description = "The id of the external network to connect the router to"
type = string
}

variable "deployment_name" {
description = "The name of the deployment (prepended to router name)"
description = "The name of the deployment (prepended to things)"
type = string
max-amb marked this conversation as resolved.
Show resolved Hide resolved
}

variable "external_network_id" {
description = "The id of the external network to connect the router to"
type = string
}

variable "floating_ip" {
description = "The floating ip to attach to the load balancer to access the grafanas"
type = string
Expand Down
8 changes: 4 additions & 4 deletions terraform_provisioning/load_balancing_application/vars.tfvars
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
external = ""
deployment_name = ""
external_network_id = ""
floating_ip = ""
max-amb marked this conversation as resolved.
Show resolved Hide resolved
image_name = "ubuntu-focal-20.04-nogui"
flavor_name = "l3.nano"
image_name = ""
flavor_name = ""
key_pair = ""
max-amb marked this conversation as resolved.
Show resolved Hide resolved
instances = 1
instances =
Loading