éçºã¡ã¢#4 : EC2ã¹ãããã·ã§ããã¨ã®å·®å㯠chef-solo ã§è§£æ±º
éçºã¡ã¢ãã®4ã§ãã
開発メモ#2 : AWS でのホスト / クラウドネイティブなデプロイ - naoyaのはてなダイアリー ã§ãã·ã¹ãã æ§æã®å¤æ´æã« EC2ã®ã¹ãããã·ã§ããããã¤ã³ã¹ã¿ã³ã¹ãè¤è£½ã㦠Elastic IP ã§åãæ¿ãã¦ããã¨ãã話ããã¾ããã
ãã ããã®æ¹æ³ã¯ãã®ã¾ã¾ã§ã¯ä¸ç¹åé¡ãããã¾ããã¹ãããã·ã§ãããåã£ãã¿ã¤ãã³ã°ã¨ç¾æç¹ã§ã·ã¹ãã æ§æã«å·®åããã£ãå ´åã«ã©ãããããã§ããä¾ãã° nginx ã®è¨å®ãã»ãã®å°ãã ãæ¸ãæããããã¨ããã®é½åº¦ã¹ãããã·ã§ãããåã£ã¦ãã¦ã¯æµç³ã«é¢åã
ãã®æã®ã¹ãããã·ã§ããæç¹ããã®å·®åãè¤è£½ãããã¤ã³ã¹ã¿ã³ã¹ã«ç°¡åã«é©ç¨ããããã«ãåºæ¬çãªãµã¼ãã¼è¨å®å¨ã㯠chef-solo ã§ç®¡çãã¦ã¾ããchef ã¯ãµã¼ãã¼æ§ç¯èªååãã¼ã«ã§ãchef-solo 㯠chef ã®ã¯ã©ã¤ã¢ã³ãã»ãµã¼ãã¼ãå¿ è¦ã¨ããªãã©ã¤ãçãã¨ã§ãããã°ããã®ãã
chef-solo ã®ã¬ã·ã (管çããããµã¼ãã¼ã®è¨å®çã ã®ãã¨) 㯠git ã«æ¾ãè¾¼ãã§ãã¼ã¸ã§ã³ç®¡çãã¦ããã
å ¨ã¦ã®ç°å¢ã chef-solo ã§æ§ç¯ãããã¨ãã§ããã§ãããããããã¯ããã§å¤§å¤ã ãããã ãã ã¨æ¯åã®å¾©å ã«æéãããã£ã¦ãã¾ããåºæ¬ãã¤ã³ã¹ã¿ã³ã¹ã®ç«ã¡ä¸ãã¯å°ãåã®ã¹ãããã·ã§ããããè¡ã£ã¦ãã®å¾ chef-solo ã¨ã¢ããªã±ã¼ã·ã§ã³ã®ãããã¤ãè¡ããã¨ã§å ¨ã¦ãææ°ã«ãªãã¾ããä½ãã©ããªã£ã¦ãããå¿ãã¦ãã³ãã³ãä¸çºã§ç¾ç¶å¾©å¸°ã§ãããããªã幸ãã
以ä¸ãchef-solo ã®ä½¿ãæ¹ã«ã¤ãã¦è¦ç¹ãç´¹ä»ã
ã¯ã¼ã¯ããã¼
chef-solo ã«ã¯ã¯ã©ã¤ã¢ã³ãããµã¼ãããªãã®ã§ãåºæ¬çã«ã¯ ssh ã§ãªã¢ã¼ããã¹ãã«ã¤ã³ã¹ãã¼ã«ããã¦ãã chef-solo ãå®è¡ãããã¨ã«ãªãã¾ãã
- ãã¼ã«ã«ã§ã¬ã·ã (chef) ãããããæ¸ã
- chef ã¬ã·ãã® lint foodcritic ã§ãã§ãã¯
- git commit ã㦠github ã« push
- chef-solo ã Cinnamon ã使ã£ã¦ããªã¢ã¼ãã§å®è¡
- ãªã¢ã¼ãã§ã¯ git fetch ã§ææ°ã®ã¬ã·ããåã£ã¦ãã¦ãchef-solo ãå®è¡ããã
ã¨ããæé ã§é²ãã¦ã¾ãã
rbenv ãªç°å¢ã§ chef-solo + Cinnamon
Amazon Linux ã«ããã©ã«ãã§å ¥ã£ã¦ãã ruby 㯠1.8ç³»ãchef ã®æ¨å¥¨ãã¼ã¸ã§ã³ã§ã¯ãªãã®ã§ ruby ã¯å¥é rbenv ã§å ¥ãã¾ããã
ã§ãchef ã®å®è¡ã¯å æ¥ç´¹ä»ãã Cinnamon ã«ã¿ã¹ã¯ãä½ã£ã¦å®è¡ãã¦ããã
task chef => { run => sub { my ($host, @args) = @_; my $deploy_to = get('deploy_to'); remote { sudo "chef-solo -c $deploy_to/chef/solo.rb -j $deploy_to/chef/nodes/appserver.json"; } $host; }, }
ãããªæãã§ããã
% cinnamon development chef:run
ã§ããªã¢ã¼ãå´ã§ chef-solo ãå®è¡ãããããªã¢ã¼ãã¦ã¼ã¶ã¼ã« sudo 㧠chef-solo ã® NOPASSWD ãä»ãã¦ããã¨ããã
chef ã®ã¬ã·ãã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ã¬ãã¸ããªã®ä¸ã«ä¸ç·ã«å ¥ãã¦ãã¾ã£ã¦ãããåãããã©ããè¿·ãã¾ããããgithub ã¨ã®è¤æ°å ¬ééµé£æºã®åé¡ãªã©è«¸ã ããã®ã§ã¨ããããã
ã¡ãªã¿ã« knife-solo ã¨ãããchef ã®ããã³ãã¨ã³ããã¼ã«ã¿ãããªãã®ãããã¾ãã¦ãããã使ãã¨ç¹ã«ãããã¤ãã¼ã«ã¨é£æºãªã©ãããªãã¦ããã¼ã«ã«ãããªã¢ã¼ãã«ã¬ã·ãã®åæ㨠chef-solo ã®å®è¡ãçºè¡ã§ããããã§ããèªåã®ç°å¢ã§ã¯ãã¾ãè¨å®ã§ããªãã£ãããã¤ã¯ã¼ã¯ããã¼çã« github ãéã«æã¿ããã£ãã®ã§åè¿°ã®ããã«ãã¦ããã¾ãã
ä¾: chef-solo 㧠supervisord ãé åãã¦ã¿ã
ã²ã¨ã¤ãä½ãè¨å®ãå¤æ´ãããã¨ããã¨ãã®ä¾ãåå¿é²ä»£ããã«ã§ãæ¸ãã¦ããã¾ããããsupervisord ã chef-solo ã§ç®¡çããã®ãæ³å®ãã¦ã¿ã¾ãã
% cd chef/site-cookbooks % knife cookbook create supervisord -o .
ã¾ãã¯ã¬ãã¸ããªå 㧠chef-solo ã®ã¬ã·ããä¿åãã¦ãããã£ã¬ã¯ããªã«ç§»åãchefãã¨ããååã«ãã¦ã¾ãããããããã¯ãã¯ããã¯ã®ã²ãªå½¢ãçæããã
site-cookbooks/supervisord/recipes/default.rb
ãç·¨éãã¦ãã¬ã·ããæ¸ãã
# supervisor ãããã±ã¼ã¸ã§ã¤ã³ã¹ãã¼ã« package "supervisor" do action :install end # è¨å®ãã¡ã¤ã«ããã³ãã¬ã¼ãããçæãã¦é ç½® template "supervisord.conf" do path "/etc/supervisord.conf" source "supervisord.conf.erb" owner "root" group "root" mode 0644 notifies :reload, 'service[supervisord]' end # supervisord ãç«ã¡ä¸ã service "supervisord" do supports :status => true, :restart => true, :reload => true action [ :enable, :start ] end
supervisord ã®è¨å®ã®ãã¢ã«ãªã supervisord.conf ã¯ãåã¯ãã¯ããã¯ã¬ã·ãå
ã® templates ãã£ã¬ã¯ããªã«ãããã®ã使ãããããã³ãã¬ã¼ã㯠erb ã§æ¸ãã¾ãããå¤æ°ã¯ä¸å使ã£ã¦ããããªã¢ã¼ãã«ãã£ã supervisord.conf ããã®ã¾ã¾ã³ãã¼ãã¦ã㦠site-cookbooks/supervisord/templates/supervisord.conf.erb
ã¨ãã¦ç½®ãã¦ãã ãã§ãã
ãã¨ã¯ foodcritic ãå®è¡ãã¦ãã·ã³ã¿ãã¯ã¹ã«åé¡ãªãããªã©ã確èªãããã¡ãªã¿ã«ãã³ãã¬ã¼ããä½ãå¿ãããããã¨
☁ chef [master] ⚡ foodcritic site-cookbooks FC033: Missing template: site-cookbooks/gemrc/recipes/default.rb:9
ãããªæãã§ã¨ã©ã¼ãåºãã
ã§ããã ndoes/appserver.json
ã¨ãããèªåã®ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼åãã®ãã¼ããã¡ã¤ã«ï½¥ï½¥ï½¥ãããããã®ã¬ã·ããããããæ¡ä»¶ã§å®è¡ããã¨ãæ¸ãè¨å®ãã¡ã¤ã«ãã« supervisord ã¬ã·ãã追è¨ããã
{ "recipes":[ "mosh", "git", "zsh", "nginx", "oh-my-zsh", "gitconfig", "supervisord", ... ] }
ãã¨ã¯ git commit, git push ãã¦
% cinnamon development deploy:update && cinnamon development chef:run
ã¨ããªã¢ã¼ãã¬ãã¸ããªãã¢ãããã¼ãã㦠chef-solo ãèµ°ããã¦å®äºã
å¾æ¥ supervisord.conf ã«å¤æ´ãå ããããªã¨æã£ãã¨ãã¯ããã³ãã¬ã¼ãã®ä¸èº«ãå¼ã£ã¦ chef-solo ãèµ°ãããã°è¯ããå¤æ´å±¥æ´ã¯ git & github ã§ç®¡çããããã¨ã«ãªããä¸æ³ã
ã¡ãªã¿ã«
ãããã®Resourceã¯ãä½åº¦ç¹°ãè¿ãã¦ãåãçµæã«ãªãã¾ãã大äºã§ããè±èªã§è¨ãã¨ãidempotentã§ããåªçæ§(ã¹ãã¨ããã)ã¨ãè¨ãã¾ãã
shell scriptã§mkdir hogeã¨äºåæã¤ã¨ãäºåç®ã¯ãFile existsãã¨ããã¨ã©ã¼ã«ãªãã¾ãããï¼(-p使ãã¨ãã話ãããã¾ãã)ããããchefã使ã£ãå ´åã§ã¯ã¨ã©ã¼ã«ãªãã¾ãããããã¯chefãèªåçã«å¤æãã¦å®è¡ããªãããã§ãã
ã¾ããtemplateã§ããã°ãããæåã§ãªã«ããæ¸ãæãã¦ããå ´åã«ã¯ãã®å¤æ´ã¯æ¸ãæ½°ããã¾ãããownerãå¤æ´ããå ´åã¯chefã®ã¬ã·ãéãã«æ»ããã¾ãã
ã¨ãããã¨ã§ãå ã®ã¬ã·ãã 㨠supervisord ã®ã¤ã³ã¹ãã¼ã«ãªãããæ¸ããã¦ãããã©ãæ¯åã¤ã³ã¹ãã¼ã©ãèµ°ããªãã¦ãã¨ãªãªãããã®è¾ºã¯ chef ãè¯ãæãã«é¢åã¿ã¦ãããããã«ãªã£ã¦ãã¾ããæ°ã«ãªã人ããããããªã®ã§ãä¸å¿è§¦ãã¦ããã¾ãã
è½ã¡ç©æ¾ã
supervisord ã®ä»ã«ã¯ãnginx ã memcachedãredis ãªã©ã®ãã¼ã«ã chef-solo ã§ç®¡çãã¦ãã¾ãããzsh ã oh-my-zsh ããzshrc ã gemrcãgitconfig ãªã©ã®è¨å®ãã¡ã¤ã«ãªããããããã¦ãã¾ãã
chef ã®ã¬ã·ãã¯èªåã§æ¸ããªãã¦ããæå¿ãä½ã£ã¦å ¬éããã¦ãããã®ã沢山ããã¾ããä¾ãã° nginx ã®ã¯ãã¯ããã¯ã欲ãã・・・ã¨æã£ãã chef nginx cookbook ã¨ãã§ã°ã°ãã°åºã¦ãã¾ããèªåã¯ãããããã¨ã¯ã·ã³ãã«ãªã®ã§ä»ã®ã¨ãããã®è¾ºã¯å©ç¨ãã¦ãã¾ããã
chef-solo ã®åä½ã¯æ¬çªé©ç¨ã®åã«æå ã§æ¤è¨¼ããããã¨ããå ´å㯠vagrant ã使ãã¨è¯ãããã§ããknife-solo ã«ã¯ vagrant ã¨ãã¾ãå ·åã«é£æºããæ©è½ãªãããã¤ãã¦ã¦ãç°¡åã«æ¤è¨¼ç¨ã® VM ãç«ã¡ä¸ãããã¨ãã§ãã模æ§ã
èªåã®å ´åãæ¤è¨¼ãå¿
è¦ã«ãªããããªå¤§ããªå¤æ´ã¯ä¾ã«ãã£ã¦ã¹ãããã·ã§ããã§æ¬çªã¨ã¯å¥ç°å¢ãç«ã¡ä¸ãã¦ããé©ç¨ããããã«ãã¦ãã¦æå
ã§ã®æ¤è¨¼ã¯ãã¾ããã¦ãã¾ãããvagrant 使ãå§ãã¾ããã便å©ããã¾ããããã¿ã¾ããã
ã¬ã·ãã®ãã¹ãããªãã®ãä¸å®ï½¥ï½¥ï½¥!! ã¨ããæ¹ã¯ Cucumber, ChefSpecとchefでテスト駆動のサーバ構築管理 - Qiita ãã®è¾ºãè¦ãã¨è¯ãã§ãããããã¹ãé§åãµã¼ãã¼æ§ç¯ããªãã¦è¸å½ãå®è¡ãã¦ãã御大ãã
ã¨ãããããã§ã·ã¹ãã æ§æã chef-solo ã§ç®¡çãããã¯ãªãã§ããã