今天搭建公司的redis服务,用来承担大并发流量场景;
基本环境
[root@localhost downnload]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost downnload]# getconf LONG_BIT
64
开始安装
- 下载、解压、编译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
不出所料,果然出错,如下
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'
[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
经过编译,成功之后会显示如下图
运行
一开始编译的时候,直接在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
启动成功,如下图所示,红框表示这是单例的模式;
配置
基本的单例模式我们启动成功了,今天我们要部署主从+哨兵模式,避免单点的问题
默认的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
搭建主从集群
- 修改主节点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
- 在从服务器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 - 主从防火墙上增加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表示主从同步数据成功
- 验证
在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