资源规划
组件 | bigdata-node1 | bigdata-node2 | bigdata-node3 |
---|---|---|---|
OS | centos7.6 | centos7.6 | centos7.6 |
Redis | Redis:6001、Redis:6002 | Redis:6003、Redis:6004 | Redis:6005、Redis:6006 |
安装介质
版本:redis-5.0.8.tar.gz
下载:https://redis.io/download
环境准备
检查并确认已经安装gcc-4.8.5+版本。(CentOS7.6自带gcc-4.8.5)
sudo yum install gcc
编译安装
# root用户
sudo su
tar -zxvf /share/redis-5.0.8.tar.gz -C /usr/local/
rm -rf /share/redis-5.0.8.tar.gz
mv /usr/local/redis-5.0.8 /usr/local/redis
cd /usr/local/redis
make PREFIX=/usr/local/redis MALLOC=libc install
单机模式
启动Redis服务
前台启动模式
在前台直接运行,若Linux关闭,则Redis服务也随即关闭。Redis默认开启的是前端模式,端口:6379。
# 方式1
cd /usr/local/redis/bin
./redis-server
# 方式2
cd /usr/local/redis/src
./redis-server
后台启动模式
编译配置文件(编辑conf文件,将daemonize属性修改为yes,表明需要在后台运行):
mkdir /usr/local/redis/conf
vi /usr/local/redis/conf/redis.conf
配置如下:
daemonize yes
后台启动:
# 方式1
cd /usr/local/redis
./bin/redis-server conf/redis.conf
# 方式2
cd /usr/local/redis
./src/redis-server conf/redis.conf
关闭Redis服务
# 方式1
/usr/local/redis/bin/redis-cli shutdown
# 方式2
/usr/local/redis/src/redis-cli shutdown
# 强行终止(强行终止redis进程可能会导致redis持久化数据丢失。正确停止Redis的方式应该是向Redis发送SHUTDOWN命令)
# 方式3
pkill redis-server
验证
# 方式1
cd /usr/local/redis
./bin/redis-cli
# 方式2
cd /usr/local/redis
./src/redis-cli
# redis操作
redis> set foo bar
redis> get foo
# 查看Redis进程
ps -ef | grep redis
netstat -lntp | grep 6379
netstat -nlt
设置开机自启动
# root用户
vi /etc/rc.local
配置如下:
# 方式1
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
# 方式2
/usr/local/redis/src/redis-server /usr/local/redis/conf/redis.conf
远程端口配置
开启Redis远程端口配置后,代码访问Linux下的Redis需要开启6379端口。
修改Redis配置:
vi /usr/local/redis/conf/redis.conf
配置如下:
# 0.0.0.0表示不限制访问来源地址,如需限制,直接改为相应IP
bind 0.0.0.0
防火墙设置(开启防火墙6379端口,在CentOS 7中防火墙由firewalld来管理,启用区域端口和协议组合):
# 若防火墙是关闭状态,则无需如下配置
# 1.启用(permanent永久生效,没有此参数重启后失效),成功执行返回“success”
firewall-cmd --zone=public --add-port=6379/tcp --permanent
# 2.重新载入,成功执行返回“success”
firewall-cmd --reload
# 3.查看端口是否开启,成功执行返回“yes”
firewall-cmd --zone=public --query-port=6379/tcp
# 补充:删除
firewall-cmd --zone=public --remove-port=6379/tcp --permanent
伪分布式
方式1(redis-cli)
1. 创建空目录
mkdir -p /usr/local/redis_cluster
cd /usr/local/redis_cluster
mkdir 6001 6002 6003 6004 6005 6006
2. 初始化配置
cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6001
cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6001
cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6002
cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6002
cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6003
cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6003
cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6004
cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6004
cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6005
cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6005
cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6006
cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6006
3. 修改配置
vi /usr/local/redis_cluster/6001/redis.conf
vi /usr/local/redis_cluster/6002/redis.conf
vi /usr/local/redis_cluster/6003/redis.conf
vi /usr/local/redis_cluster/6004/redis.conf
vi /usr/local/redis_cluster/6005/redis.conf
vi /usr/local/redis_cluster/6006/redis.conf
配置如下:
# 设置端口号
port 6001
# 数据存放位置,必须指定
dir /usr/local/redis_cluster/6001/
# 启动集群模式
cluster-enabled yes
# 集群节点信息文件
cluster-config-file nodes-6001.conf
cluster-node-timeout 5000
bind 0.0.0.0
# 关闭保护模式
protected-mode no
# 开启AOF模式,默认的持久化RDB
appendonly yes
daemonize yes
requirepass hadoopredis
4. 启动服务
/usr/local/redis_cluster/6001/redis-server /usr/local/redis_cluster/6001/redis.conf
/usr/local/redis_cluster/6002/redis-server /usr/local/redis_cluster/6002/redis.conf
/usr/local/redis_cluster/6003/redis-server /usr/local/redis_cluster/6003/redis.conf
/usr/local/redis_cluster/6004/redis-server /usr/local/redis_cluster/6004/redis.conf
/usr/local/redis_cluster/6005/redis-server /usr/local/redis_cluster/6005/redis.conf
/usr/local/redis_cluster/6006/redis-server /usr/local/redis_cluster/6006/redis.conf
# 验证服务是否启动
ps -ef |grep redis
5. 创建伪集群
cd /usr/local/redis
./bin/redis-cli -a hadoopredis --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1
参数说明:
- -a:密码 如果redis.conf中配置了密码创建集群时需要添加
—cluster-replicas:从节点个数(需写在末尾,否则报错)
方式2(create-cluster)
redis-5.x版本,集群搭建不需要我们安装ruby就可以搭建成功,并且redis给我们提供了快速搭建脚本,执行utils/create-cluster目录下的create-cluster脚本 就可以快速搭建,该脚本提供以下几个命令供我们使用。
start:创建redis集群实例。
- create:搭建redis集群。
- stop:停止redis集群实例。
- watch:显示集群节点(第一个节点)输出(前30行)。
- tail
:运行tail -f,基于port + ID。 - clean:删除生成的日志,配置,数据文件。
-
1. 修改脚本
vi /usr/local/redis/utils/create-cluster/create-cluster
start部分修改
—bind:指定绑定的ip(服务器ip)(—bind 192.168.0.101)
- —requirepass:指定密码(—requirepass hadoopredis)
—protected-mode no:关闭安全模式(外部应用可以连接到redis)(—protected-mode no)
if [ "$1" == "start" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Starting $PORT"
../../src/redis-server --port $PORT --bind 192.168.0.101 --requirepass hadoopredis --protected-mode no --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes
done
exit 0
fi
create部分修改
HOSTS :指定绑定的ip(服务器ip)(**HOSTS=”$HOSTS 192.168.0.101:$PORT”**)
-a:指定密码 **(-a hadoopredis)**
if [ "$1" == "create" ]
then
HOSTS=""
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
HOSTS="$HOSTS 192.168.0.101:$PORT"
done
../../src/redis-cli -a hadoopredis --cluster create $HOSTS --cluster-replicas $REPLICAS
exit 0
fi
stop部分修改
-h:指定绑定的ip(服务器ip)(-h 192.168.0.101)
-a:指定密码(-a hadoopredis)
if [ "$1" == "stop" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Stopping $PORT"
../../src/redis-cli -h 192.168.0.101 -p $PORT -a hadoopredis shutdown nosave
done
exit 0
fi
2. 启动服务
cd /usr/local/redis/utils/create-cluster
./create-cluster start
3. 创建集群
cd /usr/local/redis/utils/create-cluster
./create-cluster create
关闭Redis服务
# 方式1
/usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6001 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6002 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6003 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6004 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6005 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6006 shutdown
# 方式2
/usr/local/redis/src/redis-cli -a hadoopredis -c -p 6001 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -p 6002 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -p 6003 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -p 6004 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -p 6005 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -p 6006 shutdown
# 方式3
pkill redis-server
# 方式4(仅适用于create-cluster方式创建)
cd /usr/local/redis/utils/create-cluster
./create-cluster stop
验证
cd /usr/local/redis
# 注意IP要和配置中保持一致
./bin/redis-cli -a hadoopredis -h 127.0.0.1 -p 6001 -c
./bin/redis-cli -a hadoopredis -h 192.168.0.101 -p 30001 -c
# 查看集群状态
redis>> cluster info
# 查看主从关系
redis>> cluster nodes
集群模式
官方推荐集群至少需要六个节点,即三主三从。六个节点的配置文件基本相同,只需要修改端口号。由于Redis cluster是redis官方提供的,目前官方一直都在维护中,具有代表性,建议生产使用。
1. 创建空目录
mkdir -p /usr/local/redis_cluster
cd /usr/local/redis_cluster
# bigdata-node1
mkdir 6001 6002
# bigdata-node2
mkdir 6003 6004
# bigdata-node3
mkdir 6005 6006
2. 初始化配置
mkdir /usr/local/redis/conf
vi /usr/local/redis/conf/redis.conf
配置如下:
daemonize yes
拷贝及分发配置:
# bigdata-node1
cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6001
cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6001
cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6002
cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6002
# bigdata-node2
scp -r /usr/local/redis/bin/redis-server root@bigdata-node2:/usr/local/redis_cluster/6003/
scp -r /usr/local/redis/conf/redis.conf root@bigdata-node2:/usr/local/redis_cluster/6003/
scp -r /usr/local/redis/bin/redis-server root@bigdata-node2:/usr/local/redis_cluster/6004/
scp -r /usr/local/redis/conf/redis.conf root@bigdata-node2:/usr/local/redis_cluster/6004/
# bigdata-node3
scp -r /usr/local/redis/bin/redis-server root@bigdata-node3:/usr/local/redis_cluster/6005/
scp -r /usr/local/redis/conf/redis.conf root@bigdata-node3:/usr/local/redis_cluster/6005/
scp -r /usr/local/redis/bin/redis-server root@bigdata-node3:/usr/local/redis_cluster/6006/
scp -r /usr/local/redis/conf/redis.conf root@bigdata-node3:/usr/local/redis_cluster/6006/
3. 修改配置
# bigdata-node1
vi /usr/local/redis_cluster/6001/redis.conf
vi /usr/local/redis_cluster/6002/redis.conf
# bigdata-node2
vi /usr/local/redis_cluster/6003/redis.conf
vi /usr/local/redis_cluster/6004/redis.conf
# bigdata-node3
vi /usr/local/redis_cluster/6005/redis.conf
vi /usr/local/redis_cluster/6006/redis.conf
配置如下:
# 设置端口号
port 6000
# 数据存放位置,必须指定
dir /usr/local/redis_cluster/6000/
# 启动集群模式
cluster-enabled yes
# 集群节点信息文件
cluster-config-file nodes-6000.conf
cluster-node-timeout 5000
# 默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
bind 192.168.0.101
# 关闭保护模式
protected-mode no
# 开启AOF模式(它会每次写操作都记录一条日志 ),默认的持久化RDB
appendonly yes
# redis后台运行
daemonize yes
# 设置密码
requirepass hadoopredis
# pidfile文件
pidfile /var/run/redis_6000.pid
6001(# bigdata-node1)
port 6001
dir /usr/local/redis_cluster/6001/
cluster-enabled yes
cluster-config-file nodes-6001.conf
cluster-node-timeout 5000
bind 192.168.0.101
protected-mode no
appendonly yes
daemonize yes
requirepass hadoopredis
pidfile /var/run/redis-6001.pid
6002(# bigdata-node1)
port 6002
dir /usr/local/redis_cluster/6002/
cluster-enabled yes
cluster-config-file nodes-6002.conf
cluster-node-timeout 5000
bind 192.168.0.101
protected-mode no
appendonly yes
daemonize yes
requirepass hadoopredis
pidfile /var/run/redis-6002.pid
6003(# bigdata-node2)
port 6003
dir /usr/local/redis_cluster/6003/
cluster-enabled yes
cluster-config-file nodes-6003.conf
cluster-node-timeout 5000
bind 192.168.0.102
protected-mode no
appendonly yes
daemonize yes
requirepass hadoopredis
pidfile /var/run/redis-6003.pid
6004(# bigdata-node2)
port 6004
dir /usr/local/redis_cluster/6004/
cluster-enabled yes
cluster-config-file nodes-6004.conf
cluster-node-timeout 5000
bind 192.168.0.102
protected-mode no
appendonly yes
daemonize yes
requirepass hadoopredis
pidfile /var/run/redis-6004.pid
6005(# bigdata-node3)
port 6005
dir /usr/local/redis_cluster/6005/
cluster-enabled yes
cluster-config-file nodes-6005.conf
cluster-node-timeout 5000
bind 192.168.0.103
protected-mode no
appendonly yes
daemonize yes
requirepass hadoopredis
pidfile /var/run/redis-6005.pid
6006(# bigdata-node3)
port 6006
dir /usr/local/redis_cluster/6006/
cluster-enabled yes
cluster-config-file nodes-6006.conf
cluster-node-timeout 5000
bind 192.168.0.103
protected-mode no
appendonly yes
daemonize yes
requirepass hadoopredis
pidfile /var/run/redis-6006.pid
4. 启动服务
# bigdata-node1
/usr/local/redis_cluster/6001/redis-server /usr/local/redis_cluster/6001/redis.conf
/usr/local/redis_cluster/6002/redis-server /usr/local/redis_cluster/6002/redis.conf
# bigdata-node2
/usr/local/redis_cluster/6003/redis-server /usr/local/redis_cluster/6003/redis.conf
/usr/local/redis_cluster/6004/redis-server /usr/local/redis_cluster/6004/redis.conf
# bigdata-node3
/usr/local/redis_cluster/6005/redis-server /usr/local/redis_cluster/6005/redis.conf
/usr/local/redis_cluster/6006/redis-server /usr/local/redis_cluster/6006/redis.conf
# 验证服务是否启动
ps -ef |grep redis
netstat -tnlp | grep redis
5. 创建集群
cd /usr/local/redis
./bin/redis-cli -a hadoopredis --cluster create 192.168.0.101:6001 192.168.0.101:6002 192.168.0.102:6003 192.168.0.102:6004 192.168.0.103:6005 192.168.0.103:6006 --cluster-replicas 1
6. 验证
cd /usr/local/redis
./bin/redis-cli -a hadoopredis -h 192.168.0.101 -p 6001 -c
# 查看集群状态
redis>> cluster info
# 查看主从关系
redis>> cluster nodes
7. 关闭Redis服务
# 方式1
# bigdata-node1
/usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.101 -p 6001 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.101 -p 6002 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.102 -p 6003 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.102 -p 6004 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.103 -p 6005 shutdown
/usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.103 -p 6006 shutdown
# 方式2
/usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.101 -p 6001 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.101 -p 6002 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.102 -p 6003 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.102 -p 6004 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.103 -p 6005 shutdown
/usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.103 -p 6006 shutdown
# 方式3
pkill redis-server
哨兵模式
参考:(不推荐,生产环境推荐使用集群模式)
https://blog.csdn.net/lihongtai/article/details/82826809密码设置
临时密码
# 查看是否设置密码(若已设置密码则返回“(error) NOAUTH Authentication required.”)
config get requirepass
# 设置密码
config set requirepass hadoopredis
# 密码验证
auth hadoopredis
永久密码
vi /usr/local/redis/conf/redis.conf
配置如下:
requirepass hadoopredis
问题修复
问题1
错误信息:“[ERR] Node xxx.xxx.xxx.xxx:pppp is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.”
解决方案:
- 将需要新增的节点下aof、rdb等本地备份文件删除。
- 同时将新Node的集群配置文件删除,即:删除你redis.conf里面cluster-config-file所在的文件。
- 再次添加新节点如果还是报错,则登录新节点(./redis-cli –h xxx.xxx.xxx.xxx –p pppp -a password -c)对数据库进行清除(flushdb)。
参考
博文:redis5.0.4集群搭建
https://www.cnblogs.com/fousor/p/10598400.html
博文:linux环境redis5.0.8集群搭建
https://www.cnblogs.com/lyf-12/articles/12839623.html
博文:Centos7 安装redis集群哨兵模式
https://blog.csdn.net/lihongtai/article/details/82826809