Redis单机安装和哨兵模式集群安装怎么实现

1、Redis单机版安装1.1:下载安装包cd /usr/local/src/
wget https://download.redis.io/releases/redis-5.0.13.tar.gz

看到redis-5.0.13.tar.gz 文件就说明下载成功了

1.2:解压安装包tar -zxvf redis-5.0.13.tar.gz

解压完成后进入解压后的文件夹会看到如下的文件内容

cd redis-5.0.13
ll

-rw-rw-r--. 1 root root 125833 Jul 22 02:07 00-RELEASENOTES
-rw-rw-r--. 1 root root 53 Jul 22 02:07 BUGS
-rw-rw-r--. 1 root root 2381 Jul 22 02:07 CONTRIBUTING
-rw-rw-r--. 1 root root 1487 Jul 22 02:07 COPYING
drwxrwxr-x. 6 root root 124 Jul 22 02:07 deps
-rw-rw-r--. 1 root root 11 Jul 22 02:07 INSTALL
-rw-rw-r--. 1 root root 151 Jul 22 02:07 Makefile
-rw-rw-r--. 1 root root 6888 Jul 22 02:07 MANIFESTO
-rw-rw-r--. 1 root root 20555 Jul 22 02:07 README.md
-rw-rw-r--. 1 root root 63088 Jul 22 02:07 redis.conf
-rwxrwxr-x. 1 root root 275 Jul 22 02:07 runtest
-rwxrwxr-x. 1 root root 280 Jul 22 02:07 runtest-cluster
-rwxrwxr-x. 1 root root 373 Jul 22 02:07 runtest-moduleapi
-rwxrwxr-x. 1 root root 281 Jul 22 02:07 runtest-sentinel
-rw-rw-r--. 1 root root 9710 Jul 22 02:07 sentinel.conf
drwxrwxr-x. 3 root root 4096 Jul 22 02:07 src
drwxrwxr-x. 11 root root 182 Jul 22 02:07 tests
drwxrwxr-x. 8 root root 4096 Jul 22 02:07 utils 1.3:执行make命令,编译redis代码make

一般会出现一下错误,不要慌,由于 redis 是用 C 语言开发,安装之前必先确认是否安装 gcc 环境(gcc -v),如果没有安装,执行以下命令进行安装 yum install -y gcc

Redis单机安装和哨兵模式集群安装详解

gcc环境安装成功后,执行 make distclean 清楚之间的make记录,然后重新 make

1.4:安装并指定安装目录make install PREFIX=/usr/local/redis 1.5:配置环境变量vi /etc/profile
#在最后面加上以下内容
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
#使环境变量生效
source /etc/profile 1.6:让redis作为服务进行后台启动

1.6.1:从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录

cp /usr/local/src/redis-5.0.13/redis.conf /usr/local/redis/bin/

1.6.2:修改 redis.conf 文件,把 daemonize no 改为 daemonize yes,使redis作为守护线程启动

cd /usr/local/redis/bin/
vi redis.conf

1.6.3:后台启动,并且链接redis测试, 执行一下命令启动redis

redis-server /usr/local/redis/bin/redis.conf

然后执行 redis-cli 连接redis,然后set get 数据测试

至此,单机版redis已经安装成功了!!!

1.7:redis关闭服务redis-cli shutdown 2、Redis 集群版安装

说明本文的redis集群版本,使用的是主从复制的集群版本安装

2.1:基本的集群拓扑图如下

2.2:修改redis-sentinel的配置文件 sentinel.conf#sentinel-1 配置文件修改内容
port 26379
sentinel monitor mymaster 192.168.33.100 6379 2

#sentinel-2 配置文件修改内容
port 26380
sentinel monitor mymaster 192.168.33.100 6379 2

#sentinel-3 配置文件修改内容
port 26380
sentinel monitor mymaster 192.168.33.100 6379 2

sentinel monitor <
master-name>
<
ip>
<
redis-port>
<
quorum>
配置文件说明:该配置是告诉sentinel去监听地址为ip:port的一个master,这里的master-name可以自定义,quorum是一个数字,指明当有多少个sentinel认为一个master失效时,master才算真正失效。master-name只能包含英文字母,数字,和“.-_”这三个字符需要注意的是master-ip 要写真实的ip地址而不要用回环地址(127.0.0.1)。

2.3:修改redis-server的配置文件 redis.conf

# redis-1 的 redis.conf配置
port 6379

# redis-2 的 redis.conf配置
port 6380
replicaof 127.0.0.1 6379

# redis-3 的 redis.conf配置
port 6381
replicaof 127.0.0.1 6379 2.4:分别启动 redis-1,redis-2,redis-3 服务2.5:分别启动 sentinel-1, sentinel-2, sentinel-3cd /usr/local/redis/bin
redis-sentinel sentinel.conf 2.6:验证集群效果

2.6.1:连接到主redis服务,查看集群情况,可以看到有当前redis为master,其他两台redis服务为slave

