CentOS7安装Redis集群+开机自启

1、安装步骤

1)下载并解压

cd /usr/local/
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar -zxvf /redis-3.2.1.tar.gz

2)测试make redis

cd redis-3.2.1
make test
##可能会报错:You need tcl 8.5 or newer in order to run the Redis test
##安装tcl
yum -y install tcl

出现绿色成功提示

3)安装

[root@localhost redis-3.2.1]# make && make install
cd src && make all
make[1]: 进入目录“/usr/local/redis-3.2.1/src”
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb

Hint: It's a good idea to run 'make test' ;)

make[1]: 离开目录“/usr/local/redis-3.2.1/src”
cd src && make install
make[1]: 进入目录“/usr/local/redis-3.2.1/src”

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: 离开目录“/usr/local/redis-3.2.1/src”

应该算是成功了

4)更改配置

[root@localhost local]# cd /usr/local
##创建集群目录
[root@localhost local]# mkdir redis_cluster
[root@localhost local]# cd redis_cluster
##创建三个端口(节点)
[root@localhost redis_cluster]# mkdir 7000 7001 7002
##配置复制到各个节点
[root@localhost redis_cluster]# cp /usr/local/redis-3.2.1/redis.conf ./7000
[root@localhost redis_cluster]# cp /usr/local/redis-3.2.1/redis.conf ./7001
[root@localhost redis_cluster]# cp /usr/local/redis-3.2.1/redis.conf ./7002

更新相关配置

[root@localhost redis_cluster]# vi ./7000/redis.conf
[root@localhost redis_cluster]# vi ./7001/redis.conf
[root@localhost redis_cluster]# vi ./7002/redis.conf

修改项目,其中7000分别改成不同的端口号

daemonize    yes                          //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7002,7003
port  7000                                //端口7000,7002,7003
cluster-enabled  yes                      //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf      //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  5000                //请求超时  设置5秒够了
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志

5)启动节点

[root@localhost local]# redis-server ./redis_cluster/7000/redis.conf
[root@localhost local]# redis-server ./redis_cluster/7001/redis.conf
[root@localhost local]# redis-server ./redis_cluster/7002/redis.conf

6)安装另一台redis

在进行make test时出现新问题:

[exception]: Executing test client: NOREPLICAS Not enough good slaves to write…
NOREPLICAS Not enough good slaves to write.

修改配置:

[root@localhost redis-3.2.1]# vi ./tests/integration/replication-2.tcl

-1000 -------------------------------> -10000

make & make install,安装成功

重复4)~5),注意不同的端口号

7)查看redis服务是否启动

ps -ef | grep redis   #查看是否启动成功
netstat -tnlp | grep redis #可以看到redis监听端口

8)创建集群

①安装ruby【坑】

官方提供了一个工具:redis-trib.rb(/usr/local/redis-3.2.1/src/redis-trib.rb) ,是用ruby写的一个程序,所以我们还得安装ruby

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

再用 gem 这个命令来安装 redis接口 gem是ruby的一个工具包.

##gem install redis   
##上面报错,redis requires Ruby version >= 2.2.2问题;
##Centos默认支持ruby到2.0.0,可gem 安装redis需要最低是2.2.2

然后需要升级Ruby,又需要安装ruby的版本管理器:rvm

#执行curl -L get.rvm.io | bash -s stable
#结果又出现公钥问题gpg: 无法检查签名:没有公钥
#按照提示执行:
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462                                                     A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
#再执行:
curl -L get.rvm.io | bash -s stable
#更新
source /usr/local/rvm/scripts/rvm

安装ruby新版

##安装一个ruby版本
rvm install 2.4.1
##使用一个ruby版本
rvm use 2.4.1
##设置默认版本
rvm use 2.4.1 --default
##卸载已知版本
rvm remove 2.0.0
##查看ruby版本,安装redis
ruby --version

安装集群redis

gem install redis

另一台主机安装rubby

②关闭两台虚机的防火墙

systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl mask firewalld.service

③创建集群

##/usr/local/redis-3.2.1/src/redis-trib.rb  create  --replicas  1  192.168.73.136:7000 192.168.73.136:7001  192.168.73.136:7002 192.168.73.137:7003  192.168.73.137:7004  192.168.73.137:7005
##报错:[ERR] Sorry, can't connect to node 192.168.X.X

修改700*的redis.conf

##修改这句
bind 127.0.0.1
==》
bind 192.168.73.XXX

重启redis服务,需要注意的是原来127.0.0.1的服务还在,要kill掉

[root@localhost redis_cluster]# ps -ef|grep redis
root       9421      1  0 14:46 ?        00:00:11 redis-server 127.0.0.1:7000 [cluster]
root       9425      1  0 14:46 ?        00:00:11 redis-server 127.0.0.1:7001 [cluster]
root       9429      1  0 14:46 ?        00:00:11 redis-server 127.0.0.1:7002 [cluster]
root      15997      1  0 16:57 ?        00:00:00 redis-server 192.168.73.136:7000 [cluster]
root      16001      1  0 16:57 ?        00:00:00 redis-server 192.168.73.136:7001 [cluster]
root      16005      1  0 16:57 ?        00:00:00 redis-server 192.168.73.136:7002 [cluster]
root      16013   9521  0 16:59 pts/1    00:00:00 grep --color=auto redis
[root@localhost redis_cluster]# kill -9 9421
[root@localhost redis_cluster]# kill -9 9425
[root@localhost redis_cluster]# kill -9 9429
[root@localhost redis_cluster]# ps -ef|grep redis
root      15997      1  0 16:57 ?        00:00:00 redis-server 192.168.73.136:7000 [cluster]
root      16001      1  0 16:57 ?        00:00:00 redis-server 192.168.73.136:7001 [cluster]
root      16005      1  0 16:57 ?        00:00:00 redis-server 192.168.73.136:7002 [cluster]
root      16016   9521  0 16:59 pts/1    00:00:00 grep --color=auto redis

