前言
知名网站建设定制对于一个初学者来说,知名网站建设定制心里一直有一个疑惑,知名网站建设定制主从复制不就是集群嘛(一主多从),知名网站建设定制干嘛还要搭建集群。知名网站建设定制主从复制和集群又是什么关系呢?知名网站建设定制接下来就让我来带大家知名网站建设定制一起了解一下!
主从复制
主从复制 的模式一般是 一主多从
,也就意味着 写 的速度大约在 10W次 / S , 而 读 的速度是根据从机数量来决定的, 也就是 10W次 * 从机数量 / S (有的小伙伴会想, 如果服务器配置好一些, 会不会读写快一些,这里我想说的是, 读写熟读和CPU 无关, 只和内存有关, 具体的可以看我的另外一篇文章:) 。 这种读写速度对于一般中小型的公司是完全够用的。但是在这里请大家思考一个问题, 我们都知道主从复制
, 只有主(Master) 服务器才具备 写 的功能, 从 服务器是只有读的功能的。 想象一下, 像 BAT 这种大公司, 10W / 秒 写 速度够用吗?答案肯定是不够用的,那么我们就想, 能不能有多台 主(Master) 服务器呢,也就是 多主多从 ,就这样 Redis 集群出现了。Redis 集群很好的解决了 写 不够的问题。
一主多从 还存在另外一个问题。使用 一主多从 多数会使用哨兵模式, 当 master 服务器 “宕机”之后,Sentinel 会有一个主观下线的问题。(sentinel down-after-milliseconds
参数就是指定多少毫秒之后 主节点没有应答哨兵sentinel , 此时哨兵主观上认为主节点下线 默认30秒)也就意味着,在主节点宕机之后到选举产生新的节点之前, Redis至少存在30 秒的不可用状态, 虽然可以将数值调小,但是在某些应用场景这是无法容忍的。 而 Redis 集群就很好的解决了这种问题。
Redis集群
搭建Redis集群
准备搭建一个 三主三从
的Redis集群。 注:Redis 集群是 Redis 3.0 版本以后才有的功能, 3.0 以前的版本是没有的 。
环境准备
- 三台云服务器(106.14.157.48,49.232.112.117,101.34.253.57)。大家也可以使用一台云服务器,部署多个Redis 实例也是可以的。 我这里是使用三台云服务器。想购买的小伙伴可以看下,挺便宜的
- 三台服务器都安装上 Redis(不会安装的小伙伴可以看我另外一篇文章: )
- Redis 5.0 及以上版本(本次演示使用
redis-cli
命令创建集群, 不在使用ruby了)
1、创建配置文件
首先在每台服务器的 /usr/local/
路径下创建 redis-cluster 文件夹, 然后在 redis-cluster 文件夹在创建 master , slave 文件夹,并且把 /opt/redis-6.2.5/redis.conf
配置文件在每台服务器的 master ,slave 文件夹中复制一份。
[root@TR ~]# cd /usr/local/[root@TR local]# mkdir redis-cluster[root@TR local]# cd redis-cluster[root@TR redis-cluster]# mkdir master[root@TR redis-cluster]# mkdir slave[root@TR redis-cluster]# lltotal 0drwxr-xr-x 2 root root 6 Oct 16 20:36 masterdrwxr-xr-x 2 root root 6 Oct 16 20:37 slave# 复制配置文件[root@TR ~]# cd /opt/redis-6.2.5[root@TR redis-6.2.5]# cp redis.conf /usr/local/redis-cluster/master/[root@TR redis-6.2.5]# cp redis.conf /usr/local/redis-cluster/slave/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2、修改配置文件
- 守护进程运行
daemonize yes
- 将6个配置文件中的端口一次修改成
port 6379 、port 6380 、port 6381、 port 6382 、port 6383、port 6384
- 修改
bind
绑定的地址, 改成对应服务器的 ip 即可(注:这里的 ip 不是云服务器的Ip , 需要填写eth0
网卡的ip) - 修改数据目录
dir
, 改成redis.conf
配置文件的位置即可, 例如:dir /usr/local/redis-cluster/master
- 启动集群
cluster-enabled yes
- 修改
cluster-config-file nodes-6379.conf
(这里的 nodes-63XX.conf最好和port 对应上) - cluster-node-timeout 5000
- appendonly yes
3、启动6个Redis实例
# 106.14.157.48 服务器redis-server /usr/local/redis-cluster/master/redis.confredis-server /usr/local/redis-cluster/slave/redis.conf# 49.232.112.117 服务器redis-server /usr/local/redis-cluster/master/redis.confredis-server /usr/local/redis-cluster/slave/redis.conf# 101.34.253.57 服务器redis-server /usr/local/redis-cluster/master/redis.confredis-server /usr/local/redis-cluster/slave/redis.conf# 查询redis 运行情况 , 这里只有两个redis 实例,是因为这是查询一台服务器的原因[root@TR ~]# ps -ef | grep redisroot 109791 1 0 18:24 ? 00:00:00 redis-server 10.0.4.7:6383 [cluster]root 109805 1 0 18:24 ? 00:00:00 redis-server 10.0.4.7:6384 [cluster]root 109945 104144 0 18:25 pts/0 00:00:00 grep --color=auto redis[root@TR ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
4、启动集群
启动集群的时候,如果遇到 Waiting for the cluster to join
的字样, 请看我的另外一篇文章:
# 1. 执行启动集群的命令# 命令最后一个参数 ‘1’ 的意思是 “一主一从”的配置。是通过计算得来的, 计算方式为: slave 总数量 / master 总数量[root@TR src]# redis-cli --cluster create 106.14.157.48:6379 49.232.112.117:6381 101.34.253.57:6383 106.14.157.48:6380 49.232.112.117:6382 101.34.253.57:6384 --cluster-replicas 1>>> Performing hash slots allocation on 6 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica 49.232.112.117:6382 to 106.14.157.48:6379Adding replica 101.34.253.57:6384 to 49.232.112.117:6381Adding replica 106.14.157.48:6380 to 101.34.253.57:6383M: 2d9ab9c1c95c13478758ae6ec45434fdb620688c 106.14.157.48:6379 slots:[0-5460] (5461 slots) masterM: edd1856f57ffbb50ee5ce690964b5e780118608e 49.232.112.117:6381 slots:[5461-10922] (5462 slots) masterM: 5998783763f617cc7ff40dd2d55ad0cbce72ea66 101.34.253.57:6383 slots:[10923-16383] (5461 slots) masterS: f0fdd2cd69c1e37ff510cf990ae381005609fc81 106.14.157.48:6380 replicates 5998783763f617cc7ff40dd2d55ad0cbce72ea66S: b2893c8a996c726264096a49b84df367509b9ceb 49.232.112.117:6382 replicates 2d9ab9c1c95c13478758ae6ec45434fdb620688cS: 560ef0cd7c6799a12a75c930cb0063cc2d613cb4 101.34.253.57:6384 replicates edd1856f57ffbb50ee5ce690964b5e780118608eCan 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 clusterWaiting for the cluster to join>>> Performing Cluster Check (using node 106.14.157.48:6379)M: 2d9ab9c1c95c13478758ae6ec45434fdb620688c 106.14.157.48:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s)M: edd1856f57ffbb50ee5ce690964b5e780118608e 49.232.112.117:6381 slots:[5461-10922] (5462 slots) master 1 additional replica(s)S: f0fdd2cd69c1e37ff510cf990ae381005609fc81 106.14.157.48:6380 slots: (0 slots) slave replicates 5998783763f617cc7ff40dd2d55ad0cbce72ea66S: b2893c8a996c726264096a49b84df367509b9ceb 49.232.112.117:6382 slots: (0 slots) slave replicates 2d9ab9c1c95c13478758ae6ec45434fdb620688cM: 5998783763f617cc7ff40dd2d55ad0cbce72ea66 101.34.253.57:6383 slots:[10923-16383] (5461 slots) master 1 additional replica(s)S: 560ef0cd7c6799a12a75c930cb0063cc2d613cb4 101.34.253.57:6384 slots: (0 slots) slave replicates edd1856f57ffbb50ee5ce690964b5e780118608e[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.[root@TR src]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
到这里集群就搭建完成了。现在简单的来测试一下。
# 连接集群# 参数解释:# -c: cluster集群的意思# -h: host 主机的意思# -p: port 端口的意思[root@TR redis-6.2.5]# redis-cli -c -h 106.14.157.48 -p 6379106.14.157.48:6379> set name "zhangsan"-> Redirected to slot [5798] located at 49.232.112.117:6381OK49.232.112.117:6381> get name"zhangsan"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
测试二: 将 6379的master主机“宕机”后, 发现 6379 的slave 服务器的角色编程了master
集群其余常用命令
# 查询集群的状态cluster info # 查看集群节点列表cluster nodes
- 1
- 2
- 3
- 4
- 5