一、集群架构
172.18.3.18:7000,7001
172.18.3.19:7000,7001
172.18.3.20:7000,7001
OS: CentOS7
二、安装redis
cd /data/server tar -zxv -f redis-4.0.14.tar.gz cd redis-4.0.14 make MALLOC=jemalloc && make install
三、配置redis node
1、启动node
mkdir -p /data/server/redis-cluster/{7000,7001} mkdir -p /data/redis/logs
/data/server/redis-cluster/7000/redis.conf
port 7000 bind 0.0.0.0 daemonize yes dir /data/server/redis-cluster/7000/ cluster-enabled yes cluster-config-file nodes_7000.conf cluster-node-timeout 8000 appendonly yes logfile "/data/redis/logs/redis.log" pidfile /var/run/redis_7000.pid
/data/server/redis-cluster/7001/redis.conf
port 7001 bind 0.0.0.0 daemonize yes dir /data/server/redis-cluster/7001/ cluster-enabled yes cluster-config-file nodes_7001.conf cluster-node-timeout 8000 appendonly yes logfile "/data/redis/logs/redis.log" pidfile /var/run/redis_7001.pid
useradd redis
chown -R redis:redis /data/server/redis-cluster/
chown redis:redis /data/logs/redis
su redis -s /bin/bash -c "/usr/local/bin/redis-server /data/server/redis-cluster/7000/redis.conf"
su reids -s /bin/bash -c "/usr/local/bin/redis-server /data/server/redis-cluster/7001/redis.conf"
2、iptables开放端口(互相开放7000,17000,7001,17001端口)
-A INPUT -s 172.188.3.19/32 -p tcp -m tcp -m multiport --dports 7000,17000,7001,17001 -m comment --comment "redis cluster" -j ACCEPT -A INPUT -s 172.188.3.20/32 -p tcp -m tcp -m multiport --dports 7000,17000,7001,17001 -m comment --comment "redis cluster" -j ACCEPT
3、配置redis集群
3.1、安装ruby (安装redis集群需要ruby, centos默认的ruby版本不符合要求,以下使用rvm安装,只需要在其中任意一台node上安装)
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB curl -L get.rvm.io | bash -s stable source /etc/profile.d/rvm.sh rvm install 2.5.5 # ruby2.5.5
gem install redis # redis ruby接口
3.2、创建redis集群
rvm use 2.5.5 cp /data/server/redis-4.0.14/src/redis-trib.rb /usr/local/bin/ /usr/local/bin/redis-trib.rb create --replicas 1 172.188.3.18:7000 172.188.3.18:7001 172.188.3.19:7000 172.188.3.19:7001 172.188.3.20:7000 172.188.3.20:7001
3.3、集群添加密码认证
进入到每个节点
/usr/local/bin/redis-cli -c -h 127.0.0.1 -p port
config set masterauth pdd123456
config set requirepass pdd123456
auth pdd123456
config rewrite
3.4、验证
echo 'cluster nodes' | /usr/local/bin/redis-cli -c -h 127.0.0.1 -p 7000 -a 'pdd123456'