2020-10-13 CentOS + Redis6 主从集群+哨兵服务实践部署

今天搭建公司的redis服务,用来承担大并发流量场景;

基本环境

[root@localhost downnload]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost downnload]# getconf LONG_BIT
64

开始安装

  1. 下载、解压、编译Redis
[root@localhost downnload]# wget http://download.redis.io/releases/redis-6.0.6.tar.gz
[root@localhost downnload]# tar xzf redis-6.0.6.tar.gz
[root@localhost downnload]# cd redis-6.0.6
[root@localhost downnload]# make

不出所料,果然出错,如下

image.png

google下 make[1]: *** [server.o] 错误 1都是说gcc版本低

[root@localhost redis-6.0.6]# gcc -v
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 

升级gcc到9

[root@localhost redis-6.0.6]# yum -y install centos-release-scl
[root@localhost redis-6.0.6]# yum list all --enablerepo='centos-sclo-rh' | grep 'gcc'
image.png
[root@localhost redis-6.0.6]# yum -y install devtoolset-9-gcc
切换gcc版本 [root@localhost redis-6.0.6]# scl enable devtoolset-9 bash

解决完gcc版本问题
重新编译
[root@localhost redis-6.0.6]# make distclean
[root@localhost redis-6.0.6]# make
经过编译,成功之后会显示如下图

image.png

运行

一开始编译的时候,直接在download目录中操作的,别扭及了,把redis服务放到/servers目录中

[root@localhost downnload]# mkdir /servers
[root@localhost servers]# cp -r /root/downnload/redis-6.0.6/ ./redis-6.0.6
运行
[root@localhost redis-6.0.6]# /servers/redis-6.0.6/src/redis-server ./redis.conf 

启动成功,如下图所示,红框表示这是单例的模式;


image.png

配置

基本的单例模式我们启动成功了,今天我们要部署主从+哨兵模式,避免单点的问题
默认的redis.conf里启动redis是非守护进程,当前启动方式redis要和终端同在;

[root@localhost redis-6.0.6]# vim redis.conf

额,这么多的注释,忍不了;
新建conf目录 放没有注释的配置文件,使用grep命令过滤

[root@localhost redis-6.0.6]#  mkdir conf
[root@localhost redis-6.0.6]#  cat redis.conf | grep -v ^# | grep -v ^$ > conf/redis.conf
[root@localhost redis-6.0.6]#  vim conf/redis.conf
bind 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
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 ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ./redis.log
databases 16
always-show-logo yes
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 ./
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
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 4096
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

修改两行
bind 0.0.0.0
protected-mode no
daemonize yes
logfile ./redis.log
重新启动redis服务
[root@localhost redis-6.0.6]# src/redis-server ./conf/redis.conf
23567:C 13 Oct 2020 14:04:15.723 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23567:C 13 Oct 2020 14:04:15.723 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=23567, just started
23567:C 13 Oct 2020 14:04:15.723 # Configuration loaded
[root@localhost redis-6.0.6]# ps -ef |grep 'redis'
root 23568 1 0 14:04 ? 00:00:00 src/redis-server 127.0.0.1:6379
root 23573 23427 0 14:04 pts/3 00:00:00 src/redis-cli
root 23578 23406 0 14:13 pts/2 00:00:00 grep --color=auto redis


搭建主从集群

  1. 修改主节点ip[10.1.1.114] /servers/redis-6.0.6/conf/redis.conf
    bind:0.0.0.0
    port:6379
    protected-mode:no
    daemonize:yes
    logfile:./redis.log

    kill掉redis服务,重新启动
    [root@localhost redis-6.0.6]# src/redis-server ./conf/redis.conf
  2. 在从服务器10.1.1.113搭建redis服务,重复上述下载编译安装过程;
    修改从节点 /servers/redis-6.0.6/conf/redis.conf
    bind:0.0.0.0
    port:6379
    protected-mode:no
    daemonize:yes
    logfile:./redis.log
    replicaof 10.1.1.114 6379
  3. 主从防火墙上增加6379端口,启动redis服务
