一,本次实验在腾讯云
前提关闭防火墙 想要后期再开 增加防火墙策略
主机3台分别:t1db,t2db,t3db
系统:centos7.6
腾讯云的安全组必须打开就算几台主机在一个安全组也要打开,腾讯比较安全组权限狠低,不开我就等了半天
redis端口:3121,3122
redis集群端口:13121,13122 备注:集群端口为redis端口加10000,不需要配置,但防火墙或者安全组tcp必须打开
redis版本:5.0.2
2,安装redis
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
tar -zxvf redis-5.0.2.tar.gz
yum install -y make gcc
make
make install
cd utils/
./install_server.sh
提示默认端口port:6379 我修改为3121
redis文件位置,下面的6379都变成3121,后面我就不说位置在哪了
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
增加3122实例启动,这一步应该放在修改3212.conf之后,但既然说安装了 我就放这了
mkdir -p /var/lib/redis/3122
cp /etc/init.d/redis_3122 /etc/init.d/redis_3122
vim /etc/init.d/redis_3122
把里面3121的都改成3122
chkconfig --add redis_3122
启动关闭重启命令
systemctl restart redis_3121 /重启
systemctl restart redis_3122 /重启
systemctl stop redis_3121 /关闭
systemctl stop redis_3122 /关闭
systemctl start redis_3121 /启动
systemctl start redis_3122 /启动
设置了密码,关闭就会无效
可以试用pkill redis-server
启动不了用这种方法
/usr/local/bin/redis-server /etc/redis/3121.conf
/usr/local/bin/redis-server /etc/redis/3122.conf
redis-cli -h ip(例如:127.0.0.1) -p 3121 shutdown /关闭
查看端口netstat -apn | grep 3121
三,3121.conf和3122.conf配置
vi /etc/redis/3121.conf
允许远程访问
bind 0.0.0.0
protected-mode no
# 绑定服务器IP地址,这个其实0,0,0,0也行 为了安全用本地ip只能本机登录
bind 0.0.0.0
# 绑定端口号,必须修改,以此来区分Redis实例
port 3121
# 后台运行
daemonize yes
# 修改pid进程文件名,以端口号命名
pidfile /var/run/redis_3121.pid
# 修改日志文件名称,以端口号为目录来区分
logfile /var/log/redis_3121.log
# 修改数据文件存放地址,以端口号为目录名来区分
dir /var/lib/redis/3121
# 启用集群
cluster-enabled yes
# 配置每个节点的配置文件,同样以端口号为名称
cluster-config-file nodes-3121.conf
# 配置集群节点的超时时间,可改可不改
cluster-node-timeout 15000
# 启动AOF增量持久化策略
appendonly yes
# 发生改变就记录日志
appendfsync always
#设置redis访问密码 ,测试中所以没用,为了安全生产环境请加上
requirepass ssxx
#设置redis集群间的访问密码,同上面密码一致,测试中所以没用,为了安全生产环境请加上
masterauth ssxx
把3121.conf复制命名3122.conf
vi /etc/redis/3122.conf
把3121都修改成3122
好了这里第一台主机t1db的就配置完成 ,启动
systemctl start redis_3121 /启动
systemctl start redis_3122
t2db和t3db一样 安装
然后把3121.conf和3122.conf的配置丢到, t2db和t3db主机里只需要修改3121.conf和3122.conf绑定ip
bind 0.0.0.0
bind 0.0.0.0
三台主机6个实例都启动
四,创建集群
建立集群时
Waiting for the cluster to join
..........................................................如果这一步等的太久,这么多点点 点 大概率是防火墙或者安全组没开集群端口
如果建立集群失败,删除6给实例的文件/var/lib/redis/3122和/var/lib/redis/3121这里的文件目下的
dump.rdb 和nodes-3122.conf
dump.rdb 和 nodes-3121.conf
无密码建立
redis-cli --cluster create 172.16.0.8:3121 172.16.0.8:3122 172.16.0.9:3121 172.16.0.9:3122 172.16.0.10:3121 172.16.0.10:3122 --cluster-replicas 1
有密码建立
redis-cli -a ssxx --cluster create 172.16.0.8:3121 172.16.0.8:3122 172.16.0.9:3121 172.16.0.9:3122 172.16.0.10:3121 172.16.0.10:3122 --cluster-replicas 1
下面是无密码建立后的显示
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.16.0.9:3122 to 172.16.0.8:3121
Adding replica 172.16.0.8:3122 to 172.16.0.9:3121
Adding replica 172.16.0.10:3122 to 172.16.0.10:3121
>>> Trying to optimize slaves allocation for anti-affinity
[OK] Perfect anti-affinity obtained!
M: 1c708a6b01f860835c209e8881d3bc0934734076 172.16.0.8:3121
slots:[0-5460] (5461 slots) master
S: 2d46d434da5a196fd435f5a4ffb09b9cefbc8f58 172.16.0.8:3122
replicates 28dc96a888d087c20306231d5713672da0ec8951
M: 28dc96a888d087c20306231d5713672da0ec8951 172.16.0.9:3121
slots:[5461-10922] (5462 slots) master
S: 8696e44653ae48dc85209face86e02ba0de65e56 172.16.0.9:3122
replicates 4716bd5554eb9514f5ffba973682ec4bf302f7e9
M: 4716bd5554eb9514f5ffba973682ec4bf302f7e9 172.16.0.10:3121
slots:[10923-16383] (5461 slots) master
S: 7317a02362de2199e32b3c570f93149041882e6d 172.16.0.10:3122
replicates 1c708a6b01f860835c209e8881d3bc0934734076
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 172.16.0.8:3121)
M: 1c708a6b01f860835c209e8881d3bc0934734076 172.16.0.8:3121
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 7317a02362de2199e32b3c570f93149041882e6d 172.16.0.10:3122
slots: (0 slots) slave
replicates 1c708a6b01f860835c209e8881d3bc0934734076
M: 4716bd5554eb9514f5ffba973682ec4bf302f7e9 172.16.0.10:3121
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 28dc96a888d087c20306231d5713672da0ec8951 172.16.0.9:3121
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 8696e44653ae48dc85209face86e02ba0de65e56 172.16.0.9:3122
slots: (0 slots) slave
replicates 4716bd5554eb9514f5ffba973682ec4bf302f7e9
S: 2d46d434da5a196fd435f5a4ffb09b9cefbc8f58 172.16.0.8:3122
slots: (0 slots) slave
replicates 28dc96a888d087c20306231d5713672da0ec8951
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到此处成功
修改好配置可以继续建立集群
五,查看集群状态
redis-cli --cluster check 172.16.0.8:3121
172.16.0.8:3121 (1c708a6b...) -> 0 keys | 5461 slots | 1 slaves.
172.16.0.10:3121 (4716bd55...) -> 0 keys | 5461 slots | 1 slaves.
172.16.0.9:3121 (28dc96a8...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.16.0.8:31621)
M: 1c708a6b01f860835c209e8881d3bc0934734076 172.16.0.8:31621
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 7317a02362de2199e32b3c570f93149041882e6d 172.16.0.12:31622
slots: (0 slots) slave
replicates 1c708a6b01f860835c209e8881d3bc0934734076
M: 4716bd5554eb9514f5ffba973682ec4bf302f7e9 172.16.0.12:31621
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 28dc96a888d087c20306231d5713672da0ec8951 172.16.0.9:31621
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 8696e44653ae48dc85209face86e02ba0de65e56 172.16.0.9:31622
slots: (0 slots) slave
replicates 4716bd5554eb9514f5ffba973682ec4bf302f7e9
S: 2d46d434da5a196fd435f5a4ffb09b9cefbc8f58 172.16.0.8:31622
slots: (0 slots) slave
replicates 28dc96a888d087c20306231d5713672da0ec8951
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
六,使用集群,随意一个端口实例都行
[root@t1db ~]# redis-cli -c -h 172.16.0.8 -p 3121
172.16.0.8:3121> set a test
-> Redirected to slot [15495] located at 172.16.0.10:3121
OK
172.16.0.10:3121> get a
"test"
172.16.0.10:31621> exit
[root@t1db ~]# redis-cli -c -h 172.16.0.8 -p 3122
172.16.0.8:3122> get a
-> Redirected to slot [15495] located at 172.16.0.10:3121
"test"
172.16.0.12:3121> exit
[root@t1db ~]# redis-cli -c -h 172.16.0.9 -p 3122
172.16.0.9:3122> get a
-> Redirected to slot [15495] located at 172.16.0.10:3121
"test"
172.16.0.10:3121> exit