欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

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

在用OpenStack實(shí)施云計算之前,要實(shí)現應用的水平擴展,通常是用這樣的架構:



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

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

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



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



接下來(lái)寫(xiě)一個(gè)腳本,能夠用這個(gè)snapshot來(lái)自動(dòng)的創(chuàng )建實(shí)例,因為我對Ruby比較熟悉,這里用Ruby的openstack-computegem 來(lái)寫(xiě)(OpenStack有多種客戶(hù)端可以調用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對應的ID,這里用1,就是默認最小的1cpu, 512M內存的規格  
  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"  


當需要擴展一臺新的nginx instance時(shí)候,只需要執行上面的代碼:
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ò )速度),這臺虛擬機就創(chuàng )建好了,我們可以在dashboard看到這臺最新的機器:




接下去我們再寫(xiě)一個(gè)腳本,自動(dòng)更新haproxy的配置文件:
Ruby代碼  
  1. cs = OpenStack::Compute::Connection.new(:username => username, :api_key => api_key, :auth_url => auth_url)  
  2. #預先定義一個(gè)haproxy template文件,backed server集群部分定義留空,將它拷貝過(guò)來(lái)  
  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`  


執行該代碼:
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í)現了將剛剛創(chuàng )建的 rails201112161042 實(shí)例添加到了haproxy集群。

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

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久