安装必要插件
yum -y install gcc gcc-c++ libstdc++-devel
下载并解压
redis_version=5.0.5
wget http://download.redis.io/releases/redis-${redis_version}.tar.gz
tar xzf redis-${redis_version}.tar.gz
cd redis-5.0.5
编译安装
创建redis安装目录:
mkdir -p /usr/local/redis
进入解压后的redis目录,执行make命令,将redis安装进/usr/local/redis
make install PREFIX=/usr/local/redis
创建安装目录
在该目录下创建6个目录:7001-7006
mkdir -p /usr/local/redis-cluster
mkdir -p /data/redis
将redis配置文件redis.conf复制到集群目录下
cp redis.conf /usr/local/redis-cluster
创建redis安装目录
mkdir -p 7001 7002 7003 7004 7005 7006
将bin目录复制进6个redis节点工作目录中
cp -r /usr/local/redis/bin /usr/local/redis-cluster/7001
cp -r /usr/local/redis/bin /usr/local/redis-cluster/7002
cp -r /usr/local/redis/bin /usr/local/redis-cluster/7003
cp -r /usr/local/redis/bin /usr/local/redis-cluster/7004
cp -r /usr/local/redis/bin /usr/local/redis-cluster/7005
cp -r /usr/local/redis/bin /usr/local/redis-cluster/7006
修改redis.conf
port 7001
cluster-enabled yes
cluster-node-timeout 15000
appendonly yes
daemonize yes
protected-mode no
pidfile /var/run/redis_7001.pid
cluster-config-file redis_7001.conf
复制redis.conf
cp redis.conf 7001
cp redis.conf 7002
cp redis.conf 7003
cp redis.conf 7004
cp redis.conf 7005
cp redis.conf 7006
利用sed命令修改端口号
sed -i s/7001/7002/g /usr/local/redis-cluster/7001/redis.conf
sed -i s/7001/7002/g /usr/local/redis-cluster/7002/redis.conf
sed -i s/7001/7003/g /usr/local/redis-cluster/7003/redis.conf
sed -i s/7001/7004/g /usr/local/redis-cluster/7004/redis.conf
sed -i s/7001/7005/g /usr/local/redis-cluster/7005/redis.conf
sed -i s/7001/7006/g /usr/local/redis-cluster/7006/redis.conf
启动脚本
start-redis.sh
chmod +x start-redis.sh
start-redis.sh脚本内容
#/bin/bash
/usr/local/redis-cluster/7001/redis-server /usr/local/redis-cluster/7001/redis.conf
/usr/local/redis-cluster/7002/redis-server /usr/local/redis-cluster/7002/redis.conf
/usr/local/redis-cluster/7003/redis-server /usr/local/redis-cluster/7003/redis.conf
/usr/local/redis-cluster/7004/redis-server /usr/local/redis-cluster/7004/redis.conf
/usr/local/redis-cluster/7005/redis-server /usr/local/redis-cluster/7005/redis.conf
/usr/local/redis-cluster/7006/redis-server /usr/local/redis-cluster/7006/redis.conf
查看redis进程
[root@redis-cluster ~]# ./start-redis.sh
[root@redis-cluster ~]# ps -ef | grep redis
root 42394 1 0 08:52 ? 00:00:00 /usr/local/redis-cluster/7001/redis-server *:7001
root 42399 1 0 08:52 ? 00:00:00 /usr/local/redis-cluster/7002/redis-server *:7002
root 42404 1 0 08:52 ? 00:00:00 /usr/local/redis-cluster/7003/redis-server *:7003
root 42409 1 0 08:52 ? 00:00:00 /usr/local/redis-cluster/7004/redis-server *:7004
root 42414 1 0 08:52 ? 00:00:00 /usr/local/redis-cluster/7005/redis-server *:7005
root 42419 1 0 08:52 ? 00:00:00 /usr/local/redis-cluster/7006/redis-server *:7006
root 42424 9441 0 08:52 pts/0 00:00:00 grep --color=auto redis
[root@redis-cluster ~]#
用redis-cli创建整个redis集群(
/usr/local/redis/bin/redis-cli --cluster create --cluster-replicas 1 192.168.66.100:7001 192.168.66.100:7002 192.168.66.100:7003 192.168.66.100:7004 192.168.66.100:7005 192.168.66.100:7006
[root@redis-cluster redis-cluster]# /usr/local/redis/bin/redis-cli --cluster create --cluster-replicas 1 192.168.66.100:7001 192.168.66.100:7002 192.168.66.100:7003 192.168.66.100:7004 192.168.66.100:7005 192.168.66.100:7006
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.66.100:7005 to 192.168.66.100:7001
Adding replica 192.168.66.100:7006 to 192.168.66.100:7002
Adding replica 192.168.66.100:7004 to 192.168.66.100:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 41914e83624a070fb21bf35c5e1c3de79dacc8ad 192.168.66.100:7001
slots:[0-5460] (5461 slots) master
M: 0b344562444bb1a2915da1a7e30dd09835bd22b3 192.168.66.100:7002
slots:[5461-10922] (5462 slots) master
M: 7b8785416543f3e963d3574407a9e5ce5b194e5b 192.168.66.100:7003
slots:[10923-16383] (5461 slots) master
S: 9a0475ad5aeeab47f4627d4fe843bf062677e5ba 192.168.66.100:7004
replicates 0b344562444bb1a2915da1a7e30dd09835bd22b3
S: 2601d5e6ddf9e9f099046fa8bf73c7c337b73b9f 192.168.66.100:7005
replicates 7b8785416543f3e963d3574407a9e5ce5b194e5b
S: 089f3e29089473f6f2938a37cadfd01518614a6a 192.168.66.100:7006
replicates 41914e83624a070fb21bf35c5e1c3de79dacc8ad
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.66.100:7001)
M: 41914e83624a070fb21bf35c5e1c3de79dacc8ad 192.168.66.100:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 089f3e29089473f6f2938a37cadfd01518614a6a 192.168.66.100:7006
slots: (0 slots) slave
replicates 41914e83624a070fb21bf35c5e1c3de79dacc8ad
S: 2601d5e6ddf9e9f099046fa8bf73c7c337b73b9f 192.168.66.100:7005
slots: (0 slots) slave
replicates 7b8785416543f3e963d3574407a9e5ce5b194e5b
M: 7b8785416543f3e963d3574407a9e5ce5b194e5b 192.168.66.100:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9a0475ad5aeeab47f4627d4fe843bf062677e5ba 192.168.66.100:7004
slots: (0 slots) slave
replicates 0b344562444bb1a2915da1a7e30dd09835bd22b3
M: 0b344562444bb1a2915da1a7e30dd09835bd22b3 192.168.66.100:7002
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.
验证集群
/usr/local/redis/bin/redis-cli -c -h 192.168.66.100 -p 7001
提示:-a访问服务端密码,-c表示集群模式,指定ip地址和端口号
进入之后额外命令
进行验证: cluster info(查看集群信息)、cluster nodes(查看节点列表)
杀死redis 进程
ps -ef|grep -v grep |grep redis |awk '{print" kill -9 "$2}'|sh