用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master、3 salve 环境。
一、安装redis
1. 下载并解压
cd /data/redis
wget http://download.redis.io/releases/redis-5.0.8.tar.gz
tar -zxvf redis-5.0.8.tar.gz
2. 编译安装
cd redis-5.0.8
make (编译)
make install PREFIX=/data/redis (安装并指定安装位置)
//默认端口 6379
3.创建6个Redis配置文件,并配置
cd /data/redis
mkdir redis_cluster
cd redis_cluster
mkdir 7000 7001 7002
另一台服务器:
cd /data/redis
mkdir redis_cluster
cd redis_cluster
mkdir 7003 7004 7005
然后拷贝原来的配置文件到目录下,然就在到相应目录下进行重命名
cp /data/redis/redis-5.0.8/redis.conf /data/redis/redis_cluster/7000/
cp /data/redis/redis-5.0.8/redis.conf /data/redis/redis_cluster/7001/
cp /data/redis/redis-5.0.8/redis.conf /data/redis/redis_cluster/7002/
//另一台服务器
cp /data/redis/redis-5.0.8/redis.conf /data/redis/redis_cluster/7003/
cp /data/redis/redis-5.0.8/redis.conf /data/redis/redis_cluster/7004/
cp /data/redis/redis-5.0.8/redis.conf /data/redis/redis_cluster/7005/
4.修改各自配置文件(其实可以先修改再复制)
port 7000 //端口7000,7002,7003
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
5.启动各个节点
cd /data/redis/bin
第一台机器上执行
./redis-server ../redis_cluster/7000/redis.conf
./redis-server ../redis_cluster/7001/redis.conf
./redis-server ../redis_cluster/7002/redis.conf
另外一台机器上执行
./redis-server ../redis_cluster/7003/redis.conf
./redis-server ../redis_cluster/7004/redis.conf
./redis-server ../redis_cluster/7005/redis.conf
6. 检查 redis 启动情况
ps -ef | grep redis (检查进程)
yepikai 3291 1 0 13:54 ? 00:00:04 ./redis-server 10.51.52.24:7000 [cluster]
yepikai 3299 1 0 13:54 ? 00:00:04 ./redis-server 10.51.52.24:7001 [cluster]
yepikai 3314 1 0 13:54 ? 00:00:04 ./redis-server 10.51.52.24:7002 [cluster]
netstat -tnlp | grep redis (检查端口)
tcp 0 0 10.51.52.24:17000 0.0.0.0:* LISTEN 3291/./redis-server
tcp 0 0 10.51.52.24:17001 0.0.0.0:* LISTEN 3299/./redis-server
tcp 0 0 10.51.52.24:17002 0.0.0.0:* LISTEN 3314/./redis-server
tcp 0 0 10.51.52.24:7000 0.0.0.0:* LISTEN 3291/./redis-server
tcp 0 0 10.51.52.24:7001 0.0.0.0:* LISTEN 3299/./redis-server
tcp 0 0 10.51.52.24:7002 0.0.0.0:* LISTEN 3314/./redis-server
另一台机器
ps -ef | grep redis (检查进程)
yepikai 2128 1 0 13:57 ? 00:00:04 ./redis-server 10.51.52.25:7003 [cluster]
yepikai 2144 1 0 13:57 ? 00:00:04 ./redis-server 10.51.52.25:7004 [cluster]
yepikai 2164 1 0 13:57 ? 00:00:04 ./redis-server 10.51.52.25:7005 [cluster]
netstat -tnlp | grep redis (检查端口)
tcp 0 0 10.51.52.25:17003 0.0.0.0:* LISTEN 2128/./redis-server
tcp 0 0 10.51.52.25:17004 0.0.0.0:* LISTEN 2144/./redis-server
tcp 0 0 10.51.52.25:17005 0.0.0.0:* LISTEN 2164/./redis-server
tcp 0 0 10.51.52.25:7003 0.0.0.0:* LISTEN 2128/./redis-server
tcp 0 0 10.51.52.25:7004 0.0.0.0:* LISTEN 2144/./redis-server
tcp 0 0 10.51.52.25:7005 0.0.0.0:* LISTEN 2164/./redis-server
7. 启动集群
cd /data/redis/bin
./redis-cli --cluster create 10.51.52.24:7000 10.51.52.24:7001 10.51.52.24:7002 10.51.52.25:7003 10.51.52.25:7004 10.51.52.25:7005 --cluster-replicas 1
// --replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。(--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.51.52.25:7005 to 10.51.52.24:7000
Adding replica 10.51.52.24:7002 to 10.51.52.25:7003
Adding replica 10.51.52.25:7004 to 10.51.52.24:7001
M: ddfc4f6beab1c934605fdae94b4ecf79d553fe08 10.51.52.24:7000
slots:[0-5460] (5461 slots) master
M: 05de231edc2884e666b1edd0affc30bfe7680b98 10.51.52.24:7001
slots:[10923-16383] (5461 slots) master
S: 5ed7c95871b59078a46e1b57bb3a8374741def35 10.51.52.24:7002
replicates 303db3a879386f2c793699776cdda9d428883886
M: 303db3a879386f2c793699776cdda9d428883886 10.51.52.25:7003
slots:[5461-10922] (5462 slots) master
S: 633a827217ab65f30dd4268570baa42cc5150753 10.51.52.25:7004
replicates 05de231edc2884e666b1edd0affc30bfe7680b98
S: 6c4856a74f3bbad082f4e95d9b65019f1b1e83ec 10.51.52.25:7005
replicates ddfc4f6beab1c934605fdae94b4ecf79d553fe08
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.51.52.24:7000)
M: ddfc4f6beab1c934605fdae94b4ecf79d553fe08 10.51.52.24:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 6c4856a74f3bbad082f4e95d9b65019f1b1e83ec 10.51.52.25:7005
slots: (0 slots) slave
replicates ddfc4f6beab1c934605fdae94b4ecf79d553fe08
M: 05de231edc2884e666b1edd0affc30bfe7680b98 10.51.52.24:7001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 5ed7c95871b59078a46e1b57bb3a8374741def35 10.51.52.24:7002
slots: (0 slots) slave
replicates 303db3a879386f2c793699776cdda9d428883886
S: 633a827217ab65f30dd4268570baa42cc5150753 10.51.52.25:7004
slots: (0 slots) slave
replicates 05de231edc2884e666b1edd0affc30bfe7680b98
M: 303db3a879386f2c793699776cdda9d428883886 10.51.52.25:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
8. 客户端连接集群
./redis-cli -c -h 10.51.52.24 -p 7000
10.51.52.24:7000>
10.51.52.24:7000> auth (密码)
查看集群信息
10.51.52.24:7000> cluster info
cluster_state:ok //集群状态
cluster_slots_assigned:16384 //槽分配
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:7
cluster_my_epoch:1
cluster_stats_messages_ping_sent:5606
cluster_stats_messages_pong_sent:1579
cluster_stats_messages_fail_sent:6
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_sent:7192
cluster_stats_messages_ping_received:1579
cluster_stats_messages_pong_received:1591
cluster_stats_messages_fail_received:3
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_received:3174
查看节点信息
10.51.52.24:7000> cluster nodes
633a827217ab65f30dd4268570baa42cc5150753 10.51.52.25:7004@17004 slave 05de231edc2884e666b1edd0affc30bfe7680b98 0 1586240531000 5 connected
ddfc4f6beab1c934605fdae94b4ecf79d553fe08 10.51.52.24:7000@17000 myself,master - 0 1586240531000 1 connected 0-5460
5ed7c95871b59078a46e1b57bb3a8374741def35 10.51.52.24:7002@17002 master - 0 1586240530371 7 connected 5461-10922
303db3a879386f2c793699776cdda9d428883886 10.51.52.25:7003@17003 slave 5ed7c95871b59078a46e1b57bb3a8374741def35 0 1586240532000 7 connected
05de231edc2884e666b1edd0affc30bfe7680b98 10.51.52.24:7001@17001 master - 0 1586240532000 2 connected 10923-16383
6c4856a74f3bbad082f4e95d9b65019f1b1e83ec 10.51.52.25:7005@17005 slave ddfc4f6beab1c934605fdae94b4ecf79d553fe08 0 1586240532378 6 connected
9. 集群测试
//链接测试即可
./redis-cli -c -h 10.51.52.24 -p 7000
./redis-cli -c -h 10.51.52.24 -p 7001
#### 10.新增节点
cd /data/redis/redis_cluster
cp /data/redis/redis-5.0.8/redis.conf /data/redis/redis_cluster/7006/
cp /data/redis/redis-5.0.8/redis.conf /data/redis/redis_cluster/7007/
启动新节点
cd /data/redis/bin
./redis-server redis_cluster/7006/redis.conf
./redis-server redis_cluster/7007/redis.conf
进行查看
# ps -ef | grep redis
11. 添加节点到集群
cd /data/redis/bin
./redis-cli --cluster add-node 10.51.52.24:7006 10.51.52.24:7007
格式:
redis-cli --cluster add-node {新节点IP}:{新节点端口} {任意集群节点IP}:{对应端口} #如果添加集群中的主节点,则新添加的就是主节点,如果是从节点则是从节点
#检查集群
#./redis-cli --cluster check 10.51.52.24:7006
# 删除节点
./redis-cli --cluster del-node 10.51.52.24:7006 d9da56a8f068d5529a7771addf586d14e14ca888