51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Redis集群安装部署和基础配置

概述

redis官网:https://redis.io/

安装环境

| 部署ip | 安装软件 | 开放端口 | |:--------------:|:-----------:|:---------------------:| | 192.168.199.10 | redis-6.0.9 | 6379/16379/6380/16380 | | 192.168.199.11 | redis-6.0.9 | 6379/16379/6380/16380 | | 192.168.199.12 | redis-6.0.9 | 6379/16379/6380/16380 |

安装部署

依赖环境安装
yum -y install gcc gcc-c++ kernel-devel make cpp binutils glibc glibc-kernheaders glibc-common glibc-devel tcl wget
yum -y install centos-release-scl && yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils && scl enable devtoolset-9 bash
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
source /etc/profile
redis安装
tar zxf redis-6.0.9.tar.gz && cd redis-6.0.9
make && make PREFIX=/data/redis install
<!--根据实际情况更改路径-->
mkdir -p /data/redis/etc
cp redis.conf /data/redis/etc/
cp -ra /data/redis/ /data/redis01
基础配置
vim /data/redis/etc/redis.conf

修改一下配置
======



redis以守护进程的方式运行
===============



no表示不以守护进程的方式运行(会占用一个终端)
========================



daemonize yes


客户端闲置多长时间后断开连接,默认为0关闭此功能
========================



timeout 300


设置redis日志级别,默认级别:notice
=======================



loglevel verbose


设置日志文件的输出方式,如果以守护进程的方式运行redis 默认:""
===================================



并且日志输出设置为stdout,那么日志信息就输出到/dev/null里面去了
=======================================



logfile stdout


设置密码授权
======



requirepass \<设置密码\>
====================



监听ip
====



bind 0.0.0.0


监听端口
====



port 6379


开启集群功能
======



cluster-enabled yes


vim /data/redis01/etc/redis.conf


修改一下配置
======



redis以守护进程的方式运行
===============



no表示不以守护进程的方式运行(会占用一个终端)
========================



daemonize yes


客户端闲置多长时间后断开连接,默认为0关闭此功能
========================



timeout 300


设置redis日志级别,默认级别:notice
=======================



loglevel verbose


设置日志文件的输出方式,如果以守护进程的方式运行redis 默认:""
===================================



并且日志输出设置为stdout,那么日志信息就输出到/dev/null里面去了
=======================================



logfile stdout


设置密码授权
======



requirepass \<设置密码\>
====================



监听ip
====



bind 0.0.0.0


监听端口
====



port 6380


开启集群功能
======


`cluster-enabled yes`

防火墙开放端口

三台机器都放行端口

firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --zone=public --add-port=16379/tcp --permanent
firewall-cmd --zone=public --add-port=6380/tcp --permanent
firewall-cmd --zone=public --add-port=16380/tcp --permanent
firewall-cmd --reload
setenforce 0
配置启停脚本
vim /data/start_redis.sh

#!/bin/bash
cd /data/redis/
./bin/redis-server  ./etc/redis.conf
cd /data/redis01/
./bin/redis-server  ./etc/redis.conf


vim /data/stop_redis.sh

`#!/bin/bash
ps -aux | grep redis | awk -F " " '{print $2}' | xargs kill -9`

redis服务启动
# 停止
   sh -x /data/stop_redis.sh
   # 启动
   sh -x /data/start_redis.sh
创建集群
# 任意一台服务器
/data/redis/bin/redis-cli --cluster create 192.168.199.10:6379 192.168.199.10:6380 192.168.199.11:6379 192.168.199.11:6380 192.168.199.12:6379 192.168.199.12:6380 --cluster-replicas 1
>>> 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.199.11:6380 to 192.168.199.10:6379
Adding replica 192.168.199.12:6380 to 192.168.199.11:6379
Adding replica 192.168.199.10:6380 to 192.168.199.12:6379
M: 15987d9aa5452baeb197d31d0b28cf10403e1fc4 192.168.199.10:6379
   slots:[0-5460] (5461 slots) master
S: 76d44a73239971ab902effd30e11a2b0c108658d 192.168.199.10:6380
   replicates 46b4e50abb2dc8e14467303a59ddfa7b9a294dfe
M: eb97c3a7ad1ae973515c65a981fe2c556315639d 192.168.199.11:6379
   slots:[5461-10922] (5462 slots) master
S: db11544f435938e6f506595f0a94e6248e7a3787 192.168.199.11:6380
   replicates 15987d9aa5452baeb197d31d0b28cf10403e1fc4
M: 46b4e50abb2dc8e14467303a59ddfa7b9a294dfe 192.168.199.12:6379
   slots:[10923-16383] (5461 slots) master
S: a901eb8204b607f45c0c5d521fef422e5a60caa4 192.168.199.12:6380
   replicates eb97c3a7ad1ae973515c65a981fe2c556315639d
   <!-- 此处输入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.199.10:6379)
M: 15987d9aa5452baeb197d31d0b28cf10403e1fc4 192.168.199.10:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: db11544f435938e6f506595f0a94e6248e7a3787 192.168.199.11:6380
slots: (0 slots) slave
replicates 15987d9aa5452baeb197d31d0b28cf10403e1fc4
M: eb97c3a7ad1ae973515c65a981fe2c556315639d 192.168.199.11:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 76d44a73239971ab902effd30e11a2b0c108658d 192.168.199.10:6380
slots: (0 slots) slave
replicates 46b4e50abb2dc8e14467303a59ddfa7b9a294dfe
S: a901eb8204b607f45c0c5d521fef422e5a60caa4 192.168.199.12:6380
slots: (0 slots) slave
replicates eb97c3a7ad1ae973515c65a981fe2c556315639d
M: 46b4e50abb2dc8e14467303a59ddfa7b9a294dfe 192.168.199.12:6379
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.`

查看集群状态
/data/redis/bin/redis-cli -c -h 192.168.199.10 -p 6379

192.168.199.10:6379\> cluster nodes
15987d9aa5452baeb197d31d0b28cf10403e1fc4 192.168.199.10:6379@16379 myself,master - 0 1610613629000 1 connected 0-5460
db11544f435938e6f506595f0a94e6248e7a3787 192.168.199.11:6380@16380 slave 15987d9aa5452baeb197d31d0b28cf10403e1fc4 0 1610613631000 1 connected
eb97c3a7ad1ae973515c65a981fe2c556315639d 192.168.199.11:6379@16379 master - 0 1610613632028 3 connected 5461-10922
76d44a73239971ab902effd30e11a2b0c108658d 192.168.199.10:6380@16380 slave 46b4e50abb2dc8e14467303a59ddfa7b9a294dfe 0 1610613632000 5 connected
a901eb8204b607f45c0c5d521fef422e5a60caa4 192.168.199.12:6380@16380 slave eb97c3a7ad1ae973515c65a981fe2c556315639d 0 1610613631000 3 connected
46b4e50abb2dc8e14467303a59ddfa7b9a294dfe 192.168.199.12:6379@16379 master - 0 1610613631020 5 connected 10923-16383


192.168.199.10:6379\> cluster info


显示ok集群状态为正常
===========


`cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:248
cluster_stats_messages_pong_sent:236
cluster_stats_messages_sent:484
cluster_stats_messages_ping_received:231
cluster_stats_messages_pong_received:248
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:484`

集群重启
# 停止
sh -x /data/stop_redis.sh
# 启动
sh -x /data/start_redis.sh
# 查看集群状态
/data/redis/bin/redis-cli -c -h 192.168.199.10 -p 6379
192.168.199.10:6379> cluster nodes
192.168.199.10:6379> cluster info
赞(0)
未经允许不得转载:工具盒子 » Redis集群安装部署和基础配置