34-redis缓存服务(二)

2020-06-01  本文已影响0人  Liang_JC

三: redis 高可用与集群

3.1:配置 reids 主从

3.1.1: Slave 主要配置:

3.1.1.1:命令行配置:

3.1.1.2: 同步日志

image.png

3.1.1.3: 当前 slave 状态

image.png

3.1.1.4: 保存配置到 redis.conf
286 replicaof 192.168.7.103 6379
293 masterauth 123456 #master 如果密码需要设置

3.1.1.5: 重启 slave 验证

info replication

image.png

3.1.1.6: 验证 slave 数据

127.0.0.1:6379> KEYS *

  1. "num"
  2. "hset1"
  3. "key1"
  4. "name1"
  5. "zset2"
  6. "key2"
  7. "zset1"
  8. "set2"

3.1.1.7: slave 状态只读无法写入数据

image.png

3.1.1.8: Master 日志

image.png

小笔记:redis主从

#master
vim /apps/redis/etc/redis.conf
    bind 127.0.0.1 192.168.37.7
    requirepass 123456
    
#slave1
vim /apps/redis/etc/redis.conf
    bind 127.0.0.1 192.168.37.17
redis-server /apps/redis/etc/redis.conf
#手动设置为从服务器
redis-cli
auth 123456
slaveof 192.168.37.7 6379
config set masterauth 123456

#slave切换master(手动)
info Replication
SLAVEOF no one
set key1 value1
vim /apps/redis/etc/redis.conf
    bind 127.0.0.1 192.168.37.17
    #slaveof 192.168.37.7 6379
    #masterauth 123456

#slave2
vim /apps/redis/etc/redis.conf
    bind 127.0.0.1 192.168.37.27
    slaveof 192.168.37.7 6379
    masterauth 123456
redis-server /apps/redis/etc/redis.conf

#slave节点再有slave
vim /apps/redis/etc/redis.conf
    bind 127.0.0.1 192.168.37.37
    slaveof 192.168.37.27 6379      #指向slave
    masterauth 123456

3.1.1.9:主从复制过程

3.1.1.10: 主从同步优化

3.1.1.11: Slave 同步过程日志

image.png

3.1.1.12: master 同步日志

image.png

3.1.1.13: slave 切换 master

#当前状态:
192.168.7.101:6379> info Replication
# Replication
role:slave
master_host:192.168.7.103
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0

#停止 slave 同步并查看当前状态:
192.168.7.101:6379> SLAVEOF no one
OK
192.168.7.101:6379> info Replication
# Replication
role:master
connected_slaves:0
master_replid:ac3475e5e4fae8c5f47711a643e465b9520c4182
master_replid2:8ee6bc1ac452fd4d2ccbaa660a219f78d218399a
master_repl_offset:8840
second_repl_offset:8841
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:8547
repl_backlog_histlen:294

#测试能否写入数据:
192.168.7.101:6379> set key1 value1
OK

3.1.1.14: Slave 节点再有 Slave

#在有 slave 的”master”查看状态:
# Replication
role:slave
master_host:192.168.7.102
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9 #最近一次与 master 通信已经过去多少秒。
master_sync_in_progress:0 #是否正在与 master 通信。
slave_repl_offset:5334 #当前同步的偏移量。
slave_priority:100 #slave 优先级, master 故障后值越小越优先同步。
slave_read_only:1
connected_slaves:1
slave0:ip=192.168.7.104,port=6379,state=online,offset=5334,lag=1
master_replid:0f0318c25a022add7fd51d4438c470cf608631f9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5334
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5334

3.1.2: 常见问题汇总

3.1.2.1: master 密码不对
即配置的 master 密码不对,导致验证不通过而无法建立主从同步关系。

3.1.2.2: Redis 版本不一致
不同的 redis 版本之间存在兼容性问题, 因此各 master 和 slave 之间必须保持版本一致。

3.1.2.3:无法远程连接
在开启了安全模式情况下,没有设置 bind 地址和密码