redis-cli -h 192.168.33.100 -p 6379
info replication
#得到以下内容
role:master
connected_slaves:2
slave0:ip=192.168.33.101,port=6380,state=online,offset=246573,lag=1
slave1:ip=192.168.33.102,port=6381,state=online,offset=246706,lag=0
master_replid:bc4a9331019d765fe16199fcdcb357b4681a9f75
master_replid2:b41587d209ec96c2b2c634c1880fed7b12ded6e8
master_repl_offset:246839
second_repl_offset:221755
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:9553
repl_backlog_histlen:237287

2.6.2:停止redis主服务

redis-cli -h 192.168.33.100 -p 6379 shutdown

2.6.3:查看sentinel服务的日志,可以看到redis主服务宕机,sentinel服务主动切换6381为主redis服务

2.6.4:再次启动6379端口 redis 服务查看集群情况,可以看到,6379已经变为从服务了



Redis作为一种高性能的NoSQL数据库,被广泛应用于各行各业。但在使用过程中,单机可能会存在单点故障的问题,因此有必要采用哨兵模式集群安装,本文将为大家详细介绍Redis单机安装和哨兵模式集群安装的实现方法。
一、Redis单机安装
1. 安装Redis
通过官网或其他第三方渠道下载并解压Redis安装包,使用make和make install命令进行安装。安装完成后,就可以启动Redis服务器。
2. 配置Redis
Redis默认配置文件名为redis.conf,在安装完成后需要对其进行修改:
①配置Redis的ip和端口号,如果需要支持多个客户端同时连接Redis,可以将端口号设置为6380、6381等。
②配置Redis的密码,避免其被外部攻击者获取。
3. 启动Redis
在完成Redis的配置后,使用Redis的bin目录下的redis-server命令即可启动Redis服务器。
二、哨兵模式集群安装
1. 配置Redis哨兵模式
Redis哨兵模式可以自动判断主节点的状态,当发现主节点宕机时,会自动选择备用节点中的一台作为主节点,从而保证了Redis数据的可靠性。
①启用哨兵模式需要在Redis配置文件中进行如下修改:
sentinel monitor / sentinel config-epoch sentinel leader-epoch sentinel known-slave sentinel known-sentinel sentinel current-epoch
其中,为主节点的名称(可以自定义),分别为主节点的IP和端口号,为包括主节点在内需达成共识的哨兵数,为配置版本号,为哨兵选举版本号。
②启动Redis哨兵模式
启动Redis哨兵模式需要使用redis-sentinel命令,该命令会启动一个哨兵进程并与其他哨兵进程协同工作,从而完成Redis哨兵模式的启动。
2. 配置Redis集群模式
Redis集群模式可以实现数据的高可用和自动分片,支持在线扩容和在线缩容。
①启用Redis集群需要在Redis配置文件中进行如下修改:
cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 15000 appendonly yes
其中,cluster-enabled为启用集群的开关,cluster-config-file指定了Redis集群的配置文件名称,cluster-node-timeout指定了集群节点之间心跳的超时时间,appendonly为Redis的AOF持久化方式。
②启动Redis集群模式
使用redis-trib.rb工具即可启动Redis集群模式。
三、Redis single-machine installation and sentinel mode cluster installation
Redis, as a high-performance NoSQL database, is widely used in various industries. However, in the process of use, there may be single point of failure in a single machine, so it is necessary to use the sentinel mode cluster installation. This article will introduce the implementation methods of Redis single-machine installation and sentinel mode cluster installation in detail.
I. Redis single machine installation
1. Install Redis
Download and unzip the Redis installation package through the official website or other third-party channels, and use the make and make install commands to install it. After the installation is completed, the Redis server can be started.
2. Configure Redis
The default configuration file name of Redis is redis.conf, and it needs to be modified after the installation is completed:
Configure the IP and port number of Redis. If you need to support multiple clients to connect to Redis at the same time, you can set the port number to 6380, 6381, etc.
Configure the password of Redis to prevent it from being obtained by external attackers.
3. Start Redis
After completing the configuration of Redis, use the redis-server command under the Redis bin directory to start the Redis server.
II. Sentinel mode cluster installation
1. Configure Redis sentinel mode
Redis sentinel mode can automatically determine the status of the main node. When it detects that the main node is down, it will automatically select one of the standby nodes as the main node, thereby ensuring the reliability of Redis data.
Enable the sentinel mode needs to be modified in the Redis configuration file:
sentinel monitor / sentinel config-epoch sentinel leader-epoch sentinel known-slave sentinel known-sentinel sentinel current-epoch
Among them, is the name of the main node (can be customized), and are the IP and port number of the main node, is the number of sentinels that need to reach a consensus including the master node, is the version number of the configuration, and is the sentinel election version number.
② Start Redis sentinel mode
Starting Redis sentinel mode requires the redis-sentinel command, which will start a sentinel process and work together with other sentinel processes to complete the startup of Redis sentinel mode.
2. Configure Redis cluster mode
Redis cluster mode can achieve high availability and automatic sharding of data, and supports online expansion and online contraction.
Enable Redis cluster needs to be modified in the Redis configuration file:
cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 15000 appendonly yes
Among them, cluster-enabled is the switch for enabling the cluster, cluster-config-file specifies the Redis cluster configuration file name, cluster-node-timeout specifies the timeout of the heartbeat between cluster nodes, and appendonly is Redis's AOF persistence method.
② Start Redis cluster mode
Redis cluster mode can be started using the redis-trib.rb tool.