Redis 创建和使用集群
服务器架构
192.168.188.136 192.168.188.137
下载Redis(两台服务器均需要操作)
【1】环境准备
yum -y install gcc automake autoconf libtool make
【2】下载解压
cd /home/ wget http://download.redis.io/releases/redis-5.0.3.tar.gz tar xzf redis-5.0.3.tar.gz
【3】编译
cd redis-5.0.3 make MALLOC=libc
【4】开放端口或关闭防火墙
service iptables stop
创建集群节点(两台服务器均需要操作)
【1】创建集群节点目录
mkdir /home/redis-cluster mkdir /home/redis-cluster/7000 /home/redis-cluster/7001 /home/redis-cluster/7002
【2】修改redis默认的配置文件
vim /home/redis-5.0.3/redis.conf port 7000 daemonize yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes bind Server_IP(192.168.188.136 / 192.168.188.137) 127.0.0.1
【3】复制redis-server & redis.conf
cp -a /home/redis-5.0.3/src/redis-server /home/redis-cluster/ cp -a /home/redis-5.0.3/redis.conf /home/redis-cluster/7000/; \ cp -a /home/redis-5.0.3/redis.conf /home/redis-cluster/7001/; \ cp -a /home/redis-5.0.3/redis.conf /home/redis-cluster/7002/;
【4】修改端口号 & 启动redis服务
<7000>
cd /home/redis-cluster/7000/ ../redis-server ./redis.conf
<7001>
# port 7001 cd ../7001/; vim redis.conf ../redis-server ./redis.conf
<7002>
# port 7002 cd ../7002/; vim redis.conf ../redis-server ./redis.conf
【5】查看节点启动状态
netstat -tunlp | grep redis
创建集群添加集群节点(192.168.188.136服务器)
cd /home/redis-5.0.3/src/ ./redis-cli --cluster create 192.168.188.136:7000 192.168.188.136:7001 192.168.188.136:7002 \ 192.168.188.137:7000 192.168.188.137:7001 192.168.188.137:7002 \ --cluster-replicas 1
集群测试
【1】192.168.188.136服务器操作
./redis-cli -c -h 192.168.188.136 -p 7000 192.168.188.136:7000> set hello world OK 192.168.188.136:7000> get hello "world" 192.168.188.136:7000> exit ./redis-cli -c -h 192.168.188.136 -p 7001 192.168.188.136:7001> set name bingjjfly -> Redirected to slot [5798] located at 192.168.188.137:7000 OK 192.168.188.137:7000> get hello -> Redirected to slot [866] located at 192.168.188.136:7000 "world" 192.168.188.136:7000> get name -> Redirected to slot [5798] located at 192.168.188.137:7000 "bingjjfly" 192.168.188.137:7000> exit ./redis-cli -c -h 192.168.188.136 -p 7002 192.168.188.136:7002> set address beijing -> Redirected to slot [3680] located at 192.168.188.136:7000 OK 192.168.188.136:7000> exit
【2】192.168.188.137服务器操作
cd /home/redis-5.0.3/src ./redis-cli -c -h 192.168.188.137 -p 7000 192.168.188.137:7000> get hello -> Redirected to slot [866] located at 192.168.188.136:7000 "world" 192.168.188.136:7000> exit ./redis-cli -c -h 192.168.188.137 -p 7001 192.168.188.137:7001> set age 18 -> Redirected to slot [741] located at 192.168.188.136:7000 OK 192.168.188.136:7000> exit ./redis-cli -c -h 192.168.188.137 -p 7002 192.168.188.137:7002> get hello -> Redirected to slot [866] located at 192.168.188.136:7000 "world" 192.168.188.136:7000> get name -> Redirected to slot [5798] located at 192.168.188.137:7000 "bingjjfly" 192.168.188.137:7000> get address -> Redirected to slot [3680] located at 192.168.188.136:7000 "beijing" 192.168.188.137:7002> get age -> Redirected to slot [741] located at 192.168.188.136:7000 "18" 192.168.188.136:7000> exit
【3】192.168.188.136服务器操作
./redis-cli -c -h 192.168.188.136 -p 7002 192.168.188.136:7002> get age -> Redirected to slot [741] located at 192.168.188.136:7000 "18" 192.168.188.136:7000> exit
参考
【1】下载
【2】创建集群
https://redis.io/topics/cluster-tutorial