开启limits限制
sudo bash -c 'cat >> /etc/security/limits.conf <<-EOF
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
EOF'
reboot重启
安装redis
两台机器都运行
yum install -y wget gcc
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar xzf redis-4.0.9.tar.gz
cd redis-4.0.9
#编译
make MALLOC=libc
#安装并指定安装目录
make install PREFIX=/usr/local/redis
问题
redis直接用make编译报致命错误:jemalloc/jemalloc.h:没有那个文件或目录
分配器allocator,如果有MALLOC 这个 环境变量, 会有用这个环境变量的 去建立Redis。
而且libc 并不是默认的分配器, 默认的是 jemalloc, 因为 jemalloc 被证明 有更少的 fragmentation problems 比libc。
但是如果你又没有jemalloc而只有 libc 当然 make 出错。 所以加这么一个参数,运行如下命令:
make MALLOC=libc
修改配置
cp redis.conf /usr/local/redis/nodes-9380.conf
vi /usr/local/redis/nodes-9380.conf
修改内容如下
bind 192.168.56.105
port 9380
cluster-enabled yes
cluster-config-file /usr/local/redis/nodes-cluster-9380.conf
cluster-node-timeout 15000
appendonly yes
appendfilename "appendonly-9380.aof"
logfile "log-9380.log"
daemonize yes
cat /usr/local/redis/nodes-9380.conf | sed 's/9380/9381/' > /usr/local/redis/nodes-9381.conf
cat /usr/local/redis/nodes-9380.conf | sed 's/9380/9382/' > /usr/local/redis/nodes-9382.conf
#配置拷贝到另外一台机器上
scp /usr/local/redis/nodes-9380.conf 192.168.56.104:/usr/local/redis/
另外一台机器上修改ip
vi /usr/local/redis/nodes-9380.conf
拷贝替换文件
cat /usr/local/redis/nodes-9380.conf | sed 's/9380/9381/' > /usr/local/redis/nodes-9381.conf
cat /usr/local/redis/nodes-9380.conf | sed 's/9380/9382/' > /usr/local/redis/nodes-9382.conf
启动
kill -9 $(ps -ef|grep redis-server|grep -v grep|awk '{print $2}')
/usr/local/redis/bin/redis-server /usr/local/redis/nodes-9380.conf
/usr/local/redis/bin/redis-server /usr/local/redis/nodes-9381.conf
/usr/local/redis/bin/redis-server /usr/local/redis/nodes-9382.conf
安装集群
#yum install ruby -y
#再安装redis插件
#gem install redis
#提示ruby版本过低
wget https://github.com/postmodern/ruby-install/archive/v0.8.1.tar.gz
tar xzvf v0.8.1.tar.gz
cd ruby-install-0.8.1/bin/
#看下ruby的最新版本列表
./ruby-install --latest
#安装
./ruby-install ruby 2.7.2
会输出Successfully installed ruby 2.7.2 into /opt/rubies/ruby-2.7.2
ln -s /opt/rubies/ruby-2.7.2/bin/ruby /usr/bin/ruby
ln -s /opt/rubies/ruby-2.7.2/bin/gem /usr/bin/gem
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
#再安装redis插件
gem install redis
切换到redis源码目录
./src/redis-trib.rb create --replicas 1 192.168.56.104:9380 192.168.56.104:9381 192.168.56.104:9382 192.168.56.105:9380 192.168.56.105:9381 192.168.56.105:9382
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.56.104:9380
192.168.56.105:9380
192.168.56.104:9381
Adding replica 192.168.56.105:9382 to 192.168.56.104:9380
Adding replica 192.168.56.104:9382 to 192.168.56.105:9380
Adding replica 192.168.56.105:9381 to 192.168.56.104:9381
M: e958a652b5d0184b288796121a9b6673534675bf 192.168.56.104:9380
slots:0-5460 (5461 slots) master
M: 63292c0bcee1eff0d1f2df4a7678a3795172634d 192.168.56.104:9381
slots:10923-16383 (5461 slots) master
S: 420d577f5826ab948f3a6dc13e5d588714159fec 192.168.56.104:9382
replicates e579aef656f6bddb578a27b7ca19287d35f9430a
M: e579aef656f6bddb578a27b7ca19287d35f9430a 192.168.56.105:9380
slots:5461-10922 (5462 slots) master
S: 92e3a6e4655f0630d8d92161434a7b34a1330495 192.168.56.105:9381
replicates 63292c0bcee1eff0d1f2df4a7678a3795172634d
S: 227102968867e9dfd419b5a5729fe15c12323424 192.168.56.105:9382
replicates e958a652b5d0184b288796121a9b6673534675bf
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 192.168.56.104:9380)
M: e958a652b5d0184b288796121a9b6673534675bf 192.168.56.104:9380
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 92e3a6e4655f0630d8d92161434a7b34a1330495 192.168.56.105:9381
slots: (0 slots) slave
replicates 63292c0bcee1eff0d1f2df4a7678a3795172634d
M: 63292c0bcee1eff0d1f2df4a7678a3795172634d 192.168.56.104:9381
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 420d577f5826ab948f3a6dc13e5d588714159fec 192.168.56.104:9382
slots: (0 slots) slave
replicates e579aef656f6bddb578a27b7ca19287d35f9430a
M: e579aef656f6bddb578a27b7ca19287d35f9430a 192.168.56.105:9380
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 227102968867e9dfd419b5a5729fe15c12323424 192.168.56.105:9382
slots: (0 slots) slave
replicates e958a652b5d0184b288796121a9b6673534675bf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
有防火墙会无法连接
systemctl stop firewalld
systemctl disable firewalld
连接集群测试
./redis-cli -c -h 192.168.56.104 -p 9380
192.168.56.104:9380> set a 1
-> Redirected to slot [15495] located at 192.168.56.104:9381
OK
192.168.56.104:9381> get a
"1"
192.168.56.104:9381> get b
-> Redirected to slot [3300] located at 192.168.56.104:9380
(nil)
redis集群密码设置
1、密码设置(推荐)方式一:修改所有Redis集群中的redis.conf文件加入:
masterauth passwd123
requirepass passwd123
说明:这种方式需要重新启动各节点
方式二:进入各个实例进行设置:
./redis-cli -c -p 7000
config set masterauth passwd123
config set requirepass passwd123
config rewrite
之后分别使用./redis-cli -c -p 7001,./redis-cli -c -p 7002……命令给各节点设置上密码。
注意:各个节点密码都必须一致,否则Redirected就会失败, 推荐这种方式,这种方式会把密码写入到redis.conf里面去,且不用重启。
带密码访问集群
./redis-cli -c -p 7000 -a passwd123