Contenu connexe Similaire à Terraform을 기반한 AWS 기반 대규모 마이크로서비스 인프라 운영 노하우 - 이용욱, 삼성전자 :: AWS Summit Seoul 2019 (20) Plus de Amazon Web Services Korea (20) Terraform을 기반한 AWS 기반 대규모 마이크로서비스 인프라 운영 노하우 - 이용욱, 삼성전자 :: AWS Summit Seoul 20191. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Terraform을 이용한
AWS 기반 대규모 마이크로서비스
인프라 운영
이용욱 책임, 삼성전자
2. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
• 대규모 마이크로서비스 인프라 구성하기
= 레고 함께 조립하기
• Terraform 모듈 만들기
= 레고 블록 만들기
• Building block
= 단순하고, 확장되고, 단단해야
레고 블록처럼…
https://www.maxpixel.net/Colorful-Play-Children-Lego-Building-Blocks-2285065
3. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Who am I?
이용욱
삼성전자 / Cloud운영 그룹
uks.lee@samsung.com / uks78n@gmail.com
Dev, DA/DBA, TechOps, DevOps, 이제는 SRE
• Likes: 효율적이고 재활용 가능하면서 단순한 코드
• Dislikes: 단순 작업 2번 이상 반복하기
2012
2014
2015
2016
2017
2018
2019
awspec
4. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Agenda
IaC를 통한 대규모 마이크로 서비스 인프라 운영
운영을 고려한 Terraform 코드 구조화
Terraform 모듈 코드 테스트
5. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
마이크로서비스
• 소규모 팀이 관리하는
작은 크기의 서비스
• 다수의 팀이 관리하는
다수의 마이크로서비스
https://github.com/Netflix/vizceral
100+
microservices ~= teams
6. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
다양한 조직 및 구성원
• 초보자 ~ 전문가
• 폭발 반경(Blast radius) 제한
• 다양한 환경 및 요구사항
• 작업환경
• 네트워크 환경
• 권한 관리
https://www.maxpixel.net/Doll-Amphitheatre-Both-Lego-The-People-The-Per-1044891
7. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
일관성 유지의 어려움
• 일관된 관리 필요
• 보안 규정 준수
• 이름 규칙, 태그 규칙 등 관리 정책
• 끊임없는 변경
• 마이크로 서비스 생성/삭제
• 마이크로 서비스 구조/연관성 변경
• 담당자 변경
정책 변경?
https://www.maxpixel.net/Vote-Lego-Choice-Staff-Selector-Crowd-Survey-1699137
8. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
IaC (Infrastructure as Code)
• 인프라 생성, 설정, 관리를 코드를 통해 작업 à 반복 재현성, 이력관리
• 인프라 코드 = 인프라 설정 = 아키텍쳐 문서
• 빠른 공유 및 병렬 작업
Provisioned
Architecture
Desired
Architecture
Terraform
Code
Terraform
Plan/Apply
인프라 운영
= 유지 보수
= 변경 관리
9. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
용도에 맞는 도구 사용
도구 자원 관리 범위
인프라 관리
• 정적 인프라 자원
• VPC, IAM, …
• ECS/EKS Clusters
Immutable
image 생성
• 이미지 생성에 필요한 모든 설정
Continuous
Delivery
• 배포시 생성되는 동적 인프라 자원
• AutoScalingGroup, LaunchConfig
• 배포 파이프라인 설정
• App 배포 설정(VM/ECS)
K8s 배포 spec. • App 배포 설정(K8s)
10. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Agenda
IaC를 통한 대규모 마이크로 서비스 인프라 운영
운영을 고려한 Terraform 코드 구조화
Terraform 모듈 코드 테스트
11. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
관리 범위 쪼개기
• Communication cost
• 담당자 밖에 몰라
• 상호 간섭 최소화
• 문제 발생시 영향 범위
(Blast range) 최소화
• 개인별 필요 권한 최소화
마이크로서비스 단위로 관리
http://minifigpriceguide.com/wordpress/wp-content/uploads/2016/04/Lego-Minifigure-DIsplay-Case-with-Dust-Door-3-Copy.jpg
12. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
• 환경 별 리소스 차이 최소화
• 동일 코드를
DEV à STG à PROD
환경으로 단계적 승격
• 테스트, 테스트, 테스트
• Fail fast, Fail safe
DEV
STG
PROD
동일 코드 기반의 인프라 환경 구성
https://pixabay.com/pt/photos/stormtrooper-star-wars-lego-2899993/
13. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
재사용 가능한 공용 모듈
• VPC
• ECS, EKS Cluster
• LoadBalancer
• LB, SecurityGroup, TargetGroup, …
• ServerGroup
• AutoScalingGroup, AutoScalingPolicy,
LaunchConfig, SecurityGroup, IAM, …
1. 일관된 인프라 관리
2. 신규 서비스 추가 용이
14. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
• Local path
• Github
• Git
• S3
module "consul" {
source = "hashicorp/consul/aws"
version = "0.3.3"
aws_region = "us-east-1"
num_servers = "3"
}
Terraform modules
consul.tf
• 미리 준비해둔 모듈을 이용하여
복잡한 리소스 모음을 손쉽게 구성
source = "./consul"
source = "git@github.com:hashicorp/example.git"
username@example.com/storage.git
15. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Terraform workspaces
• 분리된 tfstate 공간 제공
• 동일 코드로 다른 세트의 인프라 관리
• Workspace variables / 별도 tfvars 파일
resource "aws_instance" "example" {
tags {
Name = "web - ${terraform.workspace}"
}
# ... other arguments
}
main.tf
.
├── envs
│ ├── dev.tfvars
│ └── stg.tfvars
├── main.tf
├── outputs.tf
├── provider.tf
├── variables.tf
└── remote.tf
$ terraform apply -var-file=./envs/dev.tfvars
숙련자 또는
자동화한
경우만 권장
16. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Terraform 코드 저장소 폴더 구조 예
• main-repo • 마이크로서비스별 모듈 repo
.
└── terraform
├── account1
│ ├── apne2
│ │ ├── microservice1
│ │ │
│ │ │
│ │ ├── microservice2
│ │ ├── databases
│ │ ├── ecs_cluster
│ │ ├── eks_cluster
│ │ └── vpc
│ ├── usw2
│ └── global
│ └── iam
├── account2
└── tfbackend
.
├── iam.tf
├── main.tf
├── outputs.tf
└── variables.tf
• 공용 리소스의 폴더 분리 관리
• VPC, database, container clusters, …
• 마이크로 서비스 별 분리 관리
• Pluggable하도록 모듈 이용, 버전 관리
• 마이크로 서비스의 모든 리소스 포함 (IAM!!)
source = "git@github.com:example/microservice1.git?ref=v0.1.0"
17. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
• microservice1-repo
.
├── iam.tf
├── s3.tf
├── servergroup.tf
├── outputs.tf
└── variables.tf
마이크로서비스 리소스
…
완전 분산형 저장소 구조
• 환경별 코드 관리
• git branch/tag 활용
• 환경별 Terraform state 관리
• Terraform workspace 활용
• Terraform apply 자동화!!
• Terraform enterprise
• Atlantis
• CI tools
Remote state 참조
모듈
사용
• vpc-module-repo
.
├── vpc.tf
├── subnets.tf
├── outputs.tf
└── variables.tf
공통 모듈
…
• vpc-repo
.
├── vpc.tf
├── outputs.tf
└── variables.tf
공용 리소스
…
모듈 사용
18. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Agenda
IaC를 통한 대규모 마이크로 서비스 인프라 운영
운영을 고려한 Terraform 코드 구조화
Terraform 모듈 코드 테스트
19. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
v0.2
Terraform 모듈 코드도 테스트?
vpc
loadbalancer
servergroup
v0.10
• ap-northeast-2
v0.11
• us-east-2
v0.2
20. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Terraform 코드 테스트
• Linting
• 정적 코드 분석을 통해 잠재적 버그 리포트
• terraform fmt
• terraform validate
• Dry-run
• terraform plan
• 단위/통합 테스트
• TDD (Test Driven Development)
• kitchen-terraform
• InSpec, awspec, terratest
• 보안 준수 검수
• terraform-compliance
• Inspec-Iggy
우선
Terraform 모듈
단위 테스트 부터!!
21. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
KitchenCI
• https://kitchen.ci
• 다양한 테스트 환경 지원을 위한 Plugin 구조
• Driver
• 테스팅을 위한 인프라 구성
• Ec2, DigitalOcean, Vagrant
• Docker, openstack, terraform
• Provisioner
• 테스팅 인프라에 설정 적용
• Ansible, chef, puppet, shell
• Verifier
• 실제 테스트 suite 수행
• Shell, InSpec, ServerSpec
22. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Infrastructure testing frameworks
• InSpec
• https://www.inspec.io/
• RSpec 기반 Auditing and Testing framework
• Support
• OS resources / AWS / Azure / GCP
• awspec
• https://github.com/k1LoW/awspec
• AWS 리소스를 위한 RSpec 테스트 툴
• terratest
• https://github.com/gruntwork-io/terratest
• gruntwork에서 만든 인프라 테스트용 Go 라이브러리
awspec
terratest
23. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
InSpec vs. awspec vs. terratest
InSpec awspec terratest
대상 리소스
• OS 리소스
• 인프라 리소스
(AWS/GCP/Azure)
• 인프라 리소스
(AWS only)
• Go 라이브러리로 접근
가능한 모든 리소스
• 인프라 리소스
(AWS, GCP)
• K8s, Helm chart
• Packer, docker
• …
AWS 리소스 속성 coverage • 부족 • 충분 • 충분
테스트 작성 언어 • Ruby (Rspec기반) • Ruby (Rspec기반) • Go
테스트 작성 난이도 • 쉬움 • 쉬움 • 어려움
KitchenCI 통합
• Kitchen-inspec
• Kitchen-terraform
• Kitchen-verifier-
awspec
• 필요 없음
24. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Terraform module testing pipeline
• Terraform module에 대한 단위 테스트
• 자동화된 테스트 환경 생성, 검증 및 삭제
• 다양한 입력 조건에 대한 기능 검증
• 다양한 환경에 대한 사전 테스트
• Terraform version
• Cloud provider region
terraform
코드 변경
Kitchen-terraform (driver plugin)
awspec (verifier plugin)
(verifier plugin)
25. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
다양한 Terraform provider
• Github
• Datadog
• Grafana
• PagerDuty
• MySQL
• PostgreSQL
• PowerDNS
…
• Community providers
• Spinnaker
• Kafka
• Keycloak
• Kibana
• Kong
• Okta
…
https://www.terraform.io/docs/providers/ https://www.terraform.io/docs/providers/type/community-index.html
26. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Terraform v0.12 개선사항
https://www.hashicorp.com/blog/terraform-0-1-2-preview
더 유연하고 이해하기 쉬운
모듈 코드 작성에 도움
27. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
• 대규모 마이크로서비스 인프라 구성 및 운영
ü Infrastructure as Code 적용하여 함께 레고 조립하듯이
ü 레고 블록과 같이 Terraform 코드 모듈화
ü 단위 테스트로 모듈 품질 확보
마무리하며
https://www.maxpixel.net/Colorful-Play-Children-Lego-Building-Blocks-2285065