2015년 3월 22일 일요일

[Chef] Solo 입문 메모

개요
Chef Solo입문에 관한 메모

참고자료
http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/cloud/automation

본문

- Chef는 Code를 통한 Infrastructure Provisioning 툴이다

- 코드로 정의된 인프라 구조를 Recipe라 한다

- Chef는 Server, Solo라는 두가지형태가 크게 있는데,
   Server는 복수대의 서버를 유지관리할 수 있는 기능까지 갖추고 있고,
   Solo는 1개 서버의 설정만 해당된다. Solo는 Server의 일부라 할 수 있다.

- Ruby에 기반한다

- 유사 툴로서 Puppet이 대표적이다.

- Facebook의 인프라 유지에 이용되고 있다.

- Idempotent. 멱등성이 보장된 환경. 같은 코드를 몇번 돌리든 코드가 의도하는 바가 유지된다.

Solo 도입순

chef-dk 인스톨

# path는 https://downloads.chef.io/chef-dk/ 에서
wget https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chefdk-0.4.0-1.x86_64.rpm
rpm -ivh chefdk-0.4.0-1.x86_64.rpm 

chef-repo init
# {REPONAME}에 리포지토리의 이름을 지정한다.
chef generate repo {REPONAME}

knife 설정
knife configure

Cookbook 작성
cd {REPONAME}
knife cookbook create {COOKBOOK_NAME} -o cookbooks

recipe json 작성
{PATH-TO-REPO}/localhost.json
{
 "run_list" : [
 "recipe[hello]"
 ]

}

recipe ruby 작성
# solo.rb
file_cache_path "/tmp/chef-solo"

cookbook_path ["/root/{REPONAME}/cookbooks"]

chef 실행
{PATH-TO-REPO}$ sudo chef-solo -c solo.rb -j localhost.json 

Starting Chef Client, version 12.0.3
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * log[hi there.] action write
  

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 2.792128558 seconds

패키지의 설정 사례

default.rb에 대해 다음과 같은 예제를 추가해보도록 하자

log "hi there"

execute 'yum update -y' do
  ignore_failure true
end

package "ImageMagick" do
 action :install
end

yum update실시후, 임의의 패키지 매니저로부터 ImageMagick을 인스톨 하는 설정이다.
패키지의 인스톨 자체에 관해서는 특정 패키지 매니저를 지정하지 않는다.

yum이 아닌 apt-get(ubuntu)이나 homebrew(OSX)와 같은 환경이라면 해당 update는 제각각 추가를 해주고, 다만 실패하는 경우를 대비해서 ignore_failure true옵션을 걸어줌으로서 환경에 독립적인 스크립트를 작성할 수 있다.

chef 실행
{PATH-TO-REPO}$ sudo chef-solo -c solo.rb -j localhost.json

Starting Chef Client, version 12.0.3
Compiling Cookbooks...
Converging 3 resources
Recipe: hello::default
  * log[hi there] action write
  
  * execute[yum update -y] action run
    - execute yum update -y
  * yum_package[ImageMagick] action install
    - install version 6.7.8.9-10.15.amzn1 of package ImageMagick

Running handlers:
Running handlers complete

Chef Client finished, 3/3 resources updated in 14.833352817 seconds

댓글 없음:

댓글 쓰기