(二)Redis_cluster集群搭建
一.集群规划
注意:
1.保证一对主从不在一台主机。
机器 | Master | Slave1 | Slave2 |
IP | 192.168.2.131 | 192.168.2.132 | 192.168.2.133 |
主 | 7000 | 7002 | 7004 |
从 | 7001\7003\7005自动分配 | 7001\7003\7005自动分配 | 7001\7003\7005自动分配 |
二.开始搭建
2.1.建立目录 # mkdir -p /redis-cluster/7000 /redis-cluster/7001 /redis-cluster/7002 /redis-cluster/7003 /redis-cluster/7004 /redis-cluster/7005 # Master执行 # mkdir -p /redis-cluster/ # Slave1执行 # mkdir -p /redis-cluster/ # Slave2执行 2.2.配置7000文件(Master) # vim /redis-cluster/7000/redis.conf bind 192.168.2.131 # 当前机器的IP port 7000 daemonize yes pidfile /redis-cluster/7000/redis-7000.pid logfile /redis-cluster/7000/redis.log dir /redis-cluster/7000/ cluster-enabled yes cluster-config-file nodes-7000.conf cluster-node-timeout 15000 appendonly yes appendfsync everysec 2.3.将配置文件复制到7001~7005下(Master) # echo /redis-cluster/7001/ /redis-cluster/7002/ /redis-cluster/7003/ /redis-cluster/7004/ /redis-cluster/7005/ |xargs -n 1 cp /redis-cluster/7000/redis.conf 2.4.批量修改端口(Master) # sed -i "s/7000/7001/g" /redis-cluster/7001/redis.conf # sed -i "s/7000/7002/g" /redis-cluster/7002/redis.conf # sed -i "s/7000/7003/g" /redis-cluster/7003/redis.conf # sed -i "s/7000/7004/g" /redis-cluster/7004/redis.conf # sed -i "s/7000/7005/g" /redis-cluster/7005/redis.conf 2.5.批量修改IP(Master) # sed -i "s/192.168.2.131/192.168.2.132/g" /redis-cluster/7002/redis.conf # 修改为slave1的IP # sed -i "s/192.168.2.131/192.168.2.132/g" /redis-cluster/7003/redis.conf # sed -i "s/192.168.2.131/192.168.2.133/g" /redis-cluster/7004/redis.conf # 修改为slave2的IP # sed -i "s/192.168.2.131/192.168.2.133/g" /redis-cluster/7005/redis.conf # scp -r /redis-cluster/7002 root@slave1:/redis-cluster/ # 发至Slave1 # scp -r /redis-cluster/7003 root@slave1:/redis-cluster/ # scp -r /redis-cluster/7004 root@slave2:/redis-cluster/ # 发至Slave2 # scp -r /redis-cluster/7005 root@slave2:/redis-cluster/ # rm -rf /redis-cluster/7002 /redis-cluster/7003 /redis-cluster/7004 /redis-cluster/7005 # 删除7002~7005 2.6.下载(3台) # yum install -y ruby # yum install gcc-c++ # 安装依赖 2.7.下载安装redis(Master) # wget http://download.redis.io/releases/redis-5.0.3.tar.gz # 下载redis稳定版 # tar -zxvf redis-5.0.3.tar.gz -C /usr/local/ # 解压 # mv /usr/local/redis-5.0.3 /usr/local/redis # 改名 # cd /usr/local/redis/ # 进入 # make # 执行 # make install PREFIX=/usr/local/redis # 将redis服务安装到指定目录 # vim /etc/profile # 打开环境变量添加以下 # redis_home PATH=$PATH:/usr/local/redis/bin export PATH 2.8.分发redis+环境(Master) # scp -r /usr/local/redis root@slave1:/usr/local/ # 分发redis至Slave1 # scp -r /usr/local/redis root@slave2:/usr/local/ # 分发redis至Slave2 # scp -r /etc/profile root@slave1:/etc/ # 将Master环境覆盖Slave1 # scp -r /etc/profile root@slave2:/etc/ # 将Master环境覆盖Slave2 # source /etc/profile # 生效环境(3台) 2.9.启动服务(3台) # redis-server /redis-cluster/7000/redis.conf (Master) # redis-server /redis-cluster/7001/redis.conf (Master) # redis-server /redis-cluster/7002/redis.conf (Slave1) # redis-server /redis-cluster/7003/redis.conf (Slave1) # redis-server /redis-cluster/7004/redis.conf (Slave2) # redis-server /redis-cluster/7005/redis.conf (Slave2) 3.0.查看(3台) ps -ef | grep redis 3.1.进入自动配置(IP不同分配至不同机器的角色) # redis-cli --cluster create 192.168.2.131:7000 192.168.2.131:7001 192.168.2.132:7002 192.168.2.132:7003 192.168.2.133:7004 192.168.2.133:7005 --cluster-replicas 1