redis集群搭建

  1. Redis的安装
    1.1 安装redis前需要首先安装gcc,使用命令yum install –y gcc进行安装。
    [root@localhost redis]# yum install -y gcc
    1.2 将redis-3.2.0.tar.gz放在/home目录下,使用 tar –zxvf redis-3.2.0.tar.gz解压
    [root@localhost home]# tar -zxvf redis-3.2.0.tar.gz
    1.3 将解压得到的redis-3.2.0重命名为redis
    [root@localhost home]# mv redis-3.2.0 redis
    1.4 进入redis目录,使用make进行编译
    [root@localhost home]# cd redis
    [root@localhost redis]# make
    1.5 使用make install 安装redis
    [root@localhost redis]# make install
    1.6 在redis目录下创建bin目录并将redis/src目录下的可执行脚本放入bin目录
    [root@localhost redis]# mkdir bin
    [root@localhost redis]# cd src
    [root@localhost src]# mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server redis-check-rdb /home/redis/bin
    1.7 复制redis目录下的redis.conf配置文件到/etc/redis/目录下
    [root@localhost src]# cd ..
    [root@localhost redis]# cp redis.conf /etc/redis/
    [root@localhost redis]# mkdir /etc/redis/
    [root@localhost redis]# cp redis.conf /etc/redis/
    1.8 修改/etc/redis/redis.conf配置文件
    [root@localhost redis]# vi /etc/redis/redis.conf
    a) 修改对外端口和IP。
    b) 修改日志级别和日志文件路径。
    c) 修改数据文件路径和名称。
    d) 为了更好地保障数据的存储,请打开appendonly,这样可以更好地保障内存数据能及时地被redis持久化到硬盘。

bind 127.0.0.1

protected-mode no
daemonize yes
logfile /home/redis/log/redis.log
dir /home/redis/data/
appendonly yes
注意新建相应的文件夹
[root@localhost redis]# mkdir log data
1.9 启动与关闭redis
redis启动命令为
[root@localhost redis]# redis-server /etc/redis/redis.conf
redis的关闭命令为
[root@localhost redis]# redis-cli shutdown

可以通过ps –aux | grep redis查看redis是否启动

  1. Redis的主从配置
    修改redis从库的/etc/redis/redis.conf配置文件,增加salveof ,重启redis服务。
    [root@localhost ~]# vi /etc/redis/redis.conf
    如主库ip为192.168.115.133,则在配置文件中添加
    slaveof 192.168.115.133 6379
  2. Redis的主从切换
    复制/home/redis/目录下的sentinel.conf至/etc/redis/,添加如下配置:
    logfile /home/redis/log/sentinel.log
    daemonize yes