[root@redis-s1 ~]# redis-cli -h 192.168.7.104
192.168.7.104:6379> KEYS *
(error) DENIED Redis is running in protected mode because protected mode is enabled, no
bind address was specified, no authentication password
is requested to clients. In this mode connections are only accepted from the loopback
interface. If you want to connect from external computers to Redis you may adopt one of
the following solutions:
1) Just disable protected mode sending the command 'CONFIG SET protected-mode no'
from the loopback interface by connecting to Redis from the same host the server is
running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use
CONFIG REWRITE to make this change permanent.
2) Alternatively you can just disable the protected mode by editing the Redis configuration
file, and setting the protected mode option to 'no', and then restarting the server.
3) If you started the server manually just for testing, restart it with the '--protected-mode n
option.
4) Setup a bind address or an authentication password. NOTE: You only need to do one of
the above things in order for the server to start accepting connections from the outside.
3.2: redis 集群
3.2.1: Sentinel(哨兵)

3.2.1.1:手动配置 master:

3.2.1.1.1: 服务器 A 配置 slave

192.168.7.102:6379> REPLICAOF 192.168.7.101 6379
OK
192.168.7.102:6379> CONFIG SET masterauth "123456"
OK
192.168.7.103:6379> REPLICAOF 192.168.7.101 6379
OK
192.168.7.103:6379> CONFIG SET masterauth "123456"
OK

image.png

3.2.1.1.2: 服务器 B 配置 slave

192.168.7.103:6379> REPLICAOF 192.168.7.101 6379
OK
192.168.7.103:6379> CONFIG SET masterauth "123456"
OK

image.png

3.2.1.1.2: 当前 master 状态

image.png

3.2.1.1.3:应用程序如何连接 redis?

3.2.1.1.4: python 连接 redis
yum install python-pip
pip install redis

[root@redis-s3 ~]# cat test.py
#!/bin/env python
#Author: ZhangJie
import redis
pool = redis.ConnectionPool(host="192.168.7.101", port=6379,password="123456")
r = redis.Redis(connection_pool=pool)
for i in range(100):
r.set("k%d" % i,"v%d" % i)
data=r.get("k%d" % i)
print(data)

3.2.1.1.5: 各 Redis 服务器验证数据

image.png

3.2.1.1:编辑配置文件 sentinel.conf

3.2.1.1.1: Server1 配置
哨兵可以不和 Redis 服务器部署在一起

[root@redis-s1 etc]# grep "^[a-Z]" /usr/local/redis/etc/sentinel.conf
bind 0.0.0.0
port 26379
daemonize yes
pidfile "/usr/local/redis/redis-sentinel.pid"
logfile "/usr/local/redis/sentinel_26379.log"
dir "/usr/local/redis"
sentinel monitor mymaster 192.168.7.101 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000 #(SDOWN)主观下线的时间
sentinel parallel-syncs mymaster 1 #发生故障转移时候同时向新 master 同步数据的 slave 数量, 数字越
小总同步时间越长
sentinel failover-timeout mymaster 180000 #所有 slaves 指向新的 master 所需的超时时间
sentinel deny-scripts-reconfig yes

3.2.1.1.2: Server2 配置

bind 192.168.7.102
port 26379
daemonize yes
pidfile "/usr/local/redis/redis-sentinel.pid"
logfile "/usr/local/redis/sentinel_26379.log"
dir "/usr/local/redis"
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.7.101 6379 2
sentinel auth-pass mymaster 123456

3.2.1.1.3: Server3 配置

bind 192.168.7.103
port 26379
daemonize yes
pidfile "/usr/local/redis/redis-sentinel.pid"
logfile "/usr/local/redis/sentinel_26379.log"
dir "/usr/local/redis"
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.7.101 6379 2
sentinel auth-pass mymaster 123456

3.2.1.2: 启动哨兵
三台哨兵都要启动
#/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf
#/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf
#/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf

3.2.1.3:验证端口

image.png

3.2.1.4: 哨兵日志

image.png

3.2.1.5: 当前 redis 状态

192.168.7.101:6379> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.7.103,port=6379,state=online,offset=2316,lag=1slave1:ip=192.168.7.102,port=6379,state=online,offset=2175,lag=1
master_replid:f62f849f8ff4b9ee6d0095b802e9189a3e84aaf3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2316
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2316

