导读 | Redis是一种高级key-value数据库。它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富。在运维的工作中,缓存是一个非常重要的技术,静态文件的缓存我们有:nginx的缓存,squid的缓存等,数据库的缓存我们有redis和memcache等,甚至有些公司会选择做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 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | //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...
我们发现这种模式下我们不能退出这个界面,当我们按ctrl+c的时候,redis服务也跟着停止了。我们尝试以后台进程的形式进行:
[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 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | //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
后台进程是ok的,我们可以正常操作,但是对于进程的管理我们只能通过"ps -ef |grep redis |grep -v redis |awk '{print$1}' | xargs kill -9" 来进程结束redis进程,通过"/usr/local/src/redis-3.2.9/src/src/redis-server &"来启动进程,这样并不是太方便。于是有了我们安装redis并以守护进程的方式运行。由于我们制定了我们redis的安装位置,我们的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-server:Redis服务器的daemon启动程序 redis-cli:Redis命令行操作工具。也可以用telnet根据其纯文本协议来操作 redis-benchmark:Redis性能测试工具,测试Redis在当前系统下的读写性能 redis-check-aof:数据修复 redis-check-dump:检查导出工具
[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 然后才能操作命令
ps -ef |grep redis lsof -i:6379 netstat -tanlp |grep 6379
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
法一: 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数字含义:
0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2,表示内核允许分配超过所有物理内存和交换空间总和的内存
redis 自带有自己的压力测试工具,在redis上生产环境的时候一定要确定redis的性能。下面是使用redis-benchmark的实例(来着互联网):
1、redis-benchmark -h 192.168.1.201 -p 6379 -c 100 -n 100000
100个并发连接,100000个请求,检测host为localhost 端口为6379的redis服务器性能2、redis-benchmark -h 192.168.1.201 -p 6379 -q -d 100
测试存取大小为100字节的数据包的性能3、redis-benchmark -t set,lpush -n 100000 -q
只测试某些操作的性能4、redis-benchmark -n 100000 -q script load "redis.call('set','foo','bar')"
只测试某些数值存取的性能
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服务!!!)
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 -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
本文原创地址://q13zd.cn/redis-install-conf2.html作者:陶武杰,审核员:逄增宝
本文原创地址://q13zd.cn/redis-install-conf2.html编辑:public,审核员:暂无