Redis basic concepts and the use of the single cluster deployment

1. Redis basis

1.1 Redis Overview

Redis is an open source, advanced key-value store, and used to build high-performance, scalable solution for perfect application.

Redis inherited from its many competitive three main features:
   ①Redis database entirely in memory, using disk only for persistence;
   ② compared to many key-value pair data storage, Redis has a wealth of data types;
   ③Redis can copy data to an arbitrary amount of data from the server;

1.2 Redis advantage

Exceptionally fast : Redis is very fast, it can perform about 11 million times set operations per second, about 81000 times get operations per second;

Support for rich data types : Redis support string string, a list of list, a collection set, ordered set sorted set, hash map data types, which makes it very easy to solve a variety of problems;

Atomic operations are : All operations are Redis atoms;

Multi-function tools : Redis is a multi-functional tool for a variety of use cases, such as caching, message queue (the Redis native support publish / subscribe), the application of any short-term data, such as: web application session, page hit count and the like;

Atomicity (atomicity) Description: A transaction is an indivisible unit of minimum work either succeed or fail.

2. Redis deploy stand-alone installation

2.1 Download redis Redis source package from the official website, and upload it to the machine by Xftp5 cluster

Download redis-4.0.9.tar.gz version and upload it to the / opt / uploads / directory on the first cluster node node1 machine by Xftp5:

2.2 decompression redis-4.0.9.tar.gz, and to extract the installation package is moved to / opt / app / directory

takes -zxvf Redis-4.0.9.tar.gz

mv redis-4.0.9.tar.gz  /opt/app/  && cd /opt/app/

Redis 2.3 compiler source code

Before compiling preparations

Redis into the directory, cd /opt/app/redis-4.0.9

Create a source directory source Redis in the directory and all files in the source directory to /opt/app/redis-4.0.9 source directory source, after re-create the installation directory redis

source mkdir
# "|" next expression indicates a data acquisition result from the expression
# "grep -v source" indicates reverse selection, i.e., to find no source line
# "xargs -i mv {} / opt / app / redis-4.0.9 / source / ", the upper xargs represents a result of expression of the expression as a parameter set, {} represents a traverse
ls | grep -v source | xargs -i mv {} / opt / app / redis -4.0.9 / source /

Create a directory to install redis

Start compiling

Redis into the build directory Source, cd /opt/app/redis-4.0.9/source

Install gcc, and then make the compiler

sudo yum install gcc
make

The installation of the executable file compiled to make the next /opt/app/redis-4.0.9/redis directory

make PREFIX=/opt/app/redis-4.0.9/redis  install

在redis安装目录/opt/app/redis-4.0.9/redis  ll查看,可以看到新创建bin目录

 

可以看到bin目录有几个可执行文件

2.4 修改环境变量(每台机器都要执行),编辑/etc/profile,并生效环境变量,输入如下命令:

sudo vi /etc/profile

添加如下内容:

export REDIS_HOME=/opt/app/redis-4.0.9/redis
export PATH=:PATH:PATH:REDIS_HOME/bin

使环境变量生效:source /etc/profile

2.5 Redis部署启动方式

启动方式一:Redis前台默认启动

直接启动Redis服务

启动方式二:Redis使用配置文件启动:

进入Redis的安装目录,cd  /opt/app/redis-4.0.9/redis

创建conf目录,并将redis的源码目录/opt/app/redis-4.0.9/redis中的redis.conf拷贝到conf目录里:

cp  /opt/app/redis-4.0.9/source/redis.conf  /opt/app/redis-4.0.9/redis/conf/

进入conf目录,修改配置文件redis.conf,有三点需要修改:

①修改属性  daemonize no -> daemonize yes 

②修改生成默认日志文件位置 

在redis.conf相应的位置添加 logfile "/opt/app/redis-4.0.9/redis/logs/redis.log" 

在/opt/app/redis-4.0.9/redis/目录下创建logs目录,mkdir logs目录

③配置持久化文件存放位置

在redis.conf相应的位置添加 dir  /opt/app/redis-4.0.9/redis/data

在/opt/app/redis-4.0.9/redis/目录下创建data目录,mkdir data目录

启动Redis服务,redis-server  /opt/app/redis-4.0.9/redis/conf/redis.conf

启动方式三:配置Linux自动启动:

1、准备自启动配置文件redisd

拷贝Redis源码目录source的utils目录下redis_init_script文件到/etc/init.id目录下

sudo cp  /opt/app/redis-4.0.9/source/utils/redis_init_script  /etc/init.d/redisd  (通常都以d结尾表示是后台自启动服务)

2、修改自启动配置文件redisd

redisd原本内容:

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis/${REDISPORT}.conf"

将redistask文件内容修改为如下:

REDISPORT=6379
EXEC=/opt/app/redis-4.0.9/redis/bin/redis-server
CLIEXEC=/opt/app/redis-4.0.9/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/app/redis-4.0.9/redis/conf/${REDISPORT}.conf"

3、准备开机启动的配置文件

cp  /opt/app/redis-4.0.9/redis/conf/redis.conf   /opt/app/redis-4.0.9/redis/conf/6379.conf

4、设置开机启动

chkconfig  redisd  on

此处直接配置开启自启动chkconfig redisd on 将报错误: service redisd does not support chkconfig 

在启动脚本redisd开头添加如下两行注释以修改其运行级别:

#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
#

chkconfig redisd on没有权限,则应用sudo去执行,sudo chkconfig redisd on

开启redisd服务,sudo service redisd start

关闭redisd服务,sudo service redisd stop

注意:开启关闭redisd服务应该用sudo,否则设置的redis_6397.id无法创建,导致能成功开启redisd服务,却不能通过service redisd stop关闭,只能通过kill关闭,如图:

正常的应为:

3. Redis集群部署

3.1 环境准备

准备三台虚拟机,如图:

 

局域网IP分别为192.168.187.201、192.168.187.202和192.168.187.203

3.2 安装Redis

详情参考第2节点redis的安装部署步骤,这里我们先在192.168.187.201上安装redis并配置好,然后其余两个节点以scp形式拷贝过去。

安装redis完毕,如图所示:

3.3 修改集群配置文件,创建节点

首先我们在192.168.187.201虚拟机里创建主从两个节点,端口分别是7001,7002

我们在/opt/app/redis-4.0.9目录下新建一个redis-cluster目录,如图:

然后在redis-cluster目录下创建conf、nodes与scripts目录,conf用于存放集群节点配置文件,scripts用于存放redis的shell脚本文件,如图:

进入conf目录,拷贝/opt/app/redis-4.0.9/source/redis.conf到redis-cluster/conf目录

分别复制redis.conf为7001.conf和7002.conf,端口7001对应7001.conf配置文件,端口7002对应7002.conf配置文件,如图:

分别修改7001.conf和7002.conf相应的端口、ip等配置信息,修改配置信息如下:

复制代码
port 7001 #六个节点配置文件分别是7001-7006
bind 192.168.187.201 #默认ip为127.0.0.1,需要改为其他节点机器可访问ip,否则创建集群时无法访,和单机集群有区别
daemonize yes #redis后台运行
pidfile /var/run/redis_7001.pid #pidfile文件对7001-7006
cluster-enabled yes #开启集群
cluster-config-file nodes_7001.conf #保存节点配置,自动创建,自动更新对应7001-7006
cluster-node-timeout 5000 #集群超时时间,节点超过这个时间没反应就断定是宕机
appendonly yes #存储方式,aof,将写操作记录保存到日志中
复制代码

3.4 scp复制redis-4.0.9整个文件夹到其他两台机器上

复制到node2、node3,node2即192.168.187.202,node3即192.168.187.203上

我们在192.168.187.202虚拟机里创建主从两个节点,端口分别是7003,7004,7003端口对应redis-cluster/conf/7003.conf配置文件,7004端口对应redis-cluster/conf/7004.conf配置文件

我们在192.168.187.203虚拟机里创建主从两个节点,端口分别是7005,7006,7005端口对应redis-cluster/conf/7005.conf配置文件,7006端口对应redis-cluster/conf/7006.conf配置文件

在192.168.187.202上查看redis-4.0.9是否拷贝过来

 

修改用户名和用户组为hadoop,如果是普通用户hadoop的话需要修改,如果是root用户登录的则不用。这里我使用hadoop用户登录,hadoop用户执行不了root的文件,故需要修改用户名和用户组为hadoop。

分别把7001.conf和7002.conf文件重命名为7003.conf和7004.conf

分别修改7003.conf和7004.conf文件里面的配置信息,按照3.3节点里面最后提供的配置信息,修改对应的端口、ip、pidfile和cluster-config-file

192.168.187.203上redis的修改如同192.168.187.202的一致

3.5 启动三台机器的六个节点

192.168.187.201机器 7001和7002节点

192.168.187.202机器 7003和7004节点

 

192.168.187.203机器 7005和7006节点

3.6 关闭防火墙

直接把三台机器的防火墙关闭

systemctl stop firewalld.service

3.7 创建集群

这里我们以192.168.187.201作为集群控制端,redis官方提供了在源码文件夹中redis-trib.rb工具,把/opt/app/redis-4.0.9/source/src/redis-trib.rb文件拷贝到/opt/app/redis-4.0.9/redis/bin目录下

在使用redis-trib.rb之前,需要安装ruby,以及redis和ruby连接

yum -y install ruby ruby-devel rubygems rpm-build
gem install redis

上述命令在机器是外网情况,可以顺利安装成功。

