redis-5.0.8集群环境搭建

1.前置准备

Linux版redis-3.2.8单机环境安装
redis-3.2.8主从复制架构搭建及哨兵配置
如果你对redis的安装很陌生,请看完我以上两期教程,对完成此期教程很有帮助

2.下载解压

redis-5.0.8.tar.gz
redis-5.0.8集群环境搭建_第1张图片

[xiaokang@hadoop01 ~]$ cd /opt/software/
[xiaokang@hadoop01 software]$ tar -zxvf redis-5.0.8.tar.gz 

3.编译安装

如果你没有安装C语言环境,需要执行以下命令进行安装,三台虚拟机都要安装C语言环境

[xiaokang@hadoop01 ~]$ sudo yum -y install gcc-c++
[xiaokang@hadoop02 ~]$ sudo yum -y install gcc-c++
[xiaokang@hadoop03 ~]$ sudo yum -y install gcc-c++
[xiaokang@hadoop01 redis-5.0.8]$ make
[xiaokang@hadoop01 redis-5.0.8]$ make PREFIX=/opt/software/redis-5.0.8/ install

4.配置环境变量

三台虚拟机均按如下所示配置环境变量

[xiaokang@hadoop01 ~]$ sudo vim /etc/profile.d/env.sh
export REDIS_HOME=/opt/software/redis-5.0.8
${REDIS_HOME}/bin:
[xiaokang@hadoop01 ~]$ . /etc/profile.d/env.sh

[xiaokang@hadoop02 ~]$ sudo vim /etc/profile.d/env.sh
export REDIS_HOME=/opt/software/redis-5.0.8
${REDIS_HOME}/bin:
[xiaokang@hadoop02 ~]$ . /etc/profile.d/env.sh

[xiaokang@hadoop03 ~]$ sudo vim /etc/profile.d/env.sh
export REDIS_HOME=/opt/software/redis-5.0.8
${REDIS_HOME}/bin:
[xiaokang@hadoop03 ~]$ . /etc/profile.d/env.sh

redis-5.0.8集群环境搭建_第2张图片

5.修改配置文件

拷贝配置文件

[xiaokang@hadoop01 redis-5.0.8]$ mkdir -p 7001 7002
[xiaokang@hadoop01 redis-5.0.8]$ cp redis.conf 7001/redis_7001.conf

配置7001端口号启动Redis服务

[xiaokang@hadoop01 redis-5.0.8]$ cd 7001
[xiaokang@hadoop01 redis-5.0.8]$ vim redis_7001.conf
## 69行,配置redis服务器接受链接的网卡
bind 0.0.0.0

## 88行,关闭保护模式
protected-mode no

## 92行,设置端口号
port 7001

## 136行,redis后台运行
daemonize yes

## 158行,Redis服务进程PID存储文件名称
pidfile /var/run/redis_7001.pid

## 171行,设置redis服务日志存储路径
logfile "/opt/software/redis-5.0.8/7001/log/redis.log"

## 263行,设置redis持久化数据存储目录
dir /opt/software/redis-5.0.8/7001/data/

## 699行,启动AOF方式持久化
appendonly yes

## 832行,启动Redis Cluster
cluster-enabled yes

## 840行,Redis服务配置保存文件名称
cluster-config-file nodes-7001.conf

## 847行,超时时间
cluster-node-timeout 15000

创建日志目录和数据目录

[xiaokang@hadoop01]$ mkdir -p /opt/software/redis-5.0.8/7001/log
[xiaokang@hadoop01]$ mkdir -p /opt/software/redis-5.0.8/7001/data

配置7002端口号启动Redis服务

[xiaokang@hadoop01 redis-5.0.8]$ cp 7001/redis_7001.conf 7002/redis_7002.conf
# 修改配置文件:redis_7002.conf
[xiaokang@hadoop01 redis-5.0.8]$ vim 7002/redis_7002.conf
#执行以下代码将7001全部替换成7002
:%s/7001/7002/g   # 表示:%s/old/new/g  g表示全部替换

# 创建目录
[xiaokang@hadoop01 redis-5.0.8]$ mkdir -p 7002/log
[xiaokang@hadoop01 redis-5.0.8]$ mkdir -p 7002/data 

6.分发安装包

[xiaokang@hadoop01 ~]$ scp -r /opt/software/redis-5.0.8 xiaokang@hadoop02:/opt/software/
[xiaokang@hadoop01 ~]$ scp -r /opt/software/redis-5.0.8 xiaokang@hadoop03:/opt/software/

7.启动redis服务

