redis6.09 集群安装

352 阅读8分钟

1. 环境准备

redis采用redis6.0.9版本

1.1 环境配置

No主机名目录假定作用IPPort
1redis11/usr/local/redisbin目录192.168.8.112
2/usr/local/redis_cluster/7000/redis.conf集群端口7000
3/usr/local/redis_cluster/7001/redis.conf集群端口7001
4/usr/local/redis_cluster/7002/redis.conf集群端口7002
5redis12/usr/local/redisbin目录192.168.8.113
6/usr/local/redis_cluster/7003/redis.conf集群端口7003
7/usr/local/redis_cluster/7004/redis.conf集群端口7004
8/usr/local/redis_cluster/7005/redis.conf集群端口7005

2 环境安装

2.1 网络设置

[root@my1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
[root@my2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static" #dhcp改为static 
ONBOOT="yes" #开机启用本配置
IPADDR=192.168.8.112 #静态IP 192.168.8.113
GATEWAY=192.168.8.2 #默认网关
NETMASK=255.255.255.0 #子网掩码
DNS1=114.114.114.114 #DNS 配置
DNS2=8.8.8.8 #DNS 配置

※重启电脑

2.2 hostname

[root@base1 ~]# hostnamectl set-hostname redis11 --static
[root@base2 ~]# hostnamectl set-hostname redis12 --static

2.3 工具安装

yum install net-tools
yum install tree

3 Redis安装

3.1 redis依赖安装

yum -y install gcc automake autoconf libtool make

3.2 下载redis源文件,并解压

yum install wget
wget http://download.redis.io/releases/redis-6.0.9.tar.gz
tar xvzf redis-6.0.9.tar.gz

3.3 安装redis

3.3.1 确认gcc版本是否在5.3以上(升级方法如下)

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils

# 需要注意的是scl命令启用只是临时的,退出shell或重新打开一个shell就会恢复原系统gcc版本
scl enable devtoolset-9 bash
#执行以下命令永久使用
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
gcc -v

3.3.2 编译安装

cd redis-6.0.9
make MALLOC=libc
make PREFIX=/usr/local/redis install

3.4 创建 Redis 节点

首先在 redis1 机器上 创建 redis_cluster 目录

mkdir /usr/local/redis_cluster
mkdir /usr/local/redis_cluster/7000 /usr/local/redis_cluster/7001 /usr/local/redis_cluster/7002

cp redis.conf /usr/local/redis_cluster/7000 
cp redis.conf /usr/local/redis_cluster/7001 
cp redis.conf /usr/local/redis_cluster/7002

vi /usr/local/redis_cluster/7000/redis.conf
port 7000 //端口7000,7001,7002 
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群 
daemonize yes //redis后台运行 
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002 
cluster-enabled yes //开启集群 把注释#去掉 
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002 把注释#去掉 
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置  把注释#去掉 
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志

其次在 redis2 机器上 创建 redis_cluster 目录

mkdir /usr/local/redis_cluster
mkdir /usr/local/redis_cluster/7003 /usr/local/redis_cluster/7004 /usr/local/redis_cluster/7005

cp redis.conf /usr/local/redis_cluster/7003 
cp redis.conf /usr/local/redis_cluster/7004 
cp redis.conf /usr/local/redis_cluster/7005

vi /usr/local/redis_cluster/7000/redis.conf
port 7003 //端口7003,7004,7005 
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群 
daemonize yes //redis后台运行 
pidfile /var/run/redis_7003.pid //pidfile文件对应7003,7004,7005 
cluster-enabled yes //开启集群 把注释#去掉 
cluster-config-file nodes_7003.conf //集群的配置 配置文件首次启动自动生成 7003,7004,7005 把注释#去掉 
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置  把注释#去掉 
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志

3.5 启动各个节点

redis1

cd /usr/local/redis
./bin/redis-server ../redis_cluster/7000/redis.conf 
./bin/redis-server ../redis_cluster/7001/redis.conf 
./bin/redis-server ../redis_cluster/7002/redis.conf

redis2

cd /usr/local/redis
./bin/redis-server ../redis_cluster/7003/redis.conf 
./bin/redis-server ../redis_cluster/7004/redis.conf 
./bin/redis-server ../redis_cluster/7005/redis.conf 

3.6 检查 redis 启动情况

查看redis端口开启情况

redis1

ps -ef | grep redis
netstat -tnlp | grep redis
tcp        0      0 192.168.8.112:7000      0.0.0.0:*               LISTEN      9225/./bin/redis-se
tcp        0      0 192.168.8.112:7001      0.0.0.0:*               LISTEN      9231/./bin/redis-se
tcp        0      0 192.168.8.112:7002      0.0.0.0:*               LISTEN      9236/./bin/redis-se
tcp        0      0 192.168.8.112:17000     0.0.0.0:*               LISTEN      9225/./bin/redis-se
tcp        0      0 192.168.8.112:17001     0.0.0.0:*               LISTEN      9231/./bin/redis-se
tcp        0      0 192.168.8.112:17002     0.0.0.0:*               LISTEN      9236/./bin/redis-se

redis2

ps -ef | grep redis
netstat -tnlp | grep redis
tcp        0      0 192.168.8.113:17003     0.0.0.0:*               LISTEN      9085/./bin/redis-se
tcp        0      0 192.168.8.113:17004     0.0.0.0:*               LISTEN      9090/./bin/redis-se
tcp        0      0 192.168.8.113:17005     0.0.0.0:*               LISTEN      9095/./bin/redis-se
tcp        0      0 192.168.8.113:7003      0.0.0.0:*               LISTEN      9085/./bin/redis-se
tcp        0      0 192.168.8.113:7004      0.0.0.0:*               LISTEN      9090/./bin/redis-se
tcp        0      0 192.168.8.113:7005      0.0.0.0:*               LISTEN      9095/./bin/redis-se

3.7 开启两台机器的防火墙

redis1

firewall-cmd --zone=public --add-port=7000-7002/tcp --permanent
firewall-cmd --zone=public --add-port=17000-17002/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports

redis2

firewall-cmd --zone=public --add-port=7003-7005/tcp --permanent
firewall-cmd --zone=public --add-port=17003-17005/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports

4 集群

4.1 创建集群

redis1

./bin/redis-cli --cluster create --cluster-replicas 1 192.168.8.112:7000 192.168.8.112:7001 192.168.8.112:7002 192.168.8.113:7003 192.168.8.113:7004 192.168.8.113:7005
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.8.113:7005 to 192.168.8.112:7000
Adding replica 192.168.8.112:7002 to 192.168.8.113:7003
Adding replica 192.168.8.113:7004 to 192.168.8.112:7001
M: 31df57911514085513af937b5ddb1d38e8a3cb37 192.168.8.112:7000
   slots:[0-5460] (5461 slots) master
M: 5967ecf1c8146482d5a10e165116dce5615b8b6a 192.168.8.112:7001
   slots:[10923-16383] (5461 slots) master
S: a1f9700771f0fe3e132faddae46f88a6342be32e 192.168.8.112:7002
   replicates 586d5f6703140ea899fedc3cef0188e8c6456223
M: 586d5f6703140ea899fedc3cef0188e8c6456223 192.168.8.113:7003
   slots:[5461-10922] (5462 slots) master
S: 230238b8ee43bfa13e838c8e584b652d41e5681c 192.168.8.113:7004
   replicates 5967ecf1c8146482d5a10e165116dce5615b8b6a
S: b0f9d8764b1e9da01928c93cdcf83f62b19bdbde 192.168.8.113:7005
   replicates 31df57911514085513af937b5ddb1d38e8a3cb37

输入 yes 即可,然后出现如下内容,说明安装成功

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 192.168.8.112:7000)
M: 31df57911514085513af937b5ddb1d38e8a3cb37 192.168.8.112:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 586d5f6703140ea899fedc3cef0188e8c6456223 192.168.8.113:7003
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: b0f9d8764b1e9da01928c93cdcf83f62b19bdbde 192.168.8.113:7005
   slots: (0 slots) slave
   replicates 31df57911514085513af937b5ddb1d38e8a3cb37
S: a1f9700771f0fe3e132faddae46f88a6342be32e 192.168.8.112:7002
   slots: (0 slots) slave
   replicates 586d5f6703140ea899fedc3cef0188e8c6456223
S: 230238b8ee43bfa13e838c8e584b652d41e5681c 192.168.8.113:7004
   slots: (0 slots) slave
   replicates 5967ecf1c8146482d5a10e165116dce5615b8b6a
M: 5967ecf1c8146482d5a10e165116dce5615b8b6a 192.168.8.112:7001
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群创建后,主从分配如下表:

主从IPPort
Master192.168.8.1127000
Master192.168.8.1127001
Master192.168.8.1137003
Slave192.168.8.1127002
Slave192.168.8.1137004
Slave192.168.8.1137005

4.2 集群验证

在第一台机器上连接集群的7000端口的节点,在另外一台连接7003节点,连接方式为 redis1

./bin/redis-cli -h 192.168.8.112 -c -p 7000
192.168.8.112:7000> set hello world
OK

然后在另外一台7005端口,查看 key 为 hello 的内容, get hello ,执行结果如下:

redis2

./bin/redis-cli -h 192.168.8.113 -c -p 7005
192.168.8.113:7005> get hello
-> Redirected to slot [866] located at 192.168.8.112:7000
"world"

4.3 bin目录介绍

├── bin
│   ├── redis-benchmark                         #Redis的压力测试工具
│   ├── redis-check-aof							#检查AOF日志文件
│   ├── redis-check-rdb							#检查RDB日志文件
│   ├── redis-cli								#Redis的客户端脚本
│   ├── redis-sentinel -> redis-server			#Redis的哨兵
│   └── redis-server							#Redis的服务器脚本

5 脚本化

5.1 redis1

start-all-redis.sh

#!/bin/bash
cd /usr/local/redis
./bin/redis-server ../redis_cluster/7000/redis.conf 
./bin/redis-server ../redis_cluster/7001/redis.conf 
./bin/redis-server ../redis_cluster/7002/redis.conf

5.2 redis2

start-all-redis.sh

#!/bin/bash
cd /usr/local/redis
./bin/redis-server ../redis_cluster/7003/redis.conf 
./bin/redis-server ../redis_cluster/7004/redis.conf 
./bin/redis-server ../redis_cluster/7005/redis.conf

5.3 创建集群

create-cluster-redis.sh

#!/bin/bash
cd /usr/local/redis
./bin/redis-cli  --cluster create --cluster-replicas 1 192.168.8.112:7000 192.168.8.112:7001 192.168.8.112:7002 192.168.8.113:7003 192.168.8.113:7004 192.168.8.113:7005

参考:redis5.09 集群安装