sentinel启动方式:
redis-sentinel /etc/redis/sentinel.conf –protected-mode no
4. Redis集群的部署
在虚拟机上部署6个节点(3个主节点,3个从节点)。
4.1 redis集群的部署需要安装ruby和rubygems,以及redis-3.2.0.gem(redis和ruby的接口)。
[root@localhost home]# yum install -y ruby
[root@localhost home]# yum install -y rubygems
将redis-3.2.0.gem拷贝至/home目录下,安装gem
[root@localhost home]# gem install redis-3.2.0.gem
https://rubygems.org/gems/redis/versions/3.2.0
下载地址
4.2 在/home下创建redis_cluster目录
[root@localhost home]# mkdir redis_cluster
4.3 进入redis_cluster目录下,创建7001,7002,7003,7004,7005,7006目录
[root@localhost home]# cd redis_cluster/
[root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006
4.4 将/home/redis/redis.conf文件分别拷贝到7001-7006中
[root@localhost redis_cluster]# cp /home/redis/redis.conf 7001
[root@localhost redis_cluster]# cp /home/redis/redis.conf 7002
[root@localhost redis_cluster]# cp /home/redis/redis.conf 7003
[root@localhost redis_cluster]# cp /home/redis/redis.conf 7004
[root@localhost redis_cluster]# cp /home/redis/redis.conf 7005
[root@localhost redis_cluster]# cp /home/redis/redis.conf 7006
4.5 将/home/redis/bin/redis-server文件分别拷贝到7001-7006中
[root@localhost redis_cluster]# cp /home/redis/bin/redis-server 7001
[root@localhost redis_cluster]# cp /home/redis/bin/redis-server 7002
[root@localhost redis_cluster]# cp /home/redis/bin/redis-server 7003
[root@localhost redis_cluster]# cp /home/redis/bin/redis-server 7004
[root@localhost redis_cluster]# cp /home/redis/bin/redis-server 7005
[root@localhost redis_cluster]# cp /home/redis/bin/redis-server 7006
4.6 分别对7001-7006中的redis.conf文件进行配置
daemonize yes
pidfile /var/run/redis-7001.pid
port 7001
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes

daemonize yes:使redis在后台运行
pidfile /var/run/redis-7001.pid:7001-7006分别改为对应的数字
port 7001:7001-7006分别改为对应的数字
cluster-enabled yes:开启集群,把文件中的#去掉即可
cluster-config-file nodes-7001.conf:集群的配置,分别改为对应的数字
cluster-node-timeout 5000:请求超时,5s
appendonly yes:开启AOF日志,每次写入操作都会记录一条日志

4.7 分别启动7001-7006
在/home/redis_cluster目录下新建all.sh文件
[root@localhost redis_cluster]# vi all.sh
输入以下内容
cd 7001
./redis-server redis.conf
cd ..
cd 7002
./redis-server redis.conf
cd ..
cd 7003
./redis-server redis.conf
cd ..
cd 7004
./redis-server redis.conf
cd ..
cd 7005
./redis-server redis.conf
cd ..
cd 7006
./redis-server redis.conf
cd ..
启动7001-7006
[root@localhost redis_cluster]# ./all.sh
使用ps aux | grep redis查看redis进程
[root@localhost redis_cluster]# ps aux | grep redis
4.8 节点创建好之后开始创建集群,使用redis-trib.rb完成redis集群的搭建
[root@localhost redis_cluster]# cd /home/redis/src
[root@localhost src]# ./redis-trib.rb create –replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

Creating cluster
Performing hash slots allocation on 6 nodes…
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: ae7e8761fc72d703891cf22e7a38071ace868820 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 745d4c65870f3c6109268adee962a9dc536e584d 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 4633eff2b9af0c329cde19028d8acd9181f95cf5 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
S: b4d520a874029ac0c7f569de57f3431eb7d2ddea 127.0.0.1:7004
replicates ae7e8761fc72d703891cf22e7a38071ace868820
S: 6cd655514fa3932e8e95e168ff541368dde0fb45 127.0.0.1:7005
replicates 745d4c65870f3c6109268adee962a9dc536e584d
S: ac1e983f0d36e2dffab4e47671f336f5634a8131 127.0.0.1:7006
replicates 4633eff2b9af0c329cde19028d8acd9181f95cf5
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 127.0.0.1:7001)
M: ae7e8761fc72d703891cf22e7a38071ace868820 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 745d4c65870f3c6109268adee962a9dc536e584d 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 4633eff2b9af0c329cde19028d8acd9181f95cf5 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
M: b4d520a874029ac0c7f569de57f3431eb7d2ddea 127.0.0.1:7004
slots: (0 slots) master
replicates ae7e8761fc72d703891cf22e7a38071ace868820
M: 6cd655514fa3932e8e95e168ff541368dde0fb45 127.0.0.1:7005
slots: (0 slots) master
replicates 745d4c65870f3c6109268adee962a9dc536e584d
M: ac1e983f0d36e2dffab4e47671f336f5634a8131 127.0.0.1:7006
slots: (0 slots) master
replicates 4633eff2b9af0c329cde19028d8acd9181f95cf5
[OK] All nodes agree about slots configuration.
Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.

注意,如果使用VMware测试是(都在一台服务器),IP应该使用127.0.0.1,如果使用局域网IP,会提示无法连接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值