自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

0x0 問題

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

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

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

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

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

0x1 相關技術

  • 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 整體架構

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

0x3 實踐

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

  consul & registrator

這兩個工具是集群中每個節(jié)點的基礎服務。我在一臺機器上,所以把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。應該聲明為host機器的docker0的ip。如果不聲明的話會默認使用容器ip,這樣registrator注冊的設備ip都是不可訪問的。

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

#p#

接下來,我們配置一個web應用測試一下。

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是隨機的。

在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ù)服務列表自動填充。

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

接下來需要安裝consule-template, 在github下載對應的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對應的Address,也就是服務ip,Port。

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

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

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

當前并沒有注冊名為hello_world的服務。

#p#

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

比如

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

生成的服務信息如下

  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": {} 

當然也可以通過環(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應用。

啟動之后,請求服務列表就可以看到helle-world服務,說明已經(jīng)實現(xiàn)自動服務發(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!

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

#p#

OK, 讓我們更進一步,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服務器只要啟動之后就自動加入集群,如果一臺節(jié)點掛掉之后,會自動從集群里去掉,基本上沒有任何影響。

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

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

責任編輯:Ophira 來源: 個人博客
相關推薦

2014-11-11 15:30:46

DockerRails集群Ruby

2023-11-01 07:55:44

K8sKubernetes

2023-12-18 09:37:37

2023-11-13 09:03:10

2018-07-10 08:42:45

Oracle高可用集群

2019-12-24 14:28:00

KeepalivedNginxTomcat

2012-02-15 22:40:23

heartbeat高可用

2023-11-07 07:30:18

Hadoop高可用

2024-02-27 09:48:25

Redis集群數(shù)據(jù)庫

2013-06-07 11:30:32

2022-09-29 15:24:15

MySQL數(shù)據(jù)庫高可用

2017-11-13 11:07:32

Nginx搭建高可用

2018-01-12 14:20:37

數(shù)據(jù)庫MySQL高可用架構

2020-10-28 07:10:07

Nginx高可用高并發(fā)

2019-08-27 15:56:44

MySQL 互聯(lián)網(wǎng)數(shù)據(jù)庫

2017-02-06 11:43:57

ZooKeeper集群

2017-02-19 19:57:05

ZooKeeper集群

2025-03-31 10:40:52

2015-10-22 10:28:45

MySQL高可用方案

2014-10-09 10:04:23

CentOS集群
點贊
收藏

51CTO技術棧公眾號