您的位置:首页 > 数据库 > Redis

Redis部署及相关的集群、主从和哨兵

2020-06-23 04:48 375 查看

Redis部署

一、环境介绍

系统及软件版本

Linux:CentOS 7

Redis:3.0.7

二、redis部署

方案一:Yum安装

1、配置yum软件仓库

​ ①检查yum源是否有redis安装包

[root@web01 ~]# yum list | grep redis

​ ②配置阿里云yum源(实现步骤①的前提下,可跳过步骤②)

[root@redis ~]# cd /etc/yum.repos.d/
[root@redis ~]# mkdir repo
[root@redis ~]# mv *.repo local.repo repo/
[root@redis ~]# wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@redis ~]# yum clean all
[root@redis ~]# yum repolist
[root@redis ~]# yum list|grep redis

​ ③yum安装redis

[root@redis ~]# yum -y install redis #yum安装redis
方案二:源码包安装

1、下载源码包并解压

[root@redis ~]# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
[root@redis ~]# tar -xf redis-3.0.7.tar.gz -C /opt

2、安装相关依赖包

​ ①检查是否有安装gcc、gcc-c++和tcl

[root@redis redis-3.0.7]# yum list grep gcc gcc-c++ tcl

​ ②若没有安装,使用yum安装相关包

[root@redis redis-3.0.7]# yum -y install gcc gcc-c++ tcl

​ ③执行make test查看是否报错

[root@redis redis-3.0.7]# make test
cd src && make test
make[1]: 进入目录“/root/redis-3.0.7/src”
CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
#include <jemalloc/jemalloc.h>
^
编译中断。
make[1]: *** [adlist.o] 错误 1
make[1]: 离开目录“/root/redis-3.0.7/src”
make: *** [test] 错误 2

​ ④出现步骤③的报错后执行make MALLOC=libc

[root@redis redis-3.0.7]# make MALLOC=libc
[root@redis redis-3.0.7]# make test #再次使用make test检查是否有报错

​ ⑤将redis服务部署在/data/redis中

[root@redis redis-3.0.7]# make PREFIX=/data/redis/ install

​ ⑥复制配置文件并修改

[root@redis redis-3.0.7]# vim /data/redis/bin/redis.conf
#将bind 127.0.0.1 修改为bind 0.0.0.0
#将daemonize no 修改为daemonize yes
#将protected_mode yes修改为protected_mode no
[root@redis bin]# ./redis-server redis.conf # 启动服务

⑦将redis配置成服务

[root@redis bin]# netstat -tnupl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      7186/./redis-server
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      649/sshd
tcp6       0      0 :::6379                 :::*                    LISTEN      7186/./redis-server
udp        0      0 127.0.0.1:323           0.0.0.0:*                           448/chronyd
udp        0      0 0.0.0.0:68              0.0.0.0:*                           604/dhclient
[root@redis bin]# kill -9 7186 # 关闭服务
[root@redis bin]# vim /etc/profile # 修改环境变量,将redis安装路径配置到环境变量
...
export REDIS_HOME=/data/redis/
export PATH=$PATH:$REDIS_HOME/bin
...
[root@redis bin]# source profile # 刷新环境变量文件
[root@redis bin]# cd ~/redis-3.0.7/utils/ # 切换到解压包目录
[root@redis utils]# ./install_server.sh # 执行安装脚本,一路回车
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/data/redis//bin/redis-server]
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /data/redis//bin/redis-server
Cli Executable : /data/redis//bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379 # 记住后者路径
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
root@redis utils]# cd /etc/init.d/ # 切换到该路径
[root@redis init.d]# mv redis_6379 redisd # 这是我们服务的配置文件,可以使用别名
[root@redis init.d]# service redisd start #使用    service  redisd  start(restart | stop)启动服务
/var/run/redis_6379.pid exists, process is already running or crashed
[root@redis init.d]# service redisd status
Redis is running (8858)

三、redis持久化

Redis 有两种持久化方案,RDB (Redis DataBase)和 AOF (Append Only File)。

RDB:RDB 是 Redis 默认的持久化方案。在指定的时间间隔内,执行指定次数的写操作,则会将内存中的数据写入到磁盘中。即在指定目录下生成一个dump.rdb文件。Redis 重启会通过加载dump.rdb文件恢复数据。

