Redis编译安装和saltstack简单配置(1)

分布式存储之-Redis
redis常用于数据库读缓存和写缓存,通常写缓存需要考虑到数据一致性的问题,读缓存应用较多
redis是一个开源的,使用c语言编写的,支持网络交互的,可基于内存也可持久化的key-value数据库

目前国内最大的redis集群--新浪微博

redis和memcached对比

[root@yum-down local]# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
[root@yum-down local]# tar xf redis-3.0.7.tar.gz
[root@yum-down local]# cd redis-3.0.7
[root@yum-down redis-3.0.7]# make PREFIX=/usr/local/redis install
[root@yum-down utils]# cp redis_init_script /etc/init.d/redis
[root@yum-down utils]# chmod +x /etc/init.d/redis

修改脚本中的安装路径

[root@yum-down utils]# vim /etc/init.d/redis
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

创建配置文件路径,并复制配置文件以端口号命名

[root@yum-down redis-3.0.7]# mkdir /etc/redis
[root@yum-down redis-3.0.7]# cp /usr/local/redis-3.0.7/redis.conf /etc/redis/6379.conf

第一次启动,如果没有修改是启动在前台页面

[root@yum-down ~]# /etc/init.d/redis start
Starting Redis server...
5228:M 24 Feb 07:16:27.342 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 5228
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5228:M 24 Feb 07:16:27.344 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5228:M 24 Feb 07:16:27.344 # Server started, Redis version 3.0.7
5228:M 24 Feb 07:16:27.344 # 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.
5228:M 24 Feb 07:16:27.344 # 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.
5228:M 24 Feb 07:16:27.344 * The server is now ready to accept connections on port 6379
^C5228:signal-handler (1456327043) Received SIGINT scheduling shutdown...
5228:M 24 Feb 07:17:23.607 # User requested shutdown...
5228:M 24 Feb 07:17:23.607 * Saving the final RDB snapshot before exiting.
5228:M 24 Feb 07:17:23.611 * DB saved on disk
5228:M 24 Feb 07:17:23.611 # Redis is now ready to exit, bye bye...

修改配置文件‘daemonize 改为yes’

[root@yum-down ~]# vim /etc/redis/6379.conf
daemonize yes

修改pid和/etc/init.d/redis中的pid一致

pidfile /var/run/redis_6379.pid
[root@yum-down ~]# /etc/init.d/redis start
Starting Redis server...
[root@yum-down ~]# netstat -ntlp |grep redis
tcp        0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      5235/redis-server 
tcp        0      0 :::6379                     :::*                        LISTEN      5235/redis-server 

关闭测试

[root@yum-down ~]# service redis stop
Stopping ...
Redis stopped
[root@yum-down ~]# 

做一个软连接启动:

[root@yum-down ~]# ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli                
[root@yum-down ~]# redis-cli 
127.0.0.1:6379> 

由于redis是单线程,只能使用一个cpu,可以启动多个,则可以指定ip和端口

[root@yum-down ~]# redis-cli -h 10.10.0.250 -p 6379
10.10.0.250:6379> 

简单的使用:
set插入字符串,get查看

[root@yum-down ~]# redis-cli -h 10.10.0.250 -p 6379
10.10.0.250:6379> set keyname hello
OK
10.10.0.250:6379> get keyname
"hello"
10.10.0.250:6379> 

过滤:
显示所有

10.10.0.250:6379> keys *
1) "keya"
2) "keyna"
3) "keyname1"
4) "keyname"

匹配keyname开头

10.10.0.250:6379> keys keyname?
1) "keyname2"
2) "keyname1"
10.10.0.250:6379> 

判断key是否存在,0不存在,非0则存在,表示数量

10.10.0.250:6379> EXISTS keya
(integer) 1
10.10.0.250:6379> EXISTS keya1
(integer) 0
10.10.0.250:6379> 

删除:0不存在,非0则存在,表示数量

10.10.0.250:6379> DEL keya
(integer) 1
10.10.0.250:6379> DEL keya
(integer) 0
10.10.0.250:6379> 

info:

# Keyspace
db0:keys=2,expires=0,avg_ttl=0

db0:实例
如下:SELECT 1和2两个实例,分别插入数据

10.10.0.250:6379[2]> SELECT 1
OK
10.10.0.250:6379[1]> SELECT 2
OK
10.10.0.250:6379[2]> SET 1 1
OK
10.10.0.250:6379[2]> SELECT 1
OK
10.10.0.250:6379[1]> SET 1 1
OK

在使用info查看就有了1和2的实例

# Keyspace
db0:keys=2,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
db2:keys=1,expires=0,avg_ttl=0

flushall可清空所有实例

10.10.0.250:6379[1]> flushall

keys:key数量
expires:过期数量
avg_ttl:ttl数量

简单的配置文件说明:

[root@yum-down ~]# egrep -v  "^#|^$" /etc/redis/6379.conf
daemonize yes  前后台启动
pidfile /var/run/redis.pid pid位置
port 6379  端口
logfile ""   日志
dir ./ 启动的目录
[root@yum-down ~]# 

redis-saltstack配置
1,复制安装包到salt/redis/files/
2,复制配置文件到salt/redis/files/
3, 复制启动脚本到salt/redis/files/目录下,没有则创建;top中指定即可
install.sls如下:

  file.managed:
    - name: /usr/local/src/redis-3.0.7.tar.gz
    - source: salt://redis/files/redis-3.0.7.tar.gz
    - user: root
    - group: root
    - mode: 755
cmd.run:
    - name: cd /usr/local/src && tar xf redis-3.0.7.tar.gz && cd redis-3.0.7 && PREFIX=/usr/local/redis install
    - unless: test -d /usr/local/redis
redis-config:
  file.managed:
    - name: /etc/redis/6379.conf
    - spurce: salt://redis/files/6379.conf
    - user: root
    - group: root 
    - mode: 644
redis-service:
  file.managed:
    - name: /etc/init.d/redis
    - source: salt://redis/files/redis.init
    - user: root
    - group: root
    - mode: 755
  cmd.run
    - name: chkconfig --add redis && chkconfig redis on
    - unless: chkconfig --list |grep redis
  service.runnig:
    - name: redis
    - emable: True
    - watch:
      - file: redis-config
    - require:
      - cmd: redis-install
      - cmd: redis-service
0 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2016-02-25分类: Redis

标签: redis

发表评论