homework 5.5: add task 2-3

This commit is contained in:
2022-05-16 10:35:39 +07:00
parent 500c7cac71
commit 991819fa39
46 changed files with 6124 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
variables.tf
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
.terraform.lock.hcl
# 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

View File

@@ -0,0 +1,6 @@
[defaults]
inventory=./inventory
deprecation_warnings=False
command_warnings=False
ansible_port=22
host_key_checking = False

View File

@@ -0,0 +1,39 @@
resource "null_resource" "wait" {
provisioner "local-exec" {
command = "sleep 100"
}
depends_on = [
local_file.inventory
]
}
resource "null_resource" "cluster" {
provisioner "local-exec" {
command = "ANSIBLE_FORCE_COLOR=1 ansible-playbook -i ../ansible/inventory ../ansible/swarm-deploy-cluster.yml"
}
depends_on = [
null_resource.wait
]
}
resource "null_resource" "sync" {
provisioner "local-exec" {
command = "ANSIBLE_FORCE_COLOR=1 ansible-playbook -i ../ansible/inventory ../ansible/swarm-deploy-sync.yml"
}
depends_on = [
null_resource.cluster
]
}
resource "null_resource" "monitoring" {
provisioner "local-exec" {
command = "ANSIBLE_FORCE_COLOR=1 ansible-playbook -i ../ansible/inventory ../ansible/swarm-deploy-stack.yml --limit=managers"
}
depends_on = [
null_resource.sync
]
}

View File

@@ -0,0 +1,36 @@
resource "local_file" "inventory" {
content = <<-DOC
# Ansible inventory containing variable values from Terraform.
# Generated by Terraform.
[nodes:children]
managers
workers
[managers:children]
active
standby
[active]
node01.netology.yc ansible_host=${yandex_compute_instance.node01.network_interface.0.nat_ip_address}
[standby]
node02.netology.yc ansible_host=${yandex_compute_instance.node02.network_interface.0.nat_ip_address}
node03.netology.yc ansible_host=${yandex_compute_instance.node03.network_interface.0.nat_ip_address}
[workers]
node04.netology.yc ansible_host=${yandex_compute_instance.node04.network_interface.0.nat_ip_address}
node05.netology.yc ansible_host=${yandex_compute_instance.node05.network_interface.0.nat_ip_address}
node06.netology.yc ansible_host=${yandex_compute_instance.node06.network_interface.0.nat_ip_address}
DOC
filename = "../ansible/inventory"
depends_on = [
yandex_compute_instance.node01,
yandex_compute_instance.node02,
yandex_compute_instance.node03,
yandex_compute_instance.node04,
yandex_compute_instance.node05,
yandex_compute_instance.node06
]
}

View File

@@ -0,0 +1,11 @@
# Network
resource "yandex_vpc_network" "default" {
name = "net"
}
resource "yandex_vpc_subnet" "default" {
name = "subnet"
zone = "ru-central1-a"
network_id = "${yandex_vpc_network.default.id}"
v4_cidr_blocks = ["192.168.101.0/24"]
}

View File

@@ -0,0 +1,30 @@
resource "yandex_compute_instance" "node01" {
name = "node01"
zone = "ru-central1-a"
hostname = "node01.netology.yc"
allow_stopping_for_update = true
resources {
cores = 4
memory = 8
}
boot_disk {
initialize_params {
image_id = "${var.centos-7-base}"
name = "root-node01"
type = "network-nvme"
size = "10"
}
}
network_interface {
subnet_id = "${yandex_vpc_subnet.default.id}"
nat = true
ip_address = "192.168.101.11"
}
metadata = {
ssh-keys = "centos:${file("~/.ssh/id_rsa.pub")}"
}
}

View File

@@ -0,0 +1,30 @@
resource "yandex_compute_instance" "node02" {
name = "node02"
zone = "ru-central1-a"
hostname = "node02.netology.yc"
allow_stopping_for_update = true
resources {
cores = 4
memory = 8
}
boot_disk {
initialize_params {
image_id = "${var.centos-7-base}"
name = "root-node02"
type = "network-nvme"
size = "10"
}
}
network_interface {
subnet_id = "${yandex_vpc_subnet.default.id}"
nat = true
ip_address = "192.168.101.12"
}
metadata = {
ssh-keys = "centos:${file("~/.ssh/id_rsa.pub")}"
}
}

View File

