os: centos7.4
redis:4.0.9
ruby:2.3.4
ip 规划
redis1 192.168.56.101
redis2 192.168.56.102
用两台虚拟机模拟6个redis节点。
分别为
192.168.56.101 的 9001、9002、9003 端口,
192.168.56.102 的 9001、9002、9003 端口。
os依赖包
# yum install gcc gcc-c++ openssl openssl-devel zlib zlib-devel tcl tcl-devel iftop sysstat telnet tree curl lrzsz
下载、编译、安装
redis1、redis2 两个节点都需要操作
# mkdir -p /usr/redis
# cd /usr/redis
# wget http://download.redis.io/releases/redis-4.0.9.tar.gz
# tar -zxvf ./redis-4.0.9.tar.gz
# cd redis-4.0.9
# make
# make test
# make install
redis1 节点上操作,创建 cluster data 目录
# mkdir -p /usr/redis/redis-cluster-data
# cd /usr/redis/redis-cluster-data
# mkdir -p 9001 9002 9003
redis2 节点上操作,创建 cluster data 目录
# mkdir -p /usr/redis/redis-cluster-data
# cd /usr/redis/redis-cluster-data
# mkdir -p 9001 9002 9003
redis1、redis2 两个节点都需要操作
创建bin目录,添加到PATH里
# cd /usr/redis/redis-4.0.9
# mkdir bin
# cd src
# cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel redis-trib.rb ../bin
# vi /etc/profile
PATH=/usr/redis/redis-4.0.9/bin:$PATH
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# source /etc/profile
# echo $PATH
/usr/redis/redis-4.0.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
redis cluster 配置
redis1 的 9001 9002 9003 目录下,
redis2 的 9001 9001 9003 目录下都需要创建 redis.conf
$ cd /usr/redis/redis-cluster-data/9001
$ vi redis.conf
#监听port
port 9001
#监听ip
bind 192.168.56.101
#redis后台运行
daemonize yes
#pidfile路径
pidfile /tmp/redis_9001.pid
#开启集群
cluster-enabled yes
#集群的配置文件
cluster-config-file cluster_9001.conf
#请求超时,默认15秒,可自行设置
cluster-node-timeout 15000
#aof模式
appendonly yes
#aof模式文件
appendfilename "appendonly.aof"
#aof同步
appendfsync everysec
#日志层级分为4个等级:debug verbose notice warning
loglevel notice
#日志文件
logfile /var/log/redis/redis-server-9001.log
#数据库个数
databases 16
#工作目录
dir /usr/redis/redis-cluster-data/9001
#最大连接数
maxclients 20000
#redis内存限制
maxmemory 1gb
#redis内存策略
maxmemory-policy allkeys-lru
#rdb校验
rdbchecksum yes
# tree
.
├── 9001
│ └── redis.conf
├── 9002
│ └── redis.conf
└── 9003
└── redis.conf
3 directories, 3 files
启动redis
redis1 上启动 redis
# cd /usr/redis/redis-cluster-data/9001
# redis-server /usr/redis/redis-cluster-data/9001/redis.conf
# cd /usr/redis/redis-cluster-data/9002
# redis-server /usr/redis/redis-cluster-data/9002/redis.conf
# cd /usr/redis/redis-cluster-data/9003
# redis-server /usr/redis/redis-cluster-data/9003/redis.conf
# tree
.
├── 9001
│ ├── appendonly.aof
│ ├── cluster_9001.conf
│ └── redis.conf
├── 9002
│ ├── appendonly.aof
│ ├── cluster_9002.conf
│ └── redis.conf
└── 9003
├── appendonly.aof
├── cluster_9003.conf
└── redis.conf
3 directories, 9 files
redis2 上启动 redis
# cd /usr/redis/redis-cluster-data/9001
# redis-server /usr/redis/redis-cluster-data/9001/redis.conf
# cd /usr/redis/redis-cluster-data/9002
# redis-server /usr/redis/redis-cluster-data/9002/redis.conf
# cd /usr/redis/redis-cluster-data/9003
# redis-server /usr/redis/redis-cluster-data/9003/redis.conf
# tree
.
├── 9001
│ ├── appendonly.aof
│ ├── cluster_9001.conf
│ └── redis.conf
├── 9002
│ ├── appendonly.aof
│ ├── cluster_9002.conf
│ └── redis.conf
└── 9003
├── appendonly.aof
├── cluster_9003.conf
└── redis.conf
3 directories, 9 files
查看redis 的启动情况
# ps -ef|grep -i redis
root 20531 1 0 May17 ? 00:00:17 redis-server 192.168.56.101:9001 [cluster]
root 20536 1 0 May17 ? 00:00:17 redis-server 192.168.56.101:9002 [cluster]
root 20541 1 0 May17 ? 00:00:17 redis-server 192.168.56.101:9003 [cluster]
root 30028 1851 0 09:14 pts/0 00:00:00 grep --color=auto -i redis
# netstat -antp|grep -i redis
tcp 0 0 192.168.56.101:9001 0.0.0.0:* LISTEN 20531/redis-server
tcp 0 0 192.168.56.101:9002 0.0.0.0:* LISTEN 20536/redis-server
tcp 0 0 192.168.56.101:9003 0.0.0.0:* LISTEN 20541/redis-server
tcp 0 0 192.168.56.101:19001 0.0.0.0:* LISTEN 20531/redis-server
tcp 0 0 192.168.56.101:19002 0.0.0.0:* LISTEN 20536/redis-server
tcp 0 0 192.168.56.101:19003 0.0.0.0:* LISTEN 20541/redis-server
# ps -ef|grep -i redis
root 16509 1 0 May17 ? 00:00:17 redis-server 192.168.56.102:9001 [cluster]
root 16537 1 0 May17 ? 00:00:17 redis-server 192.168.56.102:9002 [cluster]
root 16632 1 0 May17 ? 00:00:17 redis-server 192.168.56.102:9003 [cluster]
root 26083 1837 0 09:15 pts/0 00:00:00 grep --color=auto -i redis
# netstat -antp|grep -i redis
tcp 0 0 192.168.56.102:9001 0.0.0.0:* LISTEN 16509/redis-server
tcp 0 0 192.168.56.102:9002 0.0.0.0:* LISTEN 16537/redis-server
tcp 0 0 192.168.56.102:9003 0.0.0.0:* LISTEN 16632/redis-server
tcp 0 0 192.168.56.102:19001 0.0.0.0:* LISTEN 16509/redis-server
tcp 0 0 192.168.56.102:19002 0.0.0.0:* LISTEN 16537/redis-server
tcp 0 0 192.168.56.102:19003 0.0.0.0:* LISTEN 16632/redis-server
创建redis集群
执行 redis-trib.rb 前必须安装ruby >= 2.2.2.
# yum install ruby ruby-devel ruby-libs rubygems rpm-build
# gem install redis
Fetching: redis-4.0.1.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
默认的centos 7.4 yum源只支持到 ruby 2.0.0。所有上面的yum install 就不要执行了,我已经帮大家采坑了。
参考一哥们的解决办法:
IT虾米网
先安装rvm(rvm : Ruby enVironment Manager),再把ruby版本提升至2.3.4,中间有错误根据错误提示处理就可以了。
1.安装curl
# yum install curl
2.安装rvm
# curl -L get.rvm.io | bash -s stable
3.解压rvm
# cd /usr/local/rvm/archives
# ls -l
# tar -zxvf ./rvm-1.29.3.tgz
# source /usr/local/rvm/archives/rvm-1.29.3/scripts/rvm
4.查看rvm库中已知的ruby版本
# rvm list known
5.安装一个ruby版本
# rvm install 2.3.4
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.3.4.tar.bz2
Checking requirements for centos.
Requirements installation successful.
ruby-2.3.4 - #configure
ruby-2.3.4 - #download
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 25.2M 100 25.2M 0 0 3126k 0 0:00:08 0:00:08 --:--:-- 3384k
ruby-2.3.4 - #validate archive
ruby-2.3.4 - #extract
ruby-2.3.4 - #validate binary
ruby-2.3.4 - #setup
ruby-2.3.4 - #gemset created /usr/local/rvm/archives/rvm-1.29.3/gems/ruby-2.3.4@global
ruby-2.3.4 - #importing gemset /usr/local/rvm/archives/rvm-1.29.3/gemsets/global.gems.........................
ruby-2.3.4 - #generating global wrappers.......
ruby-2.3.4 - #gemset created /usr/local/rvm/archives/rvm-1.29.3/gems/ruby-2.3.4
ruby-2.3.4 - #importing gemsetfile /usr/local/rvm/archives/rvm-1.29.3/gemsets/default.gems evaluated to empty gem list
ruby-2.3.4 - #generating default wrappers.......
6.使用一个ruby版本
# rvm use 2.3.4
7.设置默认版本
8.卸载一个已知版本
# rvm remove 2.0.0
# ruby --version
ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux]
9.安装redis依赖
# gem install redis
Fetching: redis-4.0.1.gem (100%)
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 0 seconds
1 gem installed
修改环境变量
# echo $PATH
/usr/local/rvm/archives/rvm-1.29.3/gems/ruby-2.3.4/bin:/usr/local/rvm/archives/rvm-1.29.3/gems/ruby-2.3.4@global/bin:/usr/local/rvm/archives/rvm-1.29.3/rubies/ruby-2.3.4/bin:/usr/local/rvm/archives/rvm-1.29.3/bin:/usr/redis/redis-4.0.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# vi /etc/profile
export PATH=/usr/local/rvm/archives/rvm-1.29.3/gems/ruby-2.3.4/bin:/usr/local/rvm/archives/rvm-1.29.3/gems/ruby-2.3.4@global/bin:/usr/local/rvm/archives/rvm-1.29.3/rubies/ruby-2.3.4/bin:/usr/local/rvm/archives/rvm-1.29.3/bin:/usr/redis/redis-4.0.9/bin:$PATH
正式创建
# redis-trib.rb create --replicas 1 192.168.56.101:9001 192.168.56.101:9002 192.168.56.101:9003 192.168.56.102:9001 192.168.56.102:9002 192.168.56.102:9003
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.56.101:9001
192.168.56.102:9001
192.168.56.101:9002
Adding replica 192.168.56.102:9003 to 192.168.56.101:9001
Adding replica 192.168.56.101:9003 to 192.168.56.102:9001
Adding replica 192.168.56.102:9002 to 192.168.56.101:9002
M: 8240d131c58c8b687c11462e03825bdba00af53b 192.168.56.101:9001
slots:0-5460 (5461 slots) master
M: 0bdd8387a662984e95f5103522f52e75eceaab69 192.168.56.101:9002
slots:10923-16383 (5461 slots) master
S: e38d78fc68b69d4e5f347cf96f31a9cb6a02dccf 192.168.56.101:9003
replicates 12e2353c9f0b085d2684da46ba0dc69575d68603
M: 12e2353c9f0b085d2684da46ba0dc69575d68603 192.168.56.102:9001
slots:5461-10922 (5462 slots) master
S: 2b48fd08a8ec55eba5bfa0d163d02c8462637302 192.168.56.102:9002
replicates 0bdd8387a662984e95f5103522f52e75eceaab69
S: cf22edbc6463b8b31536819297ac50e9e39175ad 192.168.56.102:9003
replicates 8240d131c58c8b687c11462e03825bdba00af53b
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.56.101:9001)
M: 8240d131c58c8b687c11462e03825bdba00af53b 192.168.56.101:9001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: cf22edbc6463b8b31536819297ac50e9e39175ad 192.168.56.102:9003
slots: (0 slots) slave
replicates 8240d131c58c8b687c11462e03825bdba00af53b
S: e38d78fc68b69d4e5f347cf96f31a9cb6a02dccf 192.168.56.101:9003
slots: (0 slots) slave
replicates 12e2353c9f0b085d2684da46ba0dc69575d68603
S: 2b48fd08a8ec55eba5bfa0d163d02c8462637302 192.168.56.102:9002
slots: (0 slots) slave
replicates 0bdd8387a662984e95f5103522f52e75eceaab69
M: 0bdd8387a662984e95f5103522f52e75eceaab69 192.168.56.101:9002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 12e2353c9f0b085d2684da46ba0dc69575d68603 192.168.56.102:9001
slots:5461-10922 (5462 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.
# cd /usr/redis/redis-cluster-data
# tree
.
├── 9001
│ ├── appendonly.aof
│ ├── cluster_9001.conf
│ ├── dump.rdb
│ └── redis.conf
├── 9002
│ ├── appendonly.aof
│ ├── cluster_9002.conf
│ ├── dump.rdb
│ └── redis.conf
└── 9003
├── appendonly.aof
├── cluster_9003.conf
├── dump.rdb
└── redis.conf
3 directories, 12 files
连接redis
# redis-cli -h 192.168.56.101 -p 9001 -c
192.168.56.101:9001>
192.168.56.101:9001>
192.168.56.101:9001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:23175
cluster_stats_messages_pong_sent:25275
cluster_stats_messages_sent:48450
cluster_stats_messages_ping_received:25270
cluster_stats_messages_pong_received:23175
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:48450
192.168.56.101:9001> cluster nodes
cf22edbc6463b8b31536819297ac50e9e39175ad 192.168.56.102:9003@19003 slave 8240d131c58c8b687c11462e03825bdba00af53b 0 1526649623970 6 connected
8240d131c58c8b687c11462e03825bdba00af53b 192.168.56.101:9001@19001 myself,master - 0 1526649623000 1 connected 0-5460
12e2353c9f0b085d2684da46ba0dc69575d68603 192.168.56.102:9001@19001 slave e38d78fc68b69d4e5f347cf96f31a9cb6a02dccf 0 1526649621000 7 connected
2b48fd08a8ec55eba5bfa0d163d02c8462637302 192.168.56.102:9002@19002 slave 0bdd8387a662984e95f5103522f52e75eceaab69 0 1526649624972 5 connected
0bdd8387a662984e95f5103522f52e75eceaab69 192.168.56.101:9002@19002 master - 0 1526649622000 2 connected 10923-16383
e38d78fc68b69d4e5f347cf96f31a9cb6a02dccf 192.168.56.101:9003@19003 master - 0 1526649622963 7 connected 5461-10922
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/9849.html