安装Redis(单机&集群)
安装Redis(单机&集群)
由于环境差异,安装过程可能遇到各种各样的问题,不要慌,根据错误提示解决即可。
安装Redis单机
下载安装
1、下载redis
下载地址在:redis.io
比如把Redis安装到 /usr/local/redis/
mkdir -p /usr/local/redis/
cd /usr/local/redis/
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
2、解压压缩包
tar -zxvf redis-5.0.5.tar.gz -C /usr/local/redis/
3、安装gcc依赖
Redis是C语言编写的,编译需要
yum install gcc
4、编译安装
[root@localhost / ] cd /usr/local/redis/redis-5.0.5/src
[root@localhost redis-5.0.5] make
使用 make install指令,安装到其他目录,比如 /usr/local/redis目录
[root@localhost src] make install PREFIX=/usr/local/redis
修改配置
1、修改配置文件
//复制配置文件
[root@localhost redis-5.0.5]# cp /usr/local/redis/redis-5.0.5/redis.conf /usr/local/redis/
//修改配置文件
[root@localhost redis-5.0.5]# vim /usr/local/redis/redis.conf
(1)将在大致136行的 daemonize no 改成 daemonize yes,将启动的方式改成后台启动;
(2)注释掉大致69行的 bind 127.0.0.1 配置项,方法是在前面加上 #,允许远程客户端连接。
(3)将大致在88行的 protected-mode yes 改成 protected-mode no,目的是为了解决安全模式引起的报错,不需要再安全模式下运行。
(4)将大致在170行的 logfile "",改成某个自定义的文件如 logfile "/usr/local/redis/redis.log",方便后面进行Lua程序开发时,进行日志的输出和查看。
(5)将大致在566行的内容进行修改,如 maxmemory 100MB 或 maxmemory 1048576/1048576B/1000K/100M/100MB/0.1G/0.1GB
(6)将大致在507行的内容进行修改为 requirepass 123456, 设置登录密码
2、防火墙设置
设置iptables规则,允许外部访问6379端口
iptables -I INPUT 1 -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
或者设置firewall规则
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
启动停止
1、启动
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
2、停止
# 客户端执行
redis> shutdown
或
# 服务端执行
ps -aux | grep redis
kill -9 xxxx
安装Redis Sentinel
开启哨兵模式,至少需要3个Sentinel实例(奇数个,否则无法选举Leader)。
本例通过3个Sentinel实例监控3个Redis服务(1主2从)。
IP地址 节点角色&端口
192.168.8.203 Master:6379 / Sentinel : 26379
192.168.8.204 Slave :6379 / Sentinel : 26379
192.168.8.205 Slave :6379 / Sentinel : 26379
网络结构图:
在204和205的 redis.conf 配置中添加一行
slaveof 192.168.8.203 6379
# 或者
replicaof 192.168.8.203 6379
在203、204、205创建sentinel配置文件(单例安装后根目录下默认有sentinel.conf,可以先备份默认的配置)
cd /usr/local/soft/redis-5.0.5
mkdir logs
mkdir rdbs
mkdir sentinel-tmp
vim sentinel.conf
sentinle.conf 配置文件内容,三台机器相同
daemonize yes
port 26379
protected-mode no
dir "/usr/local/soft/redis-5.0.5/sentinel-tmp"
sentinel monitor redis-master 192.168.8.203 6379 2
sentinel down-after-milliseconds redis-master 30000
sentinel failover-timeout redis-master 180000
sentinel parallel-syncs redis-master 1
在3台机器上分别启动Redis和Sentinel
cd /usr/local/soft/redis-5.0.5/src
./redis-server ../redis.conf
./redis-sentinel ../sentinel.conf
在3台机器上查看集群状态:
redis> info replication

