目录

redis6.0集群搭建

了解如何在 LoveIt 主题中快速, 直观地创建和组织内容.

下载安装编译

1
2
3
4
5
6
cd /opt
wget http://download.redis.io/releases/redis-6.0.9.tar.gz
tar -xvf redis-6.0.9.tar.gz
cd redis-6.0.9
make MALLOC=libc
make install PREFIX=/usr/local/redis

redis6需要使用GCC5以上 CentOS6及以下需要安装GCC5

设置redis到环境变量

1
2
echo 'export PATH="/usr/local/redis/bin:$PATH"' >> /etc/profile
source /etc/profile

创建数据文件夹

1
2
3
4
5
6
7
mkdir -p /usr/local/redis/run
mkdir -p /usr/local/redis/log
mkdir -p /usr/local/redis/conf
# data目录主要存储redis持久化等数据,子目录按需创建(下文等同)
mkdir -p /usr/local/redis/data/7000
mkdir -p /usr/local/redis/data/7001
mkdir -p /usr/local/redis/data/7002

复制|修改 配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
cp /opt/redis-6.0.9/redis.conf /usr/local/redis/conf/redis.conf

y_port=7000
#y_ip="192.168.204.128"
y_ip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" | head -1`

# 替换配置文件,生成新配置文件

cp redis.conf redis_${y_port}.conf

sed -i "s/^bind 127.0.0.1/bind ${y_ip}/" redis_${y_port}.conf

sed -i "s/^port 6379/port ${y_port}/" redis_${y_port}.conf
sed -i "s/^pidfile \/var\/run\/redis_6379.pid/pidfile \/usr\/local\/redis\/run\/redis_${y_port}.pid/" redis_${y_port}.conf
sed -i "s/^logfile \"\"/logfile \/usr\/local\/redis\/log\/redis_${y_port}.log/" redis_${y_port}.conf
sed -i "s/^dir .\//dir \/usr\/local\/redis\/data\/${y_port}/" redis_${y_port}.conf
sed -i "s/^# cluster-config-file nodes-6379.conf/cluster-config-file nodes-${y_port}.conf/" redis_${y_port}.conf

sed -i "s/^# cluster-enabled yes/cluster-enabled yes/" redis_${y_port}.conf
sed -i "s/^# cluster-node-timeout 15000/cluster-node-timeout 15000/" redis_${y_port}.conf
sed -i "s/^appendonly no/appendonly yes/" redis_${y_port}.conf
sed -i "s/^daemonize no/daemonize yes/" redis_${y_port}.conf

启动配置

1
redis-server /usr/local/redis/conf/redis_7000.conf

停止

1
pgrep redis-server | xargs -exec kill -9

关闭防火墙

1
2
3
systemctl stop firewalld.service
#关闭防火墙开机自启
#systemctl disable firewalld.service

创建集群

1
2
# 替换参数\<ip>:\<port>,全部ip:port 
redis-cli --cluster create <ip>:<port> <ip>:<port> --cluster-replicas 1

注意看M和S,对照下集群角色表

1
2
# 例:
redis-cli --cluster create 192.168.204.128:7000 192.168.204.128:7001 192.168.204.128:7002 192.168.204.129:7000 --cluster-replicas 1

查看集群状态

1
redis-cli -c -h 192.168.204.128 -p 7001 cluster info

查看集群节点

1
redis-cli -c -h 192.168.204.128 -p 7000 cluster nodes

连接集群 使用 -c 参数

1
redis-cli -c -h 192.168.204.128 -p 7000

参考:

Redis——6.0集群安装部署




原创自制: