安装系统要求
关于操作系统的选择,Centos7
下载Redis
wget https://download.redis.io/releases/redis-6.2.4.tar.gz
Redis安装(单机版)
- ####编绎如下
yum install -y gcc gcc-c++
tar -zxvf redis-6.2.4.tar.gz -C /usr/local/
cd /usr/local/redis-6.2.4/
make
单机版完成。
##有用小知识: 这里可以考虑将安装好的redis文件压缩备用,后续在其他的系统中安装时只需要解压就可,不需要再走一遍gcc,make的流程。 ##压缩 cd /usr/local tar -zcvf redis6.2.4_install.tar.gz redis-6.2.4/ ##比如在192.168.56.102上进行解压安装,执行解压命令即可,不需要再走make的编绎流程: tar -zxvf redis6.2.4_install.tar.gz -C /usr/local
- ####接下来做些简单的配置,毕竟默认的配置有些还是不太满足我们的意愿
####配置说明redis.conf
bind 127.0.0.1 -::1 ---> bind 192.168.56.101 -::1
修改端口:port 6379
后台启动: daemonize no ---> daemonize yes
Pid文件位置修改:pidfile /var/run/redis_6379.pid ---> pidfile "/usr/local/redis-6.2.4/redis_6379.pid"
默认工作目录:dir ./ ---> dir "/usr/local/redis-6.2.4/"
Logfile: logfile "" ---> logfile "/usr/local/redis-6.2.4/logs/redis.log"
加上密码: # requirepass foobared ---> requirepass 123456
上面的配置通过vi redis.conf可以编辑,也可以直接sed命令修改,直接通过sed命令修改如下:
####批量命令如下:
sed -i 's/^bind 127.0.0.1 -::1$/bind 192.168.56.101 -::1/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^daemonize no$/daemonize yes/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^pidfile \/var\/run\/redis_6379.pid$/pidfile "\/usr\/local\/redis-6.2.4\/redis_6379.pid"/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^dir .\/$/dir "\/usr\/local\/redis-6.2.4\/"/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^logfile ""$/logfile "\/usr\/local\/redis-6.2.4\/logs\/redis.log"/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^# requirepass foobared$/requirepass 123456/' /usr/local/redis-6.2.4/redis.conf
####建立logs目录,logfile存储的目录
mkdir /usr/local/redis-6.2.4/logs/ -p
- ####启动
###启动
cd /usr/local/redis-6.2.4/
src/redis-server redis.conf &
或
/usr/local/redis-6.2.4/src/redis-server /usr/local/redis-6.2.4/redis.conf
执行结果日志如下:
[root@localhost redis-6.2.4]# /usr/local/redis-6.2.4/src/redis-server /usr/local/redis-6.2.4/redis.conf
[root@localhost redis-6.2.4]# ps -ef|grep redis
root 8277 1 0 10:04 ? 00:00:00 /usr/local/redis-6.2.4/src/redis-server *:6379
root 8283 3663 0 10:04 pts/0 00:00:00 grep --color=auto redis
Redis安装(哨兵)
Sentinel会不断地检查你的主服务器和从服务器是否运作正常。 当被监控的某个Redis服务器出现问题时,Sentinel可以通过API向管理员或者其他应用程序发送通知。 当一个主服务器不能正常工作时,Sentinel会开始一次自动故障迁移操作,它会将失效主服务器的其中一个从服务器升级为新的主服务器,并让失效服务器的其他从服务器改为新的主服务器;当客户端试图连接失效的主服务器时,集群也会向客户端返回新主服务器的地址,使得集群可以使用新主服务器代替失效服务器。
- ####安装环境
| IP | 操作系统 | 初始角色 |
|---|---|---|
| 192.168.56.101 | Centos7 | master |
| 192.168.56.102 | Centos7 | slave |
| 192.168.56.103 | Centos7 | slave |
| 192.168.56.101 | Centos7 | sentinel |
| 192.168.56.102 | Centos7 | sentinel |
| 192.168.56.103 | Centos7 | sentinel |
- ####首先在master(192.168.56.101)执行单机版安装,然后在单机版安装make成功之后,修改redis.conf
sed -i 's/^bind 127.0.0.1 -::1$/bind 192.168.56.101 -::1/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^daemonize no$/daemonize yes/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^pidfile \/var\/run\/redis_6379.pid$/pidfile "\/usr\/local\/redis-6.2.4\/redis_6379.pid"/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^dir .\/$/dir "\/usr\/local\/redis-6.2.4\/"/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^logfile ""$/logfile "\/usr\/local\/redis-6.2.4\/logs\/redis.log"/' /usr/local/redis-6.2.4/redis.conf
sed -i 's/^# requirepass foobared$/requirepass 123456/' /usr/local/redis-6.2.4/redis.conf
将# masterauth <master-password>取消注解,并设置密码为123456,这个配置是做主从复制用的密码
sed -i 's/^# masterauth <master-password>$/masterauth 123456/' /usr/local/redis-6.2.4/redis.conf
##建立logs目录
mkdir /usr/local/redis-6.2.4/logs/ -p
- ####接着通过scp命令单master的安装好的文件传送到2个slave服务器,在192.168.56.101上执行
scp -r /usr/local/redis-6.2.4/ root@192.168.56.102:/usr/local/
scp -r /usr/local/redis-6.2.4/ root@192.168.56.103:/usr/local/
- ####修改2个slave机器上的redis.conf,在master的配置的基础上增加一行,在192.168.56.102和192.168.56.103上执行
cat >> /usr/local/redis-6.2.4/redis.conf << EOF
slaveof 192.168.56.101 6379
EOF
- ####因为从master拷贝过来的bind信息的ip是bind 192.168.56.102 -::1,分别在两台node(192.168.56.102,192.168.56.103)上修改成正确的ip
# 192.168.56.102机器上执行
sed -i 's/^bind 192.168.56.101 -::1$/bind 192.168.56.102 -::1/' /usr/local/redis-6.2.4/redis.conf
# 192.168.56.103机器上执行
sed -i 's/^bind 192.168.56.101 -::1$/bind 192.168.56.103 -::1/' /usr/local/redis-6.2.4/redis.conf
- ####修改sentinel.conf,三台机一模一样
protected-mode no
daemonize yes
pidfile "/usr/local/redis-6.2.4/redis-sentinel.pid"
logfile "/usr/local/redis-6.2.4/logs/sentinel.log"
dir /usr/local/redis-6.2.4/sentinel-work
sentinel monitor mymaster 192.168.56.101 6379 2
sentinel auth-pass mymaster gzsendi1!
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 10000
快速执行命令如下,三台机执行:
##新建目录
mkdir /usr/local/redis-6.2.4/sentinel-work
##执行命令修改sentinel.conf
sed -i 's/^# protected-mode no$/protected-mode no/' /usr/local/redis-6.2.4/sentinel.conf
sed -i 's/^daemonize no$/daemonize yes/' /usr/local/redis-6.2.4/sentinel.conf
sed -i 's/^pidfile \/var\/run\/redis-sentinel.pid$/pidfile "\/usr\/local\/redis-6.2.4\/redis-sentinel.pid"/' /usr/local/redis-6.2.4/sentinel.conf
sed -i 's/^logfile ""$/logfile "\/usr\/local\/redis-6.2.4\/logs\/sentinel.log"/' /usr/local/redis-6.2.4/sentinel.conf
sed -i 's/^dir \/tmp$/dir \/usr\/local\/redis-6.2.4\/sentinel-work/' /usr/local/redis-6.2.4/sentinel.conf
sed -i 's/^sentinel monitor mymaster 127.0.0.1 6379 2$/sentinel monitor mymaster 192.168.56.101 6379 2/' /usr/local/redis-6.2.4/sentinel.conf
sed -i 's/^# sentinel auth-pass <master-name> <password>$/sentinel auth-pass mymaster 123456/' /usr/local/redis-6.2.4/sentinel.conf
sed -i 's/^# sentinel down-after-milliseconds <master-name> <milliseconds>$/sentinel down-after-milliseconds mymaster 3000/' /usr/local/redis-6.2.4/sentinel.conf
sed -i 's/^# sentinel failover-timeout <master-name> <milliseconds>$/sentinel failover-timeout mymaster 10000/' /usr/local/redis-6.2.4/sentinel.conf
- ####启动哨兵集群,三台机上直接执行下面2个命令
##先停防火墙如下:
systemctl stop firewalld
systemctl disable firewalld
##启动哨兵集群,三台机上直接执行
/usr/local/redis-6.2.4/src/redis-server /usr/local/redis-6.2.4/redis.conf
/usr/local/redis-6.2.4/src/redis-sentinel /usr/local/redis-6.2.4/sentinel.conf
- ####哨兵集群测试
- 查看
集群状态/usr/local/redis-6.2.4/src/redis-cli -h 192.168.56.101 -p 6379 -a 123456 然后执行info replication
[root@localhost src]# /usr/local/redis-6.2.4/src/redis-cli -h 192.168.56.101 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.56.101:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.56.102,port=6379,state=online,offset=112028,lag=1
slave1:ip=192.168.56.103,port=6379,state=online,offset=111885,lag=1
master_failover_state:no-failover
master_replid:db4762954bb4b448585bdcdbc7b709b1d0cc98e6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:112171
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:112171
- 查看
sentinel状态/usr/local/redis-6.2.4/src/redis-cli -h 192.168.56.101 -p 26379然后执行info sentinel
[root@localhost src]# /usr/local/redis-6.2.4/src/redis-cli -h 192.168.56.101 -p 26379
192.168.56.101:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.56.101:6379,slaves=2,sentinels=3
- ####kill掉192.168.56.101上的redis进程,然后观察是否会切换
可以看到master切换到了192.168.56.103,
[root@localhost redis-6.2.4]# /usr/local/redis-6.2.4/src/redis-cli -h 192.168.56.102 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.56.102:6379> info replication
# Replication
role:slave
master_host:192.168.56.103
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:204030
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:b99ff9a759686da5d8352db49b5c846e8117b5c4
master_replid2:db4762954bb4b448585bdcdbc7b709b1d0cc98e6
master_repl_offset:204030
second_repl_offset:192069
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:204030
然后重新执行/usr/local/redis-6.2.4/src/redis-server /usr/local/redis-6.2.4/redis.conf将192.168.56.101上的redis进程重启,然后可发现集群又自动恢复成功。
Redis安装(分片集群)
Redis集群使用数据分片(sharding)来实现:一个Redis集群包含16384个哈希槽(hash slot),数据库中的每个键都属于这16384个哈希槽的其中一个,集群使用公式CRC16(key)%16384来计算键key属于哪个槽,其中CRC16(key)语句用于计算键key的CRC16校验和。比如: 节点A负责处理0号至5500号哈希槽。 节点B负责处理5501号至11000号哈希槽。 节点C负责处理11001号至16384号哈希槽。
- ####安装环境
redis集群分片需要6台机,需要的资源太多,这里使用一台虚机的不同端口来模拟,在真实的集群搭建时,将不同端口的进程分别部署在不同的机器上即可(6001-6006端口)
- ####先进行单机版安装,执行完make,并创建目录
##编绎
yum install -y gcc gcc-c++
tar -zxvf redis-6.2.4.tar.gz -C /usr/local/
cd /usr/local/redis-6.2.4/
make
##目录创建
mkdir /usr/local/redis-6.2.4/logs/ -p
- ####复制6个目录
mkdir /usr/local/redis-cluster/ -p
cp -r /usr/local/redis-6.2.4/ /usr/local/redis-cluster/6001
cp -r /usr/local/redis-6.2.4/ /usr/local/redis-cluster/6002
cp -r /usr/local/redis-6.2.4/ /usr/local/redis-cluster/6003
cp -r /usr/local/redis-6.2.4/ /usr/local/redis-cluster/6004
cp -r /usr/local/redis-6.2.4/ /usr/local/redis-cluster/6005
cp -r /usr/local/redis-6.2.4/ /usr/local/redis-cluster/6006
- ####修改配置文件redis.conf, 以/usr/local/redis-cluster/6001/redis.conf为例
bind 192.168.56.101 -::1 daemonize yes #后台启动 port 6001 #修改端口号,从6001 到6006 pidfile "/usr/local/redis-6.2.4/redis_6379.pid" dir "/usr/local/redis-6.2.4/" logfile "/usr/local/redis-6.2.4/logs/redis.log" requirepass 123456 cluster-enabled yes #开启cluster,去掉注释 cluster-config-file nodes.conf #自动生成 cluster-node-timeout 15000 #节点通信时间 appendonly yes #持久化方式
一键执行的命令如下:
sed -i 's/^bind 127.0.0.1 -::1$/bind 192.168.56.101 -::1/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^port 6379$/port 6001/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^daemonize no$/daemonize yes/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^pidfile \/var\/run\/redis_6001.pid$/pidfile "\/usr\/local\/redis-cluster\/redis_6001.pid"/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^dir .\/$/dir "\/usr\/local\/redis-cluster\/6001\/"/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^logfile ""$/logfile "\/usr\/local\/redis-cluster\/6001\/logs\/redis.log"/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^# requirepass foobared$/requirepass 123456/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^# cluster-enabled yes$/cluster-enabled yes/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^# cluster-config-file nodes-6379.conf$/cluster-config-file nodes-6001.conf/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^# cluster-node-timeout 15000$/cluster-node-timeout 15000/' /usr/local/redis-cluster/6001/redis.conf
sed -i 's/^appendonly no$/appendonly yes/' /usr/local/redis-cluster/6001/redis.conf
其他5个目录下的redis也做相应调整,分别修改相应的配置,按各自的配置情况修改,端口,路径等
- ####集群启动
##先要将6个redis的进程启动
/usr/local/redis-cluster/6001/src/redis-server /usr/local/redis-cluster/6001/redis.conf
/usr/local/redis-cluster/6002/src/redis-server /usr/local/redis-cluster/6002/redis.conf
/usr/local/redis-cluster/6003/src/redis-server /usr/local/redis-cluster/6003/redis.conf
/usr/local/redis-cluster/6004/src/redis-server /usr/local/redis-cluster/6004/redis.conf
/usr/local/redis-cluster/6005/src/redis-server /usr/local/redis-cluster/6005/redis.conf
/usr/local/redis-cluster/6006/src/redis-server /usr/local/redis-cluster/6006/redis.conf
##然后将6进程创建成集群,6版本后不再使用ruby脚本启动,而是直接使用./redis-cli启动,进入任意一个6001-6006的目录,我这里进入6001,然后执行:
cd /usr/local/redis-cluster/6001/src
./redis-cli -a 123456 --cluster create 192.168.56.101:6001 192.168.56.101:6002 192.168.56.101:6003 192.168.56.101:6004 192.168.56.101:6005 192.168.56.101:6006 --cluster-replicas 1
执行过程中输入yes然后回车,详细结果日志如下:
[root@localhost src]# ./redis-cli -a 123456 --cluster create 192.168.56.101:6001 192.168.56.101:6002 192.168.56.101:6003 192.168.56.101:6004 192.168.56.101:6005 192.168.56.101:6006 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.56.101:6005 to 192.168.56.101:6001
Adding replica 192.168.56.101:6006 to 192.168.56.101:6002
Adding replica 192.168.56.101:6004 to 192.168.56.101:6003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 9690b102b4be885b9a872c5e2f0de94c93f6b413 192.168.56.101:6001
slots:[0-5460] (5461 slots) master
M: 0c2c87a88a3fde5af2db1fe5005ccc19c8578a5f 192.168.56.101:6002
slots:[5461-10922] (5462 slots) master
M: d0ed2b7955ab585b3c6f37c00413f48c157c0cf9 192.168.56.101:6003
slots:[10923-16383] (5461 slots) master
S: b1afdb8256f5a5f148ab43510b81110d63eac948 192.168.56.101:6004
replicates 0c2c87a88a3fde5af2db1fe5005ccc19c8578a5f
S: 33f2c653e66d50d474ec1670449258df1d2ac541 192.168.56.101:6005
replicates d0ed2b7955ab585b3c6f37c00413f48c157c0cf9
S: f4896d342ffbdc1077464f348fdff00d7976a670 192.168.56.101:6006
replicates 9690b102b4be885b9a872c5e2f0de94c93f6b413
Can 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 cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.56.101:6001)
M: 9690b102b4be885b9a872c5e2f0de94c93f6b413 192.168.56.101:6001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 33f2c653e66d50d474ec1670449258df1d2ac541 192.168.56.101:6005
slots: (0 slots) slave
replicates d0ed2b7955ab585b3c6f37c00413f48c157c0cf9
S: b1afdb8256f5a5f148ab43510b81110d63eac948 192.168.56.101:6004
slots: (0 slots) slave
replicates 0c2c87a88a3fde5af2db1fe5005ccc19c8578a5f
S: f4896d342ffbdc1077464f348fdff00d7976a670 192.168.56.101:6006
slots: (0 slots) slave
replicates 9690b102b4be885b9a872c5e2f0de94c93f6b413
M: d0ed2b7955ab585b3c6f37c00413f48c157c0cf9 192.168.56.101:6003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 0c2c87a88a3fde5af2db1fe5005ccc19c8578a5f 192.168.56.101:6002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- ####集群状态检查
任意进入一个节点执行命令
cluster nodes进行集群信息的查看,可看到各自的角色及对应的槽位。
[root@localhost src]# /usr/local/redis-cluster/6001/src/redis-cli -a 123456 -h 192.168.56.101 -p 6001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.56.101:6001> cluster nodes
33f2c653e66d50d474ec1670449258df1d2ac541 192.168.56.101:6005@16005 slave d0ed2b7955ab585b3c6f37c00413f48c157c0cf9 0 1626067299000 3 connected
b1afdb8256f5a5f148ab43510b81110d63eac948 192.168.56.101:6004@16004 slave 0c2c87a88a3fde5af2db1fe5005ccc19c8578a5f 0 1626067300471 2 connected
f4896d342ffbdc1077464f348fdff00d7976a670 192.168.56.101:6006@16006 slave 9690b102b4be885b9a872c5e2f0de94c93f6b413 0 1626067300000 1 connected
d0ed2b7955ab585b3c6f37c00413f48c157c0cf9 192.168.56.101:6003@16003 master - 0 1626067301473 3 connected 10923-16383
0c2c87a88a3fde5af2db1fe5005ccc19c8578a5f 192.168.56.101:6002@16002 master - 0 1626067300000 2 connected 5461-10922
9690b102b4be885b9a872c5e2f0de94c93f6b413 192.168.56.101:6001@16001 myself,master - 0 1626067299000 1 connected 0-5460
- ####集群分片测试 在6001上执行set hello world
[root@localhost src]# /usr/local/redis-cluster/6001/src/redis-cli -a 123456 -h 192.168.56.101 -p 6001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.56.101:6001>
192.168.56.101:6001> set hello world
OK
在6001上执行get hello
[root@localhost src]# /usr/local/redis-cluster/6001/src/redis-cli -a 123456 -h 192.168.56.101 -p 6001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.56.101:6001> get hello
"world"
192.168.56.101:6001>
在6002上执行get hello ,会报error提示数据在6001上,说明分片已经生效。
192.168.56.101:6001> exit
[root@localhost src]# /usr/local/redis-cluster/6001/src/redis-cli -a 123456 -h 192.168.56.101 -p 6002
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.56.101:6002> get hello
(error) MOVED 866 192.168.56.101:6001
总结
本文只是从应用的角度做了环境的搭建,对原理未深入了解研究,有了redis的安装环境,接下来会从redis的原理出发来深入了解与学习redis。