1.安装好Redis
2.创建相关文件夹
# mkdir -p /opt/app/redis-5.0.4/cluster/6379
# mkdir -p /opt/app/redis-5.0.4/cluster/6380
# cd /opt/app/redis-5.0.4
# ls
00-RELEASENOTES BUGS CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
bin cluster COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
# cp redis.conf /opt/app/redis-5.0.4/cluster/6379
# cp redis.conf /opt/app/redis-5.0.4/cluster/6380
3.修改配置文件
修改6379端口配置文件
vim /opt/app/redis-5.0.4/cluster/6379/redis.conf
编辑配置文件内容:
# 绑定服务器IP地址
bind 10.1.72.15
# 绑定端口号,必须修改,以此来区分Redis实例
port 6379
# 后台运行
daemonize yes
# 修改pid进程文件名,以端口号命名
pidfile /var/run/redis-6379.pid
# 修改日志文件名称,以端口号为目录来区分
logfile /opt/app/redis-5.0.4/cluster/6379/redis.log
# 修改数据文件存放地址,以端口号为目录名来区分
dir /opt/app/redis-5.0.4/cluster/6379/
# 启用集群
cluster-enabled yes
# 配置每个节点的配置文件,同样以端口号为名称
cluster-config-file nodes-6379.conf
# 配置集群节点的超时时间,可改可不改
cluster-node-timeout 15000
# 关闭记录日志
# appendfsync always
# appendfsync everysec
appendfsync no
# 关闭持久化
自动持久化规则配置
# save 900 1
# save 300 10
# save 60 10000
# redis也可以关闭自动持久化,注释掉这些save配置,或者save “”
修改6380端口配置文件
vim /opt/app/redis-5.0.4/cluster/6380/redis.conf
编辑配置文件内容:
# 绑定服务器IP地址
bind 10.1.72.15
# 绑定端口号,必须修改,以此来区分Redis实例
port 6380
# 后台运行
daemonize yes
# 修改pid进程文件名,以端口号命名
pidfile /var/run/redis-6380.pid
# 修改日志文件名称,以端口号为目录来区分
logfile /opt/app/redis-5.0.4/cluster/6380/redis.log
# 修改数据文件存放地址,以端口号为目录名来区分
dir /opt/app/redis-5.0.4/cluster/6380/
# 启用集群
cluster-enabled yes
# 配置每个节点的配置文件,同样以端口号为名称
cluster-config-file nodes-6380.conf
# 配置集群节点的超时时间,可改可不改
cluster-node-timeout 15000
# 关闭记录日志
# appendfsync always
# appendfsync everysec
appendfsync no
# 关闭持久化
自动持久化规则配置
# save 900 1
# save 300 10
# save 60 10000
# redis也可以关闭自动持久化,注释掉这些save配置,或者save “”
另外两台机器同样配置
ip16,ip17仿效ip15进行安装和配置
# scp -r redis-5.0.4 root@10.1.72.16:/opt/app/
# scp -r redis-5.0.4 root@10.1.72.17:/opt/app/
修改配置文件IP地址
# 绑定服务器IP地址
bind 10.1.72.16
---------------
# 绑定服务器IP地址
bind 10.1.72.17
配置环境变量
方便于全局直接使用Redis命令
export REDIS_HOME=/opt/app/redis-5.0.4
export PATH=$REDIS_HOME/bin:$PATH
source /etc/profile
4.单独启动3台Redis
单独启动各服务器:ip15、ip16、ip17启动Redis
# redis-server /opt/app/redis-5.0.4/cluster/6379/redis.conf
# redis-server /opt/app/redis-5.0.4/cluster/6380/redis.conf
# ps aux|grep redis
root 23423 0.1 0.2 32323 2548 ? Ssl 05:01 0:00 redis-server 10.1.72.15:6379 [cluster]
root 23423 0.4 0.2 32323 2548 ? Ssl 05:01 0:00 redis-server 10.1.72.15:6380 [cluster]
5.创建集群
redis-cli --cluster create 10.1.72.15:6379 10.1.72.15:6380 10.1.72.16:6379 10.1.72.16:6380 10.1.72.17:6379 10.1.72.17:6380 --cluster-replicas 1
#################################################################################
[root@ip15 redis-5.0.4]# redis-cli --cluster create 10.1.72.15:6379 10.1.72.15:6380 10.1.72.16:6379 10.1.72.16:6380 10.1.72.17:6379 10.1.72.17:6380 --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 10.1.72.15:6380 to 10.1.72.15:6379
Adding replica 10.1.72.16:6380 to 10.1.72.16:6379
Adding replica 10.1.72.17:6380 to 10.1.72.17:6379
M: 3784c7f8683faf1adce14c43c4274300fff33c79 10.1.72.15:6379
slots:[0-5460] (5461 slots) master
S: 8181ed88dd5fa1b1165a0b3a4c632de508d5766a 10.1.72.15:6380
replicates a88b9d61503decf0a4b599c169c693add15b2534
M: dfb1bf054d740fa7d3c5487278457034f17ca617 10.1.72.16:6379
slots:[5461-10922] (5462 slots) master
S: bb5ae386877fda949e10f88f051a9006299fba21 10.1.72.16:6380
replicates 3784c7f8683faf1adce14c43c4274300fff33c79
M: a88b9d61503decf0a4b599c169c693add15b2534 10.1.72.17:6379
slots:[10923-16383] (5461 slots) master
S: c36284d8e41f05352dbd4b9c8e88be3d9ef24aba 10.1.72.17:6380
replicates dfb1bf054d740fa7d3c5487278457034f17ca617
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 10.1.72.15:6379)
M: 3784c7f8683faf1adce14c43c4274300fff33c79 10.1.72.15:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 8181ed88dd5fa1b1165a0b3a4c632de508d5766a 10.1.72.15:6380
slots: (0 slots) slave
replicates a88b9d61503decf0a4b599c169c693add15b2534
M: dfb1bf054d740fa7d3c5487278457034f17ca617 10.1.72.16:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: bb5ae386877fda949e10f88f051a9006299fba21 10.1.72.16:6380
slots: (0 slots) slave
replicates 3784c7f8683faf1adce14c43c4274300fff33c79
M: a88b9d61503decf0a4b599c169c693add15b2534 10.1.72.17:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: c36284d8e41f05352dbd4b9c8e88be3d9ef24aba 10.1.72.17:6380
slots: (0 slots) slave
replicates dfb1bf054d740fa7d3c5487278457034f17ca617
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
6.检查集群状态
[root@ip15 redis-5.0.4]# redis-cli --cluster check 10.1.72.15:6379
10.1.72.15:6379 (3784c7f8...) -> 0 keys | 5461 slots | 1 slaves.
10.1.72.16:6379 (dfb1bf05...) -> 0 keys | 5462 slots | 1 slaves.
10.1.72.17:6379 (a88b9d61...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.1.72.15:6379)
M: 3784c7f8683faf1adce14c43c4274300fff33c79 10.1.72.15:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 8181ed88dd5fa1b1165a0b3a4c632de508d5766a 10.1.72.15:6380
slots: (0 slots) slave
replicates a88b9d61503decf0a4b599c169c693add15b2534
M: dfb1bf054d740fa7d3c5487278457034f17ca617 10.1.72.16:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: bb5ae386877fda949e10f88f051a9006299fba21 10.1.72.16:6380
slots: (0 slots) slave
replicates 3784c7f8683faf1adce14c43c4274300fff33c79
M: a88b9d61503decf0a4b599c169c693add15b2534 10.1.72.17:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: c36284d8e41f05352dbd4b9c8e88be3d9ef24aba 10.1.72.17:6380
slots: (0 slots) slave
replicates dfb1bf054d740fa7d3c5487278457034f17ca617
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@ip101 redis-5.0.4]#
7.测试使用集群
随意找到一个端口进行连接.
[root@ip15 redis-5.0.4]# redis-cli -c -h 10.1.72.17 -p 6380
10.1.72.17:6380> set key1 value1
-> Redirected to slot [9189] located at 10.1.72.17:6380
OK
10.1.72.17:6379> get key1
"value1"
10.1.72.17:6379>
8. 创建密码
如果创建了集群之后,想要设置密码,进行一下操作,不需要去修改 redis.conf 配置文件
./redis-cli -c -h bigdata24 -p 6380
config set masterauth <yourpassword>
config set requirepass <yourpassword>
config rewrite
./redis-cli -c -h 10.1.72.17 -p 6380
config set masterauth Sicc123
config set requirepass Sicc123
config rewrite
auth "Sicc123"
加入了密码之后,进入到集群时,需要加 -a 选项 ,后面跟上你设置的密码
使用redis-cli 连接集群, 指定 -c 参数
指定连接的主机节点(-h 主机ip, -a redis的密码(前提要设置密码,每个节点的密码必须保持一致))
./redis-5.0.2/src/redis-cli -p 6380 -c -a Sicc123
查看节点信息命令:
> cluster info
> cluster nodes