[xiaokang@hadoop01 ~]$ redis-server /opt/software/redis-5.0.8/7001/redis_7001.conf
[xiaokang@hadoop01 ~]$ redis-server /opt/software/redis-5.0.8/7002/redis_7002.conf
[xiaokang@hadoop01 ~]$ ps aux | grep redis

在这里插入图片描述

[xiaokang@hadoop02 ~]$ redis-server /opt/software/redis-5.0.8/7001/redis_7001.conf
[xiaokang@hadoop02 ~]$ redis-server /opt/software/redis-5.0.8/7002/redis_7002.conf
[xiaokang@hadoop02 ~]$ ps aux | grep redis

在这里插入图片描述

[xiaokang@hadoop03 ~]$ redis-server /opt/software/redis-5.0.8/7001/redis_7001.conf
[xiaokang@hadoop03 ~]$ redis-server /opt/software/redis-5.0.8/7002/redis_7002.conf
[xiaokang@hadoop03 ~]$ ps aux | grep redis

在这里插入图片描述

8.启动集群

任意选择一台机器执行如下命令,创建集群

[xiaokang@hadoop01 ~]$ /opt/software/redis-5.0.8/bin/redis-cli --cluster create 192.168.50.151:7001 192.168.50.152:7002 192.168.50.153:7001 192.168.50.151:7002 192.168.50.152:7001 192.168.50.153:7002 --cluster-replicas 1
# 是否同意上述划分,一般都是yes
Can I set the above configuration? (type 'yes' to accept): yes

redis-5.0.8集群环境搭建_第3张图片
redis-5.0.8集群环境搭建_第4张图片

9.测试集群

[xiaokang@hadoop03 ~]$ redis-cli -c -p 7001
127.0.0.1:7001> cluster nodes

redis-5.0.8集群环境搭建_第5张图片

127.0.0.1:7001> info replication

redis-5.0.8集群环境搭建_第6张图片

127.0.0.1:7001> keys *
(empty list or set)
127.0.0.1:7001> set k1 v1
OK
127.0.0.1:7001> get k1
"v1"
127.0.0.1:7001> set k2 v2
-> Redirected to slot [449] located at 192.168.50.152:7001
OK
192.168.50.152:7001> set k3 v3
OK
192.168.50.152:7001> get k3
"v3"

关于集群的说明:

  • 因为redis集群一般推荐的是有几个主节点,就需要有几个从节点,我们搭建的是三主三从。
    hadoop01:7001、7002
    hadoop02:7001、7002
    hadoop03:7001、7002

  • 为了方便搭建集群,绑定的IP地址是0.0.0.0,监听本台电脑所有能够被访问到的IP

  • 配置完后,需要启动所有的端口redis进程,一共有六个
    启动好六个节点后,需要进行集群初始化

    • 集群初始化会自动分配Master和Slave
    • redis还会自动分配槽
  • 连接redis集群

    • redis-cli -c -h ip地址 -p 端口号
    • 当我们访问某个key的时候,如果这个key不在本机,redis集群会自动跳转到另外两台的机器

10.制作启动脚本

通过ssh 在每个节点上执行执行启动命令、或者是shutdown命令
就是正常启动每一个redis集群中的节点就可以了,因为集群已经初始化了,每个节点都已经有固定的槽位

[xiaokang@hadoop01 bin]$ touch redis-cluster-start.sh
[xiaokang@hadoop01 bin]$ touch redis-cluster-stop.sh
[xiaokang@hadoop01 bin]$ chmod u+x redis-cluster-start.sh 
[xiaokang@hadoop01 bin]$ chmod u+x redis-cluster-stop.sh
[xiaokang@hadoop01 bin]$ vim redis-cluster-start.sh 
#!/bin/bash
REDIS_HOME=/opt/software/redis-5.0.8
# Start Server
## hadoop01
ssh hadoop01 "${REDIS_HOME}/bin/redis-server /opt/software/redis-5.0.8/7001/redis_7001.conf"
ssh hadoop01 "${REDIS_HOME}/bin/redis-server /opt/software/redis-5.0.8/7002/redis_7002.conf"
## hadoop02
ssh hadoop02 "${REDIS_HOME}/bin/redis-server /opt/software/redis-5.0.8/7001/redis_7001.conf"
ssh hadoop02 "${REDIS_HOME}/bin/redis-server /opt/software/redis-5.0.8/7002/redis_7002.conf"
## hadoop03
ssh hadoop03 "${REDIS_HOME}/bin/redis-server /opt/software/redis-5.0.8/7001/redis_7001.conf"
ssh hadoop03 "${REDIS_HOME}/bin/redis-server /opt/software/redis-5.0.8/7002/redis_7002.conf"

[xiaokang@hadoop01 bin]$ vim redis-cluster-stop.sh
#!/bin/bash
REDIS_HOME=/opt/software/redis-5.0.8
# Stop Server
## hadoop01
ssh hadoop01 "${REDIS_HOME}/bin/redis-cli -h hadoop01 -p 7001 SHUTDOWN"
ssh hadoop01 "${REDIS_HOME}/bin/redis-cli -h hadoop01 -p 7002 SHUTDOWN"
## hadoop02
ssh hadoop02 "${REDIS_HOME}/bin/redis-cli -h hadoop02 -p 7001 SHUTDOWN"
ssh hadoop02 "${REDIS_HOME}/bin/redis-cli -h hadoop02 -p 7002 SHUTDOWN"
## hadoop03
ssh hadoop03 "${REDIS_HOME}/bin/redis-cli -h hadoop03 -p 7001 SHUTDOWN"
ssh hadoop03 "${REDIS_HOME}/bin/redis-cli -h hadoop03 -p 7002 SHUTDOWN"

关闭全部的redis服务

[xiaokang@hadoop01 bin]$ ./redis-cluster-stop.sh
[xiaokang@hadoop01 bin]$ ps -ef | grep redis
xiaokang   9642   9387  0 21:21 pts/0    00:00:00 grep --color=auto redis
[xiaokang@hadoop02 ~]$ ps -ef | grep redis
xiaokang   9512   9385  0 21:22 pts/0    00:00:00 grep --color=auto redis
[xiaokang@hadoop03 ~]$ ps -ef | grep redis
xiaokang   9518   9416  0 21:22 pts/0    00:00:00 grep --color=auto redis

开启全部的redis服务

[xiaokang@hadoop01 bin]$ ./redis-cluster-start.sh 
[xiaokang@hadoop01 bin]$ ps -ef | grep redis
xiaokang   9663      1  0 21:24 ?        00:00:00 /opt/software/redis-5.0.8/bin/redis-server 0.0.0.0:7001 [cluster]
xiaokang   9681      1  0 21:24 ?        00:00:00 /opt/software/redis-5.0.8/bin/redis-server 0.0.0.0:7002 [cluster]
xiaokang   9692   9387  0 21:24 pts/0    00:00:00 grep --color=auto redis
[xiaokang@hadoop02 ~]$ ps -ef | grep redis
xiaokang   9526      1  0 21:24 ?        00:00:00 /opt/software/redis-5.0.8/bin/redis-server 0.0.0.0:7001 [cluster]
xiaokang   9543      1  0 21:24 ?        00:00:00 /opt/software/redis-5.0.8/bin/redis-server 0.0.0.0:7002 [cluster]
xiaokang   9550   9385  0 21:24 pts/0    00:00:00 grep --color=auto redis
[xiaokang@hadoop03 ~]$ ps -ef | grep redis
xiaokang   9533      1  0 21:24 ?        00:00:00 /opt/software/redis-5.0.8/bin/redis-server 0.0.0.0:7001 [cluster]
xiaokang   9550      1  0 21:24 ?        00:00:00 /opt/software/redis-5.0.8/bin/redis-server 0.0.0.0:7002 [cluster]
xiaokang   9557   9416  0 21:24 pts/0    00:00:00 grep --color=auto redis

11.测试主从切换

当我们搭建了redis集群后,不再需要哨兵了,redis集群会自动完成主从切换
查看集群节点信息

[xiaokang@hadoop01]$ redis-cli -c -p 7001
127.0.0.1:7001> cluster nodes

如下图所示,hadoop02上的7001端口为master,hadoop01上的7001端口为slave,因为两个结点的结点码相同,所以这两个节点互为主从节点

在这里插入图片描述

redis-5.0.8集群环境搭建_第7张图片
关闭hadoop02上的master

[xiaokang@hadoop02 ~]$ redis-cli -h hadoop02 -p 7001 shutdown
[xiaokang@hadoop02 ~]$ ps -ef | grep redis
xiaokang   9543      1  0 21:24 ?        00:00:00 /opt/software/redis-5.0.8/bin/redis-server 0.0.0.0:7002 [cluster]
xiaokang   9560   9385  0 21:33 pts/0    00:00:00 grep --color=auto redis

再次查看集群状态信息,hadoop01上的7001端口切换成了master,实现了主从切换
redis-5.0.8集群环境搭建_第8张图片

你可能感兴趣的:(redis,redis)