[root@localhost redis-6.0.6]# firewall-cmd --add-port=6379/tcp --permanent --zone=public
[root@localhost redis-6.0.6]# firewall-cmd --reload 
[root@localhost redis-6.0.6]# src/redis-server conf/redis.conf 
[root@localhost redis-6.0.6]# cat redis.log 
30359:C 13 Oct 2020 15:20:50.680 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30359:C 13 Oct 2020 15:20:50.680 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=30359, just started
30359:C 13 Oct 2020 15:20:50.681 # Configuration loaded
30360:S 13 Oct 2020 15:20:50.684 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 30360
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

30360:S 13 Oct 2020 15:20:50.690 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30360:S 13 Oct 2020 15:20:50.690 # Server initialized
30360:S 13 Oct 2020 15:20:50.691 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
30360:S 13 Oct 2020 15:20:50.691 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
30360:S 13 Oct 2020 15:20:50.692 * Ready to accept connections
30360:S 13 Oct 2020 15:20:50.692 * Connecting to MASTER 10.1.1.114:6379
30360:S 13 Oct 2020 15:20:50.693 * MASTER <-> REPLICA sync started
30360:S 13 Oct 2020 15:20:50.694 * Non blocking connect for SYNC fired the event.
30360:S 13 Oct 2020 15:20:50.695 * Master replied to PING, replication can continue...
30360:S 13 Oct 2020 15:20:50.696 * Partial resynchronization not possible (no cached master)
30360:S 13 Oct 2020 15:20:50.709 * Full resync from master: fe3a53667c25b16548eb20a4c5a13a09c3c2579c:0
30360:S 13 Oct 2020 15:20:50.812 * MASTER <-> REPLICA sync: receiving 192 bytes from master to disk
30360:S 13 Oct 2020 15:20:50.812 * MASTER <-> REPLICA sync: Flushing old data
30360:S 13 Oct 2020 15:20:50.813 * MASTER <-> REPLICA sync: Loading DB in memory
30360:S 13 Oct 2020 15:20:50.813 * Loading RDB produced by version 6.0.6
30360:S 13 Oct 2020 15:20:50.813 * RDB age 0 seconds
30360:S 13 Oct 2020 15:20:50.813 * RDB memory usage when created 1.83 Mb
30360:S 13 Oct 2020 15:20:50.814 * MASTER <-> REPLICA sync: Finished with success

通过日志,我们可以看到113上的redis启动成功之后,Connecting to MASTER 10.1.1.114:6379,同步数据;MASTER <-> REPLICA sync: Finished with success表示主从同步数据成功

  1. 验证
    在114上,我们用redis-cli命令设置值
[root@localhost redis-6.0.6]# src/redis-cli 
127.0.0.1:6379> setex test 100 2020-10-13 
OK

在113上,我们用redis-cli命令获取test的值

127.0.0.1:6379> get test
"2020-10-13"

主从结果一致
另外reids-cli命令提供了info工具来查询服务器状态,113的状态信息如下

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.1.1.114
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:1567
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:fe3a53667c25b16548eb20a4c5a13a09c3c2579c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1567
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1567

114主节点的状态信息如下

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1  #连接成功的从节点1个
slave0:ip=10.199.0.254,port=6379,state=online,offset=1693,lag=0
master_replid:fe3a53667c25b16548eb20a4c5a13a09c3c2579c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1693
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1693

至此,redis主从集群搭建成功了,此集群是单点模式,单个实例上保存了所有的数据,后续根据业务情况,搭建cluster模式的集群,cluster模式可以实现不同的分片上承接不同的redis插槽,不同的实例承接不同的数据项

代办-持久化方式RDB和AOF https://www.cnblogs.com/kismetv/p/9137897.html

参考 https://stor.51cto.com/art/202004/614443.htm

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,189评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,577评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,857评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,703评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,705评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,620评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,995评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,656评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,898评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,639评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,720评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,395评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,982评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,953评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,195评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,907评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,472评论 2 342