环境准备:Ubuntu 18.04TLS 三台

192.168.0.16

192.168.0.17

192.168.0.18

懒人自动安装版

该版本为阿紫把所有安装步骤写到一个脚本里了,你只要把密码123456改成你自己的就行了。

在三台服务器上各执行一次就可以了,脚本会自动启动两个redis节点
vim redis_install.sh

#!/bin/bash
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis -y

/etc/init.d/redis-server stop

sudo mkdir -p /data/redis/7001 /data/redis/7002

sudo mkdir -p /etc/redis/7001
sudo tee /etc/redis/7001/redis.conf <<-'EOF'
port 7001
# 不后台启动,使用systemctl启动
daemonize no
# 数据文件存放路径
dir /data/redis/7001
# 进程号
pidfile /run/redis/7001/redis-server.pid
# 开启集群
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 10000
# 关闭保护模式 其他ip可访问redis
protected-mode  no
# 开启aof持久化
appendonly yes
# 设置redis密码
requirepass 123456
# 设计集群节点访问密码 与上面一致
masterauth 123456
EOF

sudo mkdir -p /etc/redis/7002
sudo tee /etc/redis/7002/redis.conf <<-'EOF'
port 7002
# 不后台启动,使用systemctl启动
daemonize no
# 数据文件存放路径
dir /data/redis/7002
# 进程号
pidfile /run/redis/7002/redis-server.pid
# 开启集群
cluster-enabled yes
cluster-config-file nodes-7002.conf
cluster-node-timeout 10000
# 关闭保护模式 其他ip可访问redis
protected-mode  no
# 开启aof持久化
appendonly yes
# 设置redis密码
requirepass 123456
# 设计集群节点访问密码 与上面一致
masterauth 123456
EOF


sudo tee /etc/systemd/system/redis7001.service <<-'EOF'
[Unit]
Description=Redis
After=syslog.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/7001/redis.conf
RestartSec=5s
Restart=on-success

[Install]
WantedBy=multi-user.target
EOF

sudo tee /etc/systemd/system/redis7002.service <<-'EOF'
[Unit]
Description=Redis
After=syslog.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/7002/redis.conf
RestartSec=5s
Restart=on-success

[Install]
WantedBy=multi-user.target
EOF


systemctl enable /etc/systemd/system/redis7001.service

systemctl enable /etc/systemd/system/redis7002.service

systemctl start redis7001

systemctl start redis7002

查看是否启动

ps aux | grep redis

创建集群

redis-cli -a '123456' --cluster create 192.168.0.8:7001 192.168.0.8:7002 192.168.0.9:7001 192.168.0.9:7002 192.168.0.10:7001 192.168.0.10:7002 --cluster-replicas 1

安装Redis(手动安装| apt-get版)

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis -y

安装完之后默认启动redis

停止方式

/etc/init.d/redis-server stop

修改配置

/etc/redis/7001下新建配置文件

port 7001
# 不后台启动,使用systemctl启动
daemonize no
# 数据文件存放路径
dir /var/lib/redis/7001
# 进程号
pidfile /run/redis/7001/redis-server.pid
# 开启集群
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 10000
# 关闭保护模式 其他ip可访问redis
protected-mode  no
# 开启aof持久化
appendonly yes
# 设置redis密码
requirepass 123456
# 设计集群节点访问密码 与上面一致
masterauth 123456

其他5个节点类似

开启自启动

vim /etc/systemd/system/redis7001.service
[Unit]
Description=Redis
After=syslog.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/7001/redis.conf
RestartSec=5s
Restart=on-success

[Install]
WantedBy=multi-user.target
vim /etc/systemd/system/redis7002.service
[Unit]
Description=Redis
After=syslog.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/7002/redis.conf
RestartSec=5s
Restart=on-success

[Install]
WantedBy=multi-user.target
systemctl enable /etc/systemd/system/redis7001.service
systemctl enable /etc/systemd/system/redis7002.service
systemctl start redis7001
systemctl start redis7002

创建集群

redis-cli -a '123456' --cluster create 192.168.0.8:7001 192.168.0.8:7002 192.168.0.9:7001 192.168.0.9:7002 192.168.0.10:7001 192.168.0.10:7002 --cluster-replicas 1

安装Redis(手动安装| 压缩包版)

#更新依赖
sudo apt update
#安装 gcc
apt install gcc
#下载redis
wget https://download.redis.io/releases/redis-5.0.12.tar.gz?_ga=2.107670914.1067330296.1625709191-1855796397.1616491081
#重命名
mv redis-5.0.12.tar.gz?_ga=2.107670914.1067330296.1625709191-1855796397.1616491081 redis-5.0.12.tar.gz
#解压
tar -zxf redis.tar.gz
cd redis-5.0.12/
#编译
make

若make时出现 fatal error: jemalloc/jemalloc.h: No such file or directory 错误,这是由于redis默认MALLOC=jemalloc 修改为 make MALLOC=libc

CentOS参照:Redis的安装使用

将启动命令链接到用户脚本目录下

ln -s  /opt/redis/redis-5.0.12/src/redis-cli /usr/bin/redis-cli
ln -s  /opt/redis/redis-5.0.12/src/redis-server /usr/bin/redis-server

修改配置文件

创建节点目录

mkdir -p /opt/redis/6379 /opt/redis/6380

将默认配置文件拷贝到各节点目录下

cp /etc/redis/redis.conf /opt/redis/6379/
cp /etc/redis/redis.conf /opt/redis/6380/

修改redis.conf配置文件

port 6378
# 后台启动
daemonize yes
# 数据文件存放路径
dir /opt/redis/6379
# 进程号
pidfile /var/run/redis/redis-server-6379.pid
# 开启集群
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 10000
# 注释bind配置 其他ip可访问redis
# bind 127.0.0.1
# 关闭保护模式 其他ip可访问redis
protected-mode  no 
# 开启aof持久化
appendonly yes
# 设置redis密码
requirepass 123456
# 设计集群节点访问密码 与上面一致
masterauth 123456

6380的节点将有端口号的地方替换掉就行了

使用原生命令启动

redis-server /opt/redis/6379/redis.conf
redis-server /opt/redis/6380/redis.conf

/opt/redis/redis-5.0.12/src/redis-server /opt/redis/6379/redis.conf
/opt/redis/redis-5.0.12/src/redis-server /opt/redis/6380/redis.conf

查看是否启动成功

 ps aux | grep redis

其他两台服务器相同

配置集群

redis-cli -a 123456 --cluster create 192.168.0.16:6379 192.168.0.16:6380 192.168.0.17:6379 192.168.0.17:6380 192.168.0.18:6379 192.168.0.18:6380 --cluster-replicas 1

连接集群

redis-cli -c -a 123456

-c 集群模式

-h ip地址

-p 端口号

其他命令

redis-cli --help

查看子命令

redis-cli --cluster help