37
What is Chef? Infrastructure management framework 中身は日本語です。 Monday, March 4, 13

What is chef

Embed Size (px)

DESCRIPTION

OpscodeのChefとは何なのかを中級者以上向けに説明します。 普通のHow to とは方向性が違うので注意。

Citation preview

Page 1: What is chef

What is Chef?Infrastructure management framework

※中身は日本語です。

Monday, March 4, 13

Page 2: What is chef

Information

• OpscodeのChefとは何なのかを中級者以上向けに説明しますhttp://www.opscode.com/chef/

• Author Yukihiko SawanoboriHiganWorks LLC(Japan)

Monday, March 4, 13

Page 3: What is chef

Index

1. Introduction2. Inventory3. Convergence 4. Resource Correction5. Configration Management

(Automaticaly/Configration first)

Monday, March 4, 13

Page 4: What is chef

1.Introduction

Monday, March 4, 13

Page 5: What is chef

Q. Chefってサーバの自動セットアップツール

ですよね?

Monday, March 4, 13

Page 6: What is chef

A. 違います。環境のメタデータを管理し、ノードの役割を収束させる

OPSのフレームワークです。

※別にただの自動ビルドツールとして扱っても構いませんMonday, March 4, 13

Page 7: What is chef

Q. DevOpsってよく言うしDeveloperとOperaterが仲良く作ったのかな?

Monday, March 4, 13

Page 8: What is chef

A. どちらかというとOPSの怒りが集積されたツールだと思います

※OhaiとChef::Providersのソースからは特に怨念を感じます※DevOpsの本質は特定のツールと関係ありません

Monday, March 4, 13

Page 9: What is chef

Chef認識正誤集1

✕ レシピで華麗にサーバを自動設定

◯ 泥臭いプラットフォーム判別と、地べたを這いずるようなリソース判定を元に矯正を施します

✕ ChefServerはあまり必要ない

◯ ChefServerのインベントリこそ全て

Monday, March 4, 13

Page 10: What is chef

Chef認識正誤集2

✕ Cookbookにミドルウェアのサーバ別固有情報も書く

◯ Role/NodeのOverride AttributeやChefServerへのク

エリを使い、Cookbookは汎用的に書くべきです

✕ サーバ設定の変更をしたら別で作っているシステムの

構成管理台帳を更新する

◯ サーバへの直接ログインを含め、ChefServer上の構

成管理だけで終わらせるのが理想です。

Monday, March 4, 13

Page 11: What is chef

2.Inbentory

Monday, March 4, 13

Page 12: What is chef

まずはインベントリ収集から

• Chef-Client(Chef-solo)が稼働するには実行されたプラットホームの判別が最も重要です。

• Chefのマルチプラットフォーム性を支えるためには高度なインベントリの収集力が必要!

Monday, March 4, 13

Page 13: What is chef

