å ¥éChef-Soloãçæã«Railsã¢ããªãåä½ãããã¨ããã¾ã§ãã£ã¦ã¿ã
Chefã«ã¤ãã¦ã¯åã ããæ°ã«ã¯ãªã£ã¦ãããã©ã¾ã¨ã¾ã£ãæ å ±ããã¾ããªããä¸ã ãã£ããã¨åãçµãã¦ããªãã£ãã®ã§ãããã¡ããã©æ¬¡ã®ããã¸ã§ã¯ãããå°å ¥ãããã¨ãã¦ããã¨ããã«ã『入門Chef Solo - Infrastructure as Code』ã¨ãããããããã¾ã¨ãæ¬ãåºçãããã®ã§ããããçæã«è²ã ã¨å®é¨ãã¦ã¿ã¾ããã
å ¥éChef Soloã¯ããã¾ã¨ãããã¦ãã¦é常ã«å©ãã£ãã®ã§ãããKindleã§è¦ãã¨ç®æ¬¡ããªãã¦éå¼ãçã«å©ç¨ããªããæ§ç¯ããã®ãçµæ§å¤§å¤ã§ããã®ã§ãèªåç¨ã«æé æ¸ã¨ãã¦ã¾ã¨ãã¦ã¿ã¾ãããï¼ç®æ¬¡ã«é¢ãã¦ã¯ç¾å¨ä¿®æ£çãåºã¦ããããã§ãï¼
ã¨ããããRailsã®ãµã¼ãã¹éçºããã¸ã§ã¯ãã§ä½¿ãããã®ã§ã以åã«ãããVPSã®è¨å®ã§è¡ã£ããããªæãã§rvm, nginx, unicorn, mysqlã§Railsã¢ããªãåãã¨ããã¾ã§ãã£ã¦ã¿ã¾ããããã1年以ä¸åã§ãããååã¯ãããªæãã§è¨å®ãã¦ãã¾ããã
Vagrantã使ã£ã¦ä»®æ³ãµã¼ãã¼ãç«ã¦ã
ã¾ãã¯æ°è»½ã«å®é¨ã§ããããã«ä»®æ³ãµã¼ãã¼ãç«ã¦ã¾ãã
1. Oracleã®VirtualBoxãã¤ã³ã¹ãã¼ã«
2. Vagrantãã¤ã³ã¹ãã¼ã«
$ gem install vagrant
3. Vagrantã§å©ç¨ããOSã¤ã¡ã¼ã¸ã®åå¾
こちらã§ã¤ã¡ã¼ã¸ä¸è¦§ãå
¬éããã¦ãã¾ãããä»åã¯æ¸ç±ã«ãã£ããã®ããã®ã¾ã¾æå®ã
$ vagrant box add base http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.3-x86_64-v20130101.box
4. é©å½ãªãã£ã¬ã¯ããªã§initãå®è¡ãã¦ä»®æ³ãµã¼ãã¼ã®åæåãã¾ãã
$ vagrant init ï¼Vagrantfileãçæããã¾ãï¼
5. Vagrantfileã®ãããã¯ã¼ã¯è¨å®ã追å ãã¾ãã
... config.vm.network :private_network, ip: "192.168.50.12" ...
6. ä»®æ³ãµã¼ãã¼ãèµ·åãã¾ãã
$ vagrant up
7. SSHã§æ¥ç¶ã§ããã°OKã
$ vagrant ssh
8. SSHã®è¨å®ã追å ãã¦ããã¾ãã
$ vagrant ssh-config --host vagrant01 >> ~/.ssh/config $ ssh vagrant01
9. ä¸è¦ã«ãªã£ããããã§åæ¢ãåé¤ã§ãã¾ãã
$ vagrant halt $ vagrant destroy
ãã®ä»ã¡ã¢
vagrantã®åærootãã¹ã¯ã¼ã㯠'vagrant'
knife-soloã®ã¤ã³ã¹ãã¼ã«ã¨è¨å®
1. knife-soloã使ãã¨ããã¼ã«ã«ãã·ã³ãããªã¢ã¼ããµã¼ãã¼ï¼vagrantï¼ã«å¯¾ãã¦Chef Soloãå®è¡ã§ããããã«ãªãã¾ãã
$ gem install knife-solo
2. knifeã®åæè¨å®ãè¡ãã¾ãã表示ããã質åã¯å
¨ã¦ããã©ã«ãã§OKã
$ knife configure
3. ãµã¼ããã¼ãã£ã®ã¯ãã¯ããã¯ãå©ç¨ããããã«è¨å®ãã¦ããã¾ããOPSCODEã§ã¦ã¼ã¶ã¼ç»é²ãè¡ãããããã£ã¼ã«ãã¼ã¸ã«ãã get private key ããç§å¯éµãåå¾ãå
ã»ã©ã®knife configureã§çæãã ~/.chef/knife.rb ã® client_key ã«ãã®ç§å¯éµãæå®ãã¦ããã¾ãã
... client_key '/Users/ntaku/.chef/ntaku.pem' ...
Chefãªãã¸ããªã®ä½æ
knifeã使ã£ã¦ãªãã¸ããªãä½æãã¾ããï¼ããã§ã¯gitã³ãã³ãã¯çãã¦ãã¾ãï¼
$ knife solo init chef-repo $ cd chef-repo
Berkshelfã®ã¤ã³ã¹ãã¼ã«
1. ãµã¼ããã¼ãã£ã®ã¯ãã¯ããã¯ãå¹çãã管çããããã«Berkshelfã使ãããã®ã§ããã¡ããå ã«è¨å®ãã¦ããã¾ãã
$ gem install berkshelf
2. chef-repo/Berksfileãä½æãã¾ããyum以å¤ã¯èªåã§ã¯ãã¯ããã¯ãä½ãããã¨ããã§ãããã¾ã ããã¾ã§ãã¡ãã¨æ¸ããªãã®ã§ãã¨ããããæ¢åã®ã¯ãã¯ããã¯ãå©ç¨ãã¾ãã
site :opscode cookbook 'yum' cookbook 'mysql' cookbook 'rvm', git:'https://github.com/fnichol/chef-rvm.git' cookbook 'nodejs', git:'https://github.com/mdxp/nodejs-cookbook.git'
3. Berksfileã§æå®ããã¯ãã¯ããã¯ãåå¾ãã¾ãã--pathãªãã·ã§ã³ã§ã¯ãã¯ããã¯ã®ä¿åå
ãæå®ã§ãã¾ããæå®ããªãå ´åã¯ã~/.berkshelf/cookbooksã«åå¾ãããã¡ã¤ã«ãä¿åããã¾ãã
$ berks --path cookbooks
ã¯ãã¯ããã¯ã¨ã¬ã·ãã®ä½æ
ãªãªã¸ãã«ã®ã¯ãã¯ããã¯ãä½æãã¦ããã¾ãã
ã¨ã¯è¨ã£ã¦ãä»å追å ããã®ã¯2ã¤ã ãã§ãã
$ cd chef-repo $ knife cookbook create iptables -o site-cookbooks $ knife cookbook create nginx -o site-cookbooks
iptables
è¨å®ãã¡ã¤ã«ã¯å®éã«ä½¿ã£ã¦ãããã®ããã®ã¾ã¾æã£ã¦æ¥ã¾ããã
ããã§è¨å®ãã¡ã¤ã«ããã®ã¾ã¾vagrantã¸è»¢éãããiptablesãåèµ·åããã¾ãã
/chef-repo/site-cookbooks/iptables/recipes/default.rb
service "iptables" do supports :status => true, :restart => true, :reload => true action [:enable, :start] end template "/etc/sysconfig/iptables" do source "iptables" owner "root" group "root" mode 0600 notifies :restart, 'service[iptables]' end
/chef-repo/site-cookbooks/iptables/templates/default/iptables
*filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :RH-Firewall-1-INPUT - [0:0] -A INPUT -j RH-Firewall-1-INPUT -A FORWARD -j RH-Firewall-1-INPUT -A RH-Firewall-1-INPUT -i lo -j ACCEPT -A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT -A RH-Firewall-1-INPUT -p 50 -j ACCEPT -A RH-Firewall-1-INPUT -p 51 -j ACCEPT -A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT -A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT -A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT -A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # SSH, HTTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited COMMIT
nginx
è¨å®ãã¡ã¤ã«ã2ã¤ã¨ãã¬ã·ããã¡ã¤ã«ã1ã¤ã§ãã
ãã¡ããiptablesã¨åãæãã§ã
/chef-repo/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 "/etc/nginx/nginx.conf" do source "nginx.conf.erb" owner "root" group "root" mode 0644 notifies :reload, 'service[nginx]' end directory "/etc/nginx/sites-enabled" do owner "root" group "root" mode 0644 action :create end template "/etc/nginx/sites-enabled/default.conf" do source "sites-enabled.conf.erb" owner "root" group "root" mode 0644 notifies :reload, 'service[nginx]' end
/chef-repo/site-cookbooks/nginx/templates/default/nginx.conf.erb
user nginx; worker_processes 1; pid /var/run/nginx.pid; error_log /var/log/nginx/error.log; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; gzip_disable "msie6"; gzip_proxied any; gzip_min_length 500; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; # virtual Hosts include /etc/nginx/sites-enabled/*; }
/chef-repo/site-cookbooks/nginx/templates/default/sites-enabled.conf.erb
upstream unicorn_rails_proxy { server unix:/tmp/unicorn_<%= node['nginx']['application'] %>.sock fail_timeout=0; } server { listen <%= node['nginx']['port'] %>; server_name _; root /var/www/<%= node['nginx']['application'] %>/current; access_log /var/log/nginx/<%= node['nginx']['application'] %>_log; error_log /var/log/nginx/<%= node['nginx']['application'] %>_error_log; rewrite_log on; location / { proxy_pass http://unicorn_rails_proxy; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } location ~ ^/(images|javascripts|stylesheets|system)/ { root /var/www/<%= node['nginx']['application'] %>/current/public; expires max; break; } }
<%= node['nginx']['application'] %>ã¯å¾ããnodeãã¡ã¤ã«ã§å¤ãæå®ãã¾ãã
nodeãã¡ã¤ã«ã®ç·¨é
æå¾ã«nodeãã¡ã¤ã«ã§ãã¼ãã«åæ ãããã¬ã·ããæå®ãã¾ãã
/chef-repo/nodes/vagrant01.json
{ "rvm": { "rubies" : ["ruby-1.9.3-p392"], "default_ruby" : "ruby-1.9.3-p392" }, "mysql": { "server_root_password": "test", "server_repl_password": "test", "server_debian_password": "test" }, "nginx":{ "application" : "chef_rails_template", "port" : 80 }, "run_list":[ "recipe[yum::epel]", "recipe[rvm::system]", "recipe[mysql::server]", "recipe[nginx]", "recipe[iptables]", "recipe[nodejs]" ] }
Chefã®å®è¡
ããããChef Soloå®è¡ã§ããï¼å®éã¯è¡ã£ããæ¥ãããªã®ã§ãããã¾ã§ã«ä½åº¦ãå®è¡ãã¦ãã¾ãã..ï¼
1. prepareãå®è¡ãã¦æå®ãããã¹ãã«Chefã®å®è¡ç°å¢ãæºåãã¾ããï¼ååã®ã¿å®è¡ããï¼
$ cd chef-repo $ knife solo prepare vagrant01
2. é©å¿ããããã¹ããæå®ãã¦Chef Soloãå®è¡ãã¾ããã¬ã·ãã«åé¡ããªããã°ããã§ãµã¼ãã¼ã®ç°å¢æ§ç¯ã¯å
¨ã¦å®äºã§ãã
$ knife solo cook vagrant01
Railsãããã¤
ããããã¯ãã¾ãã¿ãããªãã®ãªã®ã§ãç°¡åã«ã§ããããã«ãµã³ãã«ããã¸ã§ã¯ããgithubã«ä½ãã¾ããããã¡ãã®ããã¸ã§ã¯ããcapistranoã使ã£ã¦vagrantã¸ãããã¤ãã¾ãã
- https://github.com/ntaku/chef_rails_template
- deploy.rbã¯SSHå¨ããèªåã®ãã®ã«å¤æ´ãã¦ãã ããã
- unicorn.rbã¯ãã®ã¾ã¾ã§è¡ããã¨æãã¾ãã
1. vagrantä¸ã«äºãproductionç¨ã®DBãä½ã£ã¦ããã¾ããMySQLã®ãã¹ã¯ã¼ãã¯vagrant01.jsonã§æå®ããtestã§ãã
$ ssh vagrant01 $ mysql -u root -p mysql> create database chef_rails default character set=utf8;
2. githubããããã¸ã§ã¯ããåã£ã¦æ¥ã¦ããã㤠& èµ·åãã¾ããï¼ãã¼ã«ã«ãã·ã³ï¼
$ git clone [email protected]:ntaku/chef_rails_template.git $ cd chef_rails_template $ cap deploy:setup $ cap deploy $ cap deploy:start
3. ã¢ããªã±ã¼ã·ã§ã³ã«ã¢ã¯ã»ã¹ãã¾ãã
http://192.168.50.12/ ã«ã¢ã¯ã»ã¹ãã¦ãTOP PAGEãã¨åºãã°å
¨ã¦æ£ããåä½ãã¦ãã¾ãã
ã¾ã¨ã
ãå
¥éChef Solo - Infrastructure as Codeãã®ãããã§å¤§åChefã¨ä»²è¯ããªãã¾ããã
ãã®æ¬ããªãã£ãã3åã¯æéãããã£ãã¨æãã¾ã..