Vagrantやらknifeやらで仮想環境つくる
MAC初心者ががんばって環境構築にトライしています。
http://qiita.com/taiki45/items/b46a2f32248720ec2bae
こちらのページが大変ためになり、Berkshelfの仕組みも何となくわかったんだけど、
(vmのバージョンをあわせなかったためか)mysqlのインストールでこける。
デバッグできないので勉強がてらchefのコードを自分で書く方針でいきたい
http://blog.vitamin11.org/2013/10/vagrant-chef-solo-berksfile.html
こちらのページの”パターン #1 仮想環境に対して knife-solo”に近い形でやってみた
まずvagrantで仮想サーバが動いているかvagrant statusで確認。
公開鍵を仮想サーバに渡すために鍵ペアを生成する。
$ ssh-keygenとうつ(確か)。
~/.ssh/id_rsa.pubが生成されるので
それを仮想サーバの~/ssh/authorized_keysにコピペする。
ちなみに仮想サーバにログインするには
$ vagrant ssh
でログインできるはず。
空白1行あけた後の後半部分がそれ。
Mac側のVagrantfileを編集して、IPアドレスを指定する
vim ~/Vagrant/[仮想サーバ名]/Vagrantfile
# Create a private network, which allows host-only access to the machine # using a specific IP. config.vm.network :private_network, ip: "192.168.33.10"
これで
$ ssh vagrant@192.168.33.10
でログインできるはず。
(再起動いるかも(覚えてない)、再起動はvagrant halt,vagrant up)
次にMAC側でchefリポジトリを作る
$ knife solo init chef-repo
$ cd chef-repo
$ git init
$ git add .
$ git commit -m "first commit"
nginxのcookbookを作る
$ knife cookbook create nginx -o site-cookbooks
次にnginxをインストールして立ち上げる手順をdefault.rbに書く
vim site-cookbooks/nginx/recipes/default.rb
# # Cookbook Name:: nginx # Recipe:: default # # Copyright 2013, YOUR_COMPANY_NAME # # All rights reserved - Do Not Redistribute # package "nginx" do action :install end service "nginx" do supports :status => true, :restart => true, :reload => true action [:enable, :start] end
あと、nodesフォルダ内[host].jsonファイルを編集しなきゃならない
$ vim nodes/192.168.33.10.json
{"run_list": ["recipe[nginx]"] }
で、最後に
$ knife solo cook vagrant@192.168.33.10でcookbookを仮想サーバ側に適用
Running Chef on 192.168.33.10...
Checking Chef version...
Installing Berkshelf cookbooks to 'cookbooks'...
Uploading the kitchen...
Generating solo config...
Running Chef...
Starting Chef Client, version 11.8.2
Compiling Cookbooks...
Converging 2 resources
Recipe: nginx::default
* package[nginx] action install- install version 1.1.19-1ubuntu0.5 of package nginx
* service[nginx] action enable
- enable service service[nginx]* service[nginx] action start
- start service service[nginx]Chef Client finished, 3 resources updated
成功したっぽい
$ curl "http://192.168.33.10/"
<html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx!</h1></center> </body> </html>