AOF:Redis 默认不开启。它的出现是为了弥补RDB的不足(数据的不一致性),所以它采用日志的形式来记录每个写操作,并追加到文件中。Redis 重启的会根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作。

注:若只打算用Redis 做缓存,可以关闭持久化。

1、redis默认采取rdb,可以修改所指定的时间间隔

[root@redis bin]# vim redis.conf  #如果有指定的时间间隔,可以修改save数值
...
################################ SNAPSHOTTING  ################################
...
save 900 1      # 900s内有1个更改
save 300 10     # 300s内有1个更改
save 60 10000   # 60s内有一个更改
...
# redis默认上面三个条件,满足以上条件之一则将数据从内存写入硬盘

2、开启aof持久化模式

​ ①修改配置文件

[root@redis bin]# vim redis.conf
...
############################## APPEND ONLY MODE ###############################
...
appendonly yes    #默认为no,开启aof将其修改为yes
...
appendfilename "appendonly.aof"  #指定本地数据库文件名,默认值为 appendonly.aof
...
# appendfsync always   #指定更新日志条件 always为同步持久化,每次发生数据变化会立刻写入到磁盘中。性能较差当数据完整性比较好(慢,安全)
appendfsync everysec   # 出厂默认推荐,每秒异步记录一次(默认值)
# appendfsync no  #不同步
...
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 3G #当AOF文件大小是上次rewrite后大小的一倍且文件大于64M时触发。一般都设置为3G,默认64M太小了
...

​ ②启动服务

[root@redis bin]# touch appendonly.aof #创建appendonly.aof文件
[root@redis bin]# ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set name tom
OK
127.0.0.1:6379> get name
"tom"
127.0.0.1:6379> shutdown
not connected> exit
[root@redis bin]# ./redis-server redis.conf
[root@redis bin]# ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> get name #测试成功
"tom"

四、redis集群(伪分布式)

1、安装redis(安装步骤同上一)

2、部署redis多实例

[root@redis-cluster ~]# mkdir -p /data/redis-cluster/redis{01..06}
redis01  redis02  redis03  redis04  redis05  redis06
[root@redis-cluster bin]# for i in {01..06}; do cp -r redis-cli redis-server /data/redis-cluster/redis$i/; done
[root@redis-cluster redis01]# vim ../redis01/redis.conf
ter-enabled yes
cluster-config-file "nodes.conf"
cluster-node-timeout 5000
logfile "/data/redis-cluster/redis01/redis01.log"
bind 0.0.0.0
daemonize yes
protected_mode no
appendonly yes
[root@redis-cluster redis01]# vim ../redis02/redis.conf
...
[root@redis-cluster redis01]# vim ../redis03/redis.conf
...
[root@redis-cluster redis01]# vim ../redis04/redis.conf
...
[root@redis-cluster redis01]# vim ../redis05/redis.conf
...
[root@redis-cluster redis01]# vim ../redis06/redis.conf
...

3、启动服务

[root@redis-cluster redis01]# ./redis-server redis.conf #避免麻烦可以开发启动脚本
[root@redis-cluster ~]# ps -ef | grep redis
root      7302     1  0 14:45 ?        00:00:00 ./redis-server 0.0.0.0:7001 [cluster]
root      7308     1  0 14:45 ?        00:00:00 ./redis-server 0.0.0.0:7002 [cluster]
root      7315     1  0 14:45 ?        00:00:00 ./redis-server 0.0.0.0:7003 [cluster]
root      7321     1  0 14:45 ?        00:00:00 ./redis-server 0.0.0.0:7004 [cluster]
root      7327     1  0 14:45 ?        00:00:00 ./redis-server 0.0.0.0:7005 [cluster]
root      7331     1  0 14:45 ?        00:00:00 ./redis-server 0.0.0.0:7006 [cluster]
root      7339   690  0 14:47 pts/0    00:00:00 grep --color=auto redis

4、安装ruby和gem包

[root@redis-cluster ~]# yum -y install ruby rubygems
[root@redis-cluster ~]# wget  https://rubygems.org/downloads/redis-3.0.7.gem
[root@redis-cluster ~]# gem install redis-3.0.7.gem