そこでOhaiです• Ohai (https://github.com/opscode/ohai)

• quot: Ohai detects data about your operating system.

• 参考資料:Chefの心臓、Ohaiのアトリビュート(以下略) http://qiita.com/items/

5ce72101f8dee906ccb4• OS/ディストリビューションをはじめ、動作環境を泥臭く判別

Monday, March 4, 13

Page 14: What is chef

Ohaiのソース抜粋、プラットフォーム判別の一部

#  platform  [  and  platform_version  ?  ]  should  be  lower  case  to  avoid  dealing  with  RedHat/Redhat/redhat  matching  if  File.exists?("/etc/oracle-­‐release")    contents  =  File.read("/etc/oracle-­‐release").chomp    platform  "oracle"    platform_version  get_redhatish_version(contents)elsif  File.exists?("/etc/enterprise-­‐release")    contents  =  File.read("/etc/enterprise-­‐release").chomp    platform  "oracle"    platform_version  get_redhatish_version(contents)elsif  File.exists?("/etc/debian_version")    #  Ubuntu  and  Debian  both  have  /etc/debian_version    #  Ubuntu  should  always  have  a  working  lsb,  debian  does  not  by  default    if  lsb[:id]  =~  /Ubuntu/i        platform  "ubuntu"        platform_version  lsb[:release]    else          if  File.exists?("/usr/bin/raspi-­‐config")            platform  "raspbian"        else            platform  "debian"        end        platform_version  File.read("/etc/debian_version").chomp    endelsif  File.exists?("/etc/redhat-­‐release")    contents  =  File.read("/etc/redhat-­‐release").chomp    platform  get_redhatish_platform(contents)    platform_version  get_redhatish_version(contents)elsif  File.exists?("/etc/system-­‐release")    contents  =  File.read("/etc/system-­‐release").chomp    platform  get_redhatish_platform(contents)    platform_version  get_redhatish_version(contents)elsif  File.exists?('/etc/gentoo-­‐release')    platform  "gentoo"

まるで薄氷を踏むような...地道な虱潰し

Monday, March 4, 13

Page 15: What is chef

AWS(EC2)上かどうかだって検出するよ!

def  has_ec2_mac?    network[:interfaces].values.each  do  |iface|        unless  iface[:arp].nil?            if  iface[:arp].value?("fe:ff:ff:ff:ff:ff")                Ohai::Log.debug("has_ec2_mac?  ==  true")                return  true            end        end    end    Ohai::Log.debug("has_ec2_mac?  ==  false")    falseend

def  looks_like_ec2?    #  Try  non-­‐blocking  connect  so  we  don't  "block"  if      #  the  Xen  environment  is  *not*  EC2    hint?('ec2')  ||  has_ec2_mac?  &&  can_metadata_connect?(EC2_METADATA_ADDR,80)end

if  looks_like_ec2?    Ohai::Log.debug("looks_like_ec2?  ==  true")

やっぱり地道に。

Monday, March 4, 13

Page 16: What is chef

プラットフォームを判別すると

• パッケージ管理システムがわかる

• サービスの管理方法がわかる

• 環境特有の落とし穴も事前に回避する

• Ex) ec2ならipでなくpublic hostname

を主な接続先に使うなど

Monday, March 4, 13

Page 17: What is chef

Cookbook DSLでの例

たとえばコレだけでも

• Redhat系ならyum/rpmを使って

• Debian系ならapt/debを使って

• Solaris系ならpkgin/pkgを使って

Chef::Runnerが標準パッケージシステムからnginxをインストールした状態にノードを収束させます。

package ‘nginx’ do action :installend

Monday, March 4, 13

Page 18: What is chef

Inventory重要

• Chef-client/Chef-soloの動作に重要

• ChefServerに集約されたInventoryはSearchAPIによる自動環境構築にとってもユースフル

Monday, March 4, 13

Page 19: What is chef

3.Convergence

Monday, March 4, 13

Page 20: What is chef

✕ Build / Setup◯ Convergence

Monday, March 4, 13

Page 21: What is chef

Convergenceとは• Chefではサーバ設定の変更を

Convergence(収束)と呼んでいます。

• (思想として)スクリプトを実行してサーバをセットアップするのではなく、Clientの実行によってサーバの状態をレシピに書かれている通り収束させることを表現しているためです。

Monday, March 4, 13

Page 22: What is chef

どのような手法か

• サーバの状態を取得し、レシピに書かれた内容と比較。

• レシピと違えば「合わせる」、これの繰り返しで設定を”収束”させます。(※Cookbookは冪等に記述する)

• 次のResouce Corectionでも説明します。

Monday, March 4, 13

Page 23: What is chef

4.Resouce Correcton

Monday, March 4, 13

Page 24: What is chef

レシピ適用で重要なResouce/stateという考え方

Monday, March 4, 13

Page 25: What is chef

サーバの要素は全てRESOURCEとして定義

• 種類と要素=リソース

• 例:パッケージ

• インストール状態は? / バージョンは?

• 例:サービス

• 起動中? / 自動起動?

• ファイル

• パスは? / オーナーは? / 中身は?

Monday, March 4, 13

Page 26: What is chef

Curennt ResouceとNew Resouce

1. Client/Soloは目的のリソースを定義=New

Resorce

2. 現在のリソースを取得=Current Resorce

3. Current Resouce の 要素を変更※プラットホーム別に 変更手段は違う

[New Resorce]File (:path => “/etc/hoge”,:owner => “root”,:mode => 0644,:content => “piyo”)

[Current Resorce]File (:path => “/etc/hoge”,:owner => “root”,:mode => 0600,:content => “hoge”)

Converge!

