安装TokyoCabinet
wget http://1978th.net/tokyocabinet/tokyocabinet-1.4.41.tar.gz tar -zxvf tokyocabinet-1.4.41.tar.gz cd tokyocabinet-1.4.41 ./configure --prefix=/usr/local/tokyocabinet make make install
安装boost
wget http://nchc.dl.sourceforge.net/project/boost/boost/1.41.0/boost_1_41_0.tar.bz2 tar -jxvf boost_1_41_0.tar.bz2 cd boost_1_41_0 ./bootstrap.sh --prefix=/usr/local/boost ./bjam install
安装flare
wget http://labs.gree.jp/data/source/flare-1.0.9.tgz tar -zxvf flare-1.0.9.tgz cd flare-1.0.9 ./configure --with-boost=/usr/local/boost --with-tokyocabinet=/usr/local/tokyocabinet --prefix=/usr/local/flare make make install
Flare安装完成之后在集群中的配置
主机 | 角色 |
---|---|
ecos03 | flare主 |
ecos02 | flare从 |
flare路径
/usr/local/flare
创建flare 配置文件和log文件目录
mkdir -pv /usr/local/flare/etc mkdir -pv /usr/local/flare/data
首先,创建一个配置文件的索引服务器 flarei.conf
cat > /usr/local/flare/etc/flarei.conf <<'EOF' # data directory data-dir = /usr/local/flare/data # syslog facility log-facility = local0 # max connections to accept max-connection = 512 # node down threshold monitor-threshold = 3 # node monitoring interval (index server sends "ping" w/ this interval) monitor-interval = 1 # server name of myself server-name = 10.0.0.3 # server port of myself server-port = 12120 # max thread pool size thread-pool-size = 8 EOF
下一步,我们需要创建一个节点服务器的配置文件 flared.conf
cat > /usr/local/flare/etc/flared.conf <<'EOF' # data directory data-dir = /usr/local/flare/data # name of index server (sync this to "server-name" of flarei.conf) index-server-name = flare.example.com # port number of index server (sync this to "server-port" of flarei.conf) index-server-port = 12120 # syslog facility log-facility = local0 # max-connections to accept max-connection = 1024 # number of lock slots to access records mutex-slot = 64 # number of proxy request concurrency proxy-concurrency = 2 # server name of myself server-name = flare.example.com # server port of myself server-port = 12121 # stack size of each thread (kb) stack-size = 128 # storage options storage-ap = 4 storage-bucket-size = 16777216 # storage-compress = # storage-large = true # storage type (currently only "tch" is available) storage-type = tch # max number of thread pool thread-pool-size = 16
编辑默认配置文件 flare
cat > /usr/local/flare/etc/flare <<'EOF' RUN_INDEX="yes" RUN_NODE="yes" CONF_INDEX="/usr/local/flare/etc/flarei.conf" CONF_NODE="/usr/local/falre/etc/flared.conf" DATA_INDEX="/usr/local/flare/data" DATA_NODE="/usr/local/flare/data" EOF
配置日志在syslog.conf中添加一条日志消息
cat >> /etc/syslog.conf <<'EOF' # Log for flare local0.* /var/log/flare.log EOF
现在我们就做好flare的配置了,然后你可以下面这样运行它
/usr/local/flare/bin/flarei -f /usr/local/flare/etc/flarei.conf --daemonize /usr/local/flare/bin/flared -f /usr/local/flare/etc/flared.conf --daemonize
设置开机启动
cat >> /etc/rc.local <<'EOF' /usr/local/flare/bin/flarei -f /usr/local/flare/etc/flarei.conf --daemonize /usr/local/flare/bin/flared -f /usr/local/flare/etc/flared.conf --daemonize EOF
运行后日志会是下面的样子
less /var/log/flare.log
flarei[8603]: [3069409824][NTC][flarei.cc:105-startup] flarei version 1.0.0 - system logger started flarei[8603]: [3069409824][NTC][flarei.cc:107-startup] application startup in progress... flarei[8603]: [3069409824][NTC][flarei.cc:108-startup] config_path: /usr/local/flare/etc/flarei.conf flarei[8603]: [3069409824][NTC][flarei.cc:109-startup] daemonize: true flarei[8603]: [3069409824][NTC][flarei.cc:110-startup] data_dir: /usr/local/flare/data flarei[8603]: [3069409824][NTC][flarei.cc:111-startup] max_connection: 512 flarei[8603]: [3069409824][NTC][flarei.cc:112-startup] monitor_threshold: 3 flarei[8603]: [3069409824][NTC][flarei.cc:113-startup] monitor_interval: 1 flarei[8603]: [3069409824][NTC][flarei.cc:114-startup] server_name: 10.0.0.3 flarei[8603]: [3069409824][NTC][flarei.cc:115-startup] server_port: 12120 flarei[8603]: [3069409824][NTC][flarei.cc:116-startup] stack_size: 128 flarei[8603]: [3069409824][NTC][flarei.cc:117-startup] thread_pool_size: 8 flarei[8604]: [3069409824][NTC][app.cc:90-_daemonize] daemon process created -> now i have new pid [8604] flarei[8604]: [3069409824][NTC][cluster.cc:1210-_reconstruct_node_partition] reconstructing node partition map... (from 0 entries in node map) flarei[8604]: [3069409824][NTC][cluster.cc:1309-_reconstruct_node_partition] node partition map: flarei[8604]: [3069409824][NTC][cluster.cc:1328-_reconstruct_node_partition] node partition map (prepare): flarei[8604]: [3069409824][NTC][flarei.cc:164-run] entering running loop ... flared[8607]: [3069975072][NTC][flared.cc:109-startup] flared version 1.0.0 - system logger started flared[8607]: [3069975072][NTC][flared.cc:111-startup] application startup in progress... flared[8607]: [3069975072][NTC][flared.cc:112-startup] config_path: /usr/local/flare/etc/flared.conf flared[8607]: [3069975072][NTC][flared.cc:113-startup] daemonize: true flared[8607]: [3069975072][NTC][flared.cc:114-startup] data_dir: /usr/local/flare/data flared[8607]: [3069975072][NTC][flared.cc:115-startup] index_server_name: 10.0.0.3 flared[8607]: [3069975072][NTC][flared.cc:116-startup] index_server_port: 12120 flared[8607]: [3069975072][NTC][flared.cc:117-startup] max_connection: 1024 flared[8607]: [3069975072][NTC][flared.cc:118-startup] mutex_slot: 64 flared[8607]: [3069975072][NTC][flared.cc:119-startup] proxy_concurrency: 2 flared[8607]: [3069975072][NTC][flared.cc:120-startup] server_name: 10.0.0.3 flared[8607]: [3069975072][NTC][flared.cc:121-startup] server_port: 12121 flared[8607]: [3069975072][NTC][flared.cc:122-startup] stack_size: 128 flared[8607]: [3069975072][NTC][flared.cc:123-startup] storage_ap: 4 flared[8607]: [3069975072][NTC][flared.cc:124-startup] storage_bucket_size: 16777216 flared[8607]: [3069975072][NTC][flared.cc:125-startup] storage_compress: flared[8607]: [3069975072][NTC][flared.cc:126-startup] storage_large: false flared[8607]: [3069975072][NTC][flared.cc:127-startup] storage_type: tch flared[8607]: [3069975072][NTC][flared.cc:128-startup] thread_pool_size: 16 flared[8608]: [3069975072][NTC][app.cc:90-_daemonize] daemon process created -> now i have new pid [8608]
最后,运行我们的节点服务器!但我们仍然需要配置节点设置。请检查节点的状态,这样:
telnet 10.0.0.3 12120 Trying 10.0.0.3... Connected to 10.0.0.3 (10.0.0.3). Escape character is '^]'.
输入
stats nodes
然后,我们可以得到节点统计:
STAT 10.0.0.3:12121:role proxy STAT 10.0.0.3:12121:state down STAT 10.0.0.3:12121:partition 0 STAT 10.0.0.3:12121:balance -1 STAT 10.0.0.3:12121:thread_type 16 END
第一行中role proxy说明现在是代理模式, 我们需要把他改成master
node role 10.0.0.3 12121 master 1 0
第二行说节点是关闭的, 那么我开启
node state 10.0.0.3 12121 active
查看节点统计结果
telnet 10.0.0.3 12121 ... stats nodes返回会是这样的
STAT 10.0.0.3:12121:role master STAT 10.0.0.3:12121:state active STAT 10.0.0.3:12121:partition 0 STAT 10.0.0.3:12121:balance 1 STAT 10.0.0.3:12121:thread_type 16 END
这是类似于我们有1个持续服务器。下面我们尝试set和get命令:
telnet 10.0.0.3 12121 ... set key1 0 0 4 test STORED get key1 VALUE key1 0 4 test END
如果负载太高 w/ 只有1个服务器,或添加redunduncy,添加从节点将解决这些问题。
创建配置文件flared.conf
# data directory data-dir = /usr/local/flare/data # name of index server (sync this to "server-name" of flarei.conf) index-server-name = 10.0.0.3 # port number of index server (sync this to "server-port" of flarei.conf) index-server-port = 12120 # syslog facility log-facility = local0 # max-connections to accept max-connection = 1024 # number of lock slots to access records mutex-slot = 64 # number of proxy request concurrency proxy-concurrency = 2 # server name of myself server-name = 10.0.0.2 # server port of myself server-port = 12121 # stack size of each thread (kb) stack-size = 128 # storage options storage-ap = 4 storage-bucket-size = 16777216 # storage-compress = # storage-large = true # storage type (currently only "tch" is available) storage-type = tch # max number of thread pool thread-pool-size = 16
编辑默认配置文件 flare
cat > /usr/local/flare/etc/flare <<'EOF' RUN_INDEX="no" RUN_NODE="yes" CONF_NODE="/usr/local/falre/etc/flared.conf" DATA_INDEX="/usr/local/flare/data" DATA_NODE="/usr/local/flare/data" EOF
配置日志在syslog.conf中添加一条日志消息
cat >> /etc/syslog.conf <<'EOF' # Log for flare local0.* /var/log/flare.log EOF
现在我们就做好flare的配置了,然后你可以下面这样运行它
/usr/local/flare/bin/flared -f /usr/local/flare/etc/flared.conf --daemonize
设置开机启动
cat >> /etc/rc.local <<'EOF' /usr/local/flare/bin/flared -f /usr/local/flare/etc/flared.conf --daemonize EOF
telnet 10.0.0.3 12120 ... Escape character is '^]'. stats nodes STAT 10.0.0.3:12121:role master STAT 10.0.0.3:12121:state active STAT 10.0.0.3:12121:partition 0 STAT 10.0.0.3:12121:balance 1 STAT 10.0.0.3:12121:thread_type 16 STAT 10.0.0.2:12121:role proxy STAT 10.0.0.2:12121:state active STAT 10.0.0.2:12121:partition -1 STAT 10.0.0.2:12121:balance 0 STAT 10.0.0.2:12121:thread_type 17 END
在上面结果所有求10.0.0.2:12121代理10.0.0.3:12121。检查这种行为可以连接10.0.0.2:12121,并发送一个GET命令:
telnet 10.0.0.3 12120 ... Escape character is '^]'. get key1上面的例子会得到相同的结果:
VALUE key1 0 4 test END但该节点是从slave,所以我们需要设置这个节点
使用'node role'命令:
telnet 10.0.0.3 12120 ... Escape character is '^]'. node role 10.0.0.2 12121 slave 1 0 OK
如果成功完成上述程序,你会得到以下结果:
telnet 10.0.0.3 12120 ... Escape character is '^]'. stats nodes STAT 10.0.0.3:12121:role master STAT 10.0.0.3:12121:state active STAT 10.0.0.3:12121:partition 0 STAT 10.0.0.3:12121:balance 1 STAT 10.0.0.3:12121:thread_type 16 STAT 10.0.0.2:12121:role slave STAT 10.0.0.2:12121:state active STAT 10.0.0.2:12121:partition 0 STAT 10.0.0.2:12121:balance 0 STAT 10.0.0.2:12121:thread_type 17 END
stats nodes运行的内容显示10.0.0.2:12121被转移到master,但也表明平衡设置为0。因此通过其他代理节点的所有请求不均衡到该节点, 发送另一个"node role"的命令
node role 10.0.0.2 12121 slave 2注册请求是平衡10.0.0.2:12121 twrice 10.0.0.3:12121。
启动|停止脚本
#!/bin/sh case "$1" in start) echo "start flare index" #Run flarei.conf /usr/local/flare/bin/flarei -f /usr/local/flare/etc/flarei.conf --daemonize ;; stop) echo "stop flare index" kill -9 `cat /usr/local/flare/data/flarei.pid` ;; *) echo "Usage: $0 {start|stop}" exit 1 esac
#!/bin/sh case "$1" in start) echo "start flare node" #Run flared.conf /usr/local/flare/bin/flared -f /usr/local/flare/etc/flared.conf --daemonize ;; stop) echo "stop flare node" kill -9 `cat /usr/local/flare/data/flared.pid` ;; *) echo "Usage: $0 {start|stop}" exit 1 esac