Redis安装配置与集群安装

虽然元旦已经过去7天了, 但还是跟大家说一声 ‘元旦快乐’。 这段时间比较忙啊,毕竟公司项目和money挂钩,必须要小心再小心啊O(∩_∩)O哈哈~

今天记录下redis的安装过程

介绍

根据Redis官方介绍, Redis是一种开源(BSD许可)、内存中的数据结构存储,用作数据库、缓存和消息代理,它支持数据结构,如字符串、散列、列表、集合、带范围查询的排序集、位图、超loglogs、带半径查询的地理空间索引和流。Redis具有内置的复制、Lua脚本、LRU退出、事务和不同级别的磁盘持久性,并通过Redis Sentinel和使用Redis集群的自动分区提供高可用性。

Redis一共支持5种数据类型的存储

  1. string

string类型是最基础的存储类型,一个key对应一个value。 最大可以存储512M

  1. hash

hash 是一个键值(key=>value)对集合。

  1. list

list是简单的字符串列表

  1. set

set是string类型的无序集合。 且集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。

  1. zset

zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。

Redis 单机安装

这次通过wget直接下载然后安装

  1. 进入redis官网, 找到下载地址
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
  1. 解压目录
tar -zxvf redis-5.0.3.tar.gz
  1. 我习惯将redis的启动配置文件安装到/opt/下 所以先进行这步操作,可以依据自己的喜好来安装
mkdir -p /opt/redis
  1. 安装 (使用 PREFIX 指定安装目录)
make PREFIX=/opt/redis install

如果make出错, 执行yum install gcc

  1. 安装完成后,可以看到/opt/redis 目录下有一个 bin 目录,bin 目录里就是 redis 的命令脚本:
    redis-benchmark
    redis-check-aof
    redis-check-dump
    redis-cli
    redis-server
  2. copy配置文件到/opt/redis/下
cp redis.conf /opt/redis/
  1. Redis 启动
# 进入/opt/redis下
cd /opt/redis/
# 启动
./bin/redis-server ./redis.conf
# 查看是否启动
ps -ef | grep redis

上面的启动是在前台启动, 在后台启动有一下两种方式

方式一 & 代表 运行到后台

./bin/redis-server ./redis.conf &   

方式二 修改redis.conf文件 找到daemonize 把no改成yes

daemonize yes
# 启动
./bin/redis-server ./redis.conf

集群安装

关于Redis集群的安装, 这里有Redis集群规范,大家可以参考下
针对Ruby方式的集群, 可以参考文档Redis集群指南

Redis 5.0之后放弃使用Ruby方式的集群, 改用C语言方式的redis-cli 集群, 简化了集群方式

集群环境

CentOs 7
Redis 5.x

环境配置

要让 Redis3.0 集群正常工作至少需要 3 个 Master 节点,要想实现高可用,每个 Master 节点要配备
至少 1 个 Slave 节点。根据以上特点和要求,进行如下的集群实施规划:

主从复制
主从复制
主从复制
Slave2:192.168.87.129:7112
Master1:192.168.87.129:7111
Slave4:192.168.87.130:7114
Master3:192.168.87.130:7112
Slave6:192.168.87.131:7116
Master5:192.168.87.129:7113

下面进行安装 (下载, 解压同单机安装)

  1. 因为现在一个机器上需要安装两个Redis, 所以在PREFIX指定目录的时候需要进行区分

192.168.87.129

mkdir -p /opt/redis/redis-7111
mkdir -p /opt/redis/redis-7112

192.168.87.130

mkdir -p /opt/redis/redis-7113
mkdir -p /opt/redis/redis-7114

192.168.87.131

mkdir -p /opt/redis/redis-7115
mkdir -p /opt/redis/redis-7116
  1. 安装 指定PREFIX

192.168.87.129

make PREFIX=/opt/redis/redis-7111 install
make PREFIX=/opt/redis/redis-7112 install

192.168.87.130

make PREFIX=/opt/redis/redis-7113 install
make PREFIX=/opt/redis/redis-7114 install

192.168.87.131

make PREFIX=/opt/redis/redis-7115 install
make PREFIX=/opt/redis/redis-7116 install
  1. copy配置文件

192.168.87.129

mkdir -p /opt/redis/redis-7111/cluster/7111
mkdir -p /opt/redis/redis-7112/cluster/7112
cp /usr/local/opt/redis-5.0.3/redis.conf /opt/redis/redis-7111/cluster/7111
cp /usr/local/opt/redis-5.0.3/redis.conf /opt/redis/redis-7112/cluster/7112

192.168.87.130

mkdir -p /opt/redis/redis-7113/cluster/7113
mkdir -p /opt/redis/redis-7114/cluster/7114
cp /usr/local/opt/redis-5.0.3/redis.conf /opt/redis/redis-7113/cluster/7113
cp /usr/local/opt/redis-5.0.3/redis.conf /opt/redis/redis-7114/cluster/7114

192.168.87.131