用客户端telnet 700*,可以试一下没问题就可以了,最终创建集群完成

[root@localhost redis_cluster]# /usr/local/redis-3.2.1/src/redis-trib.rb  create  --replicas  1  192.168.73.136:7000 192.168.73.136:7001  192.168.73.136:7002 192.168.73.137:7003  192.168.73.137:7004  192.168.73.137:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.73.136:7000
192.168.73.137:7003
192.168.73.136:7001
Adding replica 192.168.73.137:7004 to 192.168.73.136:7000
Adding replica 192.168.73.136:7002 to 192.168.73.137:7003
Adding replica 192.168.73.137:7005 to 192.168.73.136:7001
M: eddd675e5e36b51b1c09d23d012bfa9b15928eac 192.168.73.136:7000
   slots:0-5460 (5461 slots) master
M: 1ab32bd763be60eaf8d8e2e40a528b33391feb08 192.168.73.136:7001
   slots:10923-16383 (5461 slots) master
S: 74458981d134cbf9e6c949e72fac3aa26494c058 192.168.73.136:7002
   replicates 2796a20e01a0045d7e17372cbeddbf09234f533d
M: 2796a20e01a0045d7e17372cbeddbf09234f533d 192.168.73.137:7003
   slots:5461-10922 (5462 slots) master
S: 201833a42528e414fb5fc38421d5683c072fe270 192.168.73.137:7004
   replicates eddd675e5e36b51b1c09d23d012bfa9b15928eac
S: c3b72ba014d20cfd1bf3ce565a34670611da0943 192.168.73.137:7005
   replicates 1ab32bd763be60eaf8d8e2e40a528b33391feb08
Can I set the above configuration? (type 'yes' to accept): yes
>>> 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.73.136:7000)
M: eddd675e5e36b51b1c09d23d012bfa9b15928eac 192.168.73.136:7000
   slots:0-5460 (5461 slots) master
M: 1ab32bd763be60eaf8d8e2e40a528b33391feb08 192.168.73.136:7001
   slots:10923-16383 (5461 slots) master
M: 74458981d134cbf9e6c949e72fac3aa26494c058 192.168.73.136:7002
   slots: (0 slots) master
   replicates 2796a20e01a0045d7e17372cbeddbf09234f533d
M: 2796a20e01a0045d7e17372cbeddbf09234f533d 192.168.73.137:7003
   slots:5461-10922 (5462 slots) master
M: 201833a42528e414fb5fc38421d5683c072fe270 192.168.73.137:7004
   slots: (0 slots) master
   replicates eddd675e5e36b51b1c09d23d012bfa9b15928eac
M: c3b72ba014d20cfd1bf3ce565a34670611da0943 192.168.73.137:7005
   slots: (0 slots) master
   replicates 1ab32bd763be60eaf8d8e2e40a528b33391feb08
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis_cluster]#

9)测试

redis-cli -h 192.168.73.136 -c -p 7002

Set 数据:k1 = helloworld

[root@localhost redis_cluster]# redis-cli -h 192.168.73.136 -c -p 7000
192.168.73.136:7000> set k1 helloworld
-> Redirected to slot [12706] located at 192.168.73.136:7001
OK
192.168.73.136:7001> exit

在任何节点都可以获取此数据

[root@localhost redis_cluster]# redis-cli -h 192.168.73.137 -c -p 7004
192.168.73.137:7004> get k1
-> Redirected to slot [12706] located at 192.168.73.136:7001
"helloworld"
192.168.73.136:7001>

10)redis开机自启

vi /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 2345 80 90
# description: Redis is a persistent key-value database

REDISPORT0=7000
REDISPORT1=7001
REDISPORT2=7002
EXEC=/usr/local/redis-3.2.1/src/redis-server
CLIEXEC=/usr/local/redis-3.2.1/src/redis-cli

case "$1" in
    start)
        CONF="/usr/local/redis_cluster/${REDISPORT0}/redis.conf"    
        echo "Starting Redis server on $REDISPORT0..."
        $EXEC $CONF &
!
        CONF="/usr/local/redis_cluster/${REDISPORT1}/redis.conf"    
        echo "Starting Redis server on $REDISPORT1..."
        $EXEC $CONF &
!
        CONF="/usr/local/redis_cluster/${REDISPORT2}/redis.conf"    
        echo "Starting Redis server on $REDISPORT2..."
        $EXEC $CONF &
!
        ;;
stop)
    es_pid=`ps -aux|grep redis|grep ${REDISPORT0}|grep -v grep|awk '{print $2}'`
    kill -9 $es_pid
    echo "$REDISPORT0 stopped"
    es_pid=`ps -aux|grep redis|grep ${REDISPORT1}|grep -v grep|awk '{print $2}'`
    kill -9 $es_pid
    echo "$REDISPORT1 stopped"
    es_pid=`ps -aux|grep redis|grep ${REDISPORT2}|grep -v grep|awk '{print $2}'`
    kill -9 $es_pid
    echo "$REDISPORT2 stopped"
    ;;         
*)
    echo "start|stop"
    ;;
esac

exit $?

保存退出,添加执行权限

chmod +x /etc/init.d/redis

加入开机启动服务

chkconfig --add redis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值