共计 8851 个字符,预计需要花费 23 分钟才能阅读完成。
导读 | Redis 是一种高级 key-value 数据库。它跟 memcached 类似,不过数据可以持久化,而且支持的数据类型很丰富。在运维的工作中,缓存是一个非常重要的技术,静态文件的缓存我们有:nginx 的缓存,squid 的缓存等,数据库的缓存我们有 redis 和 memcache 等,甚至有些公司会选择做 redis 的集群。上次我们讲了 redis 的缓存的安装和简单的配置上,现在我们来看下 redis 的配置和后期的压测 |
设置环境变量添加 redis 相关命令
Redis 的运行模式对比
在 Linux 系统进行 make 后,我们其实是可以直接进入安装包的 ”/usr/local/src/redis-3.2.9/src” 直接运行 redis 的,这种呢是一种非系统守护进程的模式运行的:
[root@zbbix redis-3.2.9]# ls
00-RELEASENOTES BUGS CONTRIBUTING COPYING deps INSTALL Makefile MANIFESTO README.md redis.conf runtest runtest-cluster runtest-sentinel sentinel.conf src tests utils
[root@zbbix redis-3.2.9]# pwd
/usr/local/src/redis-3.2.9
[root@zbbix redis-3.2.9]# src/redis-server
9795:C 11 Jul 23:34:09.872 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
9795:M 11 Jul 23:34:09.875 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
(' , .-` | `,) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 9795
`-._ `-._ `-./ _.-'_.-'
|`-._`-._ `-.__.-'_.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'`-._ `-.__.-' _.-'`-._ _.-'
`-.__.-'
9795:M 11 Jul 23:34:09.883 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9795:M 11 Jul 23:34:09.884 # Server started, Redis version 3.2.9
9795:M 11 Jul 23:34:09.884 # 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.
9795:M 11 Jul 23:34:09.891 # 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.
9795:M 11 Jul 23:34:09.891 * The server is now ready to accept connections on port 6379
^C9795:signal-handler (1499787252) Received SIGINT scheduling shutdown...
9795:M 11 Jul 23:34:12.419 # User requested shutdown...
9795:M 11 Jul 23:34:12.419 * Saving the final RDB snapshot before exiting.
9795:M 11 Jul 23:34:12.481 * DB saved on disk
9795:M 11 Jul 23:34:12.481 # Redis is now ready to exit, bye bye...
[root@zbbix redis-3.2.9]# src/redis-server &
[1] 9798
[root@zbbix redis-3.2.9]# 9798:C 11 Jul 23:34:17.785 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
9798:M 11 Jul 23:34:17.786 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
(' , .-` | `,) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 9798
`-._ `-._ `-./ _.-'_.-'
|`-._`-._ `-.__.-'_.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'`-._ `-.__.-' _.-'`-._ _.-'
`-.__.-'
9798:M 11 Jul 23:34:17.788 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9798:M 11 Jul 23:34:17.788 # Server started, Redis version 3.2.9
9798:M 11 Jul 23:34:17.788 # 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.
9798:M 11 Jul 23:34:17.788 # 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.
9798:M 11 Jul 23:34:17.789 * DB loaded from disk: 0.001 seconds
9798:M 11 Jul 23:34:17.789 * The server is now ready to accept connections on port 6379
[root@zabbix redis-3.2.9]#
[root@zabbix redis-3.2.9]#
[root@zabbix redis-3.2.9]#
[root@zabbix redis-3.2.9]#
[root@zabbix redis-3.2.9]#
[root@zabbix redis-3.2.9]#
[root@zabbix redis-3.2.9]# src/redis-cli
127.0.0.1:6379>
127.0.0.1:6379> info [section]
127.0.0.1:6379> info
# Server
redis_version:3.2.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:a2c99427c4076cd4
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
添加 redis 命令的系统变量
#echo "export PATH=$PATH:/usr/local/redis/bin >>/etc/profile \\ 添加环境变量
#tail -n 1 /etc/profile \\ 检查系统环境变量
export PATH=$PATH:/usr/local/redis/bin
重新加载环境变量:
#source /etc/profile
检查 redis 命令 ---
# redis-
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
######reidis 环境变量添加完成
redis 命令含义
redis-server:Redis 服务器的 daemon 启动程序
redis-cli:Redis 命令行操作工具。也可以用 telnet 根据其纯文本协议来操作
redis-benchmark:Redis 性能测试工具,测试 Redis 在当前系统下的读写性能
redis-check-aof:数据修复
redis-check-dump:检查导出工具
Redis 启动
[root@zabbix etc]# service redis start
Starting Redis server...
[root@zabbix etc]# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 9625 root 4u IPv4 49878 0t0 TCP *:6379 (LISTEN)
[root@zabbix etc]#
[root@zabbix etc]#
[root@zabbix etc]# ps -ef |grep redis
root 9625 1 0 23:27 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 9630 4171 0 23:28 pts/1 00:00:00 grep redis
[root@zbbix redis-3.2.9]# src/redis-cli
127.0.0.1:6379>
127.0.0.1:6379> info [section]
127.0.0.1:6379> info
# Server
redis_version:3.2.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:a2c99427c4076cd4
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PS:如果启用了密码验证,在 cli 中需要先添加 auth mima 然后才能操作命令
检查 redis 服务启动
ps -ef |grep redis
lsof -i:6379
netstat -tanlp |grep 6379
查看 redis 的系统日志:
tail -f /usr/local/redis/redis.log
9766:M 11 Jul 23:32:38.320 # 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.
9766:M 11 Jul 23:32:38.320 # 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.
9766:M 11 Jul 23:32:38.321 * DB loaded from disk: 0.000 seconds
9766:M 11 Jul 23:32:38.321 * The server is now ready to accept connections on port 6379
9766:M 11 Jul 23:32:38.321 - 0 clients connected (0 slaves), 759680 bytes in use
9766:M 11 Jul 23:32:43.363 - 0 clients connected (0 slaves), 759680 bytes in use
XXXXX
redis 服务器内核调优:
法一:a) echo vm.overcommit_memory=1 >> /etc/sysctl.conf
法二:b) sysctl vm.overcommit_memory=1 或执行
echo vm.overcommit_memory=1 >>/proc/sys/vm/overcommit_memory
内核 vm.overcommit_memory 数字含义:
reids 系统压力测试工具
redis 自带有自己的压力测试工具,在 redis 上生产环境的时候一定要确定 redis 的性能。下面是使用 redis-benchmark 的实例 (来着互联网):
Redis-benchmark 使用实例:
root@iZwz94wvtlc4lisg3mgkweZ:/usr/local/redis-3.2.9/utils# redis-benchmark -c 100 -n 100000
====== PING_INLINE ======
100000 requests completed in 1.29 seconds
100 parallel clients
3 bytes payload
keep alive: 1
80.86% <= 1 milliseconds
99.83% <= 2 milliseconds
99.95% <= 3 milliseconds
100.00% <= 3 milliseconds
77639.75 requests per second
====== PING_BULK ======
100000 requests completed in 1.33 seconds
100 parallel clients
3 bytes payload
keep alive: 1
79.37% <= 1 milliseconds
99.84% <= 2 milliseconds
99.93% <= 3 milliseconds
100.00% <= 3 milliseconds
75414.78 requests per second
====== SET ======
100000 requests completed in 1.28 seconds
100 parallel clients
3 bytes payload
keep alive: 1
81.20% <= 1 milliseconds
99.85% <= 2 milliseconds
99.92% <= 3 milliseconds
100.00% <= 3 milliseconds
77942.32 requests per second
====== GET ======
100000 requests completed in 1.29 seconds
100 parallel clients
3 bytes payload
keep alive: 1
80.94% <= 1 milliseconds
99.90% <= 2 milliseconds
99.99% <= 3 milliseconds
100.00% <= 3 milliseconds
77459.34 requests per second
====== INCR ======
100000 requests completed in 1.28 seconds
100 parallel clients
3 bytes payload
keep alive: 1
81.77% <= 1 milliseconds
99.79% <= 2 milliseconds
99.92% <= 3 milliseconds
99.97% <= 4 milliseconds
100.00% <= 4 milliseconds
78308.54 requests per second
xxxxxxxxxxxxxxxx
我们看到 70000 多的峰值 5s 左右
检查 redis 重启动脚本
经过以上的操作我们的 redis 服务应该已经配置差不多了,但是我们要有一个检测机制来确定 redis 服务的正常运行。在这里呢我们写了一个脚本来检测我们的 redis 服务,如果服务出现问题,我们则重启 redis 服务。(不建议,建议使用监控系统了预警 redis 服务!!!)
检测脚本内容
cat check-redis-restart.sh
#!/bin/sh
#this is check redis and restart redis
lsof -i:6379 |grep LISTEN >> /tmp/reids-status.log
if [$? -eq 1]; then
echo "`date` redis is down,restart it" >> /usr/local/redis/redis-check-error.log
rm -rf /var/run/redis.pid >> /usr/local/redis/redis-check-error.log
/etc/init.d/redis stop >> /usr/local/redis/redis-check-error.log
/etc/init.d/redis start >> /usr/local/redis/redis-check-error.log
fi
将脚本添加到 crontab 任务中 1 分钟执行一次
crontab -e
* * * * * cd /usr/local/redis/bin/ && ./check-redis-restart.sh >> /usr/local/redis/edis-check-error.log 2>&1
重启 crontab 服务:
service cron restart
chkconfig cron on
检查重启 redis 日志:
tail -f /tmp/reids-status.log
tail -f /usr/local/redis/redis-check-error.log
正文完
星哥玩云-微信公众号