注:系统安装后执行了系统初始化优化脚本,故用户、内核参数以及依赖包均修改已安装。
#redis版本以及系统版本
Redis version=4.0.6
[root@KDXF-00 setup]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
#把redis包放在指定目录下并解压
[root@KDXF-00 setup]# pwd
/data1/setup
[root@KDXF-00 setup]# ls
redis-4.0.6 redis-4.0.6.tar.gz
#创建目录并复制配置文件
[root@KDXF-00 opt]# mkdir -p redis/log
[root@KDXF-00 opt]# mkdir redis/etc
[root@KDXF-00 opt]#cp /data1/setup/redis-4.0.6/redis.conf /data1/opt/redis/etc
#安装
[root@KDXF-00 redis-4.0.6]# pwd
/data1/setup/redis-4.0.6
[root@KDXF-00 redis-4.0.6]# less README.md //查看redis的基本信息及相关操作
[root@KDXF-00 redis-4.0.6]# make MALLOC=jemalloc
[root@KDXF-00 redis-4.0.6]# make PREFIX=/data1/opt/redis install
[root@KDXF-00 redis-4.0.6]# cd ~
[root@KDXF-00 ~]# tree /data1/opt/redis
/data1/opt/redis
├── bin
│ ├── redis-benchmark
│ ├── redis-check-aof
│ ├── redis-check-rdb
│ ├── redis-cli
│ ├── redis-sentinel -> redis-server
│ └── redis-server
├── etc
│ └── redis.conf
└── log
#配置环境变量
[root@KDXF-00 ~]# echo "export PATH=/data1/opt/redis/bin:$PATH">>/etc/profile
[root@KDXF-00 ~]# source /etc/profile
[root@KDXF-00 ~]# which redis-server
/data1/opt/redis/bin/redis-server
#启动
[root@KDXF-00 ~]# redis-server /data1/opt/redis/etc/redis.conf
注:此处会有报错
此时按ctrl+c退出即可,因为此时不是后台执行的命令
修复一:
[root@KDXF-00 ~]# echo "net.core.somaxconn=511">> /etc/sysctl.conf
[root@KDXF-00 ~]# sysctl -p
补充:
net.core.somaxconn是linux中的一个kernel参数,表示socket监听(listen)的backlog上限。backlog是socket的监听队列,当一个请求(request)尚未被处理或建立时,他会进入backlog。而socket server可以一次性处理backlog中的所有请求,处理后的请求不再位于监听队列中。当server处理请求较慢,以至于监听队列被填满后,新来的请求会被拒绝。所以说net.core.somaxconn限制了接收新 TCP 连接侦听队列的大小。对于一个经常处理新连接的高负载 web服务环境来说,默认的 128 太小,大多数环境这个值建议增加到 1024 或者更多。
修复二:
[root@KDXF-00 ~]# echo "vm.overcommit_memory = 1">> /etc/sysctl.conf
[root@KDXF-00 ~]# sysctl -p
补充:
overcommit_memory:设置内存分配策略。可选值:0、1、2。
0:表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1:表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2:表示内核允许分配超过所有物理内存和交换空间总和的内存
注意:redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)。
修复三:
[root@KDXF-00 ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@KDXF-00 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
[root@KDXF-00 ~]# vim /etc/rc.local
插入:never > /sys/kernel/mm/transparent_hugepage/enabled
补充:使用透明大页,可能导致redis延迟和内存使用问题。
再次启动成功:
#查看进程
[root@KDXF-00 ~]# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 7586 root 6u IPv4 25719 0t0 TCP localhost:6379 (LISTEN)
#指定用户并启动服务(前提是root启动的redis已进程关闭)
[root@KDXF-00 opt]# redis-cli shutdown
[root@KDXF-00 opt]# pwd
/data1/opt
[root@KDXF-00 opt]# chown -R iflyer:iflyer redis/
[root@KDXF-00 opt]# ll
total 0
drwxr-xr-x. 5 iflyer iflyer 39 Sep 30 01:42 redis
[root@KDXF-00 redis]# su iflyer
[iflyer@KDXF-00 redis]$ redis-server /data1/opt/redis/etc/redis.conf
#修改配置文件
设置redis后台启动
daemonize yes
指定PID文件
pidfile /data1/opt/redis/redis.pid
指定redis运行的端口
port 6379
设置日志级别以及日志文件
loglevel notice
logfile /data1/opt/redis/log/redis.log
注:安装过程见单实例章节
主服务器:192.168.213.23
从服务器:192.168.213.24
master操作:
#配置文件修改
设置可以连接的网段
bind 0.0.0.0
设置redis后台启动
daemonize yes
指定PID文件
pidfile /data1/opt/redis/redis.pid
指定redis运行的端口
port 6379
设置日志级别以及日志文件
loglevel notice
logfile /data1/opt/redis/log/redis.log
slave操作:
#配置文件修改
设置可以连接的网段
bind 0.0.0.0
设置redis后台启动
daemonize yes
指定master
slaveof 192.168.213.23 6379
指定PID文件
pidfile /data1/opt/redis/redis.pid
指定redis运行的端口
port 6379
设置日志级别以及日志文件
loglevel notice
logfile /data1/opt/redis/log/redis.log
#分别启动主从服务器查看日志文件:
[iflyer@KDXF-00 redis]$ redis-server /data1/opt/redis/etc/redis.conf
测试主从同步:
Master:
[root@KDXF-00 redis]# redis-cli -p 6379
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name lily
OK
127.0.0.1:6379> get name
"lily"
127.0.0.1:6379> keys *
1) "name"
Slave:
[root@KDXF-01 redis]# redis-cli -p 6379
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> get name
"lily"
注:安装过程见上文,此段主要配置sentinel(哨兵)
master操作:
#创建目录:
[iflyer@KDXF-00 redis]$ pwd
/data1/opt/redis
[iflyer@KDXF-00 redis]$ cp /data1/setup/redis-4.0.6/sentinel.conf /data1/opt/redis/etc/
[iflyer@KDXF-00 redis]$ mkdir sentinel_work
[iflyer@KDXF-00 redis]$ mkdir redisdata
#修改redis.conf配置文件:
daemonize yes
pidfile "/data1/opt/redis/redis.pid"
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 60
loglevel notice
logfile /data1/opt/redis/log/redis.log
databases 16
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum no
dbfilename "dump.rdb"
dir /data1/opt/redis/redisdata
masterauth test123
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
repl-backlog-size 512mb
slave-priority 100
requirepass test123
maxmemory 64gb
maxmemory-policy volatile-lru
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 5gb 5gb 600
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
#修改sentinel.conf配置文件:
port 26379
daemonize yes
logfile /data1/opt/redis/log/sentinel.log
dir /data1/opt/redis/sentinel_work
protected-mode no
sentinel monitor mymaster 192.168.213.23 6379 1
sentinel auth-pass mymaster test123
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 10000
sentinel config-epoch mymaster 7
sentinel leader-epoch mymaster 7
sentinel current-epoch 7
slave操作:
#创建目录:
[iflyer@KDXF-00 redis]$ pwd
/data1/opt/redis
[iflyer@KDXF-00 redis]$ cp /data1/setup/redis-4.0.6/sentinel.conf /data1/opt/redis/etc/
[iflyer@KDXF-00 redis]$ mkdir sentinel_work
[iflyer@KDXF-00 redis]$ mkdir redisdata
#修改redis.conf配置文件:
daemonize yes
pidfile "/data1/opt/redis/redis.pid"
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 60
loglevel notice
logfile /data1/opt/redis/log/redis.log
databases 16
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum no
dbfilename "dump.rdb"
dir /data1/opt/redis/redisdata
masterauth test123
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
repl-backlog-size 512mb
slave-priority 100
requirepass test123
maxmemory 64gb
maxmemory-policy volatile-lru
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 5gb 5gb 600
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
slaveof 192.168.213.23 6379
#修改sentinel.conf配置文件:
port 26379
daemonize yes
logfile /data1/opt/redis/log/sentinel.log
dir /data1/opt/redis/sentinel_work
protected-mode no
sentinel monitor mymaster 192.168.213.23 6379 1
sentinel auth-pass mymaster test123
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000
sentinel config-epoch mymaster 7
sentinel leader-epoch mymaster 5
sentinel current-epoch 7
#另搭建一个redis:192.168.213.25,sentinel.conf配置文件如下:
port 26379
daemonize yes
logfile /data1/opt/redis/log/sentinel.log
dir /data1/opt/redis/sentinel_work
protected-mode no
sentinel monitor mymaster 192.168.213.23 6379 1
sentinel auth-pass mymaster test123
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 10000
sentinel config-epoch mymaster 7
sentinel leader-epoch mymaster 7
sentinel current-epoch 7
#启动主从redis以及3个哨兵:
[iflyer@KDXF-00 bin]$ redis-server /data1/opt/redis/etc/redis.conf
[iflyer@KDXF-00 bin]$ pwd
/data1/opt/redis/bin
[iflyer@KDXF-00 bin]$ nohup ./redis-sentinel ../etc/sentinel.conf --sentinel &
#查看3个sentinel.conf:
#查看sentinel信息:
[iflyer@KDXF-01 bin]$ ./redis-cli -a test123 -p 26379 info sentinel
#查看主从信息:
[iflyer@KDXF-02 bin]$ ./redis-cli -p 6379 -h 192.168.213.23 -a test123 info replication
[iflyer@KDXF-02 bin]$ ./redis-cli -p 6379 -h 192.168.213.24 -a test123 info replication
验证:
#关闭主库:
[iflyer@KDXF-00 bin]$ redis-cli -p 6379 -a test123 shutdown
[iflyer@KDXF-00 bin]$ ps -ef | grep redis
iflyer 1670 1 0 Oct07 ? 00:01:41 ./redis-sentinel *:26379 [sentinel]
iflyer 1885 1484 0 05:02 pts/1 00:00:00 grep --color=auto redis
#查看sentinel信息:
[iflyer@KDXF-02 bin]$ ./redis-cli -a test123 -p 26379 info sentinel
#等待指定时间后(down-after-milliseconds)查看哨兵信息,可以看出从库已切换成主库: