1. #下载redis-6.0.5
    2. wget http://download.redis.io/releases/redis-6.0.5.tar.gz
    3. #解压安装包
    4. tar xf redis-6.0.5.tar.gz
    5. #清除gcc
    6. make distclean
    7. #升级gcc
    8. yum -y install centos-release-scl
    9. yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
    10. #切换gcc
    11. scl enable devtoolset-9 bash
    12. #修改配置文件redis.conf
    13. 设置daemonizeyes redis支持后台启动
    14. protected-mode 改为no,远程连接就不需要账号密码了
    15. bind 127.0.0.1改为0.0.0.0
    16. #不改密码等着被注入病毒吧(我吃了大亏)
    17. 修改密码requirepass
    18. #编译安装 安装Redis6.x的时候一定要切换到gcc7再安装,否则会报错
    19. make install PREFIX=/usr/local/server/redis-cluster/redis
    20. #创建脚本文件
    21. vi redis.sh
    22. vi stop.sh

    创建redis容器脚本文件

    1. #!/bin/bash
    2. #在/usr/local/server/redis-cluster下生成conf和data目标,并生成配置信息
    3. # 创建文件夹
    4. mkdir -p /usr/local/server/redis-cluster
    5. # 下载redis配置模板
    6. echo "正在下载redis-cluster.tmpl配置模板,请手动下载redis-cluster.tmpl文件并复制到/usr/local/server/redis-cluster目录";
    7. # 文件下载地址 请手动下载redis-cluster.tmpl文件
    8. #wget -P /usr/local/server/redis-cluster https://srv-file22.gofile.io/download/RoGvVk/redis-cluster.tmpl
    9. echo "正在创建redis-net网络";
    10. #c创建网络
    11. docker network create redis-net
    12. echo "正在创建redis配置文件";
    13. for port in `seq 7001 7006`;
    14. do
    15. 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;
    16. done
    17. echo "正在启动redis容器";
    18. #创建6个redis容器
    19. for port in `seq 7001 7006`;
    20. do
    21. 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;
    22. done
    23. #查找ip
    24. for port in `seq 7001 7006`;
    25. do
    26. echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port}" ";
    27. done
    28. #换行
    29. echo -e "\n"
    30. #输入信息
    31. read -p "请把输入要启动的docker容器名称,默认redis-7001:" DOCKER_NAME
    32. #判断是否为空
    33. if [ ! $DOCKER_NAME ];
    34. then DOCKER_NAME='redis-7001';
    35. fi
    36. #进入容器
    37. docker exec -it redis-7001 /bin/bash
    38. # 删除容器
    39. #docker rm -f $(docker ps -a | grep "redis-*" | awk '{print $1}')

    容器删除脚本

    1. #!/bin/bash
    2. echo "正在停止所有redis容器";
    3. docker stop $(docker ps -a | grep "redis-*" | awk '{print $1}')
    4. echo "正在删除所有redis容器";
    5. docker rm -f $(docker ps -a | grep "redis-*" | awk '{print $1}')
    6. echo "正在删除redis-net网络";
    7. docker network rm redis-net
    8. echo "正在删除redis文件夹";
    9. rm -rf 7001 7002 7003 7004 7005 7006
    1. #脚本文件授权
    2. chmod +x redis.sh
    3. chmod +x stop.sh
    4. #进入到任意一个安装好的redis节点的bin目录,里面有个脚本对象redis-cli,然后执行集群创建 (172.21.0.2 替换为redis.sh打印出的ip)
    5. ./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