ç°¡å極å°æ§æã§Failoverãä½é¨ãã¦ã¿ããï¼ ã Vagrant+Chef ã使ã£ã¦ ã #vgadvent2013
ããã«ã¡ã¯ï¼@at_grandpa ã§ãã
ãã®è¨äºã¯VOYAGE GROUP ã¨ã³ã¸ãã¢ããã° : Advent Calendar 2013ã®19æ¥ç®ã®è¨äºã«ãªãã¾ãã
ä»å¹´ããããã¨10æ¥åå¾ã¨ãªãã¾ãããã¿ãªããããããéããã§ããããã
ã
ãã¦ãä½ãæ¸ããããª
ã¨ã³ã¸ãã¢ã® Advent Calendar çéã§ã¯ããå¹´æ«å¹´å§ãå®å¿ãã¦éããããã«ãç³»è¨äºãå¤æ°æ稿ããã¦ãã¾ããã
ç§ãããç³»ãæ¸ããã¨æãã¾ããããã¤ã³ãã©ã¯ãç´ äººãªã®ã§ã©ããããã®ãã¨ã
ã§ã以ä¸ã®æ¬ãèªãã æã«ãã¼ãã決ã¾ãã¾ããã
- ä½è : å®äºç伸,横å·åå,ã²ããã¾ããã,ä¼è¤ç´ä¹,ç°ä¸æ å¸,åè¦ç¥å·±
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2008/08/07
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- è³¼å ¥: 133人 ã¯ãªãã¯: 2,270å
- ãã®ååãå«ãããã° (289件) ãè¦ã
ãã®åé ã«ãç°¡åãªFailoverã®ä»çµã¿ãæ¸ãã¦ããã¾ãããã§ãã®ã§ä»åã¯ãç°¡å極å°æ§æã§å®éã«Failoverãä½é¨ãã¦ã¿ããã¨æãã¾ãã
ããFailoverã£ã¦è¨èã¯ç¥ã£ã¦ãããã©ãå®éã©ããããã¨ãè¡ããã¦ããã®ï¼ã
ã¨ããæ¹ã¯ãç°¡åã§ãã®ã§ãã²ãã£ã¦ã¿ã¦ãã ããã
ã
ç°¡å極å°æ§æ
æ§æã¯ä»¥ä¸ã§ããç©ççã«å¿ è¦ãªã®ã¯Macã®ã¿ã§ããç©çãµã¼ãã¼ãã±ã¼ãã«ãä½ãããã¾ãããè¯ãä¸ã®ä¸ã§ãããã財å¸ã«ãåªããã§ãã
ã
Vagrant 㨠Chef ã«ã¤ãã¦ã¯ã以ä¸ã®ãµã¤ããåªãã解説ããã¦ããã¨æãã¾ãã
ä»ã£ã½ã Vagrant + Chef Solo ãã¥ã¼ããªã¢ã«
Berkshelfã®è¨äºã®ç´åã¾ã§èªãã°ãä»åã®æ§æã¯å®è¡ã§ãããã¨æãã¾ãã
ã
ãã®ä»ãå人çã«Vagtrantã®ã¤ã³ã¹ãã¼ã«æ¹æ³ãªã©ãã¾ã¨ããã®ã§ã以ä¸ããåç §ãã ããã
ãã
Failoverã®æµã
ã§ã¯å®éã«ã©ãå¦çãæµãã¦ããã®ããè¦ã¦ã¿ã¾ãããã
ã
å®éç¨ä¸ã¯ãã®ãããªåä½ããã¦ãã¾ããTerminal.appã§ã¯pingã³ãã³ãã§æ¥ç¶ã常ã«ç¢ºèªãã¾ãã server1ã¨server2ã®éã§ã¯ããäºãã«ãã«ã¹ãã§ãã¯ããã¾ããâ
ã
ããã¦ãã²ã¨ã¤ã®ãµã¼ãã¼ï¼server2ï¼ãæ½°ãã¾ããããã¨ãserver1ã® health check ãé害ãæ¤ç¥ããFailoverã®å¦çãèµ°ãã¾ããâ
ã
server1ã«ããIPã¢ãã¬ã¹ã®å¼ãç¶ãã¨ãå¼ãç¶ãã®å ±åããªããã¾ããâ
ã
ç¡äºã両IPå ±ãæ¥ç¶ãå¯è½ã¨ãªãã¾ããâ
ãã£ãä¸ã¤ã®ã³ãã³ãã§ç°å¢æ§ç¯
ä»åã®ç°å¢ãæ§ç¯ããã«ã¯ãVagrantfileãrecipeãã·ã§ã«ã¹ã¯ãªããã®ï¼ã¤ãç·¨éãã¾ããï¼ã¤ãã¤è¦ã¦ããã¾ãããã
Vagrantfile
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # common setting config.vm.box = "cent_os" # server1 config.vm.define "server1" do |server| server.vm.network :private_network, ip: "192.168.1.11" end # server2 config.vm.define "server2" do |server| server.vm.network :private_network, ip: "192.168.1.12" end # chef-solo config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "./chef-repo/site-cookbooks" # èªä½ã®[httpd]ã¨ããcookbookãä½æããããã§æå®ï¼å¾è¿°ï¼ chef.add_recipe "httpd" end # provision :shell config.vm.provision :shell do |sh| sh.path = "failover.sh" end end
- OSã¯centos
- server1ã¨server2ã®ããããã®ãã©ã¤ãã¼ãIPã¢ãã¬ã¹ãæå®ï¼VIPã§ã¯ãªãï¼
- provisionã¨ã¯ãVagrantã§ãã·ã³ãç«ã¡ä¸ããéã«å®è¡ããå¦ç
:chef-solo
ï¼ãã·ã³ç«ã¡ä¸ãã¨åæã«ãiptablesãapacheã®ã¤ã³ã¹ãã¼ã«ããã:shell
ï¼failoverç¨ã®ã¹ã¯ãªãã
ã
ã¯ãã¯ãã㯠"httpd" ã®recipe
# cookbook:httpd service "iptables" do action [:stop, :disable] end package "httpd" do action :install end service "httpd" do action [ :enable, :start ] end template "/var/www/html/index.html" do source "index.html.erb" mode 0644 end
ã
failover.sh
#!/bin/sh VIP="192.168.1.20 192.168.1.21" DEV="eth0" healthcheck() { # VIPã®ã«ã¼ããåã for i in $VIP;do # èªåã®VIP以å¤ã®å ´åã¯trueã§ä¸ã®å¦ç㸠if [ -z "`ip addr show $DEV | grep $i`" ]; then # ã¬ã¹ãã³ã¹ãæ ¼ç´ RES=`curl -s -I http://${i}/| head -n 1 | cut -f 2 -d ' '` # ã¬ã¹ãã³ã¹ãä½ããªãå ´å if [ -z "${RES}" ]; then CIP="$i" return 1 fi # ã¬ã¹ãã³ã¹ã¯ããã200以å¤ã®æ if [ "200" -ne "${RES}" ]; then CIP="$i" return 1 fi fi done return 0 } ip_takeover() { # é害ã®ãã£ãVIPãèªåã®ãµã¼ãã¼ã«å ãã ip addr add $CIP/24 dev $DEV # arpãéä¿¡ï¼IPãå¼ãç¶ãã ãã¨ãå ±åï¼ arping -A -I $DEV -c 1 $CIP } # 1ç§éé㧠health check while healthcheck; do echo "health ok!" sleep 1 done # failoverå¦çãå®è¡ echo "failover!" ip_takeover
ã
ãã®ã·ã§ã«ã¹ã¯ãªããã¯ãï¼åç®ã®å®è¡ã¨ï¼åç®ã®å®è¡ã§åä½ãç°ãªãã¾ãã
failover.shã®ï¼åç®å®è¡ã®åä½
- ã¾ãã¯Chef Soloã«ãã£ã¦server1ã§ã¹ã¯ãªãããèµ°ã
- server1ã«ã¦ãhealthcheck()ãèµ°ã
- 192.168.1.20ã®IPã¯åå¨ããªãã®ã§healthcheck()ãæããip_takeover()ãå®è¡ããã
- server1ã«VIP : 192.168.1.20 ãä»ä¸ããã
- 次ã«ç«ã¡ä¸ãã£ãserver2ã§ã¹ã¯ãªãããèµ°ã
- server2ã«ã¦ãhealthcheck()ãèµ°ã
- 192.168.1.20ã®IPã¯åå¨ãã¦ããã®ã§(server1)ã¬ã¹ãã³ã¹ãè¿ã£ã¦ããããã次㮠192.168.1.21 ããã§ãã¯ãã
- 192.168.1.21ã®IPã¯åå¨ããªãã®ã§healthcheck()ãæããip_takeover()ãå®è¡ããã
- server2ã«VIP : 192.168.1.21 ãä»ä¸ããã
ããã§ãï¼ã¤ã®VIPã®ãµã¼ãã¼ãçãã¦ããç¶æ ã«ãªãã¾ããã
ã
failover.shã®ï¼åç®ã®å®è¡ã®åä½
- server1ã§ããä¸åº¦ãæåã§å®è¡ããå ´åãèãã
- foræå ã®ifæ
if [ -z "ip addr show $DEV | grep $i" ]; then
ã¯ãèªåã®IP以å¤ã¯trueã¨ãªãã®ã§ãã¬ã¹ãã³ã¹ã®æç¡ããã§ãã¯ããã®ã¯å¸¸ã«ç¸æ(server2)ã®IPã¨ãªã- ããã§ãserver1ã¯ãserver2ã® health check ããã¦ããç¶æ ãã«ãªã
- server2ã®å ´åã¯IPãéã«ãªãã ãã§ãå ¨ãåãåã
- ããã§ãäºãã«health checkãã¦ãã
- 常ã«ï¼ç§ããã« health check ãããç¸æã®åå¿ããªãã£ãããç¸æã®IPãèªåã«å¼ãç¶ã
ãã¦ããããããµã¼ãã¼ãç«ã¡ä¸ãã¾ãã
ã
vagrant up !!
å¿
è¦ãªã³ãã³ãã¯ãvagrant up
ã®ã¿ã§ãã
Vagrantfileã®ãããã£ã¬ã¯ããªã«ç§»åãã¦ãã³ãã³ããå®è¡ãã¾ãããã
å®è¡çµæâ
Bringing machine 'server1' up with 'virtualbox' provider... Bringing machine 'server2' up with 'virtualbox' provider... [server1] Importing base box 'cent_os'... ... ãçç¥ããµã¼ãã¼ç«ã¡ä¸ãã®ããããã®å¦ç ... [server1] The guest additions on this VM do not match the installed version of VirtualBox! In most cases this is fine, but in rare cases it can cause things such as shared folders to not work properly. If you see shared folder errors, please update the guest additions within the virtual machine and reload your VM. Guest Additions Version: 4.1.18 VirtualBox Version: 4.2 â â â â ãµã¼ãã¼ãç«ã¡ä¸ãã£ã â â â [server1] Configuring and enabling network interfaces... [server1] Mounting shared folders... [server1] -- /vagrant [server1] -- /tmp/vagrant-chef-1/chef-solo-1/cookbooks [server1] Running provisioner: chef_solo... â â â â ããããchefã®å®è¡ â â â Generating chef JSON and uploading... load: 1.67 cmd: VBoxManage 1401 waiting 0.04u 0.03s Running chef-solo... [2013-12-19T01:54:03+01:00] INFO: *** Chef 10.12.0 *** [2013-12-19T01:54:04+01:00] INFO: Setting the run_list to ["recipe[httpd]"] from JSON [2013-12-19T01:54:04+01:00] INFO: Run List is [recipe[httpd]] [2013-12-19T01:54:04+01:00] INFO: Run List expands to [httpd] [2013-12-19T01:54:04+01:00] INFO: Starting Chef Run for localhost [2013-12-19T01:54:04+01:00] INFO: Running start handlers [2013-12-19T01:54:04+01:00] INFO: Start handlers complete. [2013-12-19T01:54:04+01:00] INFO: service[iptables] stopped â â â â iptables â â â [2013-12-19T01:54:04+01:00] INFO: service[iptables] disabled [2013-12-19T01:54:34+01:00] INFO: package[httpd] installing httpd-2.2.15-29.el6.centos from base repository â â â â apacheã¤ã³ã¹ãã¼ã« â â â [2013-12-19T01:54:42+01:00] INFO: package[httpd] installed version 2.2.15-29.el6.centos [2013-12-19T01:54:42+01:00] INFO: service[httpd] enabled [2013-12-19T01:54:42+01:00] INFO: service[httpd] started â â â â apacheèµ·å â â â [2013-12-19T01:54:42+01:00] INFO: template[/var/www/html/index.html] mode changed to 644 â â â â index.htmlã®è¨ç½® â â â [2013-12-19T01:54:42+01:00] INFO: template[/var/www/html/index.html] updated content [2013-12-19T01:54:42+01:00] INFO: Chef Run complete in 38.139665 seconds [2013-12-19T01:54:42+01:00] INFO: Running report handlers [2013-12-19T01:54:42+01:00] INFO: Report handlers complete [server1] Running provisioner: shell... â â â â ã·ã§ã«ã®å®è¡ â â â [server1] Running: /var/folders/q5/c85lqfy57t7f1sw5lsh6nzn4wnj0pf/T/vagrant-shell20131219-1258-162e2hz failover! â â â â ip_takeover()ã®å®è¡ â â â ARPING 192.168.1.20 from 192.168.1.20 eth0 â â â â VIPã®ä»ä¸ â â â Sent 1 probes (1 broadcast(s)) Received 0 response(s) ... ãçç¥ãåæ§ã®åä½ãserver2ã§ãå®è¡ ...
- centosãç«ã¡ä¸ããï¼ãã®æããã©ã¤ãã¼ãIPãä»ä¸ï¼
- ç«ã¡ä¸ãã£ãã Chef Solo ãå®è¡
- iptablesåæ¢ãapacheã®ã¤ã³ã¹ãã¼ã«ï¼èµ·åãindex.htmlã®çæ
- æå®ããã·ã§ã«ã¹ã¯ãªããã®å®è¡ï¼failover.shï¼â VIPã®ä»ä¸
ãã®ãããªæµãã«ãªã£ã¦ãã¾ãã
ç«ã¡ä¸ããã¨ãTerminal.appããã®ï¼ã¤ã®pingãéãããã«ãªãã¾ãã
ã
health check ã®èµ·å
vagrant up
ã ãã§ã¯ãã¾ã ï¼åããfailover.shã¯å®è¡ããã¦ããªãã®ã§ãVIPã®ä»ä¸ã¯çµãã£ã¦ãã¾ãã health check ã¯åä½ãã¦ãã¾ããã
vagrant ssh server1
ãæã¡ãserver1å
ã§ã·ã§ã«ã¹ã¯ãªãããå®è¡ãã¾ãããã
vagrant ssh server1
- [vagrant@localhost ~]$ â server1ã«å ¥ããã¨ãã§ãã
sudo sh /vagrant/failover.sh
â Vagrantfileã¨åããã£ã¬ã¯ããªå ã®ãã®ã¯ããã®å ±æãã£ã¬ã¯ããªã¨ãªã³ã¯ãã¦ããã®ã§ãfailover.shãåå¨ãã- health check å®è¡å®äº
- server2ãåæ§ã«
ã
Let's é害çºå
é害ãèµ·ããã¾ãããããããªçµé¨ãã¾ããªãã§ããããããã£ããé害ãèµ·ããã¦ãã ããã
ã§ã¯ãsever2ãåæ¢ãã¾ããããã³ãã³ãã¯ã¯ã©ã¤ã¢ã³ãä¸ã§vagrant halt server2
ã§ãã
åæ¢ãããã¨ã®Terminal.appã§ã®pingçµæã¯ä»¥ä¸ã§ããâ
ã
Failoverï¼å¸°ã£ã¦ãã192.168.1.21ï¼
ã
ç¡äºä»¥ä¸ã®ç¶æ ã«ãªã£ã¦ãã¾ããã
ã
server2ã¯æ®è·ãã¾ãããããµã¼ãã¹ã¯ç¶ç¶ãã¦ãã¾ãã( ï¾Ðï¾ï¾ï¾"âï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾ï¾
ã
課é¡
ãã®æ§æã¯ãç°¡åãæ ã«ã¾ã ã¾ã 課é¡ãæ®ã£ã¦ãã¾ãã
- ï¼å°ãã£ããã©ããªãã®ï¼
- ã·ã§ã«ã¹ã¯ãªããããã¡ãã¡èµ·åããªãã¨ãããªã
- ä¸åã®failoverãã対å¿ãã¦ããªã
ãªã©ãªã©ã§ãããããã解決ããããã«ããã¼ããã©ã³ãµãªã©ã®æ©å¨ãç¨ãããã¦ãã¾ãã
詳ããã¯ããã§ã¯å²æãã¾ããã[24æé365æ¥] ãµã¼ã/ã¤ã³ãã©ãæ¯ããæè¡ ?ã¹ã±ã¼ã©ããªãã£ããã¤ããã©ã¼ãã³ã¹ãçåéç¨ (WEB+DB PRESS plusã·ãªã¼ãº) ã«æ¸ããã¦ãã¾ãã®ã§ãã¾ã ã®æ¹ã¯ãä¸èªãã°ã
ã
æå¾ã«
ç¥èã¨ãã¦ç¥ã£ã¦ã¯ãã¦ããå®éã«èªåã§åããã¦ã¿ãã¨ãããã¾ãéã£ãç¥èã¨ãã¦å®çãã¾ããã
ãã¯ãæãåãããã¨ã¯å¤§åï¼ã¿ãªããããã²ä½é¨ãã¦ã¿ã¦ãã ããã
ã
ææ¥ã®AdventCalendarã¯ãæããã¢ã¤ãã«@saya_223nã§ãã ã¢ã¤ãã«ã£ã½ãè¨äºã§å¿ãçãã¦ããããã¨ã§ãããï¼