Redis-cluster configuration

cluster

  • Redis has three cluster modes

  • master-slave mode

  • Sentinel mode

  • Cluster mode

  • Disadvantages of master-slave replication

Once the master node is down and the write service is unavailable, you need to manually switch, re-select the master node, and manually set the master-slave relationship.

  • Disadvantages of Sentry Mode

  • When the master hangs up, Sentinel will elect a master. During the election, there is no way to access Redis, and there will be a momentary access interruption;

  • In sentinel mode, only the master node can write externally, and the slave node can only be used for reading. Although a single Redis node supports a maximum QPS of 10W, the pressure to write data is all on the master during e-commerce promotions.

  • The single-node memory of Redis cannot be set too large. If the data is too large, the master-slave synchronization will be very slow; when the node starts, the time is particularly long;

①, Redis master-slave replication

  • What is master-slave replication

Master-slave replication refers to copying the data of one Redis server to other Redis servers. The former is called the master node (master), and the latter is called the slave node (slave). Data replication is one-way, and can only be from the master node to the slave node.

  • The role of master-slave replication
  1. Data redundancy: master-slave replication implements hot backup of data, which is a data redundancy method other than persistence.
  2. Fault recovery: When there is a problem with the master node, the slave node can provide services to achieve rapid fault recovery; it is actually a kind of service redundancy.
  3. Load balancing: On the basis of master-slave replication, combined with read-write separation, the master node can provide write services, and the slave nodes can provide read services (that is, the application connects to the master node when writing Redis data, and the application connects to the slave node when reading Redis data) , to share the server load; especially in the scenario of writing less and reading more, sharing the read load through multiple slave nodes can greatly increase the concurrency of the Redis server.
  4. The cornerstone of high availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters, so master-slave replication is the basis for high availability of Redis.
  • Master-slave replication can be divided into 3 stages
  1. Connection establishment phase (ie preparation phase)
  2. Data Synchronization Phase
  3. Command Propagation Phase
  • Master-slave synchronization strategy

When the master-slave is just connected, full synchronization is performed; after full synchronization, incremental synchronization is performed. Of course, if necessary, the slave can initiate full synchronization at any time. The redis strategy is, no matter what, it will first try to perform incremental synchronization. If it is unsuccessful, the slave machine is required to perform full synchronization.

stand-alone configuration

①, write the configuration file

New redis6379.conf

# 修改配置文件
vim redis6379.conf

# 修改内容
include /usr/local/redis-6.2.6/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb

New redis6380.conf

# 修改配置文件
vim redis6379.conf

# 修改内容
include /usr/local/redis-6.2.6/redis.conf
pidfile /var/run/redis_6380.pid
port 6380
dbfilename dump6380.rdb

New redis6381.conf

# 修改配置文件
vim redis6379.conf

# 修改内容
include /usr/local/redis-6.2.6/redis.conf
pidfile /var/run/redis_6381.pid
port 6381
dbfilename dump6381.rdb

②, start

# 分别启动三台redis
./redis-server ../redis6379.conf
./redis-server ../redis6380.conf
./redis-server ../redis6381.conf

③, from the library settings

Configure the slave library but not the master library

# slaveof <ip> <port> - ip: 主库地址,port: 主库端口
SLAVEOF 127.0.0.1 6379

④, check the master-slave status

View status information after executing slaveof

# 查看连接信息
info replication

②, Redis sentinel mode

  • master-slave switching technology

When the master server is down, it is necessary to manually switch a slave server to the master server, which requires manual intervention, which is laborious and will cause the service to be unavailable for a period of time. This is not a recommended way, more often, we give priority to sentinel mode.

  • Sentinel overview

