centos7 下面搭建redis-cluster模式
首先确认有没有安装gcc环境,没有先另行安装
1.下载redis安装包
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
2.解压压缩包
tar -zxvf redis-4.0.6.tar.gz
3.执行安装命令:
cd redis-4.0.6
执行:make && make install
redis-server redis-cli redis-sentinel等可以执行程序默认在安装到/usr/local/bin/目录下面了
4.安装好了之后开始搭建cluster环境:
cd /usr/local/
mkdir cluster-test
下面创建6个存放不同端口启动的配置文件redis.conf,这里是在一台物理机上配置的三主三从模式,官方建议
最少就要3主3从,下面创建:
mkdir 7000 7001 7002 7003 7004 7005
cd 7000
vi redis.conf
redis.conf配置内容:
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes #后台运行
protected-mode no #允许外网访问
# bind 127.0.0.1 192.1.1.1 允许多个ip访问
接下来的7001...7005都是这样配置,修改下端口为对象文件名称就行
cp /usr/local/cluster/7000/redis.conf /usr/local/cluster/7001
5. 启动每个redis实例
cd /usr/local/cluster/7000/
复制redis-server到cluster目录下面
cp /usr/local/bin/redis-server ../
启动每个实例
cd 7000 自动生成的集群配置文件会放到当前目录下,例如nodes.conf
执行: ../redis-server redis.conf
启动成功,以次启动每个实例, ../redis-server ../7001/redis.conf
下面用命令工具创建集群
cd /redis-4.0.6/src/
目录下面有redis-trib.rb
./redis-trib.rb create --replicas 1 192.168.110.1:7000 192.168.110.1:7001 192.168.110.1:7002 192.168.110.1:7003 192.168.110.1:7004 192.168.110.1:7005
好了这样就搭建好了一个redis-cluster模式的集群
6.使用集群
$ redis-cli -c -p 7000
redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"