本文尝试两种安装模式编译安装和docker容器安装
一,编译安装
1,环境准备
系统centos7.9,IP及端口
10.161.3.12:6379
10.161.3.12:6380
10.161.3.13:6379
10.161.3.13:6380
10.161.3.14:6379
10.161.3.14:6380
防火墙端口开放:
sudo firewall-cmd --zone=public --add-port=6379/tcp --permanent
sudo firewall-cmd --zone=public --add-port=6380/tcp --permanent
firewall-cmd --list-all
关闭selinux:
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
升级gcc版本:
CentOS 7.x默认是gcc version 4.8.5,升级到最新版本,
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 #永久生效
2,安装redis
所有节点创建目录
mkdir -p /usr/local/redis_cluster/redis_63{79,80}/{conf,pid,logs}
Redis安装包下载:
wget https://download.redis.io/releases/redis-7.0.6.tar.gz
tar -zxvf redis-7.0.6.tar.gz -C /usr/local/redis_cluster/
cd /usr/local/redis_cluster/redis-7.0.6/
make && make install
3,编辑配置
cp /usr/local/redis_cluster/redis-7.0.6/redis.conf /usr/local/redis_cluster/redis_6379/conf/
cp /usr/local/redis_cluster/redis-7.0.6/redis.conf /usr/local/redis_cluster/redis_6380/conf/
相同的配置文件每节点创建一份(共6份),按照注释修改对应的配置项即可
# 守护进行模式启动
daemonize yes
# 设置数据库数量,默认数据库为0
databases 16
# 绑定地址,需要修改
bind 192.168.0.120
# 绑定端口,需要修改
port 6379
# pid文件存储位置,文件名需要修改
pidfile /usr/local/redis_cluster/redis_6379/pid/redis_6379.pid
# log文件存储位置,文件名需要修改
logfile /usr/local/redis_cluster/redis_6379/logs/redis_6379.log
# RDB快照备份文件名,文件名需要修改
dbfilename redis_6379.rdb
# 本地数据库存储目录,需要修改
dir /usr/local/redis_cluster/redis_6379
#持久化
appendonly yes
# 集群相关配置 # 是否以集群模式启动
cluster-enabled yes
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 15000
# 生成的集群节点配置文件名,文件名需要修改
cluster-config-file nodes_6379.conf
#集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通
cluster-config-file nodes_7001.conf
#设置密码
requirepass 123456
#主从切换需要配置
masterauth 123456
4,启动及创建集群
每个主机上执行如下2条命令
redis-server /usr/local/redis_cluster/redis_6379/conf/redis.cnf
redis-server /usr/local/redis_cluster/redis_6380/conf/redis.cnf
cd /usr/local/redis_cluster/redis-7.0.6/src/
./redis-cli --cluster create --cluster-replicas 1 10.161.3.12:6379 10.161.3.12:6380 10.161.3.13:6379 10.161.3.13:6380 10.161.3.14:6379 10.161.3.14:6380 -a 123456
redis-cli -h 10.161.3.12 -p 6380 -c
测试:
set test 'this is test'
get test
cluster info查看集群信息
cluster nodes查看集群节点信息
5,集群高可用
kill 掉一个master节点,其slave节点会自动变为master
恢复master节点后,其会自动变为slave
二,docker容器安装单机版集群
编辑docker-compose.yml
version: '3'
networks:
redis-net:
external: true
services:
redis-6380:
image: redis:7.0.11
restart: unless-stopped
#hostname: redis
container_name: redis-6380
privileged: true
ports:
- 6380:6380
- 16380:16380
environment:
TZ: Asia/Shanghai
volumes:
- /home/redis-cluster/node-6380/conf/redis.conf:/etc/redis/redis.conf
- /home/redis-cluster/node-6380/data:/data
#- ./logs:/logs
command: ["redis-server","/etc/redis/redis.conf"]
networks:
- redis-net
redis-6381:
image: redis:7.0.11
restart: unless-stopped
#hostname: redis
container_name: redis-6381
privileged: true
ports:
- 6381:6381
- 16381:16381
environment:
TZ: Asia/Shanghai
volumes:
- /home/redis-cluster/node-6381/conf/redis.conf:/etc/redis/redis.conf
- /home/redis-cluster/node-6381/data:/data
#- ./logs:/logs
command: ["redis-server","/etc/redis/redis.conf"]
networks:
- redis-net
redis-6382:
image: redis:7.0.11
restart: unless-stopped
#hostname: redis
container_name: redis-6382
privileged: true
ports:
- 6382:6382
- 16382:16382
environment:
TZ: Asia/Shanghai
volumes:
- /home/redis-cluster/node-6382/conf/redis.conf:/etc/redis/redis.conf
- /home/redis-cluster/node-6382/data:/data
#- ./logs:/logs
command: ["redis-server","/etc/redis/redis.conf"]
networks:
- redis-net
redis-6383:
image: redis:7.0.11
restart: unless-stopped
#hostname: redis
container_name: redis-6383
privileged: true
ports:
- 6383:6383
- 16383:16383
environment:
TZ: Asia/Shanghai
volumes:
- /home/redis-cluster/node-6383/conf/redis.conf:/etc/redis/redis.conf
- /home/redis-cluster/node-6383/data:/data
#- ./logs:/logs
command: ["redis-server","/etc/redis/redis.conf"]
networks:
- redis-net
redis-6384:
image: redis:7.0.11
restart: unless-stopped
#hostname: redis
container_name: redis-6384
privileged: true
ports:
- 6384:6384
- 16384:16384
environment:
TZ: Asia/Shanghai
volumes:
- /home/redis-cluster/node-6384/conf/redis.conf:/etc/redis/redis.conf
- /home/redis-cluster/node-6384/data:/data
#- ./logs:/logs
command: ["redis-server","/etc/redis/redis.conf"]
networks:
- redis-net
redis-6385:
image: redis:7.0.11
restart: unless-stopped
#hostname: redis
container_name: redis-6385
privileged: true
ports:
- 6385:6385
- 16385:16385
environment:
TZ: Asia/Shanghai
volumes:
- /home/redis-cluster/node-6385/conf/redis.conf:/etc/redis/redis.conf
- /home/redis-cluster/node-6385/data:/data
#- ./logs:/logs
command: ["redis-server","/etc/redis/redis.conf"]
networks:
- redis-net
编辑一键启动脚本redis_cluster.sh
#!/bin/sh
#设置redis的连接密码
redis_pwd="xxxx6688"
#当前主机IP
host_ip="192.168.31.67"
redis_net=`docker network ls | grep redis-net | awk '{print $2}'`
if [ "$redis_net"x != "redis-net"x ]; then
docker network create redis-net
echo "docker created network : redis-net"
fi
echo "docker already exists network : redis-net"
for port in $(seq 6380 6385);
do
mkdir -p /home/redis-cluster/node-${port}/conf
touch /home/redis-cluster/node-${port}/conf/redis.conf
cat << EOF > /home/redis-cluster/node-${port}/conf/redis.conf
#节点端口
port ${port}
#添加访问认证
requirepass $redis_pwd
#如果主节点开启了访问认证,从节点访问主节点需要认证
masterauth $redis_pwd
#保护模式,默认值 yes,即开启。开启保护模式以后,需配置 bind ip 或者设置访问密码;关闭保护模式,外部网络可以直接访问
protected-mode no
#bind 0.0.0.0
#是否以守护线程的方式启动(后台启动),默认 no
daemonize no
#是否开启 AOF 持久化模式,默认 no
appendonly yes
#是否开启集群模式,默认 no
cluster-enabled yes
#集群节点信息文件
cluster-config-file nodes.conf
#群节点连接超时时间
cluster-node-timeout 5000
#集群节点 IP修改
cluster-announce-ip ${host_ip}
#集群节点映射端口
cluster-announce-port ${port}
#集群节点总线端口
cluster-announce-bus-port 1${port}
EOF
done
docker-compose up -d --build
docker exec -it redis-6380 /bin/bash -c "redis-cli -a ${redis_pwd} --cluster create ${host_ip}:6380 ${host_ip}:6381 ${host_ip}:6382 ${host_ip}:6383 ${host_ip}:6384 ${host_ip}:6385 --cluster-replicas 1"
sleep 5
docker exec -it redis-6380 /bin/bash -c "redis-cli -a ${redis_pwd} -p 6380 cluster info"
docker exec -it redis-6380 /bin/bash -c "redis-cli -a ${redis_pwd} -p 6380 cluster nodes"
echo "redis-cluster is OK!!!"
docker单机
docker pull redis:latest
mkdir -p /root/redis/conf
mkdir -p /root/redis/data
vim /root/redis/conf/redis.conf
##节点端口
port 46379
##允许任何来源
bind 0.0.0.0
## 是为了禁止公网访问redis cache,加强redis安全的。它启用的条件,有两个:1) 没有bind IP 2) 没有设置访问密码 启用后只能够通过lookback ip(127.0.0.1)访问Redis cache,如果从外网访问,则会返回相应的错误信息
protected-mode yes
##开启持久化模式
appendonly yes
appendfilename appendonly.aof
#开启混合持久化
aof-use-rdb-preamble yes
# 文件达到1m时进行重写,然后如果文件大小增长了一倍,也会触发重写。
auto-aof-rewrite-min-size 1mb
auto-aof-rewrite-percentage 100
##AOF 文件和 Redis 命令是同步频率的,假设配置为 always,其含义为当 Redis 执行命令的时候,则同时同步到 AOF 文件,这样会使得 Redis 同步刷新 AOF 文件,造成缓慢。而采用 evarysec 则代表
## 每秒同步一次命令到 AOF 文件。
appendfsync everysec
#pidfile redis.pid
#进程pid文件,加port以示区分
pidfile /data/redis/data/redis_6379.pid
# 后台运行 ---- docker中使用后台运行将无法启动容器(应该是容器无法检测后台运行进程)
# daemonize yes
#databases 20 默认的数据库个数
#设置容器初始化密码
#requirepass test1323
vim docker-compose.yml
version: '3'
networks:
redis-net:
external: true
services:
redis:
image: redis:7.0.11
restart: unless-stopped
#hostname: redis
container_name: redis-7.0.11
privileged: true
ports:
- 46379:46379
environment:
TZ: Asia/Shanghai
volumes:
- /root/redis7.0.11-host/data:/data
- /root/redis7.0.11-host/redis.conf:/etc/redis/redis.conf
#- ./logs:/logs
command: ["redis-server","/etc/redis/redis.conf"]
networks:
- redis-net