Monday, March 4, 13

Page 27: What is chef

Current Resouce取得例パッケージマネージャ=PackManの場合

パッケージの状態を取得。。                    Chef::Log.debug("#{@new_resource}  checking  pacman  for  #{@new_resource.package_name}")                    status  =  popen4("pacman  -­‐Qi  #{@new_resource.package_name}")  do  |pid,  stdin,  stdout,  stderr|                        stdout.each  do  |line|                            line.force_encoding(Encoding::UTF_8)  if  line.respond_to?(:force_encoding)                            case  line                            when  /^Version(\s?)*:  (.+)$/                                Chef::Log.debug("#{@new_resource}  current  version  is  #{$2}")                                @current_resource.version($2)                            end

コマンド叩いてパース...

Monday, March 4, 13

Page 28: What is chef

New Resource適用例

パッケージをインストール状態に変更                def  install_package(name,  version)                    run_command_with_systems_locale(                        :command  =>  "pacman  -­‐-­‐sync  -­‐-­‐noconfirm  -­‐-­‐noprogressbar#{expand_options(@new_resource.options)}  #{name}"                    )                end

これもまた泥臭い。。Cronなんかもお勧めの溝さらい検出Source                if  @cron_exists                    unless  cron_different?                        Chef::Log.debug("Skipping  existing  cron  entry  '#{@new_resource.name}'")                        return                    end                    read_crontab.each_line  do  |line|                        case  line.chomp                        when  "#  Chef  Name:  #{@new_resource.name}"                            cron_found  =  true

Monday, March 4, 13

Page 29: What is chef

Resouceと冪等性• 地道なResouce Correction

• CookBookレシピ=New Resource

• Current Resouce を New Resourceと同じStatusに収束させる。

• => Client/Solo は何度実行しても同じ結果、同じ状態になるようにして、常に実行させておくことが大事。

Monday, March 4, 13

Page 30: What is chef

5.Configration Management

(Automaticaly/Configration First)

Monday, March 4, 13

Page 31: What is chef

構成管理を自動化する

Monday, March 4, 13

Page 32: What is chef

構成管理の自動化

ChefServer

Node

Node

Chef-Client

Inventory登録/更新(Ohai収集)

•プラットホーム情報•H/W情報

•N/W情報Attribute OverrideRole付与・Runlist付与

Monday, March 4, 13

Page 33: What is chef

Role[Nagios-Client]

Env / Roleの割り当て例Serverのインベントリを軸に設定更新

ChefServer

NodeNode

Role[Nagios-Server]

Node

Recipe[nagios-server]・Role[Nagis-Client]登録NodeのIPを取得して監視する・監視対象NodeのAttributeから監視項目を設定

Recipe[nagios-client]・Role[Nagis-Server]登録NodeのIP

からのリクエストを許可・監視対象リソースのプラグインをインストール

Monday, March 4, 13

Page 34: What is chef

環境の構成管理をしてサーバ構成を自動で合わせる(Configration Management

First)

Monday, March 4, 13

Page 35: What is chef

Role[Nagios-Client]

新しいNodeにRoleを割り当て=収束して環境に適応

ChefServer

NodeNode

Role[Nagios-Server]

Node

Recipe[nagios-server]・Role[Nagis-Client]登録NodeのIPを取得して監視する・監視対象NodeのAttributeから監視項目を設定

Recipe[nagios-client]・Role[Nagis-Server]登録NodeのIP

からのリクエストを許可・監視対象リソースのプラグインをインストール

NewNode

テキストRoleに追加=Serverにインベントリされる

Nagiosのクライアント関係設定が収束する

Role[Nagios-Server]はClient

のNodeの増減により自動で監視対象の追加削除を行う

Monday, March 4, 13

Page 36: What is chef

構成管理=環境構築

• Chef上の要素を変更=構成管理情報の更新をしたら

• Clientたちが適当に収束してくれる

• 構成管理することが全てにつながる

Monday, March 4, 13

Page 37: What is chef

おわりに

• Chefをただのワンショットビルドツールとして扱うのは間違っちゃ無いが勿体無い。

• Soloで更新をかけていくのも悪くはないが勿体無い。

これってActiveDirectoryなんじゃないの?と思った人、大体あってますよ。

Monday, March 4, 13