LoginSignup
4
4

More than 5 years have passed since last update.

Chefを使ってみる - お試し編(1)

Last updated at Posted at 2014-12-03

導入編ではChefを使う準備を実施した。
お試し編ではEpelのレポジトリを指定し、Nginxを適用するようなレシピを記載し、実際にリモートサーバへデプロイを実施する。

その前に

Chefを使うときのおおまかな流れの備忘録

  • [Cookbook作成]-[Berksfile記載(必要に応じて)]-[nodeオブジェクト作成]-[レシピ作成]-[Attribute作成]-[template作成]-[デプロイ準備]-[デプロイ]
  • 順番の前後とかEnvironment設定などもあるけど、基本のフローはこんな感じ。

前提

  • Chefの作業ディレクトリは/var/chef-repoとする
  • リモートサーバ名はremote-hogehogeとする(名前解決はできているものとする)
  • Chef実行ユーザはusr-fugafugaとする
  • remote-hogehoge上には予めusr-fugafugaを作成し、sudores設定(sudoパスワード無し実行許可)を実施しておく

Nginx用にクックブックなどを準備

  1. 作業ディレクトリ移動

    
    $ pwd
    /var/chef-repo
    
  2. Berksfileを作成する
    NginxはEPELリポジトリからインストールを実施するため、Nginxの依存関係としてEPELを指定する

    
    $ cat Berksfile
    source "https://supermarket.getchef.com"
     
    cookbook "yum-epel"
    cookbook "nginx" , path: "./site-cookbooks/nginx"
    
  3. berks vendorを実行し、各クックブックなどを作成する

    
    $ berks vendor ./cookbooks
    Fetching 'nginx' from source at site-cookbooks/nginx
    Using nginx (0.1.0) from source at site-cookbooks/nginx
    Using yum (3.4.1)
    Using yum-epel (0.5.2)
    Vendoring nginx (0.1.0) to cookbooks/nginx
    Vendoring yum (3.4.1) to cookbooks/yum
    Vendoring yum-epel (0.5.2) to cookbooks/yum-epel$ ls
    berks-cookbooks  Berksfile  Berksfile.lock  site-cookbooks
    
  4. nginxのRecipeを作成

    
    $ cat site-cookbooks/nginx/recipes/default.rb
    include_recipe "yum-epel"
    package "nginx" do
    action :install
    end
     
    service "nginx" do
    action [ :enable, :start ]
    supports :status => true, :restart => true, :reload => true
    end
    
  5. nodeオブジェクトを作成

    
    $ mkdir nodes
    $ cat nodes/AP-Test.json
    {
    "run_list": [
        "recipe[yum-epel]",
        "recipe[nginx]"
    ]
    }
    
  6. .chef/knife.rb編集
    knife-soloが利用するテンポラリディレクトリのパスを設定

    
    $ cat .chef/knife.rb
    cookbook_path    ["cookbooks", "site-cookbooks"]
    node_path        "nodes"
    role_path        "roles"
    environment_path "environments"
    data_bag_path    "data_bags"
    #encrypted_data_bag_secret "data_bag_key"
     
    knife[:berkshelf_path] = "cookbooks"
    

リモートサーバへデプロイ実施

  1. リモートサーバへChef-clientインストール

    
    $ knife solo bootstrap -i ~/.ssh/usr-fugafuga.pem usr-fugafuga@remote-hogehoge
    
  2. デプロイ実施

    
    $ knife solo cook -i ~/.ssh/usr-fugafuga.pem usr-fugafuga@remote-hogehoge
    Starting Chef Client, version 11.16.4
    Compiling Cookbooks...
    Converging 3 resources
    Recipe: yum-epel::default
    * yum_repository[epel] action create
    * template[/etc/yum.repos.d/epel.repo] action create
      - create new file /etc/yum.repos.d/epel.repo
      - update content in file /etc/yum.repos.d/epel.repo from none to 39e29f
      --- /etc/yum.repos.d/epel.repo    2014-11-25 07:06:47.378441508 +0000
      +++ /tmp/chef-rendered-template20141125-3626-rf9pee       2014-11-25 07:06:47.383441510 +0000
      @@ -1 +1,12 @@
      +# This file was generated by Chef
      +# Do NOT modify this file by hand.
      +
      +[epel]
      +name=Extra Packages for Enterprise Linux 6 - $basearch
      +enabled=1
      +failovermethod=priority
      +gpgcheck=1
      +gpgkey=https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
      +mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
      +sslverify=true
      - change mode from '' to '0644'
    * execute[yum-makecache-epel] action run
      - execute yum -q makecache --disablerepo=* --enablerepo=epel
    * ruby_block[yum-cache-reload-epel] action create
      - execute the ruby block yum-cache-reload-epel
    * execute[yum-makecache-epel] action nothing (skipped due to action :nothing)
    * ruby_block[yum-cache-reload-epel] action nothing (skipped due to action :nothing)
     
    Recipe: nginx::default
    * package[nginx] action install
    - install version 1.0.15-10.el6 of package nginx
    * service[nginx] action enable
    - enable service service[nginx]
    * service[nginx] action start
    - start service service[nginx]
     
    Running handlers:
    Running handlers complete
    Chef Client finished, 7/7 resources updated in 14.866675586 seconds
    
4
4
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
4
4