Redis集群部署

Redis集群模式

Redis有三种集群模式:

  1. 主从模式:提高可用性,解决了单节点故障
  2. 哨兵(需要Sentinel)模式:在主从复制的基础上实现了主从切换和故障转移
  3. Cluster模式:是官方提供的集群模式,使用 Sharding 技术,实现了高可用、读写分离、分布式存储。

下载 Redis source

安装

安装方式:

1、完全离线安装

安装关键依赖 gccmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 直接全部安装
rpm -ivh *.rpm
# 全部安装报错时,使用下面的安装找出错误
rpm -ivh kernel-headers-3.10.0-1160.el7.x86_64.rpm
rpm -ivh glibc-common-2.17-324.el7_9.x86_64.rpm
rpm -ivh glibc-headers-2.17-324.el7_9.x86_64.rpm
rpm -ivh glibc-2.17-324.el7_9.x86_64.rpm
rpm -ivh glibc-devel-2.17-324.el7_9.x86_64.rpm
rpm -ivh make-3.82-24.el7.x86_64.rpm
rpm -ivh binutils-2.27-44.base.el7.x86_64.rpm
rpm -ivh libgcc-4.8.5-44.el7.x86_64.rpm
rpm -ivh mpfr-3.1.1-4.el7.x86_64.rpm
rpm -ivh libmpc-1.0.1-3.el7.x86_64.rpm
rpm -ivh cpp-4.8.5-44.el7.x86_64.rpm
rpm -ivh gcc-4.8.5-44.el7.x86_64.rpm

编译 redis

1
2
3
4
5
6
7
8
# 解压
tar -zxvf redis-stable.tar.gz
cd redis-stable
make
#结束后
make test
# 安装到 /usr/local/bin
make install

下载依赖

下载依赖需要注意版本,依赖版本根据当前系统的版本号,特别是 glibc

编译所需要的依赖 gcc
make

下面是gcc依赖:

2、联网设备安装

  1. 配置阿里云镜像

  2. 安装依赖

    1
    yum install -y gcc make
  3. 安装 Redis

    1
    2
    3
    4
    5
    6
    7
    8
    9
    cd /opt
    wget https://download.redis.io/redis-stable.tar.gz
    # 解压
    tar -zxvf redis-stable.tar.gz
    cd redis-stable
    # 编译
    make
    #结束后
    make test

3、rpm安装(不推荐)

联网安装可能会安装旧的版本

1
2
3
4
5
6
7
#配置阿里云镜像
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 配置阿里云 epel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 安装redis
yum install -y epel-release
yum install -y redis

4、 snap 安装

1
2
3
4
5
yum install epel-release
yum install snapd
systemctl enable --now snapd.socket
ln -s /var/lib/snapd/snap /snap
snap install redis

配置

基础配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
bind * -::*                 
# 保护模式,不允许不认证连接
protected-mode yes

port 56379
tcp-backlog 511
# unixsocket /run/redis.sock
# unixsocketperm 700
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
# ack 验证client 是否存活
tcp-keepalive 300
# 守护进程运行
daemonize yes
pidfile /opt/redis56379/redis_56379.pid

# 日志级别 debug verbose notice warning
loglevel notice
logfile "/opt/redis56379/redis56379.log"

databases 16

always-show-logo no
# 启用进程标题
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}{port}"

dir /opt/redis56379/data/
# 密码
requirepass maxzhao

阿里云镜像

1
2
3
4
5
#配置阿里云镜像
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 配置阿里云 epel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache

本文地址: https://github.com/maxzhao-it/blog/post/848e11b6/