Linux :redis 3.2.1双节点集群安装部署

原创
2017/11/09 21:06
阅读数 692

一.环境准备

1.JDK环境准备,需卸载掉自带JDK

(1)检查服务器是否自带jdk环境

[root@redis1 /]# rpm -qa|grep jdk
java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.x86_64
[root@redis1 /]#

(2)卸载自带JDK

[root@redis1/]# rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.x86_64

(3)查看JDK版本,出现下面情况说明卸载成功了

[root@redis1 /]# java -version
-bash: /usr/bin/java: No such file or directory

(4)安装JDK

1.在根目录/下创建app文件夹

[root@redis1 /]# makdir /app

2.将下载好的linux版本的JDK上传到app目录下

[root@redis1 /]# ls /app
jdk-7u71-linux-x64.tar.gz

3.解压JDK

[root@redis1 ~]# cd /app
[root@redis1 app]# tar -zxvf jdk-7u71-linux-x64.tar.gz

4.配置JDK环境

[root@redis1 app]# vi /etc/profile

在profile文件的末尾添加java环境配置

#set java environment
export JAVA_HOME=/app/jdk1.7.0_71
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

刷新配置文件,使增加的配置生效

[root@redis1 app]# source /etc/profile

查看JDK版本,看java是否安装成功

[root@redis1 app]# java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

2.gcc-c ++环境准备,内网环境只能手动安装gcc-c ++环境

(1)下载要用到的rpm包,并且将这些包上传到/app目录下

下载地址http://vault.centos.org/6.3/os/x86_64/Packages/

ppl-0.10.2-11.el6.x86_64.rpm 
cloog-ppl-0.15.7-1.2.el6.x86_64.rpm 
mpfr-2.4.1-6.el6.x86_64.rpm 
cpp-4.4.6-4.el6.x86_64.rpm 
kernel-headers-2.6.32-279.el6.x86_64.rpm 
glibc-headers-2.12-1.80.el6.x86_64.rpm 
glibc-devel-2.12-1.80.el6.x86_64.rpm 
libstdc++-devel-4.4.6-4.el6.x86_64.rpm 
gcc-4.4.6-4.el6.x86_64.rpm 
gcc-c++-4.4.6-4.el6.x86_64.rpm

(2)安装gcc-c++依赖rpm文件

[root@redis1 app]# rpm -ivh ppl-0.10.2-11.el6.x86_64.rpm 
warning: ppl-0.10.2-11.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Preparing...########################################### [100%]
   1:ppl   ########################################### [100%]
[root@redis1 app]# 
[root@redis1 app]# rpm -ivh cloog-ppl-0.15.7-1.2.el6.x86_64.rpm
warning: cloog-ppl-0.15.7-1.2.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
1:cloog-ppl  ########################################### [100%]
[root@redis1 app]#  rpm -ivh mpfr-2.4.1-6.el6.x86_64.rpm 
warning: mpfr-2.4.1-6.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
  1:mpfr      ########################################### [100%]
[root@redis1 app]# rpm -ivh cpp-4.4.6-4.el6.x86_64.rpm 
warning: cpp-4.4.6-4.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing...  ########################################### [100%]
  1:cpp        ########################################### [100%]
[root@redis1 app]# rpm -ivh kernel-headers-2.6.32-279.el6.x86_64.rpm 
warning: kernel-headers-2.6.32-279.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing...########################################### [100%]
  1:kernel-headers ########################################### [100%]
[root@redis1 app]#  rpm -ivh glibc-headers-2.12-1.80.el6.x86_64.rpm
warning: glibc-headers-2.12-1.80.el6.x86_64.rpm: Header V3 RSA/SHA1   
Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
  1:glibc-headers  ########################################### [100%]
[root@redis1 app]# rpm -ivh glibc-devel-2.12-1.80.el6.x86_64.rpm 
warning: glibc-devel-2.12-1.80.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
  1:glibc-devel ########################################### [100%]
[root@redis1 app]# rpm -ivh gcc-4.4.6-4.el6.x86_64.rpm 
warning: gcc-4.4.6-4.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing...########################################### [100%]
  1:gcc  ########################################### [100%]
[root@redis1 app]# rpm -ivh libstdc++-devel-4.4.6-4.el6.x86_64.rpm 
warning: libstdc++-devel-4.4.6-4.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing...########################################### [100%]
  1:libstdc++-devel ########################################### [100%]
