From 9410f7df20ccfc1f1f53ad6a175dd177b84424a0 Mon Sep 17 00:00:00 2001 From: dannc Date: Tue, 22 Nov 2022 10:45:30 +0700 Subject: [PATCH] add terraform config for mini k8s cluster --- src/terraform/k8s-mini-cluster/.gitignore | 38 ++++++++ src/terraform/k8s-mini-cluster/main.tf | 92 +++++++++++++++++++ .../k8s-mini-cluster/variables.tf.example | 16 ++++ 3 files changed, 146 insertions(+) create mode 100644 src/terraform/k8s-mini-cluster/.gitignore create mode 100644 src/terraform/k8s-mini-cluster/main.tf create mode 100644 src/terraform/k8s-mini-cluster/variables.tf.example diff --git a/src/terraform/k8s-mini-cluster/.gitignore b/src/terraform/k8s-mini-cluster/.gitignore new file mode 100644 index 0000000..3309aee --- /dev/null +++ b/src/terraform/k8s-mini-cluster/.gitignore @@ -0,0 +1,38 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +crash.*.log + +# Exclude all .tfvars files, which are likely to contain sentitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +# +*.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc +.terraform.lock.hcl + +variables.tf diff --git a/src/terraform/k8s-mini-cluster/main.tf b/src/terraform/k8s-mini-cluster/main.tf new file mode 100644 index 0000000..7f49cb0 --- /dev/null +++ b/src/terraform/k8s-mini-cluster/main.tf @@ -0,0 +1,92 @@ +terraform { + required_providers { + yandex = { + source = "yandex-cloud/yandex" + } + } + required_version = ">= 0.13" +} + +provider "yandex" { + token = var.yandex_cloud_token + cloud_id = var.yandex_cloud_id + folder_id = var.yandex_folder_id + zone = "ru-central1-a" +} + +resource "yandex_vpc_network" "network-1" { + name = "network1" +} + +resource "yandex_vpc_subnet" "subnet-1" { + name = "subnet1" + zone = "ru-central1-a" + network_id = yandex_vpc_network.network-1.id + v4_cidr_blocks = ["192.168.10.0/24"] +} + +resource "yandex_compute_instance" "k8s-control" { + name = "control" + + resources { + cores = 2 + memory = 2 + } + + boot_disk { + initialize_params { + image_id = "fd8kdq6d0p8sij7h5qe3" # ubuntu-20-04-lts-v20220822 + size = "20" + } + } + + network_interface { + subnet_id = yandex_vpc_subnet.subnet-1.id + nat = true + } + + metadata = { + ssh-keys = "ubuntu:${file("~/.ssh/id_rsa.pub")}" + } +} + +resource "yandex_compute_instance" "k8s-node" { + for_each = toset(["node01"]) + + name = each.key + + resources { + cores = 2 + memory = 2 + } + + boot_disk { + initialize_params { + image_id = "fd8kdq6d0p8sij7h5qe3" # ubuntu-20-04-lts-v20220822 + size = "20" + } + } + + network_interface { + subnet_id = yandex_vpc_subnet.subnet-1.id + nat = true + } + + metadata = { + ssh-keys = "ubuntu:${file("~/.ssh/id_rsa.pub")}" + } +} + +output "control_ips" { + value = { + internal = yandex_compute_instance.k8s-control.network_interface.0.ip_address + external = yandex_compute_instance.k8s-control.network_interface.0.nat_ip_address + } +} + +output "node_ips" { + value = { + internal = values(yandex_compute_instance.k8s-node)[*].network_interface.0.ip_address + external = values(yandex_compute_instance.k8s-node)[*].network_interface.0.nat_ip_address + } +} diff --git a/src/terraform/k8s-mini-cluster/variables.tf.example b/src/terraform/k8s-mini-cluster/variables.tf.example new file mode 100644 index 0000000..244f0f4 --- /dev/null +++ b/src/terraform/k8s-mini-cluster/variables.tf.example @@ -0,0 +1,16 @@ +# Заменить на ID своего облака +# https://console.cloud.yandex.ru/cloud?section=overview +variable "yandex_cloud_id" { + default = "b1gu1gt5nqi6lqgu3t7s" +} + +# Заменить на Folder своего облака +# https://console.cloud.yandex.ru/cloud?section=overview +variable "yandex_folder_id" { + default = "b1gaec42k169jqpo02f7" +} + +# OAuth токен, используемый утилитой yc. Применялся на этапе с packer. +variable "yandex_cloud_token" { + default = "" +}