Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

JINIers

[PCK] Automating the Deployment of Infrastructure Using Terraform lab 본문

GCP/Qwiklabs

[PCK] Automating the Deployment of Infrastructure Using Terraform lab

JINIers 2022. 1. 29. 17:03

※ 수정

220921


terraform을 사용하여 인프라 배포 자동화

 

1. 자동모드 네트워크 구성 만들기
2. 방화벽 규칙 구성 만들기 
3. vm 인스턴스용 모듈 만들기
4. 구성 생성 및 배퍼, 구성배포 확인

 


 

이렇게 만들어야함

 

작업 1. terraform 및 cloud shell 설정

 

[terraform 설치]

# terraform 설치 확인
terraform --version

# 디렉터리 만들기
mkdir tfinfra

open editor > open in new window


[terraform 초기화]

tfinfra 우클릭 > file > new file > provider.tf 생성

 

입력▼

provider "google" {}

 

provider.tf

 

# 디렉토리 이동
cd tfinfra

# 테라폼 초기화
terraform init

 


작업 2. mynetwork 및 해당 리소스 생성

방화벽 규칙, 2개 vm 인스턴스, 자동모드 네트워크 생성(mynetwork)

 

 

[마이 네트워크 구성, 방화벽 규칙 구성(내용 추가)]

file > new file > mynetwork.tf

# mynetwork.tf

# Create the mynetwork network
resource "google_compute_network" "mynetwork" {
  name = "mynetwork"
  #RESOURCE properties go here
  auto_create_subnetworks = "true"
}

# Add a firewall rule to allow HTTP, SSH, RDP and ICMP traffic on mynetwork
resource "google_compute_firewall" "mynetwork-allow-http-ssh-rdp-icmp" {
  name = "mynetwork-allow-http-ssh-rdp-icmp"
  #RESOURCE properties go here

  network = google_compute_network.mynetwork.self_link

  allow {
    protocol = "tcp"
    ports    = ["22", "80", "3389"]
  }
  allow {
    protocol = "icmp"
  }
  source_ranges = ["0.0.0.0/0"]
}

# Create the mynet-us-vm instance
module "mynet-us-vm" {
  source           = "./instance"
  instance_name    = "mynet-us-vm"
  instance_zone    = "us-central1-a"
  instance_network = google_compute_network.mynetwork.self_link
}
# Create the mynet-eu-vm" instance
module "mynet-eu-vm" {
  source           = "./instance"
  instance_name    = "mynet-eu-vm"
  instance_zone    = "europe-west1-d"
  instance_network = google_compute_network.mynetwork.self_link
}

 

[vm 인스턴스 구성]

tfinfra > mkdir instance > main.tf

# main.tf

variable "instance_name" {}
variable "instance_zone" {}
variable "instance_type" {
  default = "n1-standard-1"
  }
variable "instance_network" {}
resource "google_compute_instance" "vm_instance" {
  name = "${var.instance_name}"
  #RESOURCE properties go here
  zone         = "${var.instance_zone}"
  machine_type = "${var.instance_type}"
    boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
      }
  }
  network_interface {
    network = "${var.instance_network}"
    access_config {
      # Allocate a one-to-one NAT IP to the instance
    }
  }
}

[mynetwork 및 해당 리소스 생성]

 

#구성파일을 표준 형식 및 스타일로
terraform fmt

#테라폼 초기화
terraform init

#실행계획 만들기
terraform plan

#테라폼 적용
terraform apply

yes

terraform init
terraform plan
terraform apply


작업 3. 배포 확인

[cloud console에서 네트워크 확인]

vpc network > vpc network > mynetwork 확인 > firewall > 'mynetwork-allow-http-ssh-rdp-icmp' 확인

 


[cloud console에서 VM 인스턴스 확인]

compute engine > vm instance > 'mynet-us-vm', 'mynet-eu-vm' 인스턴스 확인
eu : 10.132.0.2
us : 10.132.0.2

us ssh > 

# eu-vm 연결 확인
ping -c 3 10.132.0.2

 

ssh 접속 : mynet-eu-vm 연결확인

Comments