[root@redis1 app]# rpm -ivh gcc-c++-4.4.6-4.el6.x86_64.rpm 
warning: gcc-c++-4.4.6-4.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing...########################################### [100%]
  1:gcc-c++  ########################################### [100%]

(3)查看gcc-c++是否安装成功

[root@redis2 app]# gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) 

二.redis双节点集群

1.部署ip说明:

192.168.10.71     26379(redis sentinel 集群监控)

192.168.10.71     6379(redis 主)

192.168.10.72     7379(redis 从)

2.开始安装redis

(1)将下载好的linux版本的redis-3.2.11.tar.gz放到/app目录下

(2)解压redis-3.2.11.tar.gz

[root@redis2 ~]# cd /app
[root@redis2 app]# tar -zxvf redis-3.2.11.tar.gz

(3)进入到解压后的redis-3.2.11目录下,用gcc-c++对解压后的redis进行编译

[root@redis1 redis-3.2.11]# make MALLOC=libc

#编译命令执行完成后,会在当前目录的src下生成可执行文件,
#分别是redis-server、redis-cli、redis-benchmark、redis-stat,它们的作用如下:
#redis-server:Redis服务器的daemon启动程序
#redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作
#redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能
#redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况。
#安装完成,启动(启动/usr/local/bin路径下的redis程序)redis-server

#启动完成后可执行ps -ef | grep redis 命令,以检查是否启动成功。
#将编译好的redis-server,redis-cli复制到/usr/local/bin/目录下

[root@redis1 src]# cp redis-server /usr/local/bin/
[root@redis1 src]# cp redis-cli /usr/local/bin/

如下方式编译会报错:

####这里用make命令会出现下面错误
[root@redis1 redis-3.2.11]# make
cd src && make all
make[1]: Entering directory `/app/redis-3.2.11/src'
    CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/app/redis-3.2.11/src'
make: *** [all] Error 2
[root@redis1 redis-3.2.11]# ^C
[root@redis1 redis-3.2.11]# 
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
Updating certificate-based repositories.
Unable to read consumer identity
Setting up Install Process
No package gcc available.
Error: Nothing to do
#主要原因:
#说关于分配器allocator,如果有MALLOC这个环境变量, 会有用这个环境变量的去建立Redis。
#而且libc并不是默认的分配器,默认的是jemalloc,因为jemalloc被证明有更少的 fragmentation problems比libc。
#但是如果你又没有jemalloc而只有libc当然make出错。所以加这么一个参数。
#解决办法:make MALLOC=libc

(4)修改redis.conf配置文件

Master redis配置

1.创建redis配置文件目录/monitor/redis_master_slave/6379/

[root@redis1 ~]# cd /app/redis-3.2.11
[root@redis1 redis-3.2.11]# mkdir -p /monitor/redis_master_slave/6379/
####将/app/redis-3.2.11下的redis.conf复制一份到/monitor/redis_master_slave/6379/下
[root@redis1 redis-3.2.11]# cp redis.conf  /monitor/redis_master_slave/6379/

2.主的redis配置文件,使用默认的配置文件就可以了,如果你需要设计其他参数,主从节点采用AOF存储方式,主节点修改redis.conf:

[root@redis1 ~]# cd /monitor/redis_master_slave/6379/
[root@redis1 6379]# vi redis.conf
#主的redis配置文件,需要添加
daemonize yes
port 6379
bind 192.168.10.71         ####线上环境绑定IP
pidfile /monitor/redis_master_slave/6379/redis.pid
logfile "/monitor/redis_master_slave/6379/redis.log"
dir "/monitor/redis_master_slave/6379/"
appendonly yes

Slave redis配置

1.创建redis配置文件目录/monitor/redis_master_slave/7379/

[root@redis2 ~]# cd /app/redis-3.2.11
[root@redis2 redis-3.2.11]# mkdir -p /monitor/redis_master_slave/7379/
####将/app/redis-3.2.11下的redis.conf复制一份到/monitor/redis_master_slave/7379/下
[root@redis2 redis-3.2.11]# cp redis.conf  /monitor/redis_master_slave/7379/

2.从的redis配置文件,需要添加

[root@redis2 redis-3.2.11]# cd /monitor/redis_master_slave/7379/
[root@redis2 7379]# vi redis.conf
#从的redis配置文件,需要添加
daemonize yes
port 7379
bind 192.168.10.72         ####线上环境绑定IP
pidfile /monitor/redis_master_slave/7379/redis.pid
logfile "/monitor/redis_master_slave/7379/redis.log"
dir "/monitor/redis_master_slave/7379/"
appendonly yes
slaveof 192.168.10.71 6379

(5)关闭防火墙

关掉主服务器防火墙
临时关闭:

service iptables stop

永久关闭:

chkconfig iptables off

防火墙打开端口

# /sbin/iptables -I INPUT -p tcp –dport 6379 -j ACCEPT
# /etc/init.d/iptables save
# service iptables restart

(6)启动主从redis服务

#启动redis master服务
[root@redis1 ~]# cd /app/redis-3.2.11/src
[root@redis1 src]# /usr/local/bin/redis-server /monitor/redis_master_slave/6379/redis.conf
#启动redis slave服务
[root@redis2 ~]# cd /app/redis-3.2.11/src
[root@redis2 src]# /usr/local/bin/redis-server /monitor/redis_master_slave/7379/redis.conf

(7)查看主从redis信息

#查看redis的主机信息
[root@redis1 ~]# cd /app/redis-3.2.11/src
[root@redis1 src]# redis-cli -h 192.168.10.71 -p 6379 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.10.72,port=7379,state=online,offset=169,lag=0
master_repl_offset:169
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:168
#查看redis的从机信息
[root@redis2 ~]# cd /app/redis-3.2.11/src
[root@redis2 src]# redis-cli -h 192.168.10.72 -p 7379 info Replication
# Replication
role:slave
master_host:192.168.10.71
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:10165
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

那么表示一切都正常了。

到这儿,你的redis 双节点集群就已经搭建成功,但是还没有完,请继续往下!

三.配置redis sentinel集群监控服务

1.创建redis sentinel配置文件目录/monitor/redis_master_slave/sentinel/

[root@redis1 src]# cd /app/redis-3.2.11
[root@redis1 redis-3.2.11]# mkdir -p /monitor/redis_master_slave/sentinel/
#将/app/redis-3.2.11下的/sentinel.conf复制一份到/monitor/redis_master_slave/sentinel/下
[root@redis1 redis-3.2.11]# cp sentinel.conf /monitor/redis_master_slave/sentinel/sentinel26379.conf

2.Sentinel服务器多台服务器时间必须同步(TILT 模式)。

1份sentinel配置,修改sentinel26379.conf  redis-sentinel 配置文件

[root@redis1 redis-3.2.11]# vi /monitor/redis_master_slave/sentinel/sentinel26379.conf
# Example sentinel.conf
# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379
protected-mode no   ####这个必须打开,因为在redis中是保护模式的,不然无法连接
sentinel monitor mymaster 192.168.10.61 6379 1
sentinel down-after-milliseconds mymaster 2000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

启动redis sentinel做redis集群监听

[root@redis1 redis-3.2.11]# cd /monitor/redis_master_slave/sentinel
[root@redis1 sentinel]# nohup redis-sentinel /monitor/redis_master_slave/sentinel/sentinel26379.conf >sentinel.log 2>&1 &
[root@redis1 sentinel]# tail -100f sentinel.log
  [17444] 25 Aug 14:09:48.936 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.11 (00000000/0) 64 bit
   .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in sentinel mode
  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 17444
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
  |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
       `-._    `-.__.-'    _.-'
           `-._        _.-'
               `-.__.-'
[17444] 25 Aug 14:09:48.938 # Sentinel runid is f79f39d3f5cab737f21a87c32b7f6900294ec2a1
[17444] 25 Aug 14:09:48.938 # +monitor master mymaster 192.168.10.71 6379 quorum 1
[17444] 25 Aug 14:09:48.939 * +slave slave 192.168.10.72:7379 192.168.10.72 7379 @ mymaster 192.168.10.71 6379

执行以下命令,查看redis主从信息

[root@redis1 ~]# redis-cli -h 192.168.10.71 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=192.168.10.71:6379,slaves=1,sentinels=1

那么表示一切都正常了。

到这儿,你的redis sentinel集群已经配置成功!

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部