但是在内网情况,需要通过在内网建立一个yum源安装,内网yum源安装可以参考https://www.cnblogs.com/swordfall/p/8655084.html 14.2 制作本地YUM源

在内网情况下还有一个问题,就是redis和ruby连接无法安装成功,“gem install redis”该命令无法成功,这里只能采用离线安装

在这里,我们可以在redisgems官网下载redis和ruby的连接插件https://rubygems.org/gems/redis/versions/,如图,这里我们下载3.2.1版本,版本太高与ruby不兼容

下载完redis-3.2.1.gem后,通过Xftp5复制到192.168.187.201节点上,然后通过命令“gem install redis-3.2.1.gem”离线安装,如图:

通过redis-trib.rb命令启动集群, replicas表示从服务器的个数,"--replicas 1"表示6个节点三主三从

./redis-trib.rb create --replicas 1 192.168.187.201:7001 192.168.187.201:7002 192.168.187.202:7003 192.168.187.202:7004 192.168.187.203:7005 192.168.187.203:7006

从运行结果看,主节点是7001 7003 7005,从节点分别是7002 7004 7006

7001分配到的哈希值是0-5460

7003分配到的哈希值是5461-10922

7005分配到的哈希值是10923-16383

最后问我们是否接受上面的设置,输入yes就表示接受,我们输入yes

然后显示:

显示配置哈希槽,以及集群创建成功,可以用了。

3.8 集群数据测试

我们先连接任意一个节点,然后添加一个key:

redis-cli是redis默认的客户端工具,启动时加上`-c `参数,`-p `指定端口,就可以连接到集群,这里还得加-h指定机器IP

连接7001节点端口:

我们从其他集群节点,都可以获取到数据,如:

4. Redis客户端使用

4.1 启动Redis服务并使用客户端访问Redis服务

启动Redis服务

使用上述第五节点任意一种启动方式启动redis服务,在这里,我采用第三种方式启动redis服务

sudo service redisd start

进入redis安装目录log目录,查看redis.log日志,出现下图则redis正常启动

cd redis-4.0.9/redis/logs/ 
cat redis.log

redis-cli客户端访问Redis服务

 redis-cli

 

查看Redis是否存在

ps aux | grep redis 或 ps -ef | grep redis
netstat -tunlp 6379  或 netstat -nltp 6379

5. Redis持久化

Redis有两种持久化方案:RDB和AOF

1) RDB方式按照一定的时间间隔对数据集创建基于时间点的快照;

2)AOF方式记录Server收到的写操作到日志文件,在Server重启时通过回收这些写操作来重建数据集。该方式类似于MySQL中基于语句格式的binlog。当日志变大时Redis可在后台重写日志。

5.1 RDB持久化配置

 默认情况下,Redis保存数据集快照到磁盘,名为dump.rdb的二进制文件。可以设置让Redis在N秒内至少有M次数据集改动时保存数据集,或者你也可以手动调用SAVE或者BGSAVE命令。例如,这个配置会让Redis在每个60秒内至少有1000次键改动时自动转储数据集到磁盘。

save 60 1000

5.2 AOF 持久化配置

1)修改redis.config配置文件,找到appendonly。默认是appendonly no。改成appendonly yes;

2)再找到appendfsync。默认是appendfsync everysec
  appendfsync always
  # 每次收到写命令就立即强制写入磁盘,最慢的,但是保证完全的持久化,不推荐使用
  appendfsync everysec
  #每秒钟强制写入磁盘一次,在性能和持久化方面做了很好的折中,推荐
  appendfsync no
  #完全依赖os,性能最好,持久化没保证

6. Redis数据结构与常用命令

6.1 Redis数据结构

Redis数据结构有5种,分别是String、Map、List、Set、SortSet这5种。

6.2 Redis-cli中常用命令

命令 说明
info Keyspace 查看存储数据的相关信息
keys *  查看所有的 key
del key 删除单个

 

7. 可能遇到的问题

问题1:JedisConnectionException: java.net.ConnectException: Connection refused: connect 
解决如下:Redis的配置文件redis.conf里bind 127.0.0.1 注释掉。 
                  bind localhost 只能本机访问,局域网内计算机不能访问

问题2:Jedis连接Linux上的redis出现 DENIED Redis is running in protected mode问题
解决如下:由于Linux上的redis处于安全保护模式,这就让你无法从虚拟机外部去轻松建立连接,这里就有两种解决方法,一种是在redis.conf中设置保护模式为no

另外一种方式是加上安全认证,即redis默认是没有密码的可以直接登录,这里加上密码“11111”

在外部用Jedis连接linux的redis,需要添加密码,如:

 

参考资料:

https://blog.csdn.net/a532672728/article/details/78035559

Guess you like

Origin www.cnblogs.com/momoyan/p/11616458.html