mkdir -p /opt/redis/redis-7115/cluster/7115
mkdir -p /opt/redis/redis-7116/cluster/7116
cp /usr/local/opt/redis-5.0.3/redis.conf /opt/redis/redis-7115/cluster/7115
cp /usr/local/opt/redis-5.0.3/redis.conf /opt/redis/redis-7116/cluster/7116
  1. 修改配置文件, 配置文件需要修改以下几点
port 7111  #端口  一一对应修改
cluster-enabled yes #启用集群模式
cluster-config-file /opt/redis/redis-7111/cluster/7111/nodes.conf   # 一一对应修改
cluster-node-timeout 5000 #超时时间
appendonly yes
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile  /var/run/redis_7111.pid    # 一一对应修改
dir  /opt/redis/redis-7111/cluster/7111    # 一一对应修改(非必须, 建议配置)
databases  1
  1. 使用以下命令启动Redis

192.168.87.129

./opt/redis/redis-7111/bin/redis-server /opt/redis/redis-7111/cluster/7111/redis-7111.conf
./opt/redis/redis-7112/bin/redis-server /opt/redis/redis-7112/cluster/7112/redis-7112.conf

192.168.87.130

./opt/redis/redis-7113/bin/redis-server /opt/redis/redis-7113/cluster/7113/redis-7113.conf
./opt/redis/redis-7114/bin/redis-server /opt/redis/redis-7114/cluster/7114/redis-7114.conf

192.168.87.131

./opt/redis/redis-7115/bin/redis-server /opt/redis/redis-7115/cluster/7115/redis-7115.conf
./opt/redis/redis-7116/bin/redis-server /opt/redis/redis-7116/cluster/7116/redis-7116.conf

启动之后通过ps -ef | grep redis 查看启动结果

启动成功后接下来准备创建集群
随便在那一台机器上操作都可以,

 /usr/local/opt/redis-5.0.3/src/redis-cli --cluster create 192.168.87.129:7111 192.168.87.130:7113 192.168.87.131:7115 192.168.87.129:7112 192.168.87.130:7114 192.168.87.131:7116 --cluster-replicas 1

需要设置那些为master就将地址写在前面

稍等片刻会出现确认信息, 仔细查看节点配置是否正确。 正确输入yes即可

 Creating cluster
Connecting to node 192.168.87.129:7111: OK
Connecting to node 192.168.87.130:7113: OK
Connecting to node 192.168.87.131:7115: OK
Connecting to node 192.168.87.129:7112: OK
Connecting to node 192.168.87.130:7114: OK
Connecting to node 192.168.87.131:7116: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.87.129:7111
192.168.87.130:7113
192.168.87.131:7115
Adding replica 192.168.87.129:7112 to 192.168.87.129:7111
Adding replica 192.168.87.130:7114 to 192.168.87.130:7113
Adding replica 192.168.87.131:7116 to 192.168.87.131:7115
M: 007a3fe8d7451d3d0a78fffd2653c8641809499c 192.168.87.129:7111
 replicates 94e140b9ca0735040ae3428983835f1d93327aeb
M: ea69b6b6e2e7723eed50b1dabea9d244ccf3f098 192.168.87.130:7113
 replicates c642b3071c4b2b073707ed3c3a2c16d53a549eff
M: 5f09dc0671732cf06a09f28631c90e0c68408520 192.168.87.131:7115
 replicates 896a3c99da4fcf680de1f42406fccb551d8c40c3
S: 94e140b9ca0735040ae3428983835f1d93327aeb 192.168.87.129:7112
 slots:10923-16383 (5461 slots) master
S: c642b3071c4b2b073707ed3c3a2c16d53a549eff 192.168.87.130:7114
 slots:5461-10922 (5462 slots) master
S: 896a3c99da4fcf680de1f42406fccb551d8c40c3 192.168.87.131:7116
 slots:0-5460 (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
(输入 yes 并按下回车确认之后,集群就会将配置应用到各个节点,并连接起(join)各个节点,也就是
让各个节点开始互相通讯)
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 192.168.87.129)
M: 007a3fe8d7451d3d0a78fffd2653c8641809499c 192.168.87.129:7111
 slots: (0 slots) master
 replicates 94e140b9ca0735040ae3428983835f1d93327aeb
M: ea69b6b6e2e7723eed50b1dabea9d244ccf3f098 192.168.87.130:7113
 slots: (0 slots) master
 replicates c642b3071c4b2b073707ed3c3a2c16d53a549eff
M: 5f09dc0671732cf06a09f28631c90e0c68408520 192.168.87.131:7115
 slots: (0 slots) master
 replicates 896a3c99da4fcf680de1f42406fccb551d8c40c3
M: 94e140b9ca0735040ae3428983835f1d93327aeb 192.168.87.129:7112
 slots:10923-16383 (5461 slots) master
M: c642b3071c4b2b073707ed3c3a2c16d53a549eff 192.168.87.130:7114
 slots:5461-10922 (5462 slots) master
M: 896a3c99da4fcf680de1f42406fccb551d8c40c3 192.168.87.131:7116
 slots:0-5460 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

到此Redis5.x集群正式结束

使用 redis-cli 命令进入集群环境

./redis-cli -c -p 7111

你可能感兴趣的:(redis,架构,java,redis)