Added example for globaly load balanced scale sets (#14912)
This commit is contained in:
parent
423bfe0298
commit
40ba1b5487
|
@ -0,0 +1,27 @@
|
|||
# Azure traffic manager with load balanced scale sets
|
||||
|
||||
This example shows how to create a load balanced scale set in multiple locations and then geographically load balance these using traffic manager. This example the scale set uses a market place Ubuntu image, this could be customised using an extension or a generalized image created using packer.
|
||||
|
||||
This script demonstrates how variable can be passed in and out of reusable modules. You will need to run `terraform get` for terrafrom to get so that modules are pre-processed.
|
||||
|
||||
## Keys and variables
|
||||
|
||||
To use this you will need to populate the `terraform.tfvars.example` file with your Azure credentials and key. Rename this to `terraform.tfvars` and copy this somewhere private. If you need to generate credentials follow the instructions on the Azure provider documented [here](https://www.terraform.io/docs/providers/azurerm)
|
||||
|
||||
You may also want to modify some of the settings in `variables.tf`, DNS names must be unique within an Azure location and globally for traffic management
|
||||
|
||||
## To start the script
|
||||
|
||||
### Planning
|
||||
|
||||
`terraform get`
|
||||
|
||||
`terraform plan -var-file="C:\Users\eltimmo\.terraform\keys.tfvars"`
|
||||
|
||||
### Apply phase
|
||||
|
||||
`terraform apply -var-file="C:\Users\eltimmo\.terraform\keys.tfvars"`
|
||||
|
||||
### Destroy
|
||||
|
||||
`terraform destroy -var-file="C:\Users\eltimmo\.terraform\keys.tfvars"`
|
|
@ -0,0 +1,102 @@
|
|||
# Provider accounts must be passed
|
||||
|
||||
variable "subscription_id" {}
|
||||
variable "client_id" {}
|
||||
variable "client_secret" {}
|
||||
variable "tenant_id" {}
|
||||
|
||||
provider "azurerm" {
|
||||
subscription_id = "${var.subscription_id}"
|
||||
client_id = "${var.client_id}"
|
||||
client_secret = "${var.client_secret}"
|
||||
tenant_id = "${var.tenant_id}"
|
||||
}
|
||||
|
||||
# Create the resource group and assets for first location
|
||||
module "location01" {
|
||||
source = "./tf_modules"
|
||||
|
||||
location = "${var.location01_location}"
|
||||
resource_prefix = "${var.location01_resource_prefix}"
|
||||
webserver_prefix = "${var.location01_webserver_prefix}"
|
||||
lb_dns_label = "${var.location01_lb_dns_label}"
|
||||
|
||||
instance_count = "${var.instance_count}"
|
||||
instance_vmprofile = "${var.instance_vmprofile}"
|
||||
|
||||
image_admin_username = "${var.image_admin_username}"
|
||||
image_admin_password = "${var.image_admin_password}"
|
||||
|
||||
image_publisher = "${var.image_publisher}"
|
||||
image_offer = "${var.image_offer}"
|
||||
image_sku = "${var.image_sku}"
|
||||
image_version = "${var.image_version}"
|
||||
|
||||
}
|
||||
|
||||
# Create the resource group and assets for second location
|
||||
module "location02" {
|
||||
source = "./tf_modules"
|
||||
|
||||
location = "${var.location02_location}"
|
||||
resource_prefix = "${var.location02_resource_prefix}"
|
||||
webserver_prefix = "${var.location02_webserver_prefix}"
|
||||
lb_dns_label = "${var.location02_lb_dns_label}"
|
||||
|
||||
instance_count = "${var.instance_count}"
|
||||
instance_vmprofile = "${var.instance_vmprofile}"
|
||||
|
||||
image_admin_username = "${var.image_admin_username}"
|
||||
image_admin_password = "${var.image_admin_password}"
|
||||
|
||||
image_publisher = "${var.image_publisher}"
|
||||
image_offer = "${var.image_offer}"
|
||||
image_sku = "${var.image_sku}"
|
||||
image_version = "${var.image_version}"
|
||||
|
||||
}
|
||||
|
||||
# Create global resource group
|
||||
resource "azurerm_resource_group" "global_rg" {
|
||||
name = "global_rg"
|
||||
location = "${var.global_location}"
|
||||
}
|
||||
|
||||
# Create the traffic manager
|
||||
resource "azurerm_traffic_manager_profile" "trafficmanagerhttp" {
|
||||
name = "trafficmanagerhttp"
|
||||
resource_group_name = "${azurerm_resource_group.global_rg.name}"
|
||||
|
||||
traffic_routing_method = "Weighted"
|
||||
|
||||
dns_config {
|
||||
relative_name = "${var.dns_relative_name}"
|
||||
ttl = 100
|
||||
}
|
||||
|
||||
monitor_config {
|
||||
protocol = "http"
|
||||
port = 80
|
||||
path = "/"
|
||||
}
|
||||
}
|
||||
|
||||
# Add endpoint mappings to traffic manager, location01
|
||||
resource "azurerm_traffic_manager_endpoint" "trafficmanagerhttp_01" {
|
||||
name = "trafficmanagerhttp_ukw"
|
||||
resource_group_name = "${azurerm_resource_group.global_rg.name}"
|
||||
profile_name = "${azurerm_traffic_manager_profile.trafficmanagerhttp.name}"
|
||||
target_resource_id = "${module.location01.webserverpublic_ip_id}"
|
||||
type = "azureEndpoints"
|
||||
weight = 100
|
||||
}
|
||||
|
||||
# Add endpoint mappings to traffic manager, location02
|
||||
resource "azurerm_traffic_manager_endpoint" "trafficmanagerhttp_02" {
|
||||
name = "trafficmanagerhttp_wus"
|
||||
resource_group_name = "${azurerm_resource_group.global_rg.name}"
|
||||
profile_name = "${azurerm_traffic_manager_profile.trafficmanagerhttp.name}"
|
||||
target_resource_id = "${module.location02.webserverpublic_ip_id}"
|
||||
type = "azureEndpoints"
|
||||
weight = 100
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
# Azure provide keys example. This should not be included in your repository for security reasons
|
||||
# Use terrafrom -var-file="FULLPATH"
|
||||
|
||||
subscription_id = ""
|
||||
client_id = ""
|
||||
client_secret = ""
|
||||
tenant_id = ""
|
|
@ -0,0 +1,165 @@
|
|||
variable "location" {}
|
||||
variable "resource_prefix" {}
|
||||
variable "webserver_prefix" {}
|
||||
variable "lb_dns_label" {}
|
||||
|
||||
variable "instance_count" {}
|
||||
variable "instance_vmprofile" {}
|
||||
|
||||
variable "image_admin_username" {}
|
||||
variable "image_admin_password" {}
|
||||
|
||||
variable "image_publisher" {}
|
||||
variable "image_offer" {}
|
||||
variable "image_sku" {}
|
||||
variable "image_version" {}
|
||||
|
||||
# Create webserver resource group
|
||||
resource "azurerm_resource_group" "webservers_rg" {
|
||||
name = "${var.resource_prefix}_rg"
|
||||
location = "${var.location}"
|
||||
}
|
||||
|
||||
# Create virtual network
|
||||
resource "azurerm_virtual_network" "webservers_vnet" {
|
||||
name = "webservers_vnet"
|
||||
address_space = ["10.1.0.0/24"]
|
||||
location = "${var.location}"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
}
|
||||
|
||||
# Create subnet
|
||||
resource "azurerm_subnet" "webservers_subnet" {
|
||||
name = "webservers_subnet"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
virtual_network_name = "${azurerm_virtual_network.webservers_vnet.name}"
|
||||
address_prefix = "10.1.0.0/24"
|
||||
}
|
||||
|
||||
# Create a public ip for the location LB
|
||||
resource "azurerm_public_ip" "webserverpublic_ip" {
|
||||
name = "${var.resource_prefix}_publicip"
|
||||
location = "${var.location}"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
public_ip_address_allocation = "static"
|
||||
domain_name_label = "${var.lb_dns_label}"
|
||||
}
|
||||
|
||||
# Create webservers LB
|
||||
resource "azurerm_lb" "webservers_lb" {
|
||||
name = "webservers_lb"
|
||||
location = "${var.location}"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
|
||||
frontend_ip_configuration {
|
||||
name = "webserverpublic_ip"
|
||||
public_ip_address_id = "${azurerm_public_ip.webserverpublic_ip.id}"
|
||||
}
|
||||
}
|
||||
|
||||
# Add the backend for webserver LB
|
||||
resource "azurerm_lb_backend_address_pool" "webservers_lb_backend" {
|
||||
name = "webservers_lb_backend"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
loadbalancer_id = "${azurerm_lb.webservers_lb.id}"
|
||||
}
|
||||
|
||||
# Create HTTP probe on port 80
|
||||
resource "azurerm_lb_probe" "httpprobe" {
|
||||
name = "httpprobe"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
loadbalancer_id = "${azurerm_lb.webservers_lb.id}"
|
||||
protocol = "tcp"
|
||||
port = 80
|
||||
}
|
||||
|
||||
# Create LB rule for HTTP and add to webserver LB
|
||||
resource "azurerm_lb_rule" "webservers_lb_http" {
|
||||
name = "webservers_lb_http"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
loadbalancer_id = "${azurerm_lb.webservers_lb.id}"
|
||||
protocol = "Tcp"
|
||||
frontend_port = "80"
|
||||
backend_port = "80"
|
||||
frontend_ip_configuration_name = "webserverpublic_ip"
|
||||
probe_id = "${azurerm_lb_probe.httpprobe.id}"
|
||||
backend_address_pool_id = "${azurerm_lb_backend_address_pool.webservers_lb_backend.id}"
|
||||
}
|
||||
|
||||
# Create storage account
|
||||
resource "azurerm_storage_account" "webservers_sa" {
|
||||
name = "${var.resource_prefix}storage"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
location = "${var.location}"
|
||||
account_type = "Standard_LRS"
|
||||
}
|
||||
|
||||
# Create container
|
||||
resource "azurerm_storage_container" "webservers_ct" {
|
||||
name = "vhds"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
storage_account_name = "${azurerm_storage_account.webservers_sa.name}"
|
||||
container_access_type = "private"
|
||||
}
|
||||
|
||||
# Configure the scale set using library image
|
||||
resource "azurerm_virtual_machine_scale_set" "webserver_ss" {
|
||||
name = "webserver_ss"
|
||||
location = "${var.location}"
|
||||
resource_group_name = "${azurerm_resource_group.webservers_rg.name}"
|
||||
upgrade_policy_mode = "Manual"
|
||||
|
||||
sku {
|
||||
name = "${var.instance_vmprofile}"
|
||||
tier = "Standard"
|
||||
capacity = "${var.instance_count}"
|
||||
}
|
||||
|
||||
os_profile {
|
||||
computer_name_prefix = "${var.webserver_prefix}"
|
||||
admin_username = "${var.image_admin_username}"
|
||||
admin_password = "${var.image_admin_password}"
|
||||
}
|
||||
|
||||
os_profile_linux_config {
|
||||
disable_password_authentication = false
|
||||
}
|
||||
|
||||
network_profile {
|
||||
name = "web_ss_net_profile"
|
||||
primary = true
|
||||
|
||||
ip_configuration {
|
||||
name = "web_ss_ip_profile"
|
||||
subnet_id = "${azurerm_subnet.webservers_subnet.id}"
|
||||
load_balancer_backend_address_pool_ids = ["${azurerm_lb_backend_address_pool.webservers_lb_backend.id}"]
|
||||
}
|
||||
}
|
||||
|
||||
storage_profile_os_disk {
|
||||
name = "osDiskProfile"
|
||||
caching = "ReadWrite"
|
||||
create_option = "FromImage"
|
||||
vhd_containers = ["${azurerm_storage_account.webservers_sa.primary_blob_endpoint}${azurerm_storage_container.webservers_ct.name}"]
|
||||
}
|
||||
|
||||
storage_profile_image_reference {
|
||||
publisher = "${var.image_publisher}"
|
||||
offer = "${var.image_offer}"
|
||||
sku = "${var.image_sku}"
|
||||
version = "${var.image_version}"
|
||||
}
|
||||
|
||||
extension {
|
||||
name = "CustomScriptForLinux"
|
||||
publisher = "Microsoft.OSTCExtensions"
|
||||
type = "CustomScriptForLinux"
|
||||
type_handler_version = "1.4"
|
||||
settings = <<SETTINGS
|
||||
{
|
||||
"commandToExecute" : "sudo apt-get -y install apache2"
|
||||
}
|
||||
SETTINGS
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
# Output public IP ID (Load Balancer) for traffic manager
|
||||
|
||||
output "webserverpublic_ip_id" {
|
||||
value = "${azurerm_public_ip.webserverpublic_ip.id}"
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
# Traffic manager settings
|
||||
|
||||
variable "global_location" {
|
||||
default = "UK West"
|
||||
description = "Where any global resources will be placed"
|
||||
}
|
||||
|
||||
variable "dns_relative_name" {
|
||||
default = "azuretfexample"
|
||||
description = "Relative DNS name for traffic manager"
|
||||
}
|
||||
|
||||
# Location 01 Settings
|
||||
|
||||
variable "location01_location" {
|
||||
default = "UK West"
|
||||
description = "First location to build"
|
||||
}
|
||||
|
||||
variable "location01_resource_prefix" {
|
||||
default = "ukwestweb"
|
||||
description = "Prefix for naming resource group"
|
||||
}
|
||||
|
||||
variable "location01_webserver_prefix" {
|
||||
default = "ukwwebsvr"
|
||||
description = "Prefix for naming web servers"
|
||||
}
|
||||
|
||||
variable "location01_lb_dns_label" {
|
||||
default = "ukwestwebexample"
|
||||
description = "DNS name label for the locations load balancer"
|
||||
}
|
||||
|
||||
# Location 02 Settings
|
||||
|
||||
variable "location02_location" {
|
||||
default = "West US"
|
||||
description = "Second location to build"
|
||||
}
|
||||
|
||||
variable "location02_resource_prefix" {
|
||||
default = "uswestweb"
|
||||
description = "Prefix for naming resource group"
|
||||
}
|
||||
|
||||
variable "location02_webserver_prefix" {
|
||||
default = "uswwebsvr"
|
||||
description = "Prefix for naming web servers"
|
||||
}
|
||||
|
||||
variable "location02_lb_dns_label" {
|
||||
default = "uswestwebexample"
|
||||
description = "DNS name label for the locations load balancer"
|
||||
}
|
||||
|
||||
# Scale set and VM settings
|
||||
|
||||
variable "instance_count" {
|
||||
default = "2"
|
||||
description = "Number of server instances to create in scale set"
|
||||
}
|
||||
|
||||
variable "instance_vmprofile" {
|
||||
default = "Standard_A1"
|
||||
description = "VM profile of servers in scale set"
|
||||
}
|
||||
|
||||
# OS Profile
|
||||
|
||||
variable "image_admin_username" {
|
||||
default = "webadmin"
|
||||
description = "Local admin user name"
|
||||
}
|
||||
|
||||
variable "image_admin_password" {
|
||||
default = "2nmn39x#3775hh3x9"
|
||||
description = "Password"
|
||||
}
|
||||
|
||||
# Market place image to use
|
||||
|
||||
variable "image_publisher" {
|
||||
default = "Canonical"
|
||||
description = "Publisher of market place image"
|
||||
}
|
||||
variable "image_offer" {
|
||||
default = "UbuntuServer"
|
||||
description = "Market place image name"
|
||||
}
|
||||
variable "image_sku" {
|
||||
default = "16.10"
|
||||
description = "Market place image SKU"
|
||||
}
|
||||
variable "image_version" {
|
||||
default = "latest"
|
||||
description = "Market place image version"
|
||||
}
|
Loading…
Reference in New Issue