模拟master宕机,在203执行:
redis> shutdown
注意看sentinel.conf里面的redis-master被修改了,变成了当前master的IP端口。
redis> info replication
这个时候会有一个slave节点被Sentinel设置为master。
再次启动master,它不一定会被选举为master。
slave宕机和恢复测试省略。
安装Redis Cluster
为了节省机器,我们直接把6个Redis实例安装在同一台机器上(3主3从),只是使用不同的端口号。
机器IP 192.168.8.207
cd /usr/local/soft/redis-5.0.5
mkdir redis-cluster
cd redis-cluster
mkdir 7291 7292 7293 7294 7295 7296
复制redis配置文件到7291目录
cp /usr/local/soft/redis-5.0.5/redis.conf /usr/local/soft/redis-5.0.5/redis-cluster/7291
修改7291的配置文件
port 7291
dir /usr/local/soft/redis-5.0.5/redis-cluster/7291/
cluster-enabled yes
cluster-config-file nodes-7291.conf
cluster-node-timeout 5000
appendonly yes
pidfile /var/run/redis_7291.pid
把7291下的redis.conf复制到其他5个目录。
cd /usr/local/soft/redis-5.0.5/redis-cluster/7291
cp redis.conf ../7292
cp redis.conf ../7293
cp redis.conf ../7294
cp redis.conf ../7295
cp redis.conf ../7296
批量替换内容
cd /usr/local/soft/redis-5.0.5/redis-cluster
sed -i 's/7291/7292/g' 7292/redis.conf
sed -i 's/7291/7293/g' 7293/redis.conf
sed -i 's/7291/7294/g' 7294/redis.conf
sed -i 's/7291/7295/g' 7295/redis.conf
sed -i 's/7291/7296/g' 7296/redis.conf
安装ruby依赖、rubygems依赖、gem-redis依赖
yum install ruby -y
yum install rubygems -y
gem install redis -v 3.0.7
启动6个Redis节点
cd /usr/local/soft/redis-5.0.5/
./src/redis-server redis-cluster/7291/redis.conf
./src/redis-server redis-cluster/7292/redis.conf
./src/redis-server redis-cluster/7293/redis.conf
./src/redis-server redis-cluster/7294/redis.conf
./src/redis-server redis-cluster/7295/redis.conf
./src/redis-server redis-cluster/7296/redis.conf
是否启动了6个进程
ps -ef|grep redis
创建集群
旧版本中的redis-trib.rb已经废弃了,直接用–cluster命令
注意用绝对IP,不要用127.0.0.1
cd /usr/local/soft/redis-5.0.5/src/
redis-cli --cluster create 192.168.8.207:7291 192.168.8.207:7292 192.168.8.207:7293 192.168.8.207:7294 192.168.8.207:7295 192.168.8.207:7296 --cluster-replicas 1
Redis会给出一个预计的方案,对6个节点分配3主3从,如果认为没有问题,输入yes确认
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7295 to 127.0.0.1:7291
Adding replica 127.0.0.1:7296 to 127.0.0.1:7292
Adding replica 127.0.0.1:7294 to 127.0.0.1:7293
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: dfdc9c0589219f727e4fd0ad8dafaf7e0cfb4f1c 127.0.0.1:7291
slots:[0-5460] (5461 slots) master
M: 8c878b45905bba3d7366c89ec51bd0cd7ce959f8 127.0.0.1:7292
slots:[5461-10922] (5462 slots) master
M: aeeb7d7076d9b25a7805ac6f508497b43887e599 127.0.0.1:7293
slots:[10923-16383] (5461 slots) master
S: ebc479e609ff8f6ca9283947530919c559a08f80 127.0.0.1:7294
replicates aeeb7d7076d9b25a7805ac6f508497b43887e599
S: 49385ed6e58469ef900ec48e5912e5f7b7505f6e 127.0.0.1:7295
replicates dfdc9c0589219f727e4fd0ad8dafaf7e0cfb4f1c
S: 8d6227aefc4830065624ff6c1dd795d2d5ad094a 127.0.0.1:7296
replicates 8c878b45905bba3d7366c89ec51bd0cd7ce959f8
Can I set the above configuration? (type 'yes' to accept):
注意看slot的分布:
7291 [0-5460] (5461个槽)
7292 [5461-10922] (5462个槽)
7293 [10923-16383] (5461个槽)
集群创建完成
>>> 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:7291)
M: dfdc9c0589219f727e4fd0ad8dafaf7e0cfb4f1c 127.0.0.1:7291
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 8c878b45905bba3d7366c89ec51bd0cd7ce959f8 127.0.0.1:7292
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: aeeb7d7076d9b25a7805ac6f508497b43887e599 127.0.0.1:7293
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 8d6227aefc4830065624ff6c1dd795d2d5ad094a 127.0.0.1:7296
slots: (0 slots) slave
replicates aeeb7d7076d9b25a7805ac6f508497b43887e599
S: ebc479e609ff8f6ca9283947530919c559a08f80 127.0.0.1:7294
slots: (0 slots) slave
replicates dfdc9c0589219f727e4fd0ad8dafaf7e0cfb4f1c
S: 49385ed6e58469ef900ec48e5912e5f7b7505f6e 127.0.0.1:7295
slots: (0 slots) slave
replicates 8c878b45905bba3d7366c89ec51bd0cd7ce959f8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
重置集群的方式是在每个节点上个执行cluster reset,然后重新创建集群
连接到客户端
redis-cli -p 7291
redis-cli -p 7292
redis-cli -p 7293
批量写入值
cd /usr/local/soft/redis-5.0.5/redis-cluster/
vim setkey.sh
脚本内容
#!/bin/bash
for ((i=0;i<20000;i++))
do
echo -en "helloworld" | redis-cli -h 192.168.8.207 -p 7291 -c -x set name$i >>redis.log
done
chmod +x setkey.sh
./setkey.sh
每个节点分布的数据
127.0.0.1:7292> dbsize
(integer) 6683
127.0.0.1:7293> dbsize
(integer) 6665
127.0.0.1:7291> dbsize
(integer) 6652
其他命令,比如添加节点、删除节点,重新分布数据:
redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
附录:
集群命令
- cluster info :打印集群的信息
- cluster nodes :列出集群当前已知的所有节点(node),以及这些节点的相关信息。
- cluster meet :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
- cluster forget <node_id> :从集群中移除 node_id 指定的节点(保证空槽道)。
- cluster replicate <node_id> :将当前节点设置为 node_id 指定的节点的从节点。
- cluster saveconfig :将节点的配置文件保存到硬盘里面。
槽slot命令
- cluster addslots [slot …] :将一个或多个槽(slot)指派(assign)给当前节点。
- cluster delslots [slot …] :移除一个或多个槽对当前节点的指派。
- cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
- cluster setslot node <node_id> :将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
- cluster setslot migrating <node_id> :将本节点的槽 slot 迁移到 node_id 指定的节点中。
- cluster setslot importing <node_id> :从 node_id 指定的节点中导入槽 slot 到本节点。
- cluster setslot stable :取消对槽 slot 的导入(import)或者迁移(migrate)。
键命令
- cluster keyslot :计算键 key 应该被放置在哪个槽上。
- cluster countkeysinslot :返回槽 slot 目前包含的键值对数量。
- cluster getkeysinslot :返回 count 个 slot 槽中的键
参考文档:
- https://gper.club/articles/7e7e7f7ff7g5egc4g6b
- http://blog.csdn.net/zc474235918/article/details/50974483
- http://blog.csdn.net/gxw19874/article/details/51992125
- http://blog.csdn.net/luozhonghua2014/article/details/54649295
- https://www.cnblogs.com/zuidongfeng/p/8032505.html
- https://www.cnblogs.com/jinjiyese153/p/8600703.html
- https://www.cnblogs.com/crazymakercircle/p/11985983.html
- https://gper.club/articles/7e7e7f7ff7g5egc7g6a
- https://gper.club/articles/7e7e7f7ff7g5egc7g6d