SlideNow 建立記錄 - (2) Riak 測試環境

在上次決定了Docker來運作所有系統後,再來就是要決定Database
一開始就決定不使用Relation Database原因
1.本公司很窮,Relation Database的擴充不容易,沒有辦法簡單的Scale Out,只能Scale UP
2.因為SlideNow有很多像Social Media的運作方式 例如:加好友,追蹤之類的功能,需要很多Join來達到我要的目的,效能會很差。
3.Node.js跟NOSQL感覺比較潮(完全是亂講)

再來既然決定用NOSQL,就要提到CAP理論

CAP theorem (只能滿足兩個)

  • Consistency: 一致性
  • Availability: 可用性
  • Partition tolerance: 分隔容忍

這三個選擇中,我最不需要的是Consistency,又因為我想要很簡單可以擴充跟像Graph DB的功能,所以最後選擇了Riak

Riak and Docker

我們可以透過Docker很簡單的架設一個riak有五個node的環境
Riak-Docker這個Project可以很簡單的架設讓你測試Riak

首先先用Git把riak-docker clone下來,執行make build

$ git clone https://github.com/hectcastro/docker-riak.git

make build

make build會把裏面DockerFile用docker build 來建立新的Riak image

$ cd docker-riak
$ make build

裡面的DockerFile內容如下:

# Riak
#
# VERSION       0.4.3

FROM phusion/baseimage:0.9.9
MAINTAINER Hector Castro hector@basho.com

# Environmental variables
ENV DEBIAN_FRONTEND noninteractive
ENV RIAK_VERSION 1.4.8
ENV RIAK_SHORT_VERSION 1.4

# Install Riak
ADD http://s3.amazonaws.com/downloads.basho.com/riak/${RIAK_SHORT_VERSION}/${RIAK_VERSION}/ubuntu/precise/riak_${RIAK_VERSION}-1_amd64.deb /
RUN (cd / && dpkg -i "riak_${RIAK_VERSION}-1_amd64.deb")

# Setup the Riak service
RUN mkdir -p /etc/service/riak
ADD bin/riak.sh /etc/service/riak/run

# Setup automatic clustering
ADD bin/automatic_clustering.sh /etc/my_init.d/99_automatic_clustering.sh

# Tune Riak configuration settings for the container
RUN sed -i.bak 's/127.0.0.1/0.0.0.0/' /etc/riak/app.config && \
    sed -i.bak 's/{anti_entropy_concurrency, 2}/{anti_entropy_concurrency, 1}/' /etc/riak/app.config && \
    sed -i.bak 's/{map_js_vm_count, 8 }/{map_js_vm_count, 0 }/' /etc/riak/app.config && \
    sed -i.bak 's/{reduce_js_vm_count, 6 }/{reduce_js_vm_count, 0 }/' /etc/riak/app.config && \
    sed -i.bak 's/{hook_js_vm_count, 2 }/{hook_js_vm_count, 0 }/' /etc/riak/app.config && \
    sed -i.bak "s/##+zdbbl/+zdbbl/" /etc/riak/vm.args

# Make Riak's data and log directories volumes
VOLUME /var/lib/riak
VOLUME /var/log/riak

# Open ports for HTTP and Protocol Buffers
EXPOSE 8098 8087

# Enable insecure SSH key
# See: https://github.com/phusion/baseimage-docker#using_the_insecure_key_for_one_container_only
RUN /usr/sbin/enable_insecure_key

# Cleanup
RUN rm "/riak_${RIAK_VERSION}-1_amd64.deb"
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Leverage the baseimage-docker init system
CMD ["/sbin/my_init", "--quiet"]

如果在測試的時候,你想也順便測試mapreduce,請記得先改DockerFile裡面的,兩個部分
{map_js_vm_count, 0 } => {map_js_vm_count, 1 }
{reduce_js_vm_count, 0 } => {reduce_js_vm_count, 1 }
如果設定是0的話,riak是沒有javascript的vm可以執行的

make start-cluster

$ make start-cluster
./bin/start-cluster.sh

Bringing up cluster nodes:

  Successfully brought up [riak01]
  Successfully brought up [riak02]
  Successfully brought up [riak03]
  Successfully brought up [riak04]
  Successfully brought up [riak05]

Please wait approximately 30 seconds for the cluster to stabilize.

執行後,Riak便會啟動五個riak的instance
你可透過下面的指令取得要Riak的REST API的port

$ docker port $(docker ps -l -q) 8098
0.0.0.0:49206

然後透過該url,顯示如下畫面,即為成功

make stop-cluster

當要停止的時候,可以用以下指令

$ make stop-cluster
./bin/stop-cluster.sh
Stopped the cluster and cleared all of the running containers.

下一篇我們在來講解,如何用Node.js存取Riak.

comments powered by Disqus