Terraform 설치해보기

다 먼지같은 일입니다·2021년 6월 29일
0

Terraform

목록 보기
1/1

IaC

kubernetes를 다루면서 자연스레 많이 듣게 되는 용어
IaC(Infrastructure as Code)에 대해 약간 찍먹해봤다.

어떤 부분에서 IaC를 사용하는지 확인할 필요가 있었다.

출처 : https://learn.hashicorp.com/tutorials/terraform/infrastructure-as-code?in=terraform/aws-get-started

튜토리얼과 많은 기사들에서는 멀티/하이브리드 클라우드에서의 워크로드에 강점이 있다고 설명한다. AWS, Azure를 비롯한 퍼블릭 클라우드에서부터 온프레미스 서버, kubernetes 까지를 아우를 수 있는 새로운 형태를 의미한다.

각기 다른 플랫폼에 통일된 양식을 가지고 인프라를 관리할 수 있다면, 관리자 입장에서는 분명 편리한 형태일 것이다.

와닿지가 않는다. 체험을 해보자

Terraform 튜토리얼

$ sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
$ sudo apt-get update && sudo apt-get install terraform
# main.tf

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}

첫 인상은 뭐랄까, 복잡해 보였다.
단순히 docker run nginx 보다 더 나은 장점이 뭘까..?
가독성 측면에서는 괜춘

$ terraform init
$ terraform apply

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
0098e2d1486d        4f380adfc10f        "/docker-entrypoint.…"   5 minutes ago       Up 5 minutes        0.0.0.0:8000->80/tcp   tutorial

terraform으로 nginx를 배포하도록 설정해뒀으니, 마치 kubernetes의 Desired State 처럼 컨테이너를 삭제해도 다시 배포되지 않을까? 라는 생각을 했다.

컨테이너를 삭제하자 자동으로 다시 배포되거나 하지는 않았다.

$ terraform destroy
docker_image.nginx: Refreshing state... [id=sha256:4f380adfc10f4cd34f775ae57a17d2835385efd5251d6dfe0f246b0018fb0399nginx:latest]
docker_container.nginx: Refreshing state... [id=0098e2d1486d542d64eb711b3456a894fe6e2033f806c108b6aec5f42a21c1b6]

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the last "terraform apply":

...

Destroy complete! Resources: 1 destroyed.

Destory 명령을 날려보니, apply가 된 이후에 내가 수동으로 컨테이너를 삭제한 내역을 보여준다.

0개의 댓글