@@ -0,0 +1,30 @@
resource "yandex_compute_instance" "node03" {
name = "node03"
zone = "ru-central1-a"
hostname = "node03.netology.yc"
allow_stopping_for_update = true
resources {
cores = 4
memory = 8
}
boot_disk {
initialize_params {
image_id = "${var.centos-7-base}"
name = "root-node03"
type = "network-nvme"
size = "10"
}
}
network_interface {
subnet_id = "${yandex_vpc_subnet.default.id}"
nat = true
ip_address = "192.168.101.13"
}
metadata = {
ssh-keys = "centos:${file("~/.ssh/id_rsa.pub")}"
}
}

View File

@@ -0,0 +1,30 @@
resource "yandex_compute_instance" "node04" {
name = "node04"
zone = "ru-central1-a"
hostname = "node04.netology.yc"
allow_stopping_for_update = true
resources {
cores = 4
memory = 8
}
boot_disk {
initialize_params {
image_id = "${var.centos-7-base}"
name = "root-node04"
type = "network-nvme"
size = "40"
}
}
network_interface {
subnet_id = "${yandex_vpc_subnet.default.id}"
nat = true
ip_address = "192.168.101.14"
}
metadata = {
ssh-keys = "centos:${file("~/.ssh/id_rsa.pub")}"
}
}

View File

@@ -0,0 +1,30 @@
resource "yandex_compute_instance" "node05" {
name = "node05"
zone = "ru-central1-a"
hostname = "node05.netology.yc"
allow_stopping_for_update = true
resources {
cores = 4
memory = 8
}
boot_disk {
initialize_params {
image_id = "${var.centos-7-base}"
name = "root-node05"
type = "network-nvme"
size = "40"
}
}
network_interface {
subnet_id = "${yandex_vpc_subnet.default.id}"
nat = true
ip_address = "192.168.101.15"
}
metadata = {
ssh-keys = "centos:${file("~/.ssh/id_rsa.pub")}"
}
}

View File

@@ -0,0 +1,30 @@
resource "yandex_compute_instance" "node06" {
name = "node06"
zone = "ru-central1-a"
hostname = "node06.netology.yc"
allow_stopping_for_update = true
resources {
cores = 4
memory = 8
}
boot_disk {
initialize_params {
image_id = "${var.centos-7-base}"
name = "root-node06"
type = "network-nvme"
size = "40"
}
}
network_interface {
subnet_id = "${yandex_vpc_subnet.default.id}"
nat = true
ip_address = "192.168.101.16"
}
metadata = {
ssh-keys = "centos:${file("~/.ssh/id_rsa.pub")}"
}
}

View File

@@ -0,0 +1,47 @@
output "internal_ip_address_node01" {
value = "${yandex_compute_instance.node01.network_interface.0.ip_address}"
}
output "external_ip_address_node01" {
value = "${yandex_compute_instance.node01.network_interface.0.nat_ip_address}"
}
output "internal_ip_address_node02" {
value = "${yandex_compute_instance.node02.network_interface.0.ip_address}"
}
output "external_ip_address_node02" {
value = "${yandex_compute_instance.node02.network_interface.0.nat_ip_address}"
}
output "internal_ip_address_node03" {
value = "${yandex_compute_instance.node03.network_interface.0.ip_address}"
}
output "external_ip_address_node03" {
value = "${yandex_compute_instance.node03.network_interface.0.nat_ip_address}"
}
output "internal_ip_address_node04" {
value = "${yandex_compute_instance.node04.network_interface.0.ip_address}"
}
output "external_ip_address_node04" {
value = "${yandex_compute_instance.node04.network_interface.0.nat_ip_address}"
}
output "internal_ip_address_node05" {
value = "${yandex_compute_instance.node05.network_interface.0.ip_address}"
}
output "external_ip_address_node05" {
value = "${yandex_compute_instance.node05.network_interface.0.nat_ip_address}"
}
output "internal_ip_address_node06" {
value = "${yandex_compute_instance.node06.network_interface.0.ip_address}"
}
output "external_ip_address_node06" {
value = "${yandex_compute_instance.node06.network_interface.0.nat_ip_address}"
}

View File

@@ -0,0 +1,14 @@
# Provider
terraform {
required_providers {
yandex = {
source = "yandex-cloud/yandex"
}
}
}
provider "yandex" {
token = var.yandex_cloud_token
cloud_id = var.yandex_cloud_id
folder_id = var.yandex_folder_id
}

View File

@@ -0,0 +1,22 @@
# Заменить на 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 = ""
}
# Заменить на ID своего образа
# ID можно узнать с помощью команды yc compute image list
variable "centos-7-base" {
default = "fd8ft6norj68lo29qlpi"
}