不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • MySQL

  • Redis

    • 01.Redis安装
    • 02.Redis基础
    • 03.Redis底层存储 ✅
    • 04.各种数据类型操作
    • 05.Redis连接和基本操作
    • 06.Redis线程模型 ✅
    • 07.Redis持久化 ✅
    • 08.Redis集群 ✅
    • 09.雪崩穿透击穿
    • 10.Redis锁代码
    • 11.Redis缓存
    • 12.部署Redis主从
    • 13.Redis的cluster配置
      • 01.机器配置
      • 02.192.168.56.65配置
        • 2.1 7000.conf
        • 2.2 7001.conf
        • 2.3 7002.conf
        • 2.4 启动Redis服务
      • 03.192.168.56.66配置
        • 2.1 7003.conf
        • 2.2 7004.conf
        • 2.3 7005.conf
        • 2.4 启动Redis服务
      • 04.创建集群
        • 4.1 创建集群
        • 4.2 查看集群创建结果
        • 4.3 数据验证
      • 05.Python交互
        • 5.1 安装Redis-py-cluster
        • 5.2 python使用Redis集群
      • 06.django中使用
        • 6.1 Requirement
        • 6.2 How to install
        • 6.3 Django Settings
    • 14.Redis哨兵模式
    • 15.Redis淘汰策略 ✅
    • 16.CAP ✅
  • Elasticsearch

  • Kafka

  • Etcd

  • MongoDB

  • TiDB

  • RabbitMQ

  • 数据库
  • Redis
xiaonaiqiang
2021-02-22
目录
01.机器配置
02.192.168.56.65配置
2.1 7000.conf
2.2 7001.conf
2.3 7002.conf
2.4 启动Redis服务
03.192.168.56.66配置
2.1 7003.conf
2.2 7004.conf
2.3 7005.conf
2.4 启动Redis服务
04.创建集群
4.1 创建集群
4.2 查看集群创建结果
4.3 数据验证
05.Python交互
5.1 安装Redis-py-cluster
5.2 python使用Redis集群
06.django中使用
6.1 Requirement
6.2 How to install
6.3 Django Settings

13.Redis的cluster配置

# 01.机器配置

  • Redis各种集群模式 (opens new window)

  • VMware版本号:16.0.0

  • 在两台机器上分别安装好Redis服务

  • 安装Redis (opens new window)

系统 IP 运行实例端口 Redis版本
CentOS 7.5 192.168.56.65 7000
7001
7002
Redis-5.0.3
CentOS 7.5 192.168.56.66 7000
7001
7002
Redis-5.0.3

# 02.192.168.56.65配置

  • 创建conf配置文件夹
  • 三个⽂件的配置区别在port、pidfile、cluster-config-file三项
[root@k8s-node1 ~]# mkdir /root/conf/
1

# 2.1 7000.conf

[root@k8s-node1 ~]# vim /root/conf/7000.conf
port 7000
bind 192.168.56.65
daemonize yes
pidfile 7000.pid
cluster-enabled yes
cluster-config-file 7000_node.conf
cluster-node-timeout 15000
appendonly yes
1
2
3
4
5
6
7
8
9

# 2.2 7001.conf

[root@k8s-node1 ~]# vim /root/conf/7001.conf
port 7001
bind 192.168.56.65
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
1
2
3
4
5
6
7
8
9

# 2.3 7002.conf

[root@k8s-node1 ~]# vim /root/conf/7002.conf
port 7002
bind 192.168.56.65
daemonize yes
pidfile 7002.pid
cluster-enabled yes
cluster-config-file 7002_node.conf
cluster-node-timeout 15000
appendonly yes
1
2
3
4
5
6
7
8
9

# 2.4 启动Redis服务

[root@k8s-node1 conf]# Redis-server 7000.conf
[root@k8s-node1 conf]# Redis-server 7001.conf
[root@k8s-node1 conf]# Redis-server 7002.conf
1
2
3

# 03.192.168.56.66配置

  • 创建conf配置文件夹
  • 三个⽂件的配置区别在port、pidfile、cluster-config-file三项
[root@k8s-node1 ~]# mkdir /root/conf/
1

# 2.1 7003.conf

[root@k8s-node1 ~]# vim /root/conf/7003.conf
port 7003
bind 192.168.56.66
daemonize yes
pidfile 7003.pid
cluster-enabled yes
cluster-config-file 7003_node.conf
cluster-node-timeout 15000
appendonly yes
1
2
3
4
5
6
7
8
9

# 2.2 7004.conf

[root@k8s-node1 ~]# vim /root/conf/7004.conf
port 7004
bind 192.168.56.66
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes
1
2
3
4
5
6
7
8
9

# 2.3 7005.conf

[root@k8s-node1 ~]# vim /root/conf/7005.conf
port 7005
bind 192.168.56.66
daemonize yes
pidfile 7005.pid
cluster-enabled yes
cluster-config-file 7005_node.conf
cluster-node-timeout 15000
appendonly yes
1
2
3
4
5
6
7
8
9

# 2.4 启动Redis服务

[root@k8s-node1 conf]# Redis-server 7003.conf
[root@k8s-node1 conf]# Redis-server 7004.conf
[root@k8s-node1 conf]# Redis-server 7005.conf
1
2
3

# 04.创建集群

# 4.1 创建集群

  • Redis-trib.rb是官方提供的Redis Cluster的管理工具,无需额外下载,默认位于源码包的src目录下
'''将Redis-trib.rb复制到/usr/local/bin/下,以便在任何⽬录下调⽤此命令'''
[root@k8s-node1 conf]# cp /root/Redis-5.0.3/src/Redis-trib.rb  /usr/local/bin/
1
2
  • 但因该工具是用ruby开发的,所以需要准备相关的依赖环境
[root@k8s-node2 src]# yum -y install ruby
1
  • 运⾏如下命令创建集群
[root@k8s-node2 src]# Redis-cli --cluster create 192.168.56.65:7000 192.168.56.65:7001 192.168.56.65:7002 192.168.56.66:7003 192.168.56.66:7004 192.168.56.66:7005 --cluster-replicas 1
1

# 4.2 查看集群创建结果

  • 根据下图可以看出,当前搭建的主服务器为7000、7001、7003,对应的从服务器是7004、7005、7002

# 4.3 数据验证

  • 在7002上写入数据,会自动将操作转发给7003
  • 在7002和7003两台服务器中,7003是master,master能写入
[root@k8s-node1 conf]# Redis-cli -h 192.168.56.65 -c -p 7002
192.168.56.65:7002> set name tom
-> Redirected to slot [5798] located at 192.168.56.66:7003
OK
1
2
3
4
  • 在7003中可以看到刚刚在7002中写入的数据
[root@k8s-node1 ~]# Redis-cli -h 192.168.56.66 -c -p 7003
192.168.56.66:7003> get name
"tom"
1
2
3
  • 在7000客户端去查找刚刚的数据,其实当前Redis实例没有这条数据
  • 会将请求转发给了7003这个Redis进行查找
[root@k8s-node1 ~]# Redis-cli -h 192.168.56.65 -c -p 7000
192.168.56.65:7000> get name
-> Redirected to slot [5798] located at 192.168.56.66:7003
"tom"
1
2
3
4

# 05.Python交互

# 5.1 安装Redis-py-cluster

pip install Redis==2.10.6
pip install Redis-py-cluster==1.3.5
1
2
  • Redis-py-cluster源码地址https://github.com/Grokzen/Redis-py-cluster

# 5.2 python使用Redis集群

from Rediscluster import *
if __name__ == '__main__':
  try:
    # 构建所有的节点,Redis会使⽤CRC16算法,将键和值写到某个节点上
    startup_nodes = [
        {'host': '192.168.56.65', 'port': '7000'},
        {'host': '192.168.56.65', 'port': '7001'},
        {'host': '192.168.56.65', 'port': '7002'},
        {'host': '192.168.56.66', 'port': '7003'},
        {'host': '192.168.56.66', 'port': '7004'},
        {'host': '192.168.56.66', 'port': '7005'},
    ]
    # 构建StrictRedisCluster对象
    src=StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=True)
    # 设置键为name、值为itheima的数据
    result=src.set('name','itheima')
    print(result)
    # 获取键为name
    name = src.get('name')
    print(name)
  except Exception as e:
    print(e)
'''
打印结果:
True
itheima
'''
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

# 06.django中使用

  • 参考 (opens new window)

# 6.1 Requirement

Python 3.0+

Django>=1.9.6

Redis>=2.10.5

Redis-py-cluster>=1.2.0

msgpack-python>=0.4.7

# 6.2 How to install

$python setup.py install
or
$pip install django-Redis-cluster
1
2
3

# 6.3 Django Settings

CACHES = {
    'default': {
        'BACKEND': 'django_Redis_cluster.cache.RedisClusterCache',
        "LOCATION": [
            "Redis://127.0.0.1:7000/0",
            "Redis://127.0.0.1:7001/0",
            "Redis://127.0.0.1:7002/0",
        ],
        'OPTIONS': {
            "CLIENT_CLASS": "django_Redis_cluster.client.DefaultClient",
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
12.部署Redis主从
14.Redis哨兵模式

← 12.部署Redis主从 14.Redis哨兵模式→

最近更新
01
04.数组双指针排序_子数组
03-25
02
08.动态规划
03-25
03
06.回溯算法
03-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式