国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
用Haproxy+OpenStack實(shí)現(xiàn)web application auto scaling
前面寫過2篇文章介紹過OpenStack,今天介紹一下配合Haproxy實(shí)現(xiàn)Web application auto scaling。

在用OpenStack實(shí)施云計(jì)算之前,要實(shí)現(xiàn)應(yīng)用的水平擴(kuò)展,通常是用這樣的架構(gòu):



一臺(tái)Haproxy將動(dòng)態(tài)請(qǐng)求轉(zhuǎn)發(fā)到N臺(tái)nginx服務(wù)器,當(dāng)流量增加的時(shí)候,我們需要手工安裝物理的服務(wù)器,將它添加到集群中去,然后再手工修改haproxy的配置,讓它生效。就算用自動(dòng)化的安裝管理工具,這樣擴(kuò)展一臺(tái)機(jī)器也差不多要3~4小時(shí)。

用OpenStack實(shí)施云計(jì)算之后,整個(gè)架構(gòu)和上圖是一樣的,但是我們可以通過幾十行代碼在5分鐘內(nèi)實(shí)現(xiàn)auto scaling,來看一下具體的步驟:

首先,我們先在OpenStack上的一臺(tái)虛擬機(jī)安裝好nginx以及其他應(yīng)用,然后對(duì)它做一個(gè)snapshot:



記錄一下這個(gè)snapshot的ID:



接下來寫一個(gè)腳本,能夠用這個(gè)snapshot來自動(dòng)的創(chuàng)建實(shí)例,因?yàn)槲覍?duì)Ruby比較熟悉,這里用Ruby的openstack-computegem 來寫(OpenStack有多種客戶端可以調(diào)用API,比如python,php):

Ruby代碼  
  1. require 'rubygems'  
  2. require 'openstack/compute'  
  3.   
  4. username = 'quake'  
  5. api_key = 'password'  
  6. auth_url = 'http://10.199.21.210:5000/v2.0/' #OpenStack keystone auth url  
  7. image_id = '9' #前面記錄的snapshot ID  
  8. flavor_id = '1' #你想用的flavor對(duì)應(yīng)的ID,這里用1,就是默認(rèn)最小的1cpu, 512M內(nèi)存的規(guī)格  
  9.   
  10. cs = OpenStack::Compute::Connection.new(:username => username, :api_key => api_key, :auth_url => auth_url)  
  11. image = cs.get_image(image_id)  
  12. flavor = cs.get_flavor(flavor_id)  
  13. #創(chuàng)建一個(gè)新的instance  
  14. newserver = cs.create_server(:name => "rails#{Time.now.strftime("%Y%m%d%H%M")}":imageRef => image.id, :flavorRef => flavor.id)  
  15. "New Server #{newserver.name} created"  
  16. #刷新instance的狀態(tài),直到它創(chuàng)建成功  
  17. while newserver.progress < 100  
  18.   p "Server status #{newserver.status}, progress #{newserver.progress}"  
  19.   sleep 10  
  20.   newserver.refresh  
  21. end  
  22. "Server status #{newserver.status}, progress #{newserver.progress}"  
  23. "Done"  


當(dāng)需要擴(kuò)展一臺(tái)新的nginx instance時(shí)候,只需要執(zhí)行上面的代碼:
Shell代碼  
  1. # ruby create_new_server.rb  
  2. "New Server rails201112161042 created"  
  3. "Server status BUILD, progress 0"  
  4. "Server status ACTIVE, progress 100"  
  5. "Done"  


差不多30秒左右的時(shí)間(視你的鏡像大小和網(wǎng)絡(luò)速度),這臺(tái)虛擬機(jī)就創(chuàng)建好了,我們可以在dashboard看到這臺(tái)最新的機(jī)器:




接下去我們?cè)賹懸粋€(gè)腳本,自動(dòng)更新haproxy的配置文件:
Ruby代碼  
  1. cs = OpenStack::Compute::Connection.new(:username => username, :api_key => api_key, :auth_url => auth_url)  
  2. #預(yù)先定義一個(gè)haproxy template文件,backed server集群部分定義留空,將它拷貝過來  
  3. `cp haproxy.cfg.template haproxy.cfg`  
  4.   
  5. File.open('haproxy.cfg''a'do |f|  
  6.   cs.servers.each do |s|  
  7.     server = cs.server(s[:id])  
  8.     #如果該實(shí)例的鏡像等于我們之前做的snapshot,將它的IP加入配置文件  
  9.     if server.image['id'] == image_id  
  10.       ip = server.addresses.first.address  
  11.       p "Found matched server #{server.name} #{ip}, add to haproxy"  
  12.       f.puts "        server #{server.name} #{ip}:80 maxconn 512"  
  13.     end  
  14.   end  
  15. end  
  16.   
  17. #覆蓋舊的haproxy配置文件,reload haproxy  
  18. `cp haproxy.cfg /etc/haproxy.cfg`  
  19. "Reload haproxy"  
  20. `/etc/init.d/haproxy reload`  


執(zhí)行該代碼:
Shell代碼  
  1. # ruby update_haproxy.rb  
  2. "Found matched server rails201112161042 10.199.18.6, add to haproxy"  
  3. "Found matched server rails201112161003 10.199.18.5, add to haproxy"  
  4. "Found matched server rails201112160953 10.199.18.4, add to haproxy"  
  5. "Found matched server rails201112160924 10.199.18.8, add to haproxy"  
  6. "Found matched server rails201112160923 10.199.18.7, add to haproxy"  
  7. "Reload haproxy"  


這樣就實(shí)現(xiàn)了將剛剛創(chuàng)建的 rails201112161042 實(shí)例添加到了haproxy集群。

通過不到50行的代碼,就實(shí)現(xiàn)了auto scaling,是不是很方便?我們還能夠更自動(dòng)化一些,配合監(jiān)控腳本,設(shè)定一個(gè)指標(biāo),比如說當(dāng)整個(gè)集群的CPU用量達(dá)到70%的時(shí)候,自動(dòng)調(diào)用create_server,當(dāng)CPU小于10%的時(shí)候,調(diào)用delete_server,這樣就實(shí)現(xiàn)了按需scaling up/down.
5
1
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
CentOS 5安裝ROR
Ruby on Rails 學(xué)習(xí)資料
淺談五大Python Web框架
商業(yè)服務(wù)的Ruby on Rails HTTP Cluster觀念及測(cè)試
通過Socket.IO與nodeJs實(shí)現(xiàn)即時(shí)消息推送
Mac OSX 上安裝配置Ruby on Rails
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服