ページ

2015年5月16日土曜日

新しい技術を学んでみる 2 - chef -

今回は「chef」を学んでみる


あの料理するシェフという事で、コマンドもナイフやクックブックなど
しゃれんとんしゃー名前のコマンドがあります。

・概要
   インフラをコードで記述

・公式サイト

・用語
workstation
 ローカルPC
   cookbooks
   knifeコマンドでサーバへ送る
   
chef server
 今回未使用

nodes
 各サーバ

さっそく環境を作ってみる。
公式サイトからMac用のchefをダウンロード

















そのほか便利という噂のknife-soloもインストール
$ chef gem install knife-solo
Fetching: knife-solo-0.4.2.gem (100%)
WARNING:  You don't have /Users/{ユーザ名}/.chefdk/gem/ruby/2.1.0/bin in your PATH,
  gem executables will not run.
Thanks for installing knife-solo!

If you run into any issues please let us know at:
  https://github.com/matschaffer/knife-solo/issues

If you are upgrading knife-solo please uninstall any old versions by
running `gem clean knife-solo` to avoid any errors.

See http://bit.ly/CHEF-3255 for more information on the knife bug
that causes this.
Successfully installed knife-solo-0.4.2
1 gem installed

環境設定。全部エンターでいいらしい。
$ knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/Users/{ユーザ名}/.chef/knife.rb] 
Please enter the chef server URL: [https://MacBook-Air.local:443] 
Please enter an existing username or clientname for the API: [ユーザ名] 
Please enter the validation clientname: [chef-validator] 
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem] 
Please enter the path to a chef repository (or leave blank): 
*****

You must place your client key in:
  /Users/{ユーザ名}/.chef/{ユーザ名}.pem
Before running commands with Knife!

*****

You must place your validation key in:
  /etc/chef-server/chef-validator.pem
Before generating instance data with Knife!

*****
Configuration file written to /Users/{ユーザ名}/.chef/knife.rb

chefテスト用のvagrantを作成
$ cd cheftest/
$ ls
$ vagrant box list
centos6 (virtualbox, 0)
jenkins (virtualbox, 0)
$ vagrant init centos6
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ ls
Vagrantfile
$ vi Vagrantfile 
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos6'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: cheftest_default_1431773818840_89626
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/{ユーザ名}/Documents/develop/vagrant/boxes/cheftest


vagrantのssh接続の際にホスト名を設定してやる
$ vagrant ssh-config --host chopper >> ~/.ssh/config
$ ssh chopper
Last login: Sat May 16 11:05:20 2015 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.

「chopper」とういホスト名で普通にssh接続できるようになった

環境ができたので、実際に使って見る
1. chefのリポジトリ(cookbook)を作る(Mac=workstation側)
$ knife solo init chef-repo
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...
Setting up Berkshelf...

Finderで確認してみる。























実際にcookbookを作成していくのは赤枠の「site-cookbooks」を使用

2. Nodeをchef対応にする
$ ls
Vagrantfile chef-repo
$ cd chef-repo/
$ knife solo prepare chopper
Bootstrapping Chef...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18736  100 18736    0     0   2915      0  0:00:06  0:00:06 --:--:-- 36881
Downloading Chef 12.3.0 for el...
downloading https://www.opscode.com/chef/metadata?v=12.3.0&prerelease=false&nightlies=false&p=el&pv=6&m=x86_64
  to file /tmp/install.sh.2839/metadata.txt
trying wget...
url https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.3.0-1.el6.x86_64.rpm
md5 c19fefcb3d033107e9fbdb3839312584
sha256 4b7c846a9ad93564cc203a5ac99890431f7d6ad159c424aa89827fd772c9881d
downloaded metadata file looks valid...
downloading https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.3.0-1.el6.x86_64.rpm
  to file /tmp/install.sh.2839/chef-12.3.0-1.el6.x86_64.rpm
trying wget...
Comparing checksum with sha256sum...
Installing Chef 12.3.0
installing with rpm...
警告: /tmp/install.sh.2839/chef-12.3.0-1.el6.x86_64.rpm: ヘッダ V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
準備中...                ########################################### [100%]
   1:chef                   ########################################### [100%]
Thank you for installing Chef!
Generating node config 'nodes/chopper.json'...

3. cookbookを作る
簡単なhello worldを表示させるcookbookを作る
$ knife cookbook create hello -o site-cookbooks/
** Creating cookbook hello in /Users/{ユーザ名}/Documents/develop/vagrant/boxes/cheftest/chef-repo/site-cookbooks
** Creating README for cookbook: hello
** Creating CHANGELOG for cookbook: hello
** Creating metadata for cookbook: hello

「knife cookbook create {名前} -o {出力先}」コマンドで作成





















chef-repo/site-cookbooks/{名前}/recipes/default.rbに「log “Hello world"」を追加


















chef-repo/node/chopper.jsonに赤枠を追加
レシピを走らせるようにする。


4. cookbookをnodeに反映させる
$ knife solo cook chopper
Running Chef on chopper...
Checking Chef version...
Installing Berkshelf cookbooks to 'cookbooks'...
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Community Site (site :opscode). Site locations have been replaced by the source location. Change this to: 'source "https://supermarket.chef.io"' to remove this warning. For more information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations
Resolving cookbook dependencies...
Uploading the kitchen...
WARNING: Local cookbook_path '/Users/{ユーザ名}/Documents/develop/vagrant/boxes/cheftest/chef-repo/cookbooks' does not exist
Generating solo config...
Running Chef...
Starting Chef Client, version 12.3.0
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * log[Hello world] action write
  

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

knife solo cook chopperを実行
WARNINGが出ているが、一応LogでHello worldが出力されました^^;

0 件のコメント:

コメントを投稿