#下载redis-6.0.5
wget http://download.redis.io/releases/redis-6.0.5.tar.gz
#解压安装包
tar xf redis-6.0.5.tar.gz
#清除gcc
make distclean
#升级gcc
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
#切换gcc
scl enable devtoolset-9 bash
#修改配置文件redis.conf
设置daemonize为yes 让redis支持后台启动
将protected-mode 改为no,远程连接就不需要账号密码了
将bind 127.0.0.1改为0.0.0.0
#不改密码等着被注入病毒吧(我吃了大亏)
修改密码requirepass
#编译安装 安装Redis6.x的时候一定要切换到gcc7再安装,否则会报错
make install PREFIX=/usr/local/server/redis-cluster/redis
#创建脚本文件
vi redis.sh
vi stop.sh
创建redis容器脚本文件
#!/bin/bash
#在/usr/local/server/redis-cluster下生成conf和data目标,并生成配置信息
# 创建文件夹
mkdir -p /usr/local/server/redis-cluster
# 下载redis配置模板
echo "正在下载redis-cluster.tmpl配置模板,请手动下载redis-cluster.tmpl文件并复制到/usr/local/server/redis-cluster目录";
# 文件下载地址 请手动下载redis-cluster.tmpl文件
#wget -P /usr/local/server/redis-cluster https://srv-file22.gofile.io/download/RoGvVk/redis-cluster.tmpl
echo "正在创建redis-net网络";
#c创建网络
docker network create redis-net
echo "正在创建redis配置文件";
for port in `seq 7001 7006`;
do
mkdir -p /usr/local/server/redis-cluster/${port}/conf && PORT=${port} Native_IP=${Native_IP} envsubst < /usr/local/server/redis-cluster/redis-cluster.tmpl > /usr/local/server/redis-cluster/${port}/conf/redis.conf && mkdir -p /usr/local/server/redis-cluster/${port}/data;
done
echo "正在启动redis容器";
#创建6个redis容器
for port in `seq 7001 7006`;
do
docker run -d -it -p ${port}:${port} -p 1${port}:1${port} -v /usr/local/server/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v /usr/local/server/redis-cluster/${port}/data:/data --privileged=true --restart always --name redis-${port} --net redis-net --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf;
done
#查找ip
for port in `seq 7001 7006`;
do
echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port}" ";
done
#换行
echo -e "\n"
#输入信息
read -p "请把输入要启动的docker容器名称,默认redis-7001:" DOCKER_NAME
#判断是否为空
if [ ! $DOCKER_NAME ];
then DOCKER_NAME='redis-7001';
fi
#进入容器
docker exec -it redis-7001 /bin/bash
# 删除容器
#docker rm -f $(docker ps -a | grep "redis-*" | awk '{print $1}')
容器删除脚本
#!/bin/bash
echo "正在停止所有redis容器";
docker stop $(docker ps -a | grep "redis-*" | awk '{print $1}')
echo "正在删除所有redis容器";
docker rm -f $(docker ps -a | grep "redis-*" | awk '{print $1}')
echo "正在删除redis-net网络";
docker network rm redis-net
echo "正在删除redis文件夹";
rm -rf 7001 7002 7003 7004 7005 7006
#脚本文件授权
chmod +x redis.sh
chmod +x stop.sh
#进入到任意一个安装好的redis节点的bin目录,里面有个脚本对象redis-cli,然后执行集群创建 (172.21.0.2 替换为redis.sh打印出的ip)
./redis-cli --cluster create 172.21.0.2:7001 172.21.0.3:7002 172.21.0.4:7003 172.21.0.5:7004 172.21.0.6:7005 172.21.0.7:7006 --cluster-replicas 1