日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
基于 Docker 的高可用 Rails 集群方案探索

基于 docker 的高可用 Rails 集群方案探索

作者:長洪 2015-07-29 13:21:58

云計算 本文是暴走漫畫團(tuán)隊關(guān)于自動化高可用集群方案的探索。主要目的是減少人工接入,實現(xiàn)自動化。主要是用了consul和registrator兩個工具。

站在用戶的角度思考問題,與客戶深入溝通,找到平房網(wǎng)站設(shè)計與平房網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋平房地區(qū)。

0x0 問題

運維從來都是一個“不起眼”的大問題。運維工作還特別瑣碎,一般要花去工程師大量的時間,同時還有突發(fā)性,對工程師機(jī)動性要求比較高。所以運維自動化一直在困擾暴走漫畫團(tuán)隊。

雖說大部分運維工作都交給第三方云服務(wù)提供商了,但是平時還是有一些機(jī)器的維護(hù)之類的,繁瑣的事情。

我 做的最多的應(yīng)該就是加減服務(wù)器了。暴漫的流量趨勢非常規(guī)律,每年寒暑假是訪問的高峰期,一般會比平時流量高出2倍。所以每年寒暑假之前都要添加足量的服務(wù) 器以應(yīng)對馬上到來的流量高峰,而在寒暑假結(jié)束之后,要去掉一部分機(jī)器,以節(jié)省開支。剛開始完全是人肉運維,我一個人先去開5臺機(jī)器,然后一個個上去改配置 文件之類的,感覺好傻。后面就用了ansible作為自動化運維工具,這個要比puppet和chef輕量很多,對付我們這些簡單的運維工作綽綽有余。 于是現(xiàn)在就變成了開完機(jī)器,用ansible同步配置,代碼,啟動app server,然后手動更新nginx配置。

使用ansible之后其實已經(jīng)輕松很多了,但是還不夠,我想象中的自動化集群,應(yīng)該可以隨意增加刪除node,node不可用的時候自動刪除,當(dāng)然這些都不能影響服務(wù)的訪問。

下面介紹一種自動化的Rails集群方案

0x1 相關(guān)技術(shù)

  • docker
  • hashicorp/consul(https://github.com/hashicorp/consul)
  • hashicorp/consul-template(https://github.com/hashicorp/consul-template)
  • hashicorp/registrator(https://github.com/gliderlabs/registrator)

0X2 整體架構(gòu)

0x3 實踐

由于不是科普帖,各種工具的詳細(xì)信息就不自己介紹了。

  consul & registrator

這兩個工具是集群中每個節(jié)點的基礎(chǔ)服務(wù)。我在一臺機(jī)器上,所以把consul cluster給省掉了,只用了一個consul節(jié)點。下面是docker-compose配置。

consul:

hostname: node1

image: progrium/consul

command: -server -bootstrap -advertise 172.17.42.1

ports:

- "8400:8400"

- "8500:8500"

- "8600:53/udp"

registrator:

image: gliderlabs/registrator

command: consul://consul:8500

hostname: registrator1

volumes:

- /var/run/docker.sock:/tmp/docker.sock

links:

- consul

需要注意的是consul的啟動參數(shù)里的advertise。應(yīng)該聲明為host機(jī)器的docker0的ip。如果不聲明的話會默認(rèn)使用容器ip,這樣registrator注冊的設(shè)備ip都是不可訪問的。

以上配置啟動之后一套自動服務(wù)器發(fā)現(xiàn)功能就算完工了。

#p#

接下來,我們配置一個web應(yīng)用測試一下。

web:

image: nginx

volumes:

- ./sites-enabled:/etc/nginx/conf.d

ports:

- "80:80"

links:

- rails

rails:

image: tutum/hello-world

ports:

- "80"

這個配置生成了nginx和rails,并且掛載了本地的sites-enabled文件夾作為nginx的配置來源。注意rails的ports是隨機(jī)的。

在sites-enabled中配置nginx server已rails作為backend。

  
 
 
 
  1. upstream backend { 
  2. server { 
  3.     server_name "10.0.2.15"; 
  4.     location / { 
  5.             proxy_pass http://backend; 
  6.     } 
  7. }  

對你并沒有看錯, upstream并沒有定義可用的backend node。這個配置將會有consul-template根據(jù)服務(wù)列表自動填充。

讓我們啟動這個web應(yīng)用,訪問80端口是無法訪問的 應(yīng)為沒有backend node。

接下來需要安裝consule-template, 在github下載對應(yīng)的release。

然后創(chuàng)建一個配置文件ct-nginx.conf

  
 
 
 
  1. consul = "127.0.0.1:8500" 
  2. log_level = "warn" 
  3.  
  4. template { 
  5.     source = "/home/vagrant/nginx.ctmpl" 
  6.     destination = "/home/vagrant/sites-enabled/backend.conf" 
  7.     command = "docker-compose -f /home/vagrant/docker-compose.yml restart web" 
  8. }  

我有映射consul的端口,所以直接使用了127.0.0.1。source就是我們配置文件的模板。如下

  
 
 
 
  1. upstream backend { 
  2.     {{range service "hello-world"}} 
  3.     server {{.Address}}:{{.Port}};{{end}} 
  4. server { 
  5.     server_name "10.0.2.15"; 
  6.     location / { 
  7.             proxy_pass http://backend; 
  8.     } 

consul-template提供了方便的模板查詢語句,可以直接從consul中查詢并渲染出配置文件。這個模板中就找出了所有名字為“hello-world”的service,并且循環(huán)輸出service對應(yīng)的Address,也就是服務(wù)ip,Port。

接著看ct-nginx.conf,destination代表模板生成完畢之后的位置, 這里直接放在nginx的配置文件夾,***command是生成配置后的動作,可以重啟各種服務(wù)。我們這里在更新nginx之后重啟生效。

OK,配置已經(jīng)妥當(dāng),consul和registrator也已經(jīng)啟動, 讓我們看看當(dāng)前的服務(wù)列表。

  
 
 
 
  1. $ curl localhost:8500/v1/catalog/service/hello-world?pretty 
  2. $ [] 

當(dāng)前并沒有注冊名為hello_world的服務(wù)。

#p#

稍微提一下registrator注冊服務(wù)的命名機(jī)制,registaor默認(rèn)是通過提取容器各種參數(shù)來生成的的服務(wù)參數(shù)

比如

  
 
 
 
  1. docker run -d --name nginx.0 -p 4443:443 -p 8000:80 progrium/nginx 

生成的服務(wù)信息如下

  
 
 
 
  1.     "ID": "hostname:nginx.0:443", 
  2.     "Name": "nginx-443", 
  3.     "Port": 4443, 
  4.     "IP": "192.168.1.102", 
  5.     "Tags": [], 
  6.     "Attrs": {}, 
  7. }, 
  8.     "ID": "hostname:nginx.0:80", 
  9.     "Name": "nginx-80", 
  10.     "Port": 8000, 
  11.     "IP": "192.168.1.102", 
  12.     "Tags": [], 
  13.     "Attrs": {} 

當(dāng)然也可以通過環(huán)境變量來指定

  
 
 
 
  1. $ docker run -d --name nginx.0 -p 4443:443 -p 8000:80 \ 
  2.            -e "SERVICE_443_NAME=https" \ 
  3.            -e "SERVICE_443_ID=https.12345" \ 
  4.            -e "SERVICE_443_SNI=enabled" \ 
  5.            -e "SERVICE_80_NAME=http" \ 
  6.            -e "SERVICE_TAGS=www" progrium/nginx 
  
 
 
 
  1.     "ID": "https.12345", 
  2.     "Name": "https", 
  3.     "Port": 4443, 
  4.     "IP": "192.168.1.102", 
  5.     "Tags": ["www"], 
  6.     "Attrs": {"sni": "enabled"}, 
  7. }, 
  8.     "ID": "hostname:nginx.0:80", 
  9.     "Name": "http", 
  10.     "Port": 8000, 
  11.     "IP": "192.168.1.102", 
  12.     "Tags": ["www"], 
  13.     "Attrs": {} 

接下來我們啟動consul-template

  
 
 
 
  1. $ consul-template -config=./ct-nginx.conf 

會直接根據(jù)模板生成一個nginx配置 雖然現(xiàn)在并沒有backend node。

  
 
 
 
  1. $ cat /home/vagrant/sites-enabled/backend.conf 
  2. upstream backend { 
  3. server { 
  4.     server_name "10.0.2.15"; 
  5.     location / { 
  6.             proxy_pass http://backend; 
  7.     } 
  8. }  

然后啟動我們的web應(yīng)用。

啟動之后,請求服務(wù)列表就可以看到helle-world服務(wù),說明已經(jīng)實現(xiàn)自動服務(wù)發(fā)現(xiàn)。

  
 
 
 
  1. $ curl localhost:8500/v1/catalog/service/hello-world?pretty 
  2.     "Node": "node1", 
  3.     "Address": "172.17.42.1", 
  4.     "ServiceID": "registrator1:vagrant_rails_1:80", 
  5.     "ServiceName": "hello-world", 
  6.     "ServiceTags": null, 
  7.     "ServiceAddress": "", 
  8.     "ServicePort": 32783 
  9. ]  

瀏覽器中可以正常訪問了,Yeah!

#p#

OK, 讓我們更進(jìn)一步,scale我們的rails node。

  
 
 
 
  1. $ docker-compose scale rails=4 
  2.  
  3. $ curl localhost:8500/v1/catalog/service/hello-world?pretty 
  4.     "Node": "node1", 
  5.     "Address": "172.17.42.1", 
  6.     "ServiceID": "registrator1:vagrant_rails_1:80", 
  7.     "ServiceName": "hello-world", 
  8.     "ServiceTags": null, 
  9.     "ServiceAddress": "", 
  10.     "ServicePort": 32783 
  11. }, 
  12.     "Node": "node1", 
  13.     "Address": "172.17.42.1", 
  14.     "ServiceID": "registrator1:vagrant_rails_2:80", 
  15.     "ServiceName": "hello-world", 
  16.     "ServiceTags": null, 
  17.     "ServiceAddress": "", 
  18.     "ServicePort": 32784 
  19. }, 
  20.     "Node": "node1", 
  21.     "Address": "172.17.42.1", 
  22.     "ServiceID": "registrator1:vagrant_rails_3:80", 
  23.     "ServiceName": "hello-world", 
  24.     "ServiceTags": null, 
  25.     "ServiceAddress": "", 
  26.     "ServicePort": 32785 
  27. }, 
  28.     "Node": "node1", 
  29.     "Address": "172.17.42.1", 
  30.     "ServiceID": "registrator1:vagrant_rails_4:80", 
  31.     "ServiceName": "hello-world", 
  32.     "ServiceTags": null, 
  33.     "ServiceAddress": "", 
  34.     "ServicePort": 32786 
  35. ]  

此時nginx已經(jīng)重啟,我們可以看到4個backend都開始處理請求。

你可以看到,rails服務(wù)器只要啟動之后就自動加入集群,如果一臺節(jié)點掛掉之后,會自動從集群里去掉,基本上沒有任何影響。

肯定有不足的地方,歡迎討論, 發(fā)出來就是為了學(xué)習(xí)。

博文出處:http://dev.baozou.com/ye-tan-ji-yu-dockerde-railsji-qun/


網(wǎng)站標(biāo)題:基于 Docker 的高可用 Rails 集群方案探索
分享鏈接:http://www.5511xx.com/article/djjcojg.html