Centos7 installs Redis stand-alone, master-slave, sentinel, cluster practice

1. Record of single machine installation steps:

​
1、yum install wget

2、cd ~

3、mkdir /data/software(可创建其他目录)

4、cd /data/software

5、wget https://download.redis.io/releases/redis-6.2.6.tar.gz

6、tar xf redis-6.2.6.tar.gz

7、cd redis-6.2.6

8、vi README.md(查看README提供具体安装方法)

9、make

---yum install gcc

---make distclean

10、make

11、cd src

--- 可以看到生成了可执行程序

12、cd ..

13、指定安装路径(一般放在/usr/local/redis)

make install PREFIX=/usr/local/redis

启动测试:cd /usr/local/redis/bin

./redis-server(此时只是控制台启动,不是后台启动)

14、把源文件配置文件复制到/usr/local/redis/bin

cp /data/software/redis-6.2.6/redis.conf /usr/local/redis/bin

cd /usr/local/redis/bin

vi redis.conf修改配置文件属性daemonize

daemonize no -> daemonize yes

后台启动测试:./redis-server redis.conf

开启远程连接:找到bind 127.0.0.1 -::1,并注释掉

启用密码: 找到# requirepass foobared,把注释去掉foobared修改为自己的密码

requirepass 123456

15、vi /etc/profile(配置redis环境变量)

export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin

16、cd utils

17、./install_server.sh (可执行一次或多次)

  a)、一个物理机中可以有多个redis实例(进程),通过port区分

  b)、可执行程序就一份在目录,但是内存中未来的多个实例需要各自的配置文件,持久化目录等资源

  c)、service redis_6379 start/stop/status >   linux init.d(会出现实例启动文件)

  d)、脚本会帮助启动

18、查看redis进程

ps -fe|grep redis

​

2. Integrated Bloom filter

1. Visit redis.io

2. module integration

3. Visit RedisBloom's github

https://github.com/RedisBloom/RedisBloom/archive/refs/heads/master.zip

4, wget master.zip in linux

5、yum install unzip

6、unzip master.zip

7、make

8、cp bloom.so /usr/local/redis/

9、redis-server --loadmodule /usr/local/redis/redisbloom.so

10、redis-cli

11. bf.add ooxx abc (add value to Bloom filter)

        bf.exits abc (judging whether the value exists)

        bf.exits cde

12. cf.add #cuckoo filter

3. Master-slave replication cluster construction

1. Master-slave planning, first prepare three redis instances, such as port numbers 6380, 6381, 6382, all operations are performed on a virtual machine

1), enter the redis source folder utils folder, execute ./install_server.sh to create three redis instances

cd /data/software/redis-6.2.6/utils
# 创建其他redis实例(输入实例的端口号、配置文件放置地址、数据持久化地址)
./install_server.sh
# 配置文件放置在/data/software/test,分别为6380.conf,6381.conf,6382.conf

2) After the redis instance is created, modify the configuration file settings, close the background execution, and view the master-slave replication results conveniently

vim /data/software/test/6380.conf
# 找到 daemonize,设置成no
daemonize no

3) After modifying the configuration, three redis nodes need to be restarted. Here, 6382 is specified as the master node, 6380 and 6381 are respectively slave nodes

# 主节点启动
redis-server /data/software/test/6382.conf
# 从节点启动,启动时配置跟随主节点
redis-server /data/software/test/6380.conf --replicof 127.0.0.1 6382
redis-server /data/software/test/6381.conf --replicof 127.0.0.1 6382

In this way, both master and slave replication nodes are started, the master node can write exclusively, and the slave node can only read

4) If the master node hangs up, you need to switch the master node again. Assuming that 6382 is down, we set 6380 as the master node (when the slave node becomes the master node, other slave nodes will not automatically switch the master node, and need to be completely manual)

# 从节点变成主节点命令 replicaof no one
#实际操作
[root@localhost test]# redis-cli -p 6380
127.0.0.1:6380> replicaof no one

# 把6381指向的主节点切换到6380上(原来指向6382)
redis-cli -p 6281 --replicaof 127.0.0.1 6380
#实际操作
[root@localhost ~]# redis-cli -p 6281 --replicaof 127.0.0.1 6380

# 若6382节点此时重新启动需要指向6380主节点
redis-server ./6382.conf --replicof 127.0.0.1 6380
#实际操作
[root@localhost test]# redis-server ./6382.conf --replicof 127.0.0.1 6380

2. Proxy can be used to shield cluster nodes: tewemproxy, predixy (higher performance)

4. Sentinel cluster creation (convenient for master-slave high availability)

1. Still in the /data/software/test folder, create new 26380.conf, 26381.conf, 26382.conf sentinel configuration files

Configuration file content:

###########################26380.conf 配置如下 #############################
port 26380
# 监控6380 权重为2,主服务器判断为失效至少需要 2 个 Sentinel 同意
sentinel monitor mymaster 127.0.0.1 6380 2

###########################26381.conf 配置如下 #############################
port 26380
# 监控6380 权重为2,主服务器判断为失效至少需要 2 个 Sentinel 同意
sentinel monitor mymaster 127.0.0.1 6380 2

###########################26382.conf 配置如下 #############################
port 26380
# 监控6380 权重为2,主服务器判断为失效至少需要 2 个 Sentinel 同意
sentinel monitor mymaster 127.0.0.1 6380 2

2. Start three sentinel nodes respectively

redis-server /data/software/test/26380.conf --sentinel
redis-server /data/software/test/26381.conf --sentinel
redis-server /data/software/test/26382.conf --sentinel

After the startup is complete, you can test it. Manually stop the master node process to see if Sentinel will automatically switch the master node. When the original master node restarts, Sentinel will automatically switch it to a slave node and follow the new master node.

Five, cluster (cluster)

1. The planned cluster scale is three masters and three slaves, and the port numbers are 7001, 7002, 7003, 7004, 7006, and 7006

Use the install_server.sh command to instantiate the node. You can refer to the "master-slave replication" instantiation method. The configuration file is placed in

/data/software/cluster, modify 6 node configuration files

# cluster集群配置,打开集群模式(默认是单机)
cluster-enabled yes
# 设定节点配置文件名
cluster-config-file  nodes-7001.conf
# 设定节点失联时间,超过该时间(毫秒),集群自动进行主从切换。
cluster-node-timeout 15000

2. Create a cluster

# 指定从节点数量: --cluster-replicas 1
redis-cli --cluster create 127.0.0.1:7001  127.0.0.1:7002  127.0.0.1:7003  127.0.0.1:7004  127.0.0.1:7005  127.0.0.1:7006  --cluster-replicas 1

Note: The cluster requires at least 3 masters. If each slave node is configured with a master node, 6 nodes must be required.

3. Add master-slave nodes

1、添加主节点(127.0.0.1:7007为新增主节点,127.0.0.1:7000为集群中已存在的节点,可为集群中任一个已存在节点)
redis-cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7001
2、重新分配hash槽位(127.0.0.1:7001为需要转移的master节点)
redis-cli --cluster reshard  127.0.0.1:7001
2.1、输入要转移的槽位(0~16384)
2.2、输入需要接收槽位的master节点Id
2.3、输入需要转移槽位的集群master节点id
2.4、选择分配hash槽的来源,输入all 代表集群槽位平均分配,输入done 代表在指定节点拿出指定数量的hash槽位
3、添加从节点
redis-cli --cluster add-node --cluster-slave --cluster-master-id 3b1bf4b44ec0c67645cc558bbdf1b65a4076b41b 127.0.0.1:7008 127.0.0.1:7007
# cluster-master-id 后边跟 主节点id
# 127.0.0.1:7008 是新加的从节点
# 127.0.0.1:7007 作为从节点的主节点

4. Delete the master-slave node

1、移除从节点
# 从节点ip:端口号
# 从节点id
redis-cli --cluster del-node 127.0.0.1:7008 f95664b318ffd0a52792bff47dd329007bd2953a
2、移除主节点
注意:使用同样的方法移除主节点,不过在移除主节点前,需要确保这个主节点是空的. 如果不是空的,需要将这个节点的数据重新分片到其他主节点上.
替代移除主节点的方法是手动执行故障恢复,被移除的主节点会作为一个从节点存在,不过这种情况下不会减少集群节点的数量,也需要重新分片数据.
2.1 槽位转移
参考第三步槽位重新划分
2.2、删除主节点(同删除从节点命令)
redis-cli --cluster del-node 127.0.0.1:7007 3b1bf4b44ec0c67645cc558bbdf1b65a4076b41b

Guess you like

Origin blog.csdn.net/qq_21875331/article/details/120687460