programming-study icon indicating copy to clipboard operation
programming-study copied to clipboard

[devOps] Learn DevOps: Infrastructure Automation With Terraform

Open heowc opened this issue 7 years ago • 11 comments

https://www.udemy.com/learn-devops-infrastructure-automation-with-terraform/learn/v4/overview

heowc avatar Feb 18 '18 13:02 heowc

Terraform

인프라를 만들고 바꾸고 버전 관리하는 도구

소개

  • HCL
  • .tf or .tf.json
  • Infra Structure = Provider    └─ AWS, DigitalOcean, Google Cloud, Microsoft Azure, ...

특징

  • Infrastructure as Code
  • Execution Plans
  • Resource Graph
  • Change Automation

heowc avatar Feb 18 '18 13:02 heowc

설치

https://www.terraform.io/downloads.html

Demo Sample

https://github.com/wardviaene/terraform-course

환경 셋팅(+Vagrant)

https://github.com/heowc/vagrant-sample/tree/master/ubuntu1604_terraform

설정

  • .tf: 각각의 설정
  • .tfvars: 상수 값 초기화

heowc avatar Feb 19 '18 14:02 heowc

Terraform로 AWS 코딩하기

  1. AWS 계정 생성

    • IAM 추가(AdministratorAccess)
  2. terraform init

  3. region 확인(https://docs.aws.amazon.com/ko_kr/general/latest/gr/rande.html)

  4. 설정 작성(provider 지정)

  5. terraform plan

  6. terraform apply

heowc avatar Feb 20 '18 13:02 heowc

EC2 instance

  1. resoure: aws_instance
    • ami 지정
    • 인스턴스 타입 지정

S3 Bucket

  1. resoure: aws_s3_bucket

RDS MySql

  1. resource: aws_db_instance
  2. engine, username, password, ...
  3. skip_final_snapshot: 마지막 스냅샷 skip 여부

VPC

  • Virtual Private Network
  • default VPC 존재
  • VPC 안에 application, database를 구성
         main vpc
        ┌ subnet - [public], [private]
gateway ┼ subnet - [public], [private]
        └ subnet - [public], [private]
  • public subnetgateway를 통해 접근할 수 있어야 한다. (service, application, etc)
  • private subnet은 외부로 부터 접근할 수 없어야 하며, nat-gateway를 통해서만 접근 할 수 있어야 한다. (database, caching service, etc)

heowc avatar Feb 22 '18 08:02 heowc

remote-state

  • terraform remote
  • backend
  • S3에 version controll 가능

heowc avatar Feb 24 '18 04:02 heowc

security group 설정

  • aws_security_group
  • CIDR, port, protocol 등 설정

※ CIDR 사이더(Classless Inter-Domain Routing, CIDR)는 클래스 없는 도메인 간 라우팅 기법으로 1993년 도입되기 시작한, 최신의 IP 주소 할당 방법이다.

heowc avatar Feb 25 '18 13:02 heowc

aws-cli 맛보기

설치

  • https://docs.aws.amazon.com/ko_kr/cli/latest/userguide/installing.html
  • aws

ssh 접속

  • https://docs.aws.amazon.com/ko_kr/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html
  • ssh 접근: ssh -i {pem file} {user}@{public ip}
  • file 업로드: scp -i {pem file} {user}@{public ip}

heowc avatar Feb 26 '18 12:02 heowc

data sources

  • Terraform 구성의 다른 곳에서 사용하기 위해 데이터를 가져 오거나 계산할 수 있다.
  • Terraform 구성을 Terraform 외부에서 정의 된 정보 또는 다른 별도의 Terraform 구성으로 정의 된 정보를 기반으로 구축 할 수 있다.
  • data

heowc avatar Feb 27 '18 13:02 heowc

Module

  • terraform get
  • Module Registry
  • 라이브러리 형태로 만들 수 있다.

heowc avatar Feb 27 '18 14:02 heowc

NGINX 맛보기(ubuntu 기준)

설치

$ sudo apt-get update -y
$ sudo apt-get install -y nginx

실행

$ sudo systemctl start nginx.service
or
$ sudo service nginx start

설정 파일 갱신

$ sudo nginx -s reload

프록시 설정(ex. conf.d/ngx_proxy.conf 생성)

api application에 proxy 용도라면, nginx.conf에 include된 site-available를 주석하는 것이 좋다?

server {
    location / {
        proxy_pass http://localhost:8080;
    }
}

참고 https://nginx.org/en/docs/beginners_guide.html

heowc avatar Feb 28 '18 01:02 heowc

명령어 정리

명령어 설명
apply provider에 반영
destroy 테라폼으로 provider에 반영된 내용 제거
fmt 설정 파일 포맷과 스타일을 정리
get 모듈 다운로드 및 업데이트
graph Graph Viz 문법의 내용 표기
import state import
output output 내용 표기
plan 작성한 내용을 표기
push hashicorp의 엔터프라이즈 툴인 atlas에서 push
refresh remote state 갱신
remote remote storge 설정(ex. S3, consul)
show plan 내용 보기
state 강화된 state 관리 command
taint apply에서 지정한 내용을 없애고 다음 apply에 재생성
validate 테라폼 문법 검증
untaint taint 취소

heowc avatar Mar 01 '18 04:03 heowc