Linux部署-redis部署及集群搭建

Redis部署

第一步:下载安装包

访问https://redis.io/download 到官网进行下载。这里下载最新的5.0.5版本.

第二步:安装环境

redis是C语言开发,安装redis需要先将官网下载的源码进行编译,编译依赖gcc环境。如果没有gcc环境,需要安装gcc:

yum install gcc-c++  

给出提示 y 继续安装 出现如下图表示安装成功
在这里插入图片描述

第三步:进行编译安装

解压安装包

tar -zxvf redis-5.0.3.tar.gz

//进入到/data/redis/ 文件目录下

[root@localhost mnt]# cd /data/redis
[root@localhost redis]# make      //对解压后的文件进行编译
[root@localhost redis]# cd ./src   //进入到 redis/src 文件目录下
[root@localhost src]# make install    //进行redis安装

第四步:创建集群目录

创建redis_cluster目录。

mkdir redis_cluster

在这里插入图片描述
在这里插入图片描述

拷贝redis.conf 改名为redis目录名.conf,例如redis7000.conf。

cp redis.conf redis7000.conf

在这里插入图片描述

第五步:修改配置文件

依次修改配置文件,修改配置端口为当前目录。

vim redis7000.conf
port  7000                                        //端口7000,7001        
bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7000,7001
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes         //aof日志开启  有需要就开启,它会每次写操作都记录一条日志 

第六步:在另外两台服务器上重复第二到五步。

至此3台服务器中6个redis已搭建完成。

第七步:在三台服务器中依次启动redis。

redis-server /data/redis/7000/redis7000.conf
redis-server /data/redis/7001/redis7001.conf

第八步:查看redis启动状态

如图则为启动正常。

netstat -tlnp | grep redis

在这里插入图片描述

ps -ef | grep redis

在这里插入图片描述

第九步:创建redis集群

这个工具是用 ruby 实现的,所以需要安装 ruby。安装命令如下:

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

安装完成后创建集群:
//redis 5.0 之后

redis-cli --cluster create 10.37.146.73:7000 10.37.146.73:7001 10.37.146.74:7000 10.37.146.73:7001 110.37.146.75:7000 10.37.146.75:7001 --cluster-replicas 1

//redis 5.0 之前

redis-trib.rb create --replicas  1  10.37.146.73:7000 10.37.146.73:7001 10.37.146.74:7000 10.37.146.73:7001 110.37.146.75:7000 10.37.146.75:7001

创建成功后出现:

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 )
M: 略
S: 略
M: 略
M: 略
S: 略
S: 略
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

第十步:集群验证

在第一台机器上连接集群的7000端口的节点,在另外一台连接7001节点,连接方式为 redis-cli -h 10.37.146.73 -c -p 7000 ,加参数 -C 可连接到集群,因为上面 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略。
在74的7000节点执行命令 set hello world ,执行结果如下:

[root@localhost bin]# redis-cli -h 10.37.146.74 -c -p 7000
10.37.146.74:7000> set hello world
-> Redirected to slot [866] located at 10.37.146.74:7000
OK

然后在71的7000端口,查看 key 为 hello 的内容

[root@localhost bin]# redis-cli -h 10.37.146.73 -c -p 7000
10.37.146.73:7000> get hello
-> Redirected to slot [866] located at 10.37.146.74:7000
	"world"

至此,搭建完成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值