集群简介
Redis集群部署文档(centos6.4系统)
(要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下)
192.168.66.2:6379
192.168.66.2:6380
192.168.66.3:6379
192.168.66.3:6380
192.168.66.4:6379
192.168.66.5:6380
软件下载安装
- 下载redis:官网下载3.2.4版本。
-
上传服务器、解压、编译
- 解压编译
tar -zxvf redis-3.2.4-rc2.tar.gz mv redis-3.2.4-rc2.tar.gz redis3.0 cd /usr/local/redis3.0 make make install # 复制 redis-trib.rb 到 /usr/local/bin cp /opt/tools/redis-3.2.4/src/redis-trib.rb /usr/local/bin/
- 报错解决
yum install -y gcc g++ gcc-c++
-
创建所需要的目录
cd /opt/tools/ mkdir redis-cluster cd redis-cluster mkdir 6379 mkdir 6380
-
修改配置文件:
-
复制 redis.conf 文件
cp redis-3.2.4/redis.conf redis-cluster/6379
修改 文件的如下内容:
#进程端口 port 6379 #进程id文件存放位置 pidfile /var/run/redis-6379.pid #绑定机器配置 bind 192.168.66.2 #日志文件存放位置 logfile /opt/tools/redis-cluster/6379/redis.log #数据备份文件存放位置 dir /opt/tools/redis-cluster/6379 #是否是追加 appendonly yes #是否开启分布式 cluster-enabled yes #集群配置文件 cluster-config-file /opt/tools/redis-cluster/6379/nodes.conf
-
-
拷贝文件到其他几台机器
# 拷贝redis-server、redis-client... scp -r /usr/local/bin/* root@192.168.66.3:/usr/local/bin/ # 拷贝配置文件 scp -r /opt/tools/redis-cluster root@192.168.66.3:/opt/tools/ # 拷贝redis-server、redis-client... scp -r /usr/local/bin/* root@192.168.66.4:/usr/local/bin/ # 拷贝配置文件 scp -r /opt/tools/redis-cluster root@192.168.66.4:/opt/tools/
-
编写集群启动脚本redisStart.sh(集群启动脚本需要各个机器之间设置 ssh 免认证登录)
#!/bin/bash #声明服务器: server2=192.168.66.2 server3=192.168.66.3 server4=192.168.66.4 # 1.删除进程 # 本地 ps -ef | grep 6379 | grep -v grep | awk '{print $2}' | xargs kill -9 ps -ef | grep 6380 | grep -v grep | awk '{print $2}' | xargs kill -9 ssh root@$server3 "ps -ef | grep 6379 | grep -v grep | awk '{print $2}' | xargs kill -9" ssh root@$server3 "ps -ef | grep 6380 | grep -v grep | awk '{print $2}' | xargs kill -9" ssh root@$server4 "ps -ef | grep 6379 | grep -v grep | awk '{print $2}' | xargs kill -9" ssh root@$server4 "ps -ef | grep 6380 | grep -v grep | awk '{print $2}' | xargs kill -9" # 2.删除文件 # 本地 rm -rf /opt/tools/redis-cluster/6379/appendonly.aof rm -rf /opt/tools/redis-cluster/6379/dump.rdb rm -rf /opt/tools/redis-cluster/6379/nodes.conf rm -rf /opt/tools/redis-cluster/6380/appendonly.aof rm -rf /opt/tools/redis-cluster/6380/dump.rdb rm -rf /opt/tools/redis-cluster/6380/nodes.conf # 12 ssh root@$server3 "rm -rf /opt/tools/redis-cluster/6379/appendonly.aof" ssh root@$server3 "rm -rf /opt/tools/redis-cluster/6379/dump.rdb" ssh root@$server3 "rm -rf /opt/tools/redis-cluster/6379/nodes.conf" ssh root@$server3 "rm -rf /opt/tools/redis-cluster/6380/appendonly.aof" ssh root@$server3 "rm -rf /opt/tools/redis-cluster/6380/dump.rdb" ssh root@$server3 "rm -rf /opt/tools/redis-cluster/6380/nodes.conf" # 13 ssh root@$server4 "rm -rf /opt/tools/redis-cluster/6379/appendonly.aof" ssh root@$server4 "rm -rf /opt/tools/redis-cluster/6379/dump.rdb" ssh root@$server4 "rm -rf /opt/tools/redis-cluster/6379/nodes.conf" ssh root@$server4 "rm -rf /opt/tools/redis-cluster/6380/appendonly.aof" ssh root@$server4 "rm -rf /opt/tools/redis-cluster/6380/dump.rdb" ssh root@$server4 "rm -rf /opt/tools/redis-cluster/6380/nodes.conf" # 3.启动进程 # 本地 /usr/local/bin/redis-server /opt/tools/redis-cluster/6379/redis.conf /usr/local/bin/redis-server /opt/tools/redis-cluster/6380/redis.conf #12 ssh root@$server3 "/usr/local/bin/redis-server /opt/tools/redis-cluster/6379/redis.conf" ssh root@$server3 "/usr/local/bin/redis-server /opt/tools/redis-cluster/6380/redis.conf" #13 ssh root@$server4 "/usr/local/bin/redis-server /opt/tools/redis-cluster/6379/redis.conf" ssh root@$server4 "/usr/local/bin/redis-server /opt/tools/redis-cluster/6380/redis.conf" #启动集群 redis-trib.rb create --replicas 1 $server2:6379 $server2:6380 $server3:6379 $server3:6380 $server4:6379 $server4:6380
-
启动集群
./redisStart.sh
-
报错解决
- 错误内容:/usr/bin/env: ruby: No such file or directory
yum install ruby
- 错误内容:./redis-trib.rb:24:in `require’: no such file to load – rubygems (LoadError) from ./redis-trib.rb:24
yum install rubygems
- 错误内容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from ./redis-trib.rb:25
- 安装缺少软件并指定版本(版本很重要,版本不对导致无法启动):
gem install redis -v 3.2.2
- 再次执行第7步的命令,正常执行!
- 输入yes,然后配置完成,至此redis集群即搭建成功!
11. 使用redis-cli命令进入集群环境
redis-cli -h 192.168.66.2 -c -p 6379