Sentinel mode is a special mode. First, Redis provides sentinel commands. Sentinel is an independent process. As a process, it will run independently. The principle is that Sentinel monitors multiple running Redis instances by sending commands and waiting for the Redis server to respond.

  • Sentinel role
  1. Cluster monitoring: responsible for monitoring whether the redis master and slave processes are working normally
  2. Message notification: If a redis instance fails, the sentinel is responsible for sending a message as an alarm notification to the administrator
  3. Failover: If the master node hangs up, it will automatically transfer to the slave node
  4. Configuration Center: If a failover occurs, notify the client of the new master address

sentinel building

①, create a sentinel

Create a new sentinel-26379.conf file

Other clients also need to configure

  • parameter:
  1. sentinel monitor mymaster 192.168.92.128 6379 2 The meaning of the configuration is: the sentinel node monitors the master node 192.168.92.128:6379, the name of the master node is mymaster, and the meaning of the last 2 is related to the failure judgment of the master node: at least 2 Sentinel nodes agree to determine the failure of the primary node and perform failover.

    modify file

    vim sentinel-26379.conf

    Modify content

    port

    port 26379

    daemon running

    daemonize yes

    log file

    logfile “26379.log”

    Monitored master node name (mymaster) master node ip(192.168.66.11) master node port(6379) half of total nodes+1(2)

    sentinel monitor mymaster 192.168.66.11 6379 2

②, start the sentinel

# 启动哨兵
cd /.../src
./redis-sentinel ../sentinel-26379.conf

# 查看状态
./redis-cli -p 26379
info sentinel

③, Cluster mode

Redis cluster is a distributed service cluster composed of multiple master-slave node groups, which has the characteristics of replication, high availability and fragmentation. Redis cluster construction requires at least 3 master nodes. Here we build 3 master nodes, each with a slave node attached to it, for a total of 6 Redis nodes;

Cluster configuration

①, configure the Cluster file

  • Modify the following contents of the redis.conf file [All clients must be configured]

    Modify the following contents of the redis.conf file

    vim /…/redis.conf

    Modify content

    port 8001
    daemonize yes
    pidfile “/var/run/redis_8001.pid” .

    Specify the storage location of the data file, you must specify a different directory location, otherwise the data will be lost

    dir /usr/local/redis/redis-cluster/8001/

    Start cluster mode

    cluster-enabled yes

    Cluster node information file, where 800x is best to correspond to the port

    cluster-config-file nodes-8001.conf

    Timeout for node offline

    cluster-node-timeout 5000

    Remove bind binding to access ip information

    bind 127.0.0.1

    Disable protected mode

    protected-mode no

    Start AOF file

    appendonly yes

    If you want to set a password, you need to add the following configuration:

    Set redis access password

    requirepass itxiong

    Set the access password between cluster nodes, consistent with the above

    masterauth itxiong

②, create a cluster

  • parameter:
  1. -a:password

  2. –cluster-replicas:

    • –cluster-replicas 1: Indicates that 1 slave is attached to 1 master;
    • –cluster-replicas 2: Indicates that 2 slaves are attached to 1 master.

    start up

    /usr/local/redis/src/redis-cli -a redis-pw –
    cluster create --cluster-replicas 1 192.168.66.101:8001 192.168.66.101:8002 192.168.66.102:8001 192.168.66.102:8002 192.168.66.103:8001 192.168.66.103:8002

③, view the help command

  • parameter:
  1. create: create a cluster environment host1:port1 ... hostN:portN

  2. call: can execute redis command

  3. add-node: Add a node to the cluster, the first parameter is the ip:port of the new node, and the second parameter is the ip:port of any existing node in the cluster

  4. del-node: remove a node

  5. reshard: reshard

  6. check: Check the status of the cluster

    view help

    src/redis‐cli --cluster help

④, verify the cluster

connect to any client

  • parameter:
  1. ‐a indicates the server password

  2. -c means cluster mode

  3. -h specifies the ip address

  4. -p indicates the port number

    connect client

    /usr/local/redisd/src/redis-cli -a redis-pw -c -h 192.168.66.101 -p 8001

⑤. View the information of the cluster

cluster info

Guess you like

Origin blog.csdn.net/qq_56571862/article/details/128904404