feat added lab 4

This commit is contained in:
Black_Raven committed 2026-06-19 16:11:05 +03:00
1 parent aac766d4a5
commit 9f5e8ad4f8
19 files changed
+868 -1

No files matched your search

@@ -0,0 +1,11 @@
terraform {
backend "s3" {
bucket = "tf-state-lab4" # имя твоего бакета (должен существовать)
key = "lab4/terraform.tfstate" # путь к файлу состояния внутри бакета
region = "ru-msk" # твой регион
endpoint = "https://hb.ru-msk.vkcloud-storage.ru"
skip_region_validation = true
skip_credentials_validation = true
skip_metadata_api_check = true
}
}
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: web-app
data:
LB_IP: "95.163.215.1"
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: web-app
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21.6
ports:
- containerPort: 80
@@ -0,0 +1,6 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
- configmap.yaml
@@ -0,0 +1,177 @@
terraform {
required_providers {
vkcs = {
source = "vk-cs/vkcs"
version = "~> 0.1"
}
}
}
provider "vkcs" {
username = var.vkcs_username
password = var.vkcs_password
project_id = var.vkcs_project_id
region = var.vkcs_region
auth_url = var.vkcs_auth_url
}
data "vkcs_networking_network" "extnet" {
name = var.external_network_name
}
data "vkcs_networking_router" "router" {
id = var.router_id
}
data "vkcs_images_image" "ubuntu" {
name = var.image_name
}
data "vkcs_compute_flavor" "web" {
name = var.flavor_web
}
resource "vkcs_networking_network" "main" {
name = "${var.project_name}-network"
sdn = "sprut"
}
resource "vkcs_networking_subnet" "public" {
name = "${var.project_name}-public-subnet"
network_id = vkcs_networking_network.main.id
cidr = var.public_subnet_cidr
dns_nameservers = ["8.8.8.8", "1.1.1.1"]
}
resource "vkcs_networking_subnet" "private" {
name = "${var.project_name}-private-subnet"
network_id = vkcs_networking_network.main.id
cidr = var.private_subnet_cidr
dns_nameservers = ["8.8.8.8", "1.1.1.1"]
}
resource "vkcs_networking_router_interface" "public" {
router_id = data.vkcs_networking_router.router.id
subnet_id = vkcs_networking_subnet.public.id
}
resource "vkcs_networking_router_interface" "private" {
router_id = data.vkcs_networking_router.router.id
subnet_id = vkcs_networking_subnet.private.id
}
resource "vkcs_networking_secgroup" "web" {
name = "${var.project_name}-secgroup"
description = "Allow SSH and HTTP"
}
resource "vkcs_networking_secgroup_rule" "ssh" {
security_group_id = vkcs_networking_secgroup.web.id
direction = "ingress"
protocol = "tcp"
port_range_min = 22
port_range_max = 22
remote_ip_prefix = "${var.my_ip}/32"
}
resource "vkcs_networking_secgroup_rule" "http" {
security_group_id = vkcs_networking_secgroup.web.id
direction = "ingress"
protocol = "tcp"
port_range_min = 80
port_range_max = 80
remote_ip_prefix = "0.0.0.0/0"
}
resource "vkcs_networking_secgroup_rule" "egress" {
security_group_id = vkcs_networking_secgroup.web.id
direction = "egress"
remote_ip_prefix = "0.0.0.0/0"
}
resource "vkcs_compute_keypair" "my_key" {
name = "${var.project_name}-key"
public_key = file(var.ssh_public_key)
}
resource "vkcs_compute_instance" "web" {
count = 2
name = "${var.project_name}-web-${count.index + 1}"
flavor_id = data.vkcs_compute_flavor.web.id
key_pair = vkcs_compute_keypair.my_key.name
security_group_ids = [vkcs_networking_secgroup.web.id]
availability_zone = "MS1"
block_device {
uuid = data.vkcs_images_image.ubuntu.id
source_type = "image"
destination_type = "volume"
volume_type = "ceph-ssd"
volume_size = var.volume_size_web
boot_index = 0
delete_on_termination = true
}
network {
uuid = vkcs_networking_network.main.id
fixed_ip_v4 = cidrhost(var.private_subnet_cidr, count.index + 10)
}
user_data = <<-EOF
#!/bin/bash
apt update
apt install -y nginx
echo "<h1>Web Server $(hostname)</h1>" > /var/www/html/index.html
systemctl enable nginx
systemctl start nginx
EOF
}
locals {
web_private_ips = vkcs_compute_instance.web[*].network[0].fixed_ip_v4
}
resource "vkcs_lb_loadbalancer" "main" {
name = "${var.project_name}-lb"
vip_subnet_id = vkcs_networking_subnet.public.id
}
resource "vkcs_lb_listener" "http" {
name = "${var.project_name}-listener"
loadbalancer_id = vkcs_lb_loadbalancer.main.id
protocol = "HTTP"
protocol_port = 80
}
resource "vkcs_lb_pool" "web" {
name = "${var.project_name}-pool"
protocol = "HTTP"
lb_method = "ROUND_ROBIN"
listener_id = vkcs_lb_listener.http.id
}
resource "vkcs_lb_monitor" "web" {
pool_id = vkcs_lb_pool.web.id
type = "HTTP"
url_path = "/"
delay = 10
timeout = 5
max_retries = 3
}
resource "vkcs_lb_member" "web" {
count = 2
pool_id = vkcs_lb_pool.web.id
address = local.web_private_ips[count.index]
protocol_port = 80
subnet_id = vkcs_networking_subnet.private.id
}
resource "vkcs_networking_floatingip" "lb" {
pool = data.vkcs_networking_network.extnet.name
}
resource "vkcs_networking_floatingip_associate" "lb" {
floating_ip = vkcs_networking_floatingip.lb.address
port_id = vkcs_lb_loadbalancer.main.vip_port_id
}
@@ -0,0 +1,14 @@
output "load_balancer_ip" {
value = vkcs_networking_floatingip.lb.address
description = "Публичный IP балансировщика"
}
output "web_private_ips" {
value = local.web_private_ips
description = "Приватные IP веб-серверов"
}
output "lb_url" {
value = "http://${vkcs_networking_floatingip.lb.address}"
description = "URL для доступа к приложению"
}
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: web-app
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
@@ -0,0 +1,102 @@
variable "my_ip" {
description = "Ваш публичный IP для SSH (узнать через curl ifconfig.me)"
type = string
sensitive = true
}
variable "project_name" {
description = "Префикс для имен ресурсов"
type = string
default = "lab4"
}
variable "router_id" {
description = "ID существующего роутера"
type = string
default = "913bdd47-9155-45b2-b404-5c10d0086132"
}
variable "external_network_name" {
description = "Имя внешней сети для Floating IP"
type = string
default = "internet"
}
variable "ssh_public_key" {
description = "Путь к публичному SSH-ключу"
type = string
default = "~/.ssh/id_rsa.pub"
}
variable "image_name" {
description = "Имя образа Ubuntu 20.04"
type = string
default = "ubuntu-20-202602051631.gite7a38aaf"
}
variable "flavor_web" {
description = "Flavor для веб-серверов (используется только как ID, т.к. загрузка с тома)"
type = string
default = "Basic-1-1-10"
}
variable "flavor_bastion" {
description = "Flavor для ВМ бастиона"
type = string
default = "Basic-1-1-10"
}
variable "public_subnet_cidr" {
description = "CIDR публичной подсети"
type = string
default = "192.168.1.0/24"
}
variable "private_subnet_cidr" {
description = "CIDR приватной подсети"
type = string
default = "192.168.2.0/24"
}
variable "volume_size_web" {
description = "Размер диска для веб-серверов (ГБ)"
type = number
default = 10
}
variable "volume_size_bastion" {
description = "Размер диска для бастиона (ГБ)"
type = number
default = 20
}
# --- Аутентификация VKCS ---
variable "vkcs_username" {
description = "VK Cloud username (из RC-файла)"
type = string
sensitive = true
}
variable "vkcs_password" {
description = "VK Cloud password"
type = string
sensitive = true
}
variable "vkcs_project_id" {
description = "VK Cloud project ID (из RC-файла)"
type = string
sensitive = true
}
variable "vkcs_region" {
description = "VK Cloud region (например, RegionOne или MS1)"
type = string
default = "RegionOne"
}
variable "vkcs_auth_url" {
description = "VK Cloud Auth URL (например, https://infra.mail.ru:35357/v3/)"
type = string
sensitive = true
}