5、启动Redis集群

[root@redis-cluster ~]# cp redis-3.0.7/src/redis-trib.rb /data/redis-cluster
[root@redis-cluster redis-cluster]# ./redis-trib.rb  create --replicas  1  127.0.0.1:7001  127.0.0.1:7002  127.0.0.1:7003  127.0.0.1:7004  127.0.0.1:7005  127.0.0.1:7006 #分布式换成ip
...
Can I set the above configuration? (type 'yes' to accept): yes #服从默认主从分配方式,也可以通过配置文件自定义slave
...

6、验证

[root@redis-cluster redis01]# ./redis-cli -h 127.0.0.1 -p 7001 -c # 加c连接集群,不加c取其他redis实例存的数据会报错
127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:1115
cluster_stats_messages_received:1115

五、主从

1、环境说明

主机名称 IP地址 redis版本和角色说明
redis-master 192.168.122.236 redis 3.0.7(主)
redis-slave01 192.168.122.115 redis 3.0.7(从)
redis-slave02 192.168.122.137 redis 3.0.7(从)

2、修改配置文件

master:

bind  192.168.122.236
protected-mode yes
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
logfile /var/log/redis/redis.log
dir /var/lib/redis/
~

slave1:

bind  192.168.122.115
protected-mode yes
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
logfile /var/log/redis/redis.log
dir /var/lib/redis/
#appendonly yes
slaveof 192.168.122.236 6379 #新版本作者修改成了replicaof
#masterauth password #如果master有密码,添上这一行
~

slave2:

bind  192.168.122.136
protected-mode yes
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
logfile /var/log/redis/redis.log
dir /var/lib/redis/
#appendonly yes
slaveof 192.168.122.236 6379
#masterauth password
~

3、启动服务

[root@redis-master ~]# systemctl restart redis
[root@redis-slave1 ~]# systemctl restart redis
[root@redis-slave2 ~]# systemctl restart redis

4、验证

[root@redis-master ~]# redis-cli -h 192.168.122.236
192.168.122.236:6379> set k1 name
OK
[root@redis-slave1 ~]# redis-cli -h 192.168.122.115
192.168.122.115:6379> get k1
"name"
[root@redis-slave1 ~]# redis-cli -h 192.168.122.137
192.168.122.137:6379> get k1
"name"

六、哨兵机制

1、环境

主机名称 IP地址
redis-master 192.168.122.236 redis 3.0.7(主)
redis-slave01 192.168.122.115 redis 3.0.7(从)
redis-slave02 192.168.122.137 redis 3.0.7(从)
redis-master 192.168.56.11:26379 Sentinel01
redis-slave01 192.168.56.12:26379 Sentinel02
redis-slave02 192.168.56.13:26379 Sentinel03

2、部署主从(参考五)

3、Sentinel.conf

master:

[root@redis-master ~]# grep -Ev "^$|#" /usr/local/redis/sentinel.conf
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/sentinel.log"
dir "/tmp"
sentinel monitor mymaster 192.168.122.236 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

slave1:

[root@redis-master ~]# vim /etc/redis-sentinel.conf
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/sentinel.log"
dir "/tmp"
sentinel monitor mymaster  192.168.122.236 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

slave2:

[root@redis-master ~]# vim /etc/redis-sentinel.conf
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/sentinel.log"
dir "/tmp"
sentinel monitor mymaster  192.168.122.236 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

4、启动哨兵服务

启动的顺序:主Redis --> 从Redis --> Sentinel1/2/3

[root@redis-master ~]# redis-sentinel /usr/local/redis/sentinel.conf
[root@redis-slave01 ~]# redis-sentinel /usr/local/redis/sentinel.conf
[root@redis-slave02 ~]# redis-sentinel /usr/local/redis/sentinel.conf
[root@redis-slave02 ~]# ps -ef |grep redis
root      1628     1  0 14:07 ?        00:00:06 /usr/local/redis/src/redis-server 192.168.56.13:6379
root      1709     1  0 14:42 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root      1714  1575  0 14:42 pts/0    00:00:00 grep --color=auto redis
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: