1. 安装
tar -zxvf redis-3.2.1.tar.gz cd redis-3.2.1/ make test cd redis-3.2.1/src/ make make install cd /usr/local/redis/bin ll #查看安装后的文件,如有redis-server表明安装成功 cp -rf /home/gilbert/app/redis-3.2.1/redis.conf /usr/local/redis/ #复制redis.conf到安装目录 cd /usr/local/redis/ ll #查看是否复制成功
如出现以下错误
得先安装tcl
tar xzvf tcl8.6.1-src.tar.gz -C /usr/local/ cd /usr/local/tcl8.6.1/unix/ ./configure make make install
2. redis命令
1)启动
cd /usr/local/redis/bin ./redis-server #启动redis
linux redis集群搭建
一 背景
用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master、3 salve 环境。
redis 采用 redis-3.2.1 版本。
两台虚拟机都是 CentOS ,
一台 CentOS(IP:192.168.1.128,三个端口7000,7001,7002),
一台 CentOS(IP:192.168.1.129,三个端口7003,7004,7005)
本人虚拟机如下图
二 安装前置条件
1. tcl
优先考虑yum安装
yum list tcl yum install -y tcl
如果yum源里没有,刚从官网下载gz包安装。以安装到/home/gilbert/app/下为例
cd /home/gilbert/app/ tar xzvf tcl8.6.1-src.tar.gz -C /usr/local/ cd /usr/local/tcl8.6.1/unix/ ./configure make make install
2. ruby
优先考虑yum安装
yum list ruby yum install -y ruby
如果yum源里没有,刚从官网下载gz包安装。以安装到/home/gilbert/app/下为例
cd /home/gilbert/app/ tar -zxvf ruby-2.2.7.tar.gz cd ruby-2.2.7 ./configure make test make make install
3. rubygems
优先考虑yum安装
yum list rubygems yum install -y rubygems
如果yum源里没有,刚从官网下载gz包安装。以安装到/home/gilbert/app/下为例
cd /home/gilbert/app/ tar -zxvf rubygems-2.7.7.tgz cd rubygems-2.7.7 ruby setup.rb
三 集群搭建
1.复制redis-trib.rb
cd /home/gilbert/app/ cd redis-3.2.1 cp src/redis-trib.rb /usr/local/bin/
2.创建 Redis 节点
1) 首先在 192.168.1.128 机器上 redis-3.2.1 目录下创建 redis_cluster 目录以及7000,7001,7002目录;
cd /home/gilbert/app/ cd redis-3.2.1 mkdir redis_cluster cd redis_cluster mkdir 7000 mkdir 7001 mkdir 7002
2) 将redis.conf拷贝到7000,7001和7002目录下
cp redis.conf redis_cluster/7001 cp redis.conf redis_cluster/7002 cp redis.conf redis_cluster/7003
3) 分别修改7000,7001和7002目录下redis.conf。需要修改的内容如下
port 7000 //端口7000,7001,7002 bind 192.168.1.128 //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群 daemonize yes //redis后台运行 pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002 cluster-enabled yes //开启集群 把注释#去掉 cluster-config-file nodes_7000.conf //集群的配置,配置文件首次启动自动生成 7000,7001,7002 cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置 appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
4)在192.168.1.129服务器重复以上3个步骤,但是目录分别为7003,7004和7005,redis.conf也修改为对应的7003,7004和7005
3.启动节点
1) 在 192.168.1.128 机器上启动
cd /home/gilbert/app/redis-3.2.1 redis-server redis_cluster/7000/redis.conf redis-server redis_cluster/7001/redis.conf redis-server redis_cluster/7002/redis.conf
2)在 192.168.1.129 机器上启动
cd /home/gilbert/app/redis-3.2.1 redis-server redis_cluster/7003/redis.conf redis-server redis_cluster/7004/redis.conf redis-server redis_cluster/7005/redis.conf
4. 检查 redis 启动情况
1) 在 192.168.1.128 机器上查看
ps -ef | grep redis root 2608 1 2 05:38 ? 00:00:00 redis-server 192.168.1.128:7000 [cluster] root 2610 1 1 05:38 ? 00:00:00 redis-server 192.168.1.128:7001 [cluster] root 2616 1 1 05:38 ? 00:00:00 redis-server 192.168.1.128:7002 [cluster] root 2620 2544 0 05:38 pts/0 00:00:00 grep redis netstat -tnlp | grep redis tcp 0 0 192.168.1.128:7000 0.0.0.0:* LISTEN 2608/redis-server 1 tcp 0 0 192.168.1.128:7001 0.0.0.0:* LISTEN 2610/redis-server 1 tcp 0 0 192.168.1.128:7002 0.0.0.0:* LISTEN 2616/redis-server 1 tcp 0 0 192.168.1.128:17000 0.0.0.0:* LISTEN 2608/redis-server 1 tcp 0 0 192.168.1.128:17001 0.0.0.0:* LISTEN 2610/redis-server 1 tcp 0 0 192.168.1.128:17002 0.0.0.0:* LISTEN 2616/redis-server 1
2) 在 192.168.1.129 机器上查看
ps -ef | grep redis root 2206 1 0 05:39 ? 00:00:00 redis-server 192.168.1.129:7003 [cluster] root 2208 1 0 05:39 ? 00:00:00 redis-server 192.168.1.129:7004 [cluster] root 2216 1 0 05:39 ? 00:00:00 redis-server 192.168.1.129:7005 [cluster] root 2221 2159 0 05:39 pts/0 00:00:00 grep redis netstat -tnlp | grep redis tcp 0 0 192.168.1.129:17003 0.0.0.0:* LISTEN 2206/redis-server 1 tcp 0 0 192.168.1.129:17004 0.0.0.0:* LISTEN 2208/redis-server 1 tcp 0 0 192.168.1.129:17005 0.0.0.0:* LISTEN 2216/redis-server 1 tcp 0 0 192.168.1.129:7003 0.0.0.0:* LISTEN 2206/redis-server 1 tcp 0 0 192.168.1.129:7004 0.0.0.0:* LISTEN 2208/redis-server 1 tcp 0 0 192.168.1.129:7005 0.0.0.0:* LISTEN 2216/redis-server 1
5.创建集群
1) 前置条件,安装了redis-3.2.1.gem并且redis安装到了gem
gem install -l /home/gilbert/app/redis-3.2.1.gem gem isntall redis
2) 创建集群节点
cd /home/gilbert/app/redis-3.2.1 redis-trib.rb create --replicas 1 192.168.1.128:7000 192.168.1.128:7001 192.168.1.128:7002 192.168.1.129:7003 192.168.1.129:7004 192.168.1.129:7005 >>> Creating cluster [ERR] Sorry, can't connect to node 192.168.1.129:7003 [root@master redis-3.2.1]# redis-trib.rb create --replicas 1 192.168.1.128:7000 192.168.1.128:7001 192.168.1.128:7002 192.168.1.129:7003 192.168.1.129:7004 192.168.1.129:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.1.129:7003 192.168.1.128:7000 192.168.1.129:7004 Adding replica 192.168.1.128:7001 to 192.168.1.129:7003 Adding replica 192.168.1.129:7005 to 192.168.1.128:7000 Adding replica 192.168.1.128:7002 to 192.168.1.129:7004 M: 3bf938e2aa2aa67fb734a3d5d9d238f0c0263655 192.168.1.128:7000 slots:5461-10922 (5462 slots) master S: 53d16b0d49c3acb955e0db24feed48f6e29eb1c6 192.168.1.128:7001 replicates 574a1ee8efa5cbf65e869a80e7a9fbed8efbf34a S: 328a6c998de381a4d14ededbb642f77b3275fbbb 192.168.1.128:7002 replicates aa5ca621c042b3e96c18db3d7af17ec1f1bce22f M: 574a1ee8efa5cbf65e869a80e7a9fbed8efbf34a 192.168.1.129:7003 slots:0-5460 (5461 slots) master M: aa5ca621c042b3e96c18db3d7af17ec1f1bce22f 192.168.1.129:7004 slots:10923-16383 (5461 slots) master S: fdcd0fa6de5e4166a893e4b1dc7f4cef5f6d78ac 192.168.1.129:7005 replicates 3bf938e2aa2aa67fb734a3d5d9d238f0c0263655 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join...... >>> Performing Cluster Check (using node 192.168.1.128:7000) M: 3bf938e2aa2aa67fb734a3d5d9d238f0c0263655 192.168.1.128:7000 slots:5461-10922 (5462 slots) master M: 53d16b0d49c3acb955e0db24feed48f6e29eb1c6 192.168.1.128:7001 slots: (0 slots) master replicates 574a1ee8efa5cbf65e869a80e7a9fbed8efbf34a M: 328a6c998de381a4d14ededbb642f77b3275fbbb 192.168.1.128:7002 slots: (0 slots) master replicates aa5ca621c042b3e96c18db3d7af17ec1f1bce22f M: 574a1ee8efa5cbf65e869a80e7a9fbed8efbf34a 192.168.1.129:7003 slots:0-5460 (5461 slots) master M: aa5ca621c042b3e96c18db3d7af17ec1f1bce22f 192.168.1.129:7004 slots:10923-16383 (5461 slots) master M: fdcd0fa6de5e4166a893e4b1dc7f4cef5f6d78ac 192.168.1.129:7005 slots: (0 slots) master replicates 3bf938e2aa2aa67fb734a3d5d9d238f0c0263655 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
6.集群验证
1) 在192.168.1.128服务器上连接7002端口节点
cd /home/gilbert/app/redis-3.2.1 redis-cli -h 192.168.1.128 -c -p 7002 192.168.1.128:7002>
2)在192.168.1.129服务器上连接7005端口节点
cd /home/gilbert/app/redis-3.2.1 redis-cli -h 192.168.1.129 -c -p 7005 192.168.1.129:7005> set hello word -> Redirected to slot [866] located at 192.168.1.129:7003 OK 192.168.1.129:7003>
3) 在192.168.1.128服务器上命令窗口中获取key的内容,如可获取key为hello的内容为word,表明搭建成功
192.168.1.128:7002> get hello -> Redirected to slot [866] located at 192.168.1.129:7003 "word" 192.168.1.129:7003> keys * 1) "hello" 192.168.1.129:7003>