Redis安装与集群部署

安装

当前安装环境为:
- 系统:Ubuntu 14.04.4 LTS
- 单节:虚拟机单节点
- 地址:192.168.0.18
- 硬件:1核1G

apt-get包管理安装

安装

apt-get install redis-server

远程访问

使用包管理工具安装的redis默认是仅本机可访问,不支持远程访问的。所以需要修改redis的配置文件,取消绑定ip。

vi /etc/redis/redis.conf
# 注释掉: bind 127.0.0.1这一行

源码编译安装

安装

源码编译安装其实只要完成编译工作,安装也就完成了。在编译过程中首先发现机器没有安装make工具,等把make装好后发现cc命令执行失败,所以又安装了gcc。等这些条件都准备好后,报了一个文件缺失的错误,网上查了一下,发现这是一个bug,好的是也给出了解决方案,这才顺利的完成编译过程。

# 源码下载
wget http://download.redis.io/releases/redis-3.0.6.tar.gz

# 解压
tar xzf redis-3.0.6.tar.gz

# 编译
cd redis-3.0.6
make MALLOC=libc
# 直接执行make有可能会报错:fatal error: jemalloc/jemalloc.h

# 无make模块的时候:apt-get install make
# cc命令执行失败的时候:apt-get install gcc

启动

进入src目录,直接执行redis-server脚本即可以redis默认参数启动。另外也可以编写自定义配置文件,然后根据配置启动redis。

cd src
./redis-server 或者 ./redis-server [configPath]

配置集群

环境准备

redis3.0以后开始支持集群,并提供了相关工具。集群工具位于源码src目录下的redis-trib.rb,该脚本是使用ruby书写的,所以当前环境还需要安装ruby。

安装ruby

apt-get install ruby
gem install redis

修改配置文件

默认配置文件仅能开启一个单节点redis,所以我们需要对配置文件做一定的修改。一下是配置集群的必须修改项。

cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

本次安装因为是单机多节点redis集群,而且每个节点都以守护进程模式启动。所以还需要修改pid文件和端口,已经启动模式。

daemonize yes
pidfile /var/run/redis0.pid
port 6380

启动redis

本次集群计划6个redis实例:3个主节点,3个复制节点。端口分别位:6380,6381,6382,6383,6384,6385。

./redis-server ../shards/6380/shard.conf
./redis-server ../shards/6381/shard.conf
./redis-server ../shards/6382/shard.conf
./redis-server ../shards/6383/shard.conf
./redis-server ../shards/6384/shard.conf
./redis-server ../shards/6385/shard.conf

组建集群

root@ubuntu:/opt/sbin/redis-3.0.6/src# ./redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
./redis-trib.rb:1573: warning: key "threshold" is duplicated and overwritten on line 1573
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6380
127.0.0.1:6381
127.0.0.1:6382
Adding replica 127.0.0.1:6383 to 127.0.0.1:6380
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
Adding replica 127.0.0.1:6385 to 127.0.0.1:6382
M: 1b85ea9b2e569906d6d6fb99ef59434690bb5314 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
M: 861172b6acc35c101eb349cf550a5f270b3b4260 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: ac7155ff194714b67597c848d187d01c002fac91 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
S: 8f1b32c678e136c2d5abba75c6b85d6c001ae297 127.0.0.1:6383
   replicates 1b85ea9b2e569906d6d6fb99ef59434690bb5314
S: 34ac1fc6237f84d7be105da86d7fc305757a8ae3 127.0.0.1:6384
   replicates 861172b6acc35c101eb349cf550a5f270b3b4260
S: 2b5e12e4ace97e8964abc5b596b065058f879086 127.0.0.1:6385
   replicates ac7155ff194714b67597c848d187d01c002fac91
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 127.0.0.1:6380)
M: 1b85ea9b2e569906d6d6fb99ef59434690bb5314 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
M: 861172b6acc35c101eb349cf550a5f270b3b4260 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: ac7155ff194714b67597c848d187d01c002fac91 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
M: 8f1b32c678e136c2d5abba75c6b85d6c001ae297 127.0.0.1:6383
   slots: (0 slots) master
   replicates 1b85ea9b2e569906d6d6fb99ef59434690bb5314
M: 34ac1fc6237f84d7be105da86d7fc305757a8ae3 127.0.0.1:6384
   slots: (0 slots) master
   replicates 861172b6acc35c101eb349cf550a5f270b3b4260
M: 2b5e12e4ace97e8964abc5b596b065058f879086 127.0.0.1:6385
   slots: (0 slots) master
   replicates ac7155ff194714b67597c848d187d01c002fac91
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试集群

存取测试

root@ubuntu:~# redis-cli -c -h 192.168.0.18 -p 6380
192.168.0.18:6380> set name kim
-> Redirected to slot [5798] located at 127.0.0.1:6381
OK
127.0.0.1:6381> set bllow 192.168.0.1
-> Redirected to slot [12497] located at 127.0.0.1:6382
OK
127.0.0.1:6382> set cat tom
-> Redirected to slot [1075] located at 127.0.0.1:6380
OK
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WuZuoDingFeng/article/details/78622999
个人分类: 运维 Linux
上一篇Nginx配置多域名代理
下一篇IDEA中配置OpenResty开发环境
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