Redis3.2.8集群部署
第一步:安装redis
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar xfredis-3.2.8.tar.gz
cd redis-3.2.8/
make --编译安装
make install #将可执行文件拷贝到/usr/local/bin目录下
也可以PREFIX=“”指定目录
makePREFIX=/some/other/directory install #指定二进制命令目录
cdredis-3.2.8/
cp redis-3.2.8/utils/redis_init_script/etc/init.d/redis #复制启动脚本
编辑
vi/etc/rc.d/init.d/redis,并在文档第二行添加# chkconfig: 2345 8090。如果/usr/local/redis/bin中没有redis-server和redis-cli文件的话,我们要在我们的安装目录下复制一个过来。否则服务启动时找不到redis-server
在$EXEC $CONF后面加$
mkdir /etc/redis
cpredis.conf /etc/redis/6379.conf
启动脚本
推荐在生产环境中使用启动脚本方式启动redis服务。启动脚本redis_init_script 位于位于Redis的 /utils/ 目录下。
#大致浏览下该启动脚本,发现redis习惯性用监听的端口名作为配置文件等命名,我们后面也遵循这个约定。
#redis服务器监听的端口
REDISPORT=6379
#服务端所处位置,在make install后默认存放与`/usr/local/bin/redis-server`,如果未make install则需要修改该路径,下同。
EXEC=/usr/local/bin/redis-server
#客户端位置
CLIEXEC=/usr/local/bin/redis-cli
#Redis的PID文件位置
PIDFILE=/var/run/redis_${REDISPORT}.pid
#配置文件位置,需要修改
CONF="/etc/redis/${REDISPORT}.conf"
第二步:集群配置
创建redis节点
测试我们选择2台服务器,分别为:192.168.100.11,192.168.100.15.每分服务器有3个节点。
在192.168.100.11上创建三个节点
mkdir -p /usr/local/redis-cluster
cd redis-cluster
mkdir 7000 7001 7002
复制redis.conf到7000 7001 7002目录下并修改配置文件端口
redis-server是从redis的安装目录中的src里copy过来就好
redis.conf内容如下;
#======================================#
port 6379 #不同目录,这里端口对应不同
cluster-enabled yes #开启集群功能
cluster-config-file nodes.conf #节点配置文件,这个文件是服务启动时自己配置创建的
cluster-node-timeout 5000
appendonly yes
mkdir /usr/local/redis-cluster
rsync -avz /usr/local/redis-cluster/ root@192.168.18.115:/usr/local/redis-cluster/
在192.168.100.11修改完成节点,通过rsync同步到192.168.100.15上。
第三步:创建集群
[root@mylab1src]# ./redis-trib.rb create --replicas 1 192.168.100.11:7000192.168.100.11:7001 192.168.100.11:7002 192.168.100.15:7000 192.168.100.15:7001192.168.100.15:7002
>>>Creating cluster
>>>Performing hash slots allocation on 6 nodes...
Using3 masters:
192.168.100.15:7000
192.168.100.11:7000
192.168.100.15:7001
Addingreplica 192.168.100.11:7001 to 192.168.100.15:7000
Addingreplica 192.168.100.15:7002 to 192.168.100.11:7000
Addingreplica 192.168.100.11:7002 to 192.168.100.15:7001
M:730e03c2092124f1c284c79266eded188aee0388 192.168.100.11:7000
slots:5461-10922 (5462 slots) master
S:23c6df13bfc80f7980dd9735e91561cc073e9c1f 192.168.100.11:7001
replicatese55076d490739db93ec7ca21031b85abb5904555
S:169c46253de8bea9e4f4763d6f222400ad52cbf6 192.168.100.11:7002
replicatesbb62ebc69e14ffc258bc8862e3cfe32b8391a5bb
M:e55076d490739db93ec7ca21031b85abb5904555 192.168.100.15:7000
slots:0-5460 (5461 slots) master
M:bb62ebc69e14ffc258bc8862e3cfe32b8391a5bb 192.168.100.15:7001
slots:10923-16383 (5461 slots) master
S:e06ad950d3cc1724990e85d4bfa3b0381d4e74a7 192.168.100.15:7002
replicates730e03c2092124f1c284c79266eded188aee0388
Can Iset the above configuration? (type 'yes' to accept):
>>>Nodes configuration updated
>>>Assign a different config epoch to each node
>>>Sending CLUSTER MEET messages to join the cluster
Waitingfor the cluster to join...
>>>Performing Cluster Check (using node 192.168.100.11:7000)
M:730e03c2092124f1c284c79266eded188aee0388 192.168.100.11:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S:169c46253de8bea9e4f4763d6f222400ad52cbf6 192.168.100.11:7002
slots: (0 slots) slave
replicatesbb62ebc69e14ffc258bc8862e3cfe32b8391a5bb
S:23c6df13bfc80f7980dd9735e91561cc073e9c1f 192.168.100.11:7001
slots: (0 slots) slave
replicatese55076d490739db93ec7ca21031b85abb5904555
S:e06ad950d3cc1724990e85d4bfa3b0381d4e74a7 192.168.100.15:7002
slots: (0 slots) slave
replicates730e03c2092124f1c284c79266eded188aee0388
M:e55076d490739db93ec7ca21031b85abb5904555 192.168.100.15:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M:bb62ebc69e14ffc258bc8862e3cfe32b8391a5bb 192.168.100.15:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK]All nodes agree about slots configuration.
>>>Check for open slots...
>>>Check slots coverage...
[OK]All 16384 slots covered.
第四步:测试
使用redis-cli
首先登入7000这个端口的redis服务
./redis-cli -c -h 192.168.100.11 -p 7000
192.168.100.11:7000> set name andyhua.chen
在192.168.100.15上登录7002端口
./redis-cli -c -h 192.168.100.15 -p 7002
192.168.100.15:7002> get name #如果没有集群的话,这里肯定是没有的,现在则出现如下情况
-> Redirected to slot [5798] located at 192.168.100.11:7000
"andyhua.chen"
下面关于Redis的文章您也可能喜欢,不妨参考下: