环境准备
一个机器上面整两个节点(机器不太够),三主三备:
序号 |
IP |
hostname |
OS |
软件 |
1 |
172.31.31.170 |
node1 |
RockyLinux 9.3 |
Redis 6379(主) Redis 6380(备) |
2 |
172.31.31.171 |
node2 |
RockyLinux 9.3 |
Redis 6379(主) Redis 6380(备) |
3 |
172.31.31.172 |
node3 |
RockyLinux 9.3 |
Redis 6379(主) Redis 6380(备) |
登录各个节点分别执行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config && setenforce 0
# 关闭防火墙
systemctl disable firewalld --now
# 分别在1、2、3机器上执行
hostnamectl set-hostname node1/node2/node3
# 添加hosts解析
cat >> /etc/hosts <<-EOF
172.31.31.170 node1
172.31.31.171 node2
172.31.31.172 node3
EOF
# 新增redis用户
useradd redis
|
安装Redis程序
编译环境:
1
2
|
# 你可以直接安装这个,也可以自己安装gcc和make等工具
dnf groupinstall "Development Tools"
|
安装好了以后下载源码编译就可以了,Redis源码地址:GitHub - redis/redis.
1
2
3
4
5
6
|
##下载最新源码
wget https://github.com/redis/redis/archive/refs/tags/7.2.4.tar.gz
##解压Redis 源码
tar zxvf 7.2.4.tar.gz
##进入目录进行编译
cd redis-7.2.4 && make
|
配置Redis
配置systemd
每个节点执行:
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
31
32
33
34
35
36
37
38
39
|
cat > /usr/li/systemd/system/redis-6379.service<<-EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/bin/redis-server /etc/redis/redis_6379.conf --daemonize no --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
cat > /usr/lib/systemd/system/redis-6380.service<<-EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/bin/redis-server /etc/redis/redis_6380.conf --daemonize no --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
|
新建配置文件
每个节点执行:
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
|
cat > /etc/redis/redis_6379.conf <<-EOF
bind [机器IP,如:172.31.31.170]
port 6380
pidfile /var/run/redis_6379.pid
logfile /var/log/redis/redis_6379.log
dir /var/lib/redis/data/6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
appendonly yes
daemonize yes
EOF
cat > /etc/redis/redis_6379.conf <<-EOF
bind [机器IP,如:172.31.31.170]
port 6380
pidfile /var/run/redis_6380.pid
logfile /var/log/redis/redis_6380.log
dir /var/lib/redis/data/6380
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
appendonly yes
daemonize yes
EOF
|
文件夹及其权限
每个机器执行:
1
2
3
4
5
6
7
8
|
# 创建数据目录和日志目录
mkdir -p /var/lib/redis/data/6379
mkdir -p /var/lib/redis/data/6380
mkdir -p /var/log/redis
# 给与数据目录和日志目录权限
chown -R redis:redis /var/lib/redis
chown -R redis:redis /var/log/redis
|
启动redis
每个机器执行:
1
2
3
4
5
|
# 载入systemd配置文件
systemctl daemon-reload
# 启动程序
systemctl enable redis_6379 --now && systemctl enable redis_6380 --now
|
创建集群
1
2
3
4
5
|
# 创建
redis-cli --cluster create 172.31.31.170:6379 172.31.31.171:6379 172.31.31.172:6379 172.31.31.170:6380 172.31.31.171:6380 172.31.31.172:6380 --cluster-replicas 1
# 查看集群状态
redis-cli -c -h 172.31.31.170 -p 6379 cluster info
|