mirror of
https://github.com/kelseyhightower/kubernetes-the-hard-way.git
synced 2025-12-17 02:08:58 +03:00
add codes
This commit is contained in:
131
codes/kubernetes/terraform/gcp/main.tf
Normal file
131
codes/kubernetes/terraform/gcp/main.tf
Normal file
@@ -0,0 +1,131 @@
|
||||
# networks
|
||||
resource "google_compute_network" "vnet" {
|
||||
name = "${var.environment}-vnet"
|
||||
auto_create_subnetworks = false
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "subnet" {
|
||||
name = "container"
|
||||
ip_cidr_range = var.address_prefix
|
||||
region = var.region
|
||||
network = google_compute_network.vnet.id
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "internal" {
|
||||
name = "internal"
|
||||
network = google_compute_network.vnet.id
|
||||
|
||||
allow {
|
||||
protocol = "icmp"
|
||||
}
|
||||
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
}
|
||||
allow {
|
||||
protocol = "udp"
|
||||
}
|
||||
|
||||
source_ranges = var.internal_cidr
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "external" {
|
||||
name = "external"
|
||||
network = google_compute_network.vnet.id
|
||||
|
||||
allow {
|
||||
protocol = "icmp"
|
||||
}
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
ports = ["22", "6443"]
|
||||
}
|
||||
source_ranges = var.external_cidr
|
||||
}
|
||||
|
||||
resource "google_compute_address" "extip" {
|
||||
name = "external-ip"
|
||||
region = var.region
|
||||
}
|
||||
|
||||
# Compute instances (we use instance template here)
|
||||
data "google_compute_image" "ubuntu" {
|
||||
family = "ubuntu-2004-lts"
|
||||
project = "ubuntu-os-cloud"
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "controller" {
|
||||
count = var.controller_count
|
||||
name = "${var.environment}-controller-${count.index}"
|
||||
machine_type = var.vm_size
|
||||
zone = var.zone
|
||||
can_ip_forward = true
|
||||
|
||||
network_interface {
|
||||
network = google_compute_network.vnet.self_link
|
||||
subnetwork = google_compute_subnetwork.subnet.name
|
||||
network_ip = element(var.controller_ip_list, count.index)
|
||||
# we dont have enough quota for external ip address
|
||||
# access_config {}
|
||||
}
|
||||
|
||||
boot_disk {
|
||||
initialize_params {
|
||||
image = data.google_compute_image.ubuntu.self_link
|
||||
size = var.boot_disk_size
|
||||
type = var.boot_disk_type
|
||||
}
|
||||
}
|
||||
|
||||
service_account {
|
||||
scopes = var.controller_scopes
|
||||
}
|
||||
|
||||
# resize VM after initial creation
|
||||
allow_stopping_for_update = true
|
||||
|
||||
description = "kubernetes Controller Nodes"
|
||||
|
||||
tags = var.controller_node_tags
|
||||
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "worker" {
|
||||
count = var.worker_count
|
||||
name = "${var.environment}-worker-${count.index}"
|
||||
machine_type = var.vm_size
|
||||
zone = var.zone
|
||||
can_ip_forward = true
|
||||
|
||||
network_interface {
|
||||
network = google_compute_network.vnet.self_link
|
||||
subnetwork = google_compute_subnetwork.subnet.name
|
||||
network_ip = element(var.worker_ip_list, count.index)
|
||||
# we dont have enough quota for external ip address
|
||||
# access_config {}
|
||||
}
|
||||
|
||||
boot_disk {
|
||||
initialize_params {
|
||||
image = data.google_compute_image.ubuntu.self_link
|
||||
size = var.boot_disk_size
|
||||
type = var.boot_disk_type
|
||||
}
|
||||
}
|
||||
|
||||
metadata = {
|
||||
pod-cidr = element(var.pod_address_prefix, count.index)
|
||||
}
|
||||
|
||||
service_account {
|
||||
scopes = var.worker_scopes
|
||||
}
|
||||
|
||||
# resize VM after initial creation
|
||||
allow_stopping_for_update = true
|
||||
|
||||
description = "kubernetes Worker Nodes"
|
||||
|
||||
tags = var.worker_node_tags
|
||||
|
||||
}
|
||||
7
codes/kubernetes/terraform/gcp/output.tf
Normal file
7
codes/kubernetes/terraform/gcp/output.tf
Normal file
@@ -0,0 +1,7 @@
|
||||
output "controller_private_ip" {
|
||||
value = google_compute_instance.controller.*.network_interface.0.network_ip
|
||||
}
|
||||
|
||||
output "worker_private_ip" {
|
||||
value = google_compute_instance.worker.*.network_interface.0.network_ip
|
||||
}
|
||||
87
codes/kubernetes/terraform/gcp/variables.tf
Normal file
87
codes/kubernetes/terraform/gcp/variables.tf
Normal file
@@ -0,0 +1,87 @@
|
||||
variable "environment" {
|
||||
description = "Name of this lab"
|
||||
}
|
||||
|
||||
variable "address_prefix" {
|
||||
description = "Network CIDR"
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
description = "Region of this lab"
|
||||
}
|
||||
|
||||
variable "zone" {
|
||||
description = "Zone of VM"
|
||||
}
|
||||
|
||||
variable "internal_cidr" {
|
||||
description = "CIDR Allowed internal"
|
||||
}
|
||||
|
||||
variable "external_cidr" {
|
||||
description = "CIDR Allowed external"
|
||||
}
|
||||
|
||||
variable "vm_size" {
|
||||
description = "The machine type to create."
|
||||
}
|
||||
|
||||
variable "boot_disk_type" {
|
||||
description = "The GCE disk type. Can be either pd-ssd, local-ssd, or pd-standard"
|
||||
default = "pd-standard"
|
||||
}
|
||||
|
||||
variable "boot_disk_size" {
|
||||
type = number
|
||||
description = "The size of the image in gigabytes"
|
||||
default = 200
|
||||
}
|
||||
|
||||
variable "controller_count" {
|
||||
type = number
|
||||
description = "Number of controller nodes"
|
||||
default = 3
|
||||
}
|
||||
|
||||
variable "worker_count" {
|
||||
type = number
|
||||
description = "Number of worker nodes"
|
||||
default = 3
|
||||
}
|
||||
|
||||
variable "controller_ip_list" {
|
||||
type = list(string)
|
||||
description = "list of controller ip"
|
||||
}
|
||||
|
||||
variable "worker_ip_list" {
|
||||
type = list(string)
|
||||
description = "list of worker ip"
|
||||
}
|
||||
|
||||
variable "controller_scopes" {
|
||||
type = list(string)
|
||||
description = "Scopes of controller Nodes"
|
||||
default = ["compute-rw", "storage-ro", "service-management", "service-control", "logging-write", "monitoring"]
|
||||
}
|
||||
|
||||
variable "worker_scopes" {
|
||||
type = list(string)
|
||||
description = "Scopes of Worker Nodes"
|
||||
default = ["compute-rw", "storage-ro", "service-management", "service-control", "logging-write", "monitoring"]
|
||||
}
|
||||
|
||||
variable "controller_node_tags" {
|
||||
type = list(string)
|
||||
description = "A list of network tags to attach to the instance."
|
||||
}
|
||||
|
||||
variable "worker_node_tags" {
|
||||
type = list(string)
|
||||
description = "A list of network tags to attach to the instance."
|
||||
}
|
||||
|
||||
variable "pod_address_prefix" {
|
||||
type = list(string)
|
||||
description = "Pod Address Space prefix"
|
||||
}
|
||||
Reference in New Issue
Block a user