목표
load balancing modules
regional TCP load balancer
regional internal TCP load balancer
global HTTP load balancer with Kubernetes Engine
global HTTPS content-based load balancer
1
gcloud auth list
2
gcloud config list project
1
export GOOGLE_PROJECT=$(gcloud config get-value project)
2
$ ls *.tf
locals.tf main.tf network.tf outputs.tf provider.tf variables.tf versions.tf
$ more *.tf
::::::::::::::
locals.tf
::::::::::::::
locals {
health_check = {
check_interval_sec = 1
healthy_threshold = 4
timeout_sec = 1
unhealthy_threshold = 5
port = 8080
request_path = "/mypath"
host = "1.2.3.4"
}
}
::::::::::::::
main.tf
data "template_file" "instance_startup_script" {
template = file("${path.module}/templates/gceme.sh.tpl")
vars = {
PROXY_PATH = ""
}
}
resource "google_service_account" "instance-group" {
account_id = "instance-group"
}
module "instance_template" {
source = "terraform-google-modules/vm/google//modules/instance_template"
version = "~> 8.0"
subnetwork = google_compute_subnetwork.subnetwork.self_link
source_image_family = var.image_family
source_image_project = var.image_project
startup_script = data.template_file.instance_startup_script.rendered
service_account = {
email = google_service_account.instance-group.email
scopes = ["cloud-platform"]
}
}
module "managed_instance_group" {
source = "terraform-google-modules/vm/google//modules/mig"
version = "~> 8.0"
region = var.region
target_size = 2
hostname = "mig-simple"
instance_template = module.instance_template.self_link
target_pools = [
module.load_balancer_default.target_pool,
module.load_balancer_no_hc.target_pool,
module.load_balancer_custom_hc.target_pool
]
named_ports = [{
name = "http"
port = 80
}]
}
module "load_balancer_default" {
name = "basic-load-balancer-default"
source = "../../"
region = var.region
service_port = 80
network = google_compute_network.network.name
target_service_accounts = [google_service_account.instance-group.email]
}
module "load_balancer_no_hc" {
name = "basic-load-balancer-no-hc"
source = "../../"
region = var.region
service_port = 80
network = google_compute_network.network.name
disable_health_check = true
target_service_accounts = [google_service_account.instance-group.email]
}
module "load_balancer_custom_hc" {
name = "basic-load-balancer-custom-hc"
source = "../../"
region = var.region
service_port = 8080
network = google_compute_network.network.name
health_check = local.health_check
target_service_accounts = [google_service_account.instance-group.email]
}
::::::::::::::
network.tf
::::::::::::::
resource "google_compute_network" "network" {
name = "load-balancer-module-network"
auto_create_subnetworks = "false"
}
resource "google_compute_subnetwork" "subnetwork" {
name = "load-balancer-module-subnetwork"
region = var.region
network = google_compute_network.network.self_link
ip_cidr_range = "10.0.0.0/16"
}
resource "google_compute_router" "router" {
name = "load-balancer-module-router"
region = var.region
network = google_compute_network.network.self_link
}
module "cloud_nat" {
project_id = var.project_id
region = var.region
name = "load-balancer-module-nat"
source = "terraform-google-modules/cloud-nat/google"
version = "~> 2.2"
router = google_compute_router.router.name
}
::::::::::::::
outputs.tf
::::::::::::::
output "load_balancer_default_ip" {
description = "The external ip address of the forwarding rule for default lb."
value = module.load_balancer_default.external_ip
}
::::::::::::::
provider.tf
::::::::::::::
provider "google" {
project = var.project_id
}
provider "google-beta" {
project = var.project_id
}
::::::::::::::
variables.tf
::::::::::::::
variable "region" {
default = "us-central1"
}
variable "project_id" {
description = "GCP Project used to create resources."
}
variable "image_family" {
description = "Image used for compute VMs."
default = "debian-11"
}
variable "image_project" {
description = "GCP Project where source image comes from."
default = "debian-cloud"
}
::::::::::::::
versions.tf
::::::::::::::
terraform {
required_version = ">= 0.13"
required_providers {
google = {
source = "hashicorp/google"
version = ">= 3.53, < 5.0"
}
google-beta = {
source = "hashicorp/google-beta"
version = ">= 3.53, < 5.0"
}
template = {
source = "hashicorp/template"
}
}
}
3
terraform init
4
terraform plan
5
terraform apply
6
콘솔에서 보기
7
EXTERNAL_IP=$(terraform output | grep load_balancer_default_ip | cut -d = -f2 | xargs echo -n)
8
echo "
http://${EXTERNAL_IP}
"
다음과정
https://brunch.co.kr/@topasvga/3393
전체보기
https://brunch.co.kr/@topasvga/3394
감사합니다.