feat: Added lab 2
This commit is contained in:
1 parent
b599bd410b
commit
ee54718ba0
19 files changed
+1359
No files matched your search
@@ -0,0 +1,16 @@
|
||||
terraform {
|
||||
backend "s3" {
|
||||
bucket = "terraform-state-lab2" # замените на имя вашего бакета
|
||||
key = "lab2/terraform.tfstate"
|
||||
region = "RegionOne"
|
||||
endpoint = "https://hb.vkcloud-storage.ru"
|
||||
|
||||
skip_region_validation = true
|
||||
skip_credentials_validation = true
|
||||
skip_requesting_account_id = true
|
||||
skip_metadata_api_check = true
|
||||
|
||||
access_key = ""
|
||||
secret_key = ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
resource "vkcs_compute_keypair" "my_key" {
|
||||
name = var.keypair_name
|
||||
public_key = file(var.ssh_public_key_path)
|
||||
}
|
||||
|
||||
resource "vkcs_networking_port" "bastion_port" {
|
||||
name = "bastion-port"
|
||||
network_id = vkcs_networking_network.lab1_vpc.id
|
||||
admin_state_up = true
|
||||
|
||||
fixed_ip {
|
||||
subnet_id = vkcs_networking_subnet.public.id
|
||||
}
|
||||
|
||||
security_group_ids = [vkcs_networking_secgroup.bastion_sg.id]
|
||||
}
|
||||
|
||||
resource "vkcs_compute_instance" "bastion" {
|
||||
name = "bastion"
|
||||
flavor_id = var.flavor_bastion
|
||||
image_id = var.ubuntu_image_id
|
||||
key_pair = vkcs_compute_keypair.my_key.name
|
||||
|
||||
network {
|
||||
port = vkcs_networking_port.bastion_port.id
|
||||
}
|
||||
}
|
||||
|
||||
resource "vkcs_networking_floatingip" "bastion_fip" {
|
||||
pool = "internet"
|
||||
}
|
||||
|
||||
resource "vkcs_compute_floatingip_associate" "bastion_fip_assoc" {
|
||||
floating_ip = vkcs_networking_floatingip.bastion_fip.address
|
||||
instance_id = vkcs_compute_instance.bastion.id
|
||||
}
|
||||
|
||||
resource "vkcs_networking_port" "web_port" {
|
||||
count = 2
|
||||
name = "web-port-${count.index + 1}"
|
||||
network_id = vkcs_networking_network.lab1_vpc.id
|
||||
admin_state_up = true
|
||||
|
||||
fixed_ip {
|
||||
subnet_id = vkcs_networking_subnet.private.id
|
||||
}
|
||||
|
||||
security_group_ids = [vkcs_networking_secgroup.web_sg.id]
|
||||
}
|
||||
|
||||
resource "vkcs_compute_instance" "web" {
|
||||
count = 2
|
||||
name = "web-${count.index + 1}"
|
||||
flavor_id = var.flavor_web
|
||||
image_id = var.image_packer_id
|
||||
key_pair = vkcs_compute_keypair.my_key.name
|
||||
|
||||
network {
|
||||
port = vkcs_networking_port.web_port[count.index].id
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
resource "vkcs_db_instance" "postgres" {
|
||||
name = "lab1-postgres"
|
||||
flavor_id = var.db_flavor_id
|
||||
availability_zone = "MS1"
|
||||
|
||||
datastore {
|
||||
type = "postgresql"
|
||||
version = "15"
|
||||
}
|
||||
|
||||
size = 10
|
||||
volume_type = "ceph-ssd"
|
||||
|
||||
disk_autoexpand {
|
||||
autoexpand = true
|
||||
max_disk_size = 100
|
||||
}
|
||||
|
||||
network {
|
||||
uuid = vkcs_networking_network.lab1_vpc.id
|
||||
}
|
||||
|
||||
depends_on = [
|
||||
vkcs_networking_router_interface.private,
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
resource "vkcs_lb_loadbalancer" "main" {
|
||||
name = "lab1-lb"
|
||||
vip_subnet_id = vkcs_networking_subnet.public.id
|
||||
}
|
||||
|
||||
resource "vkcs_lb_listener" "http" {
|
||||
name = "http-listener"
|
||||
protocol = "HTTP"
|
||||
protocol_port = 80
|
||||
loadbalancer_id = vkcs_lb_loadbalancer.main.id
|
||||
}
|
||||
|
||||
resource "vkcs_lb_pool" "web" {
|
||||
name = "web-targets"
|
||||
protocol = "HTTP"
|
||||
lb_method = "ROUND_ROBIN"
|
||||
listener_id = vkcs_lb_listener.http.id
|
||||
}
|
||||
|
||||
resource "vkcs_lb_monitor" "web" {
|
||||
name = "web-monitor"
|
||||
type = "HTTP"
|
||||
delay = 10
|
||||
timeout = 5
|
||||
max_retries = 3
|
||||
url_path = "/"
|
||||
pool_id = vkcs_lb_pool.web.id
|
||||
}
|
||||
|
||||
resource "vkcs_lb_member" "web" {
|
||||
count = 2
|
||||
name = "web-member-${count.index + 1}"
|
||||
address = vkcs_compute_instance.web[count.index].network[0].fixed_ip_v4 # берём IP из ВМ
|
||||
protocol_port = 80
|
||||
pool_id = vkcs_lb_pool.web.id
|
||||
subnet_id = vkcs_networking_subnet.private.id
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
data "vkcs_networking_network" "ext_net" {
|
||||
name = "internet"
|
||||
}
|
||||
|
||||
data "vkcs_networking_router" "existing" {
|
||||
id = "913bdd47-9155-45b2-b404-5c10d0086132"
|
||||
}
|
||||
|
||||
resource "vkcs_networking_network" "lab1_vpc" {
|
||||
name = "lab1-vpc"
|
||||
admin_state_up = true
|
||||
}
|
||||
|
||||
resource "vkcs_networking_subnet" "public" {
|
||||
name = "lab1-public"
|
||||
network_id = vkcs_networking_network.lab1_vpc.id
|
||||
cidr = "10.0.1.0/24"
|
||||
dns_nameservers = ["8.8.8.8", "8.8.4.4"]
|
||||
}
|
||||
|
||||
resource "vkcs_networking_subnet" "private" {
|
||||
name = "lab1-private"
|
||||
network_id = vkcs_networking_network.lab1_vpc.id
|
||||
cidr = "10.0.2.0/24"
|
||||
dns_nameservers = ["8.8.8.8", "8.8.4.4"]
|
||||
}
|
||||
|
||||
resource "vkcs_networking_router_interface" "public" {
|
||||
router_id = data.vkcs_networking_router.existing.id
|
||||
subnet_id = vkcs_networking_subnet.public.id
|
||||
}
|
||||
|
||||
resource "vkcs_networking_router_interface" "private" {
|
||||
router_id = data.vkcs_networking_router.existing.id
|
||||
subnet_id = vkcs_networking_subnet.private.id
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
output "lb_public_ip" {
|
||||
value = vkcs_lb_loadbalancer.main.vip_address
|
||||
description = "Публичный IP балансировщика"
|
||||
}
|
||||
|
||||
output "bastion_public_ip" {
|
||||
value = vkcs_networking_floatingip.bastion_fip.address
|
||||
}
|
||||
|
||||
output "web_private_ips" {
|
||||
value = vkcs_compute_instance.web[*].network[0].fixed_ip_v4
|
||||
description = "Приватные IP веб-серверов"
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
# Переменные окружения
|
||||
variable "network_id" {
|
||||
type = string
|
||||
default = env("NETWORK_ID")
|
||||
}
|
||||
variable "source_image" {
|
||||
type = string
|
||||
default = env("SOURCE_IMAGE")
|
||||
}
|
||||
|
||||
source "openstack" "ubuntu-nginx" {
|
||||
flavor = "STD3-2-6"
|
||||
source_image = var.source_image
|
||||
networks = [var.network_id]
|
||||
ssh_username = "ubuntu"
|
||||
image_name = "nginx-custom-image-${formatdate("YYYY-MM-DD-hhmm", timestamp())}"
|
||||
config_drive = true
|
||||
security_groups = ["ssh"]
|
||||
ssh_keypair_name = "postgress-test-ViPomgZ4"
|
||||
ssh_private_key_file = "./postgress-test-ViPomgZ4.pem"
|
||||
ssh_timeout = "10m"
|
||||
}
|
||||
|
||||
build {
|
||||
sources = ["source.openstack.ubuntu-nginx"]
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
vkcs = {
|
||||
source = "vk-cs/vkcs"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "vkcs" {}
|
||||
@@ -0,0 +1,46 @@
|
||||
resource "vkcs_networking_secgroup" "bastion_sg" {
|
||||
name = "bastion-sg"
|
||||
description = "SSH from my IP"
|
||||
}
|
||||
resource "vkcs_networking_secgroup_rule" "bastion_ssh" {
|
||||
direction = "ingress"
|
||||
protocol = "tcp"
|
||||
port_range_min = 22
|
||||
port_range_max = 22
|
||||
remote_ip_prefix = var.my_ip
|
||||
security_group_id = vkcs_networking_secgroup.bastion_sg.id
|
||||
}
|
||||
|
||||
resource "vkcs_networking_secgroup" "web_sg" {
|
||||
name = "web-sg"
|
||||
description = "SSH from bastion, HTTP from LB"
|
||||
}
|
||||
resource "vkcs_networking_secgroup_rule" "web_ssh" {
|
||||
direction = "ingress"
|
||||
protocol = "tcp"
|
||||
port_range_min = 22
|
||||
port_range_max = 22
|
||||
remote_group_id = vkcs_networking_secgroup.bastion_sg.id
|
||||
security_group_id = vkcs_networking_secgroup.web_sg.id
|
||||
}
|
||||
resource "vkcs_networking_secgroup_rule" "web_http" {
|
||||
direction = "ingress"
|
||||
protocol = "tcp"
|
||||
port_range_min = 80
|
||||
port_range_max = 80
|
||||
remote_group_id = vkcs_networking_secgroup.lb_sg.id
|
||||
security_group_id = vkcs_networking_secgroup.web_sg.id
|
||||
}
|
||||
|
||||
resource "vkcs_networking_secgroup" "lb_sg" {
|
||||
name = "lb-sg"
|
||||
description = "HTTP from internet"
|
||||
}
|
||||
resource "vkcs_networking_secgroup_rule" "lb_http" {
|
||||
direction = "ingress"
|
||||
protocol = "tcp"
|
||||
port_range_min = 80
|
||||
port_range_max = 80
|
||||
remote_ip_prefix = "0.0.0.0/0"
|
||||
security_group_id = vkcs_networking_secgroup.lb_sg.id
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
variable "my_ip" {
|
||||
type = string
|
||||
default = "95.26.148.233/32"
|
||||
}
|
||||
|
||||
variable "flavor_web" {
|
||||
type = string
|
||||
default = "25ae869c-be29-4840-8e12-99e046d2dbd4"
|
||||
}
|
||||
|
||||
variable "flavor_bastion" {
|
||||
type = string
|
||||
default = "df3c499a-044f-41d2-8612-d303adc613cc"
|
||||
}
|
||||
|
||||
variable "image_packer_id" {
|
||||
type = string
|
||||
default = "a31acc16-4703-468b-bc5b-bf07952f66b2"
|
||||
}
|
||||
|
||||
variable "ubuntu_image_name" {
|
||||
type = string
|
||||
default = "ubuntu-22-202602051629.gite7a38aaf"
|
||||
}
|
||||
|
||||
variable "ssh_public_key_path" {
|
||||
type = string
|
||||
default = "~/.ssh/id_rsa.pub"
|
||||
}
|
||||
|
||||
variable "keypair_name" {
|
||||
type = string
|
||||
default = "terraform-key-local"
|
||||
}
|
||||
|
||||
variable "ubuntu_image_id" {
|
||||
type = string
|
||||
default = "a4e699d3-a66d-45e5-bb5d-70ea7c8de62d"
|
||||
}
|
||||
|
||||
variable "db_flavor_id" {
|
||||
type = string
|
||||
default = "2d9866a9-e955-4986-b00a-340ca54b2cac"
|
||||
}
|
||||
Reference in new issue
Block a user