menu
書いてる野郎
orebike@gmail.com
もうすでに ver 4.3.20 がインストール済み。アップデートをチェックしても「最新」という答え。公式も 2015-02-03 現在それが最新だったのでOK
やり方は Downloads – Oracle VM VirtualBoxここからダウンロードしてぶっこむだけ
本よりもちょっとだけバージョンが上がってるがバグ取りぐらいの変更だろう。OK。
現状でもうインストールされている。ver 1.6.5。
2015-02-03 現在で最新版が 1.7.2 になってたのでダウンロードして上げる。
Download Vagrant - VagrantここからダウンロードしてpkgドンドンOK
本の中では ver 1.5.1 なのでちょっと開きがある。大丈夫か・・・?
VMそのものの雛形となる Vagrant の Box ファイルをVagrantに登録する。これ登録だけでまだVMは作ってないよ。 これは登録だけなので、別にどこのディレクトリでやってもよい。
自己流ではなくここは完全に本に従う。本に指定されている Box ファイルは、A list of base boxes for Vagrant - Vagrantbox.esの中程に「Opscode centos-6.5」という名前である。
CentOS は 7 から結構変わってるっぽいのでここは従うほうが安牌。
$ vagrant box add opscode-centos-6.5 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box ==> box: Adding box 'opscode-centos-6.5' (v0) for provider: box: Downloading: http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box ==> box: Successfully added box 'opscode-centos-6.5' (v0) for 'virtualbox'!
ダウンロードが始まるので気長に待つ
$ vagrant box list opscode-centos-6.5 (virtualbox, 0)
OK
適当なディレクトリを作る
$ mkdir reading_chef_jissen_nyumon_01 $ cd reading_chef_jissen_nyumon_01
Vagrant 設定ファイルを生成する
$ vagrant init opscode-centos-6.5 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
とりあえず何も書き換えず、デフォルトで起動
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... default: The Berkshelf shelf is at "/Users/hoge/.berkshelf/vagrant-berkshelf/shelves/berkshelf20150204-28654-1iu4smq-default" ==> default: Importing base box 'opscode-centos-6.5'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: reading_chef_jissen_nyumon_01_default_1423023549829_63846 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> 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: 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: Mounting shared folders... default: /vagrant => /Users/hoge/myvagrant/reading_chef_jissen_nyumon_01 $
OK
接続
$ vagrant ssh Last login: Fri Mar 7 16:57:20 2014 from 10.0.2.2 [vagrant@localhost ~]$ cat /etc/redhat-release CentOS release 6.5 (Final)
プロンプトが変わって Vagrant で作った環境に入れたのがわかる。
OK
なんか操作
[vagrant@localhost ~]$ pwd /home/vagrant [vagrant@localhost ~]$ touch hoge.txt [vagrant@localhost ~]$ ls hoge.txt [vagrant@localhost ~]$
とりあえずサーバから出る
[vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed.
止める
$ vagrant halt ==> default: Attempting graceful shutdown of VM...
確認
$ vagrant ssh VM must be running to open SSH connection. Run `vagrant up` to start the virtual machine.
OK止まっているようだ
再起動
$vagrant up
OK
確認
$ vagrant ssh Last login: Wed Feb 4 04:21:31 2015 from 10.0.2.2 [vagrant@localhost ~]$ ls hoge.txt
前回の状態が保たれてるOK。
出る。
[vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed.
破壊する。
$ vagrant destroy default: The Berkshelf shelf is at "/Users/hoge/.berkshelf/vagrant-berkshelf/shelves/berkshelf20150204-28941-hv89e0-default" ==> default: Running cleanup tasks for 'berkshelf'... default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives...
確認
$ vagrant ssh VM must be created before running this command. Run `vagrant up` first.
破壊OK。
再作成
$ vagrant up
OK
確認
$ vagrant ssh Last login: Fri Mar 7 16:57:20 2014 from 10.0.2.2 [vagrant@localhost ~]$ ls [vagrant@localhost ~]$
はい、ファイルがなくなって破壊されて再作成されたことOK
通常のSSHの設定を吐いてくれるようなので確認する
$ vagrant ssh-config --host webdb Host webdb HostName 127.0.0.1 User vagrant Port 2222 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /Users/hoge/myvagrant/reading_chef_jissen_nyumon_01/.vagrant/machines/default/virtualbox/private_key IdentitiesOnly yes LogLevel FATAL
という感じらしいので、これを自分のSSHの設定ファイルに追記する
もうすでにあるので
$ vagrant ssh-config --host webdb >> ~/.ssh/config $ less ~/.ssh/config
確認OK
繋いでみる
$ ssh webdb Last login: Wed Feb 4 04:36:46 2015 from 10.0.2.2 [vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed.
サクッとOK。
自分は今のところローカルローカルでやるつもりなのでこれはほっとく
Chef Solo というのは Chef の設定を駆動するアプリなので構築対象そのものにインストールする
なので接続する
$ ssh webdb
インストールが一発でできるスクリプトを実行
[vagrant@localhost ~]$ curl -L https://www.opscode.com/chef/install.sh | sudo bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 18285 100 18285 0 0 8916 0 0:00:02 0:00:02 --:--:-- 23292 Downloading Chef for el... downloading https://www.opscode.com/chef/metadata?v=&prerelease=false&nightlies=false&p=el&pv=6&m=x86_64 to file /tmp/install.sh.2014/metadata.txt trying wget... url https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.0.3-1.x86_64.rpm md5 3634d1a3b6ae2e5977361075da0f44cc sha256 0ec6162b9d0ca2b2016ff02781d84905f712d64c7a81d01b0df88f977832f310 downloaded metadata file looks valid... downloading https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.0.3-1.x86_64.rpm to file /tmp/install.sh.2014/chef-12.0.3-1.x86_64.rpm trying wget... Comparing checksum with sha256sum... Installing Chef installing with rpm... warning: /tmp/install.sh.2014/chef-12.0.3-1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY Preparing... ########################################### [100%] 1:chef ########################################### [100%] Thank you for installing Chef!
確認
[vagrant@localhost ~]$ chef-solo -v Chef: 12.0.3
本のバージョンは 11.10.4 だったのでちょっと開きがある。
Chefでは、構築する環境の手順を
という階層で管理する。つまり最小の本当の手順が書いてあるのはレシピだってことで、それは Ruby コード。
デフォルトでは、 Chef のリポジトリは /var/chef ディレクトリが使われます。
いきなりリポジトリはディレクトリだと言っているのでリポジトリというのは単なる入れ物だということがわかる。
さっきのインストールで knife というツールが入ってるはずなのでそれを使ってこのリポジトリにクックブックを作る。
確認
[vagrant@localhost ~]$ knife -v Chef: 12.0.3
ということでこれは単体のツールではなくて Chef に必ずついてくるおまけツールのような位置づけなようだ。
作る
[vagrant@localhost ~]$ sudo knife cookbook create hello -o /var/chef/cookbooks WARNING: No knife configuration file found ** Creating cookbook hello in /var/chef/cookbooks ** Creating README for cookbook: hello ** Creating CHANGELOG for cookbook: hello ** Creating metadata for cookbook: hello
確認
[vagrant@localhost ~]$ ls /var/chef/ cookbooks [vagrant@localhost ~]$ ls /var/chef/cookbooks/ hello
OK
[vagrant@localhost ~]$ cd /var/chef/cookbooks/hello/recipes [vagrant@localhost ~]$ sudo vi default.rb
これは普通に Ruby コードなので全部コメントだということ。
追記する
log "Hello, World!"
保存してOK。
[vagrant@localhost ~]$ sudo chef-solo -o hello {:config_missing=>true} [2015-02-04T08:39:22+00:00] WARN: ***************************************** [2015-02-04T08:39:22+00:00] WARN: Did not find config file: /etc/chef/solo.rb, using command line options. [2015-02-04T08:39:22+00:00] WARN: ***************************************** Starting Chef Client, version 12.0.3 [2015-02-04T08:39:24+00:00] WARN: Run List override has been provided. [2015-02-04T08:39:24+00:00] WARN: Original Run List: [] [2015-02-04T08:39:24+00:00] WARN: Overridden Run List: [recipe[hello]] 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 2.360321395 seconds
できたOK。
いきなりパッケージという謎の単語が出てきたのだが気にせず進めることにする
テストとして dstat パッケージをインストールする cookbook を作る
[vagrant@localhost ~]$ sudo knife cookbook create dstat -o /var/chef/cookbooks
OK
[vagrant@localhost ~]$ cd /var/chef/cookbooks/dstat/recipes [vagrant@localhost ~]$ sudo vi default.rb
このように記述
package "dstat" do action :install end
よくある ruby コードに見える
実行する
[vagrant@localhost ~]$ sudo chef-solo -o hello,dstat {:config_missing=>true} [2015-02-04T09:14:53+00:00] WARN: ***************************************** [2015-02-04T09:14:53+00:00] WARN: Did not find config file: /etc/chef/solo.rb, using command line options. [2015-02-04T09:14:53+00:00] WARN: ***************************************** Starting Chef Client, version 12.0.3 [2015-02-04T09:14:55+00:00] WARN: Run List override has been provided. [2015-02-04T09:14:55+00:00] WARN: Original Run List: [] [2015-02-04T09:14:55+00:00] WARN: Overridden Run List: [recipe[hello], recipe[dstat]] Compiling Cookbooks... Converging 2 resources Recipe: hello::default * log[Hello, World!] action write Recipe: dstat::default * yum_package[dstat] action install - install version 0.7.0-1.el6 of package dstat Running handlers: Running handlers complete Chef Client finished, 2/2 resources updated in 10.06425633 seconds
出力を見ると yum で dstat をインストールしているようにみえる。つまりそういうことみたいだ。
確認
[vagrant@localhost ~]$ rpm -q dstat dstat-0.7.0-1.el6.noarch
はい yum でマネージされた rpm が入ってることを確認
つまり、各ディストリビューションのパッケージ管理をChef が抽象化してる
[vagrant@localhost cookbooks]$ sudo chef-solo -o hello,dstat
何度実行してもエラーとかにならない。結果が変わらない。これが「冪等性」
ここから Chef Solo の記述が chef-solo に変わっている何か意味があるんだろうか・・・この揺らぎ。
chef-solo をリモート実行するもの。つまりさっきの knife コマンドとは特になんの繋がりもなし?か?
今回ならばホストである Mac 側に knife-solo を入れて、vagrant で作った vm上の chef-solo をリモコンする
インストール。このへんが ChefDK 版と本とで違うところか。 ChefDK版では各種ツールを駆動するためのRubyがChefDKに同梱されてくるので、ホストのRuby 環境の gem でインストールしない。 ChefDK環境の gem でインストールする。
berkshelfに関しては ChefDK に同梱されてインストール済みなのでほっとく
VMから抜けて
[vagrant@localhost cookbooks]$ exit
ドン
$ chef gem install knife-solo 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
OK
Vagrantfile と同じディレクトリにリポジトリを用意。このリポジトリの内容を使ってリモート・コントロールする。 さっきはデフォルトのリポジトリを使ったので作ってなかった。
$ knife solo init . WARNING: No knife configuration file found Creating kitchen... Creating knife.rb in kitchen... Creating cupboards... Setting up Berkshelf...
イロイロディレクトリができた
さっきはSSHで入ってコマンドでスクリプト起動してインストールしていたが、これを knife-solo でリモート・コントロールでインストールする
ということで一旦デストローイ
$ vagrant destroy $ vagrant up
Chef solo をリモコンでインストール
$ knife solo bootstrap webdb Bootstrapping Chef... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 18285 100 18285 0 0 9730 0 0:00:01 0:00:01 --:--:-- 23352 Downloading Chef 12.0.3 for el... downloading https://www.opscode.com/chef/metadata?v=12.0.3&prerelease=false&nightlies=false&p=el&pv=6&m=x86_64 to file /tmp/install.sh.1975/metadata.txt trying wget... url https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.0.3-1.x86_64.rpm md5 3634d1a3b6ae2e5977361075da0f44cc sha256 0ec6162b9d0ca2b2016ff02781d84905f712d64c7a81d01b0df88f977832f310 downloaded metadata file looks valid... downloading https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.0.3-1.x86_64.rpm to file /tmp/install.sh.1975/chef-12.0.3-1.x86_64.rpm trying wget... Comparing checksum with sha256sum... Installing Chef 12.0.3 installing with rpm... warning: /tmp/install.sh.1975/chef-12.0.3-1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY Preparing... ########################################### [100%] 1:chef ########################################### [100%] Thank you for installing Chef! Generating node config 'nodes/webdb.json'... Running Chef on webdb... 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/hoge/myvagrant/reading_chef_jissen_nyumon_01/cookbooks' does not exist Generating solo config... Running Chef... Starting Chef Client, version 12.0.3 Compiling Cookbooks... Converging 0 resources Running handlers: Running handlers complete Chef Client finished, 0/0 resources updated in 4.193955884 seconds
なんだか見たところ 単に Chef Solo をインストールしてるだけでは無いっぽい。
先ほどと同じように knife コマンドを使う。作る場所が VM かホストかの違いだ。
$ ls Berksfile Vagrantfile data_bags environments nodes roles site-cookbooks
$ knife cookbook create dstat -o site-cookbooks
ということでホスト側では site-cookbooks がデフォルトのリポジトリになるようだ
さっきと同じように default.rb を編集する。今回は Mac 上なので慣れたエディタで編集できる
package "dstat" do action :install end
レシピと言うネタはあるのでどのネタをサーバに反映させるかを記述するのが Node オブジェクト
ファイルとしては nodes ディレクトリ以下の webdb.json にあるので編集して dstat の cookbook を使うようにする。 内容を確認するとデフォルト状態が本と少し違う。このへんにバージョンの違いがでているのか?
{ "run_list": [ ], "automatic": { "ipaddress": "webdb" } }
dstat の cookbook を追加する
{ "run_list": [ "recipe[dstat]" ], "automatic": { "ipaddress": "webdb" } }
$ knife solo cook webdb Running Chef on webdb... 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/hoge/myvagrant/reading_chef_jissen_nyumon_01/cookbooks' does not exist Generating solo config... Running Chef... Starting Chef Client, version 12.0.3 Compiling Cookbooks... Converging 1 resources Recipe: dstat::default * yum_package[dstat] action install - install version 0.7.0-1.el6 of package dstat Running handlers: Running handlers complete Chef Client finished, 1/1 resources updated in 6.645357015 seconds
OK
確認してみる
$ ssh webdb [vagrant@localhost ~]$ rpm -q dstat dstat-0.7.0-1.el6.noarch
OK
今のVM継続で
ホスト側に knife で作る
$ knife cookbook create apache -o site-cookbooks $ knife cookbook create mysql -o site-cookbooks
そのまんま nodes/webdb.json に追記
{ "run_list": [ "recipe[dstat]", "recipe[apache]", "recipe[mysql]" ], "automatic": { "ipaddress": "webdb" } }
インストール指定と有効化してGo!
package "httpd" do action :install end service "httpd" do action [ :enable, :start ] end
しかし、パッケージ名がどのディストリビューションでも httpd なわけじゃないよね? このへんの中途半端抽象化どうなの?これだと Chef 側の負担ってでかくない?
これ Ruby コードで上から順にメソッドを「実行」しているので定義っぽく見えるけど実行順は単純に上から順番。
インストール指定と有効化してGo!
package "mysql-server" do action :install end service "mysqld" do action [ :enable, :start ] end
$ knife solo cook webdb
OK
確認
$ ssh webdb [vagrant@localhost ~]$ ps auxw | egrep "(httpd|mysql)"
OK なんか動いている
ネットワーク設定やってないから飛ばす
設定ファイルを取ってくる
共有ディレクトリ
[vagrant@localhost ~]$ ls /vagrant/ Berksfile Vagrantfile data_bags environments nodes roles site-cookbooks
ここが Mac 側と繋がってることがわかる
Apache の設定ファイルをコピーする
[vagrant@localhost ~]$ cp /etc/httpd/conf/httpd.conf /vagrant/site-cookbooks/apache/templates/default/httpd.conf.erb
コピーした設定を編集する
# # ServerName gives the name and port that the server uses to identify itself. # This can often be determined automatically, but we recommend you specify # it explicitly to prevent problems during startup. # # If this is not set to valid DNS name for your host, server-generated # redirections will not work. See also the UseCanonicalName directive. # # If your host doesn't have a registered DNS name, enter its IP address here. # You will have to access it by its address anyway, and this will make # redirections work in a sensible way. # #ServerName www.example.com:80 ServerName webdb:80
ちょっと追記
この設定ファイルをコピーする記述をレシピに追記
package "httpd" do action :install end service "httpd" do action [ :enable, :start ] end template "httpd.conf" do path "/etc/httpd/conf/httpd.conf" owner "root" group "root" mode 0644 notifies :reload, 'service[httpd]' end
このへんが文字列がシングルクォートとかダブルとか数値とかシンボルに揺らぐあたりがよくわからない。意味ある?
っで実行
$ knife solo cook webdb
確認
[vagrant@localhost ~]$ less /etc/httpd/conf/httpd.conf
コピーされている OK
デストローイ
$ vagrant destroy -f
作り直し
$ vagrant up
一気構築 Chef Solo インスコからNode適用まで
$ knife solo bootstrap webdb Bootstrapping Chef... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 18285 100 18285 0 0 4359 0 0:00:04 0:00:04 --:--:-- 5925 Downloading Chef 12.0.3 for el... downloading https://www.opscode.com/chef/metadata?v=12.0.3&prerelease=false&nightlies=false&p=el&pv=6&m=x86_64 to file /tmp/install.sh.1992/metadata.txt trying wget... url https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.0.3-1.x86_64.rpm md5 3634d1a3b6ae2e5977361075da0f44cc sha256 0ec6162b9d0ca2b2016ff02781d84905f712d64c7a81d01b0df88f977832f310 downloaded metadata file looks valid... downloading https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.0.3-1.x86_64.rpm to file /tmp/install.sh.1992/chef-12.0.3-1.x86_64.rpm trying wget... Comparing checksum with sha256sum... Installing Chef 12.0.3 installing with rpm... warning: /tmp/install.sh.1992/chef-12.0.3-1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY Preparing... ########################################### [100%] 1:chef ########################################### [100%] Thank you for installing Chef! Running Chef on webdb... 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/hoge/myvagrant/reading_chef_jissen_nyumon_01/cookbooks' does not exist Generating solo config... Running Chef... Starting Chef Client, version 12.0.3 Compiling Cookbooks... Converging 6 resources Recipe: dstat::default * yum_package[dstat] action install - install version 0.7.0-1.el6 of package dstat Recipe: apache::default * yum_package[httpd] action install - install version 2.2.15-39.el6.centos of package httpd * service[httpd] action enable - enable service service[httpd] * service[httpd] action start - start service service[httpd] * template[httpd.conf] action create - update content in file /etc/httpd/conf/httpd.conf from beb8a6 to 2b811c --- /etc/httpd/conf/httpd.conf 2014-08-15 06:57:48.000000000 +0000 +++ /tmp/chef-rendered-template20150204-2210-2x1a9e 2015-02-04 12:43:14.410988863 +0000 @@ -274,6 +274,7 @@ # redirections work in a sensible way. # #ServerName www.example.com:80 +ServerName webdb:80 # # UseCanonicalName: Determines how Apache constructs self-referencing - restore selinux security context Recipe: mysql::default * yum_package[mysql-server] action install - install version 5.1.73-3.el6_5 of package mysql-server * service[mysqld] action enable - enable service service[mysqld] * service[mysqld] action start - start service service[mysqld] Recipe: apache::default * service[httpd] action reload - reload service service[httpd] Running handlers: Running handlers complete Chef Client finished, 9/9 resources updated in 19.022621785 seconds
OK!
Git 知ってるので飛ばす
実際の現状
. .chef .vagrant Vagrantfile environments roles .. .gitignore Berksfile data_bags nodes site-cookbooks
本と違うところは cookbooks ディレクトリが無いということ。確かに警告が出てたけど使ってないからな
SSH 接続してパスワード無し sudo できるなら通常の Linux とかならどこでも knife-solo できるよ
冪等性、アプリのデプロイ以前まで