3.2.1.6: 当前 sentinel 状态

尤其是最后一行, 涉及到 master IP 是多少,有几个 slave,有几个 sentinels, 必须是符合全部服务器
数量的。

[root@redis-s1 etc]# redis-cli -h 192.168.7.101 -p 26379
192.168.7.101:26379> info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0

3.2.1.6: 停止 Redis Master 测试故障转移

[root@redis-s1 ~]# systemctl stop redis
#查看集群信息:
[root@redis-s1 ~]# redis-cli -h 192.168.7.101 -p 6379
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.7.103,port=6379,state=online,offset=51206,lag=0
slave1:ip=192.168.7.102,port=6379,state=online,offset=51065,lag=1
master_replid:416707fb0a8197595e16d6af8a8241c41cd8e992
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:51347
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:51347
#查看哨兵信息:[root@redis-s1 ~]# redis-cli -h 192.168.7.101 -p 26379
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.7.101:6379,slaves=2,sentinels=3

故障转移时 sentinel 的信息

image.png

3.2.1.7:故障转移后的 redis 配置文件

3.2.1.8: 当前 reids 状态

[图片上传失败...(image-f0b6cd-1591525426362)]

小笔记:配置哨兵监控redis

#master
vim /apps/redis/etc/redis.conf
    bind 127.0.0.1 192.168.37.7
    requirepass 123456
    
cp /usr/local/src/redis/sentinel.conf /apps/redis/etc/sentinel.conf
vim /apps/redis/etc/sentinel.conf
    bind 0.0.0.0
    port 26379
    daemonize yes                   #守护进程
    pidfile "redis-sentinel.pid"
    logfile "sentinel.log"
    dir "/apps/redis/logs"
    sentinel monitor master1 192.168.37.7 6379 2        #最后2是指redis-server的总数量
    sentinel auth-pass master1 123456
    sentinel down-after-milliseconds master1 30000      #(SDOWN)主观下线的时间,单位毫秒
    sentinel parallel-syncs master1 1       #发生故障转移时候同时向新 master 同步数据的 slave 数量, 数字越小总同步时间越长
    sentinel failover-timeout master1 180000 #所有 slaves 指向新的 master 所需的超时时间,单位毫秒
    sentinel deny-scripts-reconfig yes      #禁止修改脚本
    
scp /apps/redis/etc/sentinel.conf root@192.168.37.17:/apps/redis/etc
scp /apps/redis/etc/sentinel.conf root@192.168.37.27:/apps/redis/etc
redis-server /apps/redis/etc/redis.conf
redis-sentinel /apps/redis/etc/sentinel.conf

#slave1
vim /apps/redis/etc/redis.conf
    bind 127.0.0.1 192.168.37.17
    slaveof 192.168.37.7 6379
    masterauth 123456
redis-server /apps/redis/etc/redis.conf
redis-sentinel /apps/redis/etc/sentinel.conf

#slave2
vim /apps/redis/etc/redis.conf
    bind 127.0.0.1 192.168.37.27
    slaveof 192.168.37.7 6379
    masterauth 123456
redis-server /apps/redis/etc/redis.conf
redis-sentinel /apps/redis/etc/sentinel.conf

#查看日志观察down变化
tail /apps/redis/logs/sentinel.log
3.2.2: Redis Cluster

3.2.2.1: Redis cluster 架构

3.2.2.1.1: Redis cluster 基本架构:

3.2.2.1.2: Redis cluster 主从架构:

3.2.2.2: 部署 redis 集群

3.2.2.2.1: 创建 redis cluster 集群的前提

3.2.2.2.2: 创建集群:

3.2.2.2.3:检查状态

由于未设置 masterauth 认证密码, 所以主从未建立起来, 但是集群已经运行,所以需要在每个 slave控制台使用 config set 设置 masterauth 密码,或者写在每个 redis 配置文件中,最好是在控制点设置密码之后再写入配置文件当中

image.png

3.2.2.2.4: 分别设置 masterauth 密码

[root@redis-s1 ~]# redis-cli -h 192.168.7.101 -p 6380 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.7.101:6380> CONFIG SET masterauth 123456
OK
[root@redis-s1 ~]# redis-cli -h 192.168.7.102 -p 6380 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.7.102:6380> CONFIG SET masterauth 123456
OK
[root@redis-s1 ~]# redis-cli -h 192.168.7.103 -p 6380 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.7.103:6380> CONFIG SET masterauth 123456
OK

3.2.2.2.5: 确认 slave 状态为 up

image.png

3.2.2.2.6: 验证 master 状态

[root@redis-s1 ~]# redis-cli -h 192.168.7.101 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be
safe.
192.168.7.101:6379> INFO Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.7.102,port=6380,state=online,offset=840,lag=0
master_replid:0aa3281030eb29bf268f3317d4afe401f661a917
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:840
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:4026531840
repl_backlog_first_byte_offset:1
repl_backlog_histlen:840

192.168.7.101:6379> CLUSTER INFO
cluster_state:okcluster_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:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1474
cluster_stats_messages_pong_sent:1507
cluster_stats_messages_sent:2981
cluster_stats_messages_ping_received:1502
cluster_stats_messages_pong_received:1474
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2981

3.2.2.2.8: 查看集群 node 对应关系

192.168.7.103:6380> cluster nodes
7186c6d03dd9a5e3be658f2d08e800bc55b04a09 192.168.7.102:6380@16380 slave
f4cfc5cf821c0d855016488d6fbfb62c03a14fda 0 1545659135000 4 connected
7eda0bcf0c01bb7343313b81267c42dd1b26c8a6 192.168.7.103:6380@16380 myself,slave
116c4c6de036fdbac5aaad25eb1a61ea262b64af 0 1545659135000 6 conne
ctedf4cfc5cf821c0d855016488d6fbfb62c03a14fda 192.168.7.101:6379@16379 master - 0
1545659135000 1 connected 0-5460
116c4c6de036fdbac5aaad25eb1a61ea262b64af 192.168.7.102:6379@16379 master - 0
1545659136000 3 connected 5461-10922
70de3821dde4701c647bd6c23b9dd3c5c9f24a62 192.168.7.103:6379@16379 master - 0
1545659134000 5 connected 10923-16383
2b6e5d9c3944d79a5b64a19e54e52f83d48438d6 192.168.7.101:6380@16380 slave
70de3821dde4701c647bd6c23b9dd3c5c9f24a62 0 1545659135946 5 connected

3.2.2.2.9: 验证集群写入 key

192.168.7.101:6379> SET key1 value1 #经过算法计算,当前 key 的槽位需要写入指定的 node
(error) MOVED 9189 192.168.7.102:6379 #槽位不在当前 node 所以无法写入
192.168.7.103:6379> SET key1 value1
(error) MOVED 9189 192.168.7.102:6379
192.168.7.102:6379> SET key1 value1 #指定的 node 就可以写入
OK
192.168.7.102:6379> KEYS *
1) "key1
192.168.7.101:6379> KEYS *
(empty list or set)
192.168.7.103:6379> KEYS *
(empty list or set)

3.2.2.2.10: 集群状态监控

#Redis 4
[root@s1 ~]# redis-trib.rb check 172.18.200.105:6379
>>> Performing Cluster Check (using node 172.18.200.105:6379)
S: dfa53d634b3bd798e256ef9861579d5e637fc4b0 172.18.200.105:6379
slots: (0 slots) slave
replicates 99e09216d4ca5739788791da81a816bd2322802d
M: 3ed26459bcdf4bbd3004c9a7506ba1f6e87dd55a 172.18.200.102:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 99e09216d4ca5739788791da81a816bd2322802d 172.18.200.101:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 45e28f36573eb5123c27b359ae870e55a7d73017 172.18.200.104:6379
slots: (0 slots) slave
replicates 4d6357633a82f4ab48910d9c26dec8a2ef5b757c
S: adc943d76aa9ef1b123319f0772e4322756f34a2 172.18.200.106:6379
slots: (0 slots) slave
replicates 3ed26459bcdf4bbd3004c9a7506ba1f6e87dd55a
M: 4d6357633a82f4ab48910d9c26dec8a2ef5b757c 172.18.200.103:6379
slots:10923-16383 (5461 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.
[root@s1 ~]# redis-trib.rb info 172.18.200.105:6379
172.18.200.102:6379 (3ed26459...) -> 1 keys | 5462 slots | 1 slaves.
172.18.200.101:6379 (99e09216...) -> 0 keys | 5461 slots | 1 slaves.
172.18.200.103:6379 (4d635763...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.

#Redis 5
redis-cli -a 123456 --cluster check 192.168.7.101:6379

小笔记:配置集群

#6台机器
#A:192.168.37.7 B:192.168.37.17 C:192.168.37.27 D:192.168.37.37 E:192.168.37.47 F:192.168.37.57

#先把哨兵取消
mv /apps/redis/etc/sentinel.conf /apps/redis/etc/sentinel.conf.bak
killall redis-sentinel
redis-cli -a 123456
flushall                    #清空数据
rm -rf /apps/redis/data/*
rm -rf /apps/redis/logs/*

#集群
#A到F
vim /apps/redis/etc/redis.conf
    bind 0.0.0.0
    requirepass 123456
    cluster-enabled yes         #开启集群
    cluster-config-file nodes-6379.conf
    #slaveof 192.168.37.7 6379
    masterauth 123456

#创建集群,所有机器都启动服务,并保证没有数据
#随便一台机器执行集群即可,以A为例,redis5.0以下需要redis-trib建立集群,5.0以上则用redis-cli即可
cp /usr/local/src/redis-4.1.2/src/redis-trib.rb /usr/sbin
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
tar xf ruby-2.5.5.tar.gz
cd ruby-2.5.5
./configure
make -j 2
make install
gem install redis

vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.2/lib/redis/client.rb
    :password ==> 123456;
redis-trib.rb create --replicas 192.168.37.7:6379 192.168.37.17:6379  192.168.37.27:6379  192.168.37.37:6379  192.168.37.47:6379  192.168.37.57:6379

#redis5
redis-cli -a 123456 --cluster create 192.168.7.101:6379 192.168.7.101:6380 192.168.7.102:6379 192.168.7.102:6380 192.168.7.103:6379 192.168.7.103:6380 --cluster-replicas 1

#查看集群状态信息
#redis 4
redis-trib.rb info 192.168.37.7:6379
redis-trib.rb check 192.168.37.7:6379
#redis 5
redis-cli -a 123456 --cluster check 192.168.37.7:6379

#创建集群后6台机器会变成3组主从集群
1:192.168.37.7:6379,192.168.37.37:6379
2:192.168.37.17:6379,192.168.37.47:6379
3:192.168.37.27:6379,192.168.37.57:6379
3.2.3: Redis cluster 集群节点维护
#验证当前状态
#Redis 3/4:
[root@s1 ~]# redis-trib.rb reshard 172.18.200.107:6379
[root@s1 ~]# redis-trib.rb check 172.18.200.101:6379  
#Redis 5:
[root@s1 ~]# redis-cli -a 123456 --cluster check 192.168.7.103:6379
image.png
#使用命令对新加的主机重新分配槽位:
[root@redis-s1 ~]# redis-cli -a 123456 --cluster reshard 192.168.7.104:6379
[root@redis-s1 ~]# redis-cli -a 123456 --cluster reshard 192.168.7.104:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.7.104:6379)
M: 886338acd50c3015be68a760502b239f4509881c 192.168.7.104:6379
slots: (0 slots) master
M: 116c4c6de036fdbac5aaad25eb1a61ea262b64af 192.168.7.102:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 2b6e5d9c3944d79a5b64a19e54e52f83d48438d6 192.168.7.101:6380
slots: (0 slots) slave
replicates 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
S: 7186c6d03dd9a5e3be658f2d08e800bc55b04a09 192.168.7.102:6380
slots: (0 slots) slave
replicates f4cfc5cf821c0d855016488d6fbfb62c03a14fda
M: 70de3821dde4701c647bd6c23b9dd3c5c9f24a62 192.168.7.103:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 7eda0bcf0c01bb7343313b81267c42dd1b26c8a6 192.168.7.103:6380
slots: (0 slots) slave
replicates 116c4c6de036fdbac5aaad25eb1a61ea262b64af
M: f4cfc5cf821c0d855016488d6fbfb62c03a14fda 192.168.7.101:6379
slots:[0-5460] (5461 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096 #分配多少个槽位 192.168.7.104:6379
What is the receiving node ID? 886338acd50c3015be68a760502b239f4509881c #接收 slot 的服务器 ID, 手动输入
192.168.7.104 的 node ID
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: all #将哪些源主机的槽位分配给 192.168.7.104:6379, all 是自动在所有的 redis node 选择划分,如果是从 redis cluster 删除主机可以使用此方式将主机上的槽位全部移动到别的 redis 主机
………………………………..
Moving slot 6823 from 116c4c6de036fdbac5aaad25eb1a61ea262b64af
Moving slot 6824 from 116c4c6de036fdbac5aaad25eb1a61ea262b64af
Moving slot 6825 from 116c4c6de036fdbac5aaad25eb1a61ea262b64af
Moving slot 6826 from 116c4c6de036fdbac5aaad25eb1a61ea262b64af
Moving slot 10923 from 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
Moving slot 10924 from 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
Moving slot 10925 from 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
Moving slot 10926 from 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
…………………………………..
Moving slot 1364 from f4cfc5cf821c0d855016488d6fbfb62c03a14fda
Do you want to proceed with the proposed reshard plan (yes/no)? yes #确认分配
image.png

3.2.3.1.3: 验证重新分配槽位之后的集群状态

3.2.3.1.4: 为新的 master 添加 slave 节点

3.2.3.1.5:更改新节点更改状态为 slave

3.2.3.1.6:验证当前集群状态

3.2.3.2:集群维护之动态删除节点

3.2.3.3:集群维护之模拟 Master 宕机

3.2.3.4: 集群维护之导入现有 Redis 数据

3.2.4: redis 扩展集群方案:

小笔记:集群管理

#添加新机器
1、添加新机器192.168.37.67
vim /apps/redis/etc/redis.conf
    bind 0.0.0.0
    requirepass 123456
    cluster-enabled yes         #开启集群
    cluster-config-file nodes-6379.conf
    #slaveof 192.168.37.7 6379
    masterauth 123456
systemctl start redis

2、新节点添加到集群
#redis 4
redis-trib.rb add-node 192.168.37.67:6379 192.168.37.57:6379
#redis 5
redis-cli -a 123456 --cluster add-node 192.168.37.67:6379 192.168.37.57:6379
#新机器再添加一个6380节点
cp -a /apps/redis /apps/redis-6380
vim /apps/redis-6380/etc/redis.conf
    pidfile /apps/redis/run/redis-6380.pid
    logfile "/apps/redis/logs/redis-6380.log"
    port 6380
    cluster-config-file nodes-6380.conf
redis-server /apps/redis-6380/etc/redis.conf

3、分配槽位
#集群管理机器上操作(这里是A机器)
redis-cli -a 123456 cluster nodes       #找到107:6379的id号
redis-cli -h 192.168.37.67 -p 6380
    AUTH 123456
    CLUSTER REPLICATE node-id
    CLUSTER nodes
    
redis-trib.rb reshard 192.168.37.57:6379    #分配槽位
#redis-cli -a 123456 --cluster reshard 192.168.37.57:6379   #redis 5
    How many slots do you want to move (from 1 to 16384)? 4096  #分配多少个槽位 192.168.37.57:6379
    What is the receiving node ID? node-id      #接收slot的服务器 ID,手动输入192.168.37.57 的 node ID
    Source node #1: all     #将哪些源主机的槽位分配给 192.168.7.104:6379,all是自动在所有的 redis node 选择划分,如果是从 redis cluster 删除主机可以使用此方式将主机上的槽位全部移动到别的 redis 主机
    Do you want to proceed with the proposed reshard plan (yes/no)? yes #确认分配

#!!!注意:节点上有数据会导致分配槽位失败,需要清空对应的redis数据
redis-trib.rb fix 192.168.37.57:6379        #修复
redis-cli -a 123456 cluster nodes           #找到107:6379的id号和已分配的slot
redis-trib.rb reshard 192.168.37.57:6379    #分配槽位
    How many slots do you want to move (from 1 to 16384)? 4096-已分配slot  
    What is the receiving node ID? node-id      
    Source node #1: all
    Do you want to proceed with the proposed reshard plan (yes/no)? yes 

#---------------分割线-----------------#

#下线节点,被迁移 Redis 服务器必须保证没有数据 
redis-trib.rb reshard 192.168.37.67:6379
#redis-cli -a 123456 --cluster reshard 192.168.37.67:6379       #redis 5
    How many slots do you want to move (from 1 to 16384)? 4096  #迁移 master 上的多少个槽位
    What is the receiving node ID? 迁移到的机器node-id        #接收槽位的服务器 ID
    Source node #1: 67-node-id                          #从哪个服务器迁移 4096 个槽位
    Source node #2: done                                #写done,表示没有其他 master 了
    Do you want to proceed with the proposed reshard plan (yes/no)? yes #是否继续
#迁移失败的话执行以下修复
redis-trib.rb fix 192.168.37.67:6379
#删除节点
redis-trib.rb del-node 192.168.37.67:6379 node-id
#redis-cli -a 123456 --cluster 192.168.37.67:6379 node-id       #redis5

四: memcached

Memcache 官网: http://memcached.org/

4.1: 单机部署:

4.1.1: yum 安装与启动

4.1.2: python 操作 memcache

#!/usr/bin/env python
#coding:utf-8
#Author:Zhang ShiJie
import memcache
m = memcache.Client(['172.18.200.106:11211'], debug=True)
for i in range(100):
    m.set("key%d" % i,"v%d" % i)
    ret = m.get('key%d' % i)
    print ret

4.1.2: 编译安装:

yum install libevent libevent-devel –y
pwd
/usr/local/src
tar xvf memcached-1.5.12.tar.gz
./configure --prefix=/usr/local/memcache
make && make install 

启动 memcached
# /usr/local/memcache/bin/memcached -u memcached -p 11211 -m 2048 -c 65536 &
4.2: memcached 集群部署架构:
4.3: 启动 memcache:

小笔记:高可用memcache(很少用了)

#2台机器

1、安装
#memcache-1和memcache-2
yum install libevent libevent-devel
wget https://sourceforge.net/projects/repcached/files/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz
tar xvf memcached-1.2.8-repcached-2.2.1.tar.gz
cd memcached-1.2.8-repcached-2.2.1
vim memcached.c
#删除57、60行,删除后如下
56 #ifndef IOV_MAX
57 # define IOV_MAX 1024
58 #endif
./configure --prefix=/usr/local/repcached --enable-replication
make && make install

2、启动memcache
#memcache-1
# -d:后台 -m:内存 -p:端口 -u:运行用户 -c:并发数 -x:mencache对方IP -X:本地监听端口
/usr/local/repcached/bin/memcached -d -m 2048 -p 11211 -u root -c 2048 -x 192.168.37.17 -X 16000
#memcache-2
/usr/local/repcached/bin/memcached -d -m 2048 -p 11211 -u root -c 2048 -x 192.168.37.7 -X 16000

3、验证数据
[root@ha1 src]# telnet 192.168.37.7 11211
Trying 192.168.37.7...
Connected to 192.168.37.7.
Escape character is '^]'.
set name2 0 0 4     
mage
STORED
get name2
VALUE name2 0 4
mage
END
quit
Connection closed by foreign host.
[root@ha1 src]# telnet 192.168.37.17 11211
Trying 192.168.37.17...
Connected to 192.168.37.17.
Escape character is '^]'.
get name2
VALUE name2 0 4
mage
END
上一篇下一篇

猜你喜欢

热点阅读