centos7---redis安装
一、环境介绍
system-version | centos7.8 |
---|---|
redis-version | 6.2.9 |
192.168.150.61 | 主 |
192.168.150.62 | 从 |
二、简介
reids: 是一个开源的内存数据库,遵守BSD协议,它提供了一个高性能的键值(key-value)存储系统,常用于缓存、消息队列、会话存储等应用场景。
Sentinel哨兵职责如下:
- 监控(Monitoring): Sentinel会不断地定期检查你的主服务器和从服务器是否运作正常
- 提醒(Notification): 当备监控的某个Redis服务器出现问题时,Sentinel可以通过API向管理员或者其他应用程序发送通知
- 自动故障迁移(Automaticfailover):当一个主服务器不能正常工作时,Sentinel会开始一次自动故障迁移操作,它会将失效主服务器的其中一个服务器升级 为新的主服务器,并让失效主服务器的其他从服务器改为复制新的主服务器;当客户端试图连接失效的主服务器时,集群会向客户端返回新主服务器的地址,使得集群可以使用新主服务器代替失效服务器。
- 统一的配置管理:连接者询问sentinel取到主从的地址
三、安装
1、 主-redis
1、 下载
wget http://download.redis.io/releases/redis-6.2.9.tar.gz
2、 安装gcc
yum install -y gcc
3、解压
tar xf redis-6.2.9.tar.gz
4、编译
cd redis-6.2.9/
make && make install
5、编辑配置文件
egrep -v "^#|^$" redis.conf
bind 0.0.0.0
protected-mode yes # 启用保护模式
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes # 后台运行
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16
always-show-logo yes
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/root/redis-6.2.9"
masterauth "gitlab@123" # master节点设置该属性后,所有redis节点均需配masterauth属性
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass "gitlab@123" # 连接密码
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4kb
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
6、 启动服务
redis-server ./redis.conf
2、从-redis
# 安装、配置同主服务器
3、主-sentinel
redis-sentinel ./sentinel.conf# 配置文件
egrep -v "^#|^$" ../redis-6.2.9/sentinel.conf
bind 192.168.150.61
protected-mode no
port 26379
masterauth "gitlab@123"
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis/sentinel.log"
dir "/tmp"
sentinel monitor master 192.168.150.61 6379 1 # 这里监控的是主服务器的地址
sentinel auth-pass master gitlab@123
acllog-max-len 128
requirepass "gitlab@123"
sentinel failover-timeout master 30000
sentinel deny-scripts-reconfig yes
sentinel resolve-hostnames no
sentinel myid 7fa3653efc0e6699880d41ba48c00d4d27bd5bf7
sentinel config-epoch master 611
sentinel leader-epoch master 611
sentinel known-replica master 192.168.150.62 6379
sentinel known-sentinel master 192.168.150.62 26379 064378128898c3f6d84cdb8de79abb4975ba04ca
sentinel current-epoch 611
sentinel announce-hostnames no
# 启动方式
redis-sentinel ./sentinel.conf
4、从-sentinel
# 配置文件
egrep -v "^#|^$" ../redis-6.2.9/sentinel.conf
bind 192.168.150.62
sentinel myid 064378128898c3f6d84cdb8de79abb4975ba04ca
protected-mode no
port 26379
masterauth "gitlab@123"
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis/sentinel.log"
dir "/tmp"
sentinel monitor master 192.168.150.61 6379 1 # 监控的是主服务器的地址
sentinel auth-pass master gitlab@123
acllog-max-len 128
requirepass "gitlab@123"
sentinel failover-timeout master 30000
sentinel deny-scripts-reconfig yes
sentinel resolve-hostnames no
sentinel config-epoch master 611
sentinel leader-epoch master 611
sentinel known-replica master 192.168.150.61 6379
sentinel known-sentinel master 192.168.150.61 26379 7fa3653efc0e6699880d41ba48c00d4d27bd5bf7
sentinel current-epoch 611
sentinel announce-hostnames no
# 启动
redis-sentinel ./sentinel.conf
四、验证
# 主服务器查看状态
[root@master redis-6.2.9]# redis-cli
127.0.0.1:6379> AUTH gitlab@123
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.150.62,port=6379,state=online,offset=513788,lag=0
master_failover_state:no-failover
master_replid:39a7e1fa7d3883f17f1d7e7b68118ffdc3762f6d
master_replid2:ade3c909314712f8a7b6a569eca1792617c321d2
master_repl_offset:513788
second_repl_offset:48621
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:513788
# 从服务器查看状态
redis-cli
127.0.0.1:6379> AUTH gitlab@123
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.150.61
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:522717
slave_repl_offset:522717
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:39a7e1fa7d3883f17f1d7e7b68118ffdc3762f6d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:522717
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:67784
repl_backlog_histlen:454934
# 杀掉主服务器redis
[root@master redis-6.2.9]# ps -ef | grep redis
root 4753 1 0 10:24 ? 00:00:10 redis-server 0.0.0.0:6379
root 4759 1 0 10:24 ? 00:00:14 redis-sentinel 192.168.150.61:26379 [sentinel]
root 4852 1381 0 11:27 pts/0 00:00:00 grep --color=auto redis
[root@master redis-6.2.9]# kill -9 4753
[root@master redis-6.2.9]# ps -ef | grep redis
root 4759 1 0 10:24 ? 00:00:14 redis-sentinel 192.168.150.61:26379 [sentinel]
root 4854 1381 0 11:27 pts/0 00:00:00 grep --color=auto redis
# 查看从服务器已切换为主
127.0.0.1:6379> AUTH gitlab@123
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:be5ac5004bc96e7b7dc54fe99d1bdffd754ae38f
master_replid2:39a7e1fa7d3883f17f1d7e7b68118ffdc3762f6d
master_repl_offset:542806
second_repl_offset:528574
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:67784
repl_backlog_histlen:475023
标签:no,redis,哨兵,repl,master,sentinel,yes
From: https://www.cnblogs.com/kkit/p/18356689