LoginSignup
45
44

More than 5 years have passed since last update.

chef-solo導入メモ

Last updated at Posted at 2013-05-27

chef

入門Chef Solo - Infrastructure as Code
を読んで試した事のメモです。
chef soleでvagrantで作成したサーバーにnginxを動かすまでのメモです。

レポジトリ(キッチン) > クックブック > レシピ

説明
レポジトリ knife solo init test-chefで作成する。(test-chef/)
クックブック $ knife cookbook create hello -o cookbooks で作成する。(test-chef/cookbooks/hello)
レシピ test-chef/cookbooks/hello/recipes/default.rb

冪等性 (idempotence)

wikipedia

ある操作を1回行っても複数回行っても結果が同じであることをいう概念である。

1. vagrant導入

chefを試すのに便利らしいので、vagrantという仮想pcを簡単に作成できるgemを入れる。

  1. https://www.virtualbox.org/wiki/Downloadsでvmwareをダウンロード。

  2. vagrantのinstall

    $ gem install vagrant
    
  3. http://www.vagrantbox.es/
    から使いたいOSを選択。
    私はCentOS 6.3 x86_64 minimalを選択しました。
    (https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box)

    Vagrant Documentation - Documentation - Boxes

    $ vagrant box add base https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box # baseという名前で作成する。
    $ mkdir /Users/daichi/Documents/vargrant/base
    $ cd /Users/daichi/Documents/vargrant/base
    $ vagrant init #defaultではinitはbaseで作成される。 
    $ vim Vagrantfile
      config.vm.network :hostonly, "192.168.50.12"  #追加
    
    #Vagrantfileのあるdirectoryで実行する
    $ vagrant up #サーバーを起動する
    $ vagrant ssh #ssh loginする
    $ vagrant halt #一時停止
    $ vagrant destroy #削除
    

    vagrantをsshでログイン出来るようにする。
    $ vagrant ssh-config --host melody >> ~/.ssh/config
    $ ssh melody
    

    melodyはsshでログインするときの名前です。好きに決められます。


    sandbox環境を作るプラグインsahara

    osの状態を保持してある状態から試行錯誤できる。

    $ vagrant gem install sahara # saharaをインストール
    
    #以下Vagrantfileがあるdirectoryで
    $ vagrant sandbox on # sandboxモードを有効にする
    $ vagrant sandbox rollback # sandbox on した所まで OSの状態を戻す
    $ vagrant sandbox commit # OSの状態変更を確定
    $ vagrant sandbox off # sandbox モードを解除 
    $ vagrant sandbox status #sandboxモードか確認
    

    vagrantの初期rootパスワードは'vagrant'です。

2. knife-soloでnginxをサーバーにインストールしてみる

*先ほどvagrant upしたmelodyサーバーを利用する。

$ gem install knife-solo 
$ knife configure#とりあえず全部enterをおした。
$ knife solo init test-chef #test-chef cookbook作成
$ cd test-chef/
$ knife solo prepare melody #remoteサーバー(melody)にchefをインストールする。nodesに新しく追加する。

レポジトリ(キッチン)の構成

test-chef/
test-chef/
├── cookbooks/ #外部からダウンロードしたcookbook
├── data_bags/
├── nodes/ #書くサーバーごとの設定
│   └── melody.json #melodyサーバーで実行する設定
├── roles/
├── site-cookbooks/ #自分で設定するcookbook
└── solo.rb

nodesはノード(ipとかホスト名)ごとの JSON ファイルの格納場所。
knife solo cook melody とかでここを見る。

cent osのyumのEPELをchefでやる。

Opscode Communityにsign upする。

sign upしたら認証key をダウンロードします。
Logged in as: user みたいなところをクリックして get private key をクリックするといけます。

そこでGet a new keyを押すと認証keyがダウンロードされるのでそのファイルを以下のようにします。
(私の場合はprinum.pem)

$ cp /Users/daichi/Downloads/prinum.pem ~/.chef/
$ chmod 600 ~/.chef/prinum.pem 
$ vim  ~/.chef/knife.rb #下記の内容を追加する
  client_key               '/Users/daichi/.chef/prinum.pem'
  cookbook_path            [ './cookbooks' ] #recipeをrootと見た時の相対path

test-chef/ に移動してyumクックブックをインストール

$ cd test-chef/
$ knife cookbook site vendor yum
$ vim nodes/melody.json #下に内容あります。
$ knife solo cook melody #melody.jsonの設定が実行される。
melody.json
{
  "run_list":[
    "yum::epel"
  ]
}

これで$ yum repolistをサーバーで実行すると
* epel: ftp.iij.ad.jp
が追加されているのが確認できる。

iptablesの設定を無効化

cent osはデフォルトで、iptablesが有効になっており、
80番ポートでnginxにアクセス出来ないので、実験環境だけは無効化します。
セキュリティ上危ないので、実際に公開するサーバーではきっちり設定する。

$ knife cookbook create nginx -o site-cookbooks  #site-cookbooksにnginx
$ knife cookbook create iptables -o site-cookbooks

次のように編集する。

site-cookbooks/nginx/recipes/default.rb
package "nginx" do 
  action :install
end

service "nginx" do
  supports :status => true,
           :restart => true,
           :reload => true 
  action [:enable, :start]
end

template "nginx.conf" do
  path "/etc/nginx/nginx.conf" 
  source "nginx.conf.erb"
  owner "root"
  group "root"
  mode 0644
  notifies :reload , 'service[nginx]'
end
site-cookbooks/nginx/templates/default/nginx.conf.erb
user             nginx;
worker_processes 1;
error_log        /var/log/nginx/error.log; 
pid              /var/run/nginx.pid;
events{
  worker_connections 1024;
}

http {
  include /etc/nginx/mime.types; 
  default_type application/octet-stream;
  server {
    listen <%= node['nginx']['port'] %>; 
    server_name localhost;
    location / {
      root /usr/share/nginx/html;
      index index.html index.htm; 
    }
  } 
}
site-cookbooks/iptables/recipes/default.rb
service 'iptables' do
  action [:disable, :stop]
end
test-chef/nodes/melody.json
{
  "nginx": {
    "port" : 80 
  },
  "run_list":[
    "yum::epel",
    "nginx",
    "iptables"
  ]
}

実行

$ knife solo cook melody

うまく設定できたら、
http://192.168.50.12/
でnginxのdefault pageにアクセス出来ます。

45
44
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
44