2022-10-04 04:39:10 -07:00
|
|
|
/**
|
|
|
|
* Copyright 2022 Google LLC
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
locals {
|
|
|
|
monitoring_agent_unit = <<-EOT
|
|
|
|
[Unit]
|
|
|
|
Description=Start monitoring agent container
|
|
|
|
After=gcr-online.target docker.socket
|
|
|
|
Wants=gcr-online.target docker.socket docker-events-collector.service
|
2022-10-07 01:53:53 -07:00
|
|
|
|
2022-10-04 04:39:10 -07:00
|
|
|
[Service]
|
|
|
|
Environment="HOME=/home/opsagent"
|
|
|
|
ExecStartPre=/usr/bin/docker-credential-gcr configure-docker
|
|
|
|
ExecStart=/usr/bin/docker run --rm --name=monitoring-agent \
|
|
|
|
--network host \
|
|
|
|
-v /etc/google-cloud-ops-agent/config.yaml:/etc/google-cloud-ops-agent/config.yaml \
|
|
|
|
${var.ops_agent_image}
|
|
|
|
ExecStop=/usr/bin/docker stop monitoring-agent
|
|
|
|
EOT
|
|
|
|
monitoring_agent_config = <<-EOT
|
|
|
|
logging:
|
|
|
|
service:
|
|
|
|
pipelines:
|
2022-10-07 01:53:53 -07:00
|
|
|
default_pipeline:
|
2022-10-04 04:39:10 -07:00
|
|
|
receivers: []
|
|
|
|
metrics:
|
|
|
|
receivers:
|
|
|
|
hostmetrics:
|
|
|
|
type: hostmetrics
|
|
|
|
nginx:
|
|
|
|
type: nginx
|
|
|
|
collection_interval: 10s
|
|
|
|
stub_status_url: http://localhost/healthz
|
|
|
|
service:
|
|
|
|
pipelines:
|
|
|
|
default_pipeline:
|
|
|
|
receivers:
|
|
|
|
- hostmetrics
|
|
|
|
- nginx
|
|
|
|
EOT
|
|
|
|
nginx_config = <<-EOT
|
|
|
|
server {
|
|
|
|
listen 80;
|
|
|
|
server_name HOSTNAME localhost;
|
|
|
|
%{if var.tls}
|
|
|
|
listen 443 ssl;
|
|
|
|
ssl_certificate /etc/ssl/self-signed.crt;
|
|
|
|
ssl_certificate_key /etc/ssl/self-signed.key;
|
|
|
|
%{endif}
|
|
|
|
|
|
|
|
keepalive_timeout 650s;
|
|
|
|
keepalive_requests 10000;
|
|
|
|
|
|
|
|
proxy_connect_timeout 60s;
|
|
|
|
proxy_read_timeout 5m;
|
|
|
|
proxy_send_timeout 5m;
|
|
|
|
|
|
|
|
error_log stderr;
|
|
|
|
access_log /dev/stdout combined;
|
|
|
|
|
2022-12-08 08:35:44 -08:00
|
|
|
set_real_ip_from ${module.glb.address}/32;
|
2022-10-04 04:39:10 -07:00
|
|
|
set_real_ip_from 35.191.0.0/16;
|
|
|
|
set_real_ip_from 130.211.0.0/22;
|
|
|
|
real_ip_header X-Forwarded-For;
|
|
|
|
real_ip_recursive off;
|
|
|
|
|
|
|
|
location /healthz {
|
|
|
|
stub_status on;
|
|
|
|
access_log off;
|
|
|
|
allow 127.0.0.1;
|
|
|
|
allow 35.191.0.0/16;
|
|
|
|
allow 130.211.0.0/22;
|
|
|
|
deny all;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_page 500 502 503 504 /50x.html;
|
|
|
|
location = /50x.html {
|
|
|
|
root /usr/share/nginx/html;
|
|
|
|
}
|
|
|
|
|
|
|
|
${var.backends}
|
|
|
|
}
|
|
|
|
EOT
|
|
|
|
nginx_files = {
|
|
|
|
"/etc/systemd/system/monitoring-agent.service" = {
|
|
|
|
content = local.monitoring_agent_unit
|
|
|
|
owner = "root"
|
|
|
|
permissions = "0644"
|
|
|
|
}
|
|
|
|
"/etc/nginx/conf.d/default.conf" = {
|
|
|
|
content = local.nginx_config
|
|
|
|
owner = "root"
|
|
|
|
permissions = "0644"
|
|
|
|
}
|
|
|
|
"/etc/google-cloud-ops-agent/config.yaml" = {
|
|
|
|
content = local.monitoring_agent_config
|
|
|
|
owner = "root"
|
|
|
|
permissions = "0644"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
users = [
|
|
|
|
{
|
|
|
|
username = "opsagent"
|
|
|
|
uid = 2001
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
module "project" {
|
|
|
|
source = "../../../modules/project"
|
2022-12-08 08:35:44 -08:00
|
|
|
billing_account = (
|
|
|
|
var.project_create != null
|
2022-10-04 04:39:10 -07:00
|
|
|
? var.project_create.billing_account_id
|
|
|
|
: null
|
|
|
|
)
|
|
|
|
name = var.project_name
|
|
|
|
parent = (var.project_create != null
|
|
|
|
? var.project_create.parent
|
|
|
|
: null
|
|
|
|
)
|
2022-12-08 08:35:44 -08:00
|
|
|
project_create = var.project_create != null
|
2022-10-04 04:39:10 -07:00
|
|
|
services = [
|
|
|
|
"cloudresourcemanager.googleapis.com",
|
|
|
|
"compute.googleapis.com",
|
|
|
|
"iam.googleapis.com",
|
|
|
|
"logging.googleapis.com",
|
|
|
|
"monitoring.googleapis.com",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
module "vpc" {
|
|
|
|
source = "../../../modules/net-vpc"
|
|
|
|
project_id = module.project.project_id
|
|
|
|
name = var.network
|
2022-12-08 08:35:44 -08:00
|
|
|
subnets = [{
|
|
|
|
name = var.subnetwork
|
|
|
|
ip_cidr_range = var.cidrs[var.subnetwork]
|
|
|
|
region = var.region
|
|
|
|
}]
|
2022-10-04 04:39:10 -07:00
|
|
|
vpc_create = var.network_create
|
|
|
|
}
|
|
|
|
|
|
|
|
module "firewall" {
|
|
|
|
source = "../../../modules/net-vpc-firewall"
|
|
|
|
project_id = module.project.project_id
|
|
|
|
network = module.vpc.name
|
2022-11-04 05:56:07 -07:00
|
|
|
ingress_rules = {
|
2022-11-23 02:09:00 -08:00
|
|
|
"${var.prefix}-allow-http-to-proxy-cluster" = {
|
2022-11-04 05:56:07 -07:00
|
|
|
description = "Allow Nginx HTTP(S) ingress traffic"
|
|
|
|
source_ranges = [
|
|
|
|
var.cidrs[var.subnetwork], "35.191.0.0/16", "130.211.0.0/22"
|
|
|
|
]
|
2022-10-04 04:39:10 -07:00
|
|
|
targets = [module.service-account-proxy.email]
|
|
|
|
use_service_accounts = true
|
|
|
|
rules = [{ protocol = "tcp", ports = [80, 443] }]
|
|
|
|
}
|
2022-11-23 02:09:00 -08:00
|
|
|
"${var.prefix}-allow-iap-ssh" = {
|
2022-10-04 04:39:10 -07:00
|
|
|
description = "Allow Nginx SSH traffic from IAP"
|
2022-11-04 05:56:07 -07:00
|
|
|
source_ranges = ["35.235.240.0/20"]
|
2022-10-04 04:39:10 -07:00
|
|
|
targets = [module.service-account-proxy.email]
|
|
|
|
use_service_accounts = true
|
|
|
|
rules = [{ protocol = "tcp", ports = [22] }]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module "nat" {
|
2022-12-08 08:35:44 -08:00
|
|
|
source = "../../../modules/net-cloudnat"
|
|
|
|
project_id = module.project.project_id
|
|
|
|
region = var.region
|
|
|
|
name = "${var.prefix}-nat"
|
2022-10-04 04:39:10 -07:00
|
|
|
config_min_ports_per_vm = 4000
|
2022-12-08 08:35:44 -08:00
|
|
|
config_source_subnets = "LIST_OF_SUBNETWORKS"
|
|
|
|
logging_filter = "ALL"
|
|
|
|
router_network = module.vpc.name
|
|
|
|
subnetworks = [{
|
|
|
|
self_link = (
|
|
|
|
module.vpc.subnet_self_links[format("%s/%s", var.region, var.subnetwork)]
|
|
|
|
)
|
|
|
|
config_source_ranges = ["ALL_IP_RANGES"]
|
|
|
|
secondary_ranges = null
|
|
|
|
}]
|
2022-10-04 04:39:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Proxy resources #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
module "service-account-proxy" {
|
|
|
|
source = "../../../modules/iam-service-account"
|
|
|
|
project_id = module.project.project_id
|
2022-11-23 02:09:00 -08:00
|
|
|
name = "${var.prefix}-reverse-proxy"
|
2022-10-04 04:39:10 -07:00
|
|
|
iam_project_roles = {
|
|
|
|
(module.project.project_id) = [
|
|
|
|
"roles/logging.logWriter",
|
|
|
|
"roles/monitoring.metricWriter",
|
|
|
|
"roles/storage.objectViewer", // For pulling the Ops Agent image
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module "cos-nginx" {
|
2022-10-07 01:53:53 -07:00
|
|
|
count = !var.tls ? 1 : 0
|
|
|
|
source = "../../../modules/cloud-config-container/nginx"
|
|
|
|
image = var.nginx_image
|
|
|
|
files = local.nginx_files
|
|
|
|
users = local.users
|
2022-10-04 04:39:10 -07:00
|
|
|
runcmd_pre = ["sed -i \"s/HOSTNAME/$${HOSTNAME}/\" /etc/nginx/conf.d/default.conf"]
|
|
|
|
runcmd_post = ["systemctl start monitoring-agent"]
|
|
|
|
}
|
|
|
|
|
|
|
|
module "cos-nginx-tls" {
|
2022-10-07 01:53:53 -07:00
|
|
|
count = var.tls ? 1 : 0
|
|
|
|
source = "../../../modules/cloud-config-container/nginx-tls"
|
2022-10-04 04:39:10 -07:00
|
|
|
nginx_image = var.nginx_image
|
|
|
|
files = local.nginx_files
|
|
|
|
users = local.users
|
|
|
|
runcmd_post = ["systemctl start monitoring-agent"]
|
|
|
|
}
|
|
|
|
|
|
|
|
module "mig-proxy" {
|
|
|
|
source = "../../../modules/compute-mig"
|
|
|
|
project_id = module.project.project_id
|
2022-10-07 01:53:53 -07:00
|
|
|
location = var.region
|
2022-11-23 02:09:00 -08:00
|
|
|
name = "${var.prefix}-proxy-cluster"
|
2022-10-04 04:39:10 -07:00
|
|
|
named_ports = {
|
|
|
|
http = "80"
|
|
|
|
https = "443"
|
|
|
|
}
|
|
|
|
autoscaler_config = var.autoscaling == null ? null : {
|
|
|
|
min_replicas = var.autoscaling.min_replicas
|
|
|
|
max_replicas = var.autoscaling.max_replicas
|
|
|
|
cooldown_period = var.autoscaling.cooldown_period
|
|
|
|
cpu_utilization_target = null
|
|
|
|
load_balancing_utilization_target = null
|
|
|
|
metric = var.autoscaling_metric
|
|
|
|
}
|
|
|
|
update_policy = {
|
2022-12-08 08:35:44 -08:00
|
|
|
minimal_action = "REPLACE"
|
|
|
|
type = "PROACTIVE"
|
|
|
|
min_ready_sec = 30
|
|
|
|
max_surge = {
|
|
|
|
fixed = 1
|
|
|
|
}
|
2022-10-04 04:39:10 -07:00
|
|
|
}
|
2022-12-08 08:35:44 -08:00
|
|
|
instance_template = module.proxy-vm.template.self_link
|
2022-10-04 04:39:10 -07:00
|
|
|
health_check_config = {
|
|
|
|
type = "http"
|
|
|
|
check = {
|
|
|
|
port = 80
|
|
|
|
request_path = "/healthz"
|
|
|
|
}
|
|
|
|
config = {
|
|
|
|
check_interval_sec = 10
|
|
|
|
healthy_threshold = 1
|
|
|
|
unhealthy_threshold = 1
|
|
|
|
timeout_sec = 10
|
|
|
|
}
|
|
|
|
logging = true
|
|
|
|
}
|
|
|
|
auto_healing_policies = {
|
|
|
|
health_check = module.mig-proxy.health_check.self_link
|
|
|
|
initial_delay_sec = 60
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module "proxy-vm" {
|
2022-10-07 01:53:53 -07:00
|
|
|
source = "../../../modules/compute-vm"
|
|
|
|
project_id = module.project.project_id
|
|
|
|
zone = format("%s-c", var.region)
|
|
|
|
name = "nginx-test-vm"
|
2022-10-04 04:39:10 -07:00
|
|
|
instance_type = "e2-standard-2"
|
2022-10-07 01:53:53 -07:00
|
|
|
tags = ["proxy-cluster"]
|
2022-10-04 04:39:10 -07:00
|
|
|
network_interfaces = [{
|
|
|
|
network = module.vpc.self_link
|
|
|
|
subnetwork = module.vpc.subnet_self_links[format("%s/%s", var.region, var.subnetwork)]
|
|
|
|
}]
|
|
|
|
boot_disk = {
|
2023-03-16 06:30:05 -07:00
|
|
|
initialize_params = {
|
|
|
|
image = "projects/cos-cloud/global/images/family/cos-stable"
|
|
|
|
}
|
2022-10-04 04:39:10 -07:00
|
|
|
}
|
|
|
|
create_template = true
|
|
|
|
metadata = {
|
2022-11-15 04:19:52 -08:00
|
|
|
user-data = !var.tls ? module.cos-nginx.0.cloud_config : module.cos-nginx-tls.0.cloud_config
|
|
|
|
google-logging-enabled = true
|
2022-10-04 04:39:10 -07:00
|
|
|
}
|
|
|
|
service_account = module.service-account-proxy.email
|
|
|
|
service_account_create = false
|
|
|
|
}
|
|
|
|
|
2022-12-08 08:35:44 -08:00
|
|
|
module "glb" {
|
|
|
|
source = "../../../modules/net-glb"
|
|
|
|
project_id = module.project.project_id
|
|
|
|
name = "${var.prefix}-reverse-proxy-glb"
|
|
|
|
health_check_configs = {
|
|
|
|
default = {
|
|
|
|
check_interval_sec = 10
|
|
|
|
healthy_threshold = 1
|
|
|
|
unhealthy_threshold = 1
|
|
|
|
timeout_sec = 10
|
|
|
|
http = {
|
2022-10-04 04:39:10 -07:00
|
|
|
port_specification = "USE_NAMED_PORT"
|
|
|
|
port_name = "http"
|
|
|
|
request_path = "/healthz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 08:35:44 -08:00
|
|
|
backend_service_configs = {
|
|
|
|
default = {
|
|
|
|
backends = [{ backend = module.mig-proxy.group_manager.instance_group }]
|
|
|
|
port_name = !var.tls ? "http" : "https"
|
|
|
|
protocol = !var.tls ? "HTTP" : "HTTPS"
|
2022-10-04 04:39:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|