千家信息网

多节点 安装redis cluster安装部署-4.0.1

发表于:2024-11-11 作者:千家信息网编辑
千家信息网最后更新 2024年11月11日,环境节点数量IP:172.17.7.11 CPU :12 核 MEM:96G 启动服务数量:6 使用端口:7001~12IP:172.17.7.25 CPU :12 核 MEM:9
千家信息网最后更新 2024年11月11日多节点 安装redis cluster安装部署-4.0.1

环境

节点数量
IP:172.17.7.11   CPU :12 核  MEM:96G   启动服务数量:6   使用端口:7001~12IP:172.17.7.25   CPU :12 核  MEM:96G   启动服务数量:6   使用端口:7001~12IP:172.17.7.26   CPU :12 核  MEM:96G   启动服务数量:6   使用端口:7001~12
kernel

uname -a
Linux jp33e503-7-11.ptfuture.com 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

ststem version

more /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

software version

redis-4.0.1

software download

wget http://download.redis.io/releases/redis-4.0.1.tar.gz

install (三台各自执行)

mkdir /redis cd /redis wget -P /redis http://download.redis.io/releases/redis-4.0.1.tar.gz

tar -xzf redis-4.0.1.tar.gz cd redis-4.0.1 make && make install

验证安装:

[root@jp33e503-7-11 redis-4.0.1]# redis- tab键

redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli        redis-sentinel   redis-server

出现上面的命令

whereis redis-server

redis-server: /usr/local/bin/redis-server

将redis-trib.rb 复制到/usr/local/bin

cd src/
cp -rp redis-trib.rb /usr/local/bin/

集群搭建:

创建目录

mkdir -p /redis/cluster/700{1..9}
mkdir -p /redis/cluster/701{0..2}

配置redis 配置文件

[root@jp33e503-7-11 cluster]# pwd /redis/cluster

more redis.conf#redis后台运行daemonize    yes                         #pidfile文件pidfile  /var/run/redis_7001.pid         #锦亭端口port  7001                               #开启集群cluster-enabled  yes                     #配置文件首次启动自动生成cluster-config-file  /redis/cluster/7001/nodes_7001.conf     #请求超时cluster-node-timeout  5000               #aof日志appendonly  no  #绑定地址,需要别的机器能ping通的地址bind 172.17.7.11
拷贝配置文件

rsync /redis/cluster/redis.conf /redis/cluster/7001/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7002/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7003/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7004/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7005/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7006/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7007/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7008/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7009/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7010/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7011/redis.conf &&
rsync /redis/cluster/redis.conf /redis/cluster/7012/redis.conf

修改端口号

sed -i "s/7001/7001/g" /redis/cluster/7001/redis.conf &&
sed -i "s/7001/7002/g" /redis/cluster/7002/redis.conf &&
sed -i "s/7001/7003/g" /redis/cluster/7003/redis.conf &&
sed -i "s/7001/7004/g" /redis/cluster/7004/redis.conf &&
sed -i "s/7001/7005/g" /redis/cluster/7005/redis.conf &&
sed -i "s/7001/7006/g" /redis/cluster/7006/redis.conf &&
sed -i "s/7001/7007/g" /redis/cluster/7007/redis.conf &&
sed -i "s/7001/7008/g" /redis/cluster/7008/redis.conf &&
sed -i "s/7001/7009/g" /redis/cluster/7009/redis.conf &&
sed -i "s/7001/7010/g" /redis/cluster/7010/redis.conf &&
sed -i "s/7001/7011/g" /redis/cluster/7011/redis.conf &&
sed -i "s/7001/7012/g" /redis/cluster/7012/redis.conf

验证配置

more /redis/cluster/7001/redis.conf|grep 70 &&
more /redis/cluster/7002/redis.conf|grep 70 &&
more /redis/cluster/7003/redis.conf|grep 70 &&
more /redis/cluster/7004/redis.conf|grep 70 &&
more /redis/cluster/7005/redis.conf|grep 70 &&
more /redis/cluster/7006/redis.conf|grep 70 &&
more /redis/cluster/7007/redis.conf|grep 70 &&
more /redis/cluster/7008/redis.conf|grep 70 &&
more /redis/cluster/7009/redis.conf|grep 70 &&
more /redis/cluster/7010/redis.conf|grep 70 &&
more /redis/cluster/7011/redis.conf|grep 70 &&
more /redis/cluster/7012/redis.conf|grep 70

启动服务 (3台主机分别启动)

redis-server /redis/cluster/7001/redis.conf && redis-server /redis/cluster/7002/redis.conf && redis-server /redis/cluster/7003/redis.conf && redis-server /redis/cluster/7004/redis.conf && redis-server /redis/cluster/7005/redis.conf && redis-server /redis/cluster/7006/redis.conf && redis-server /redis/cluster/7007/redis.conf && redis-server /redis/cluster/7008/redis.conf && redis-server /redis/cluster/7009/redis.conf && redis-server /redis/cluster/7010/redis.conf && redis-server /redis/cluster/7011/redis.conf && redis-server /redis/cluster/7012/redis.conf

检查是否启动成功及信息

[root@jp33e503-7-11 cluster]# ps -ef |grep redis root 28364 1 0 13:33 ? 00:00:00 redis-server 172.17.7.11:7001 [cluster] root 29859 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7002 [cluster] root 29861 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7003 [cluster] root 29869 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7004 [cluster] root 29874 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7005 [cluster] root 29879 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7006 [cluster] root 29884 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7007 [cluster] root 29889 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7008 [cluster] root 29891 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7009 [cluster] root 29896 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7010 [cluster] root 29901 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7011 [cluster] root 29906 1 0 13:34 ? 00:00:00 redis-server 172.17.7.11:7012 [cluster]

然后配置下一台,注意修改服务器监听IP地址:

搭建集群

redis 官方提供了一个 redis-trib.rb (src目录下) 工具用于搭建集群。很明显是 ruby 写的,所以需要 ruby 环境。

安装 ruby

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

用 gem 安装 redis 接口

gem install redis

安装报错:

[root@jp33e503-7-11 cluster]# gem install redisFetching: redis-4.0.0.gem (100%)ERROR:  Error installing redis:        redis requires Ruby version >= 2.2.2.  #提示ruby版本较低  查看现在版本:[root@jp33e503-7-11 cluster]# ruby -vruby 2.0.0p648 (2015-12-16) [x86_64-linux]解决办法:wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gztar -xvzf ruby-2.4.1.tar.gz cd ruby-2.4.1./configuremake && make installruby  -v[root@jp33e503-7-11 ~]# ruby  -vruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

再次执行:

gem install redis

Fetching: redis-4.0.0.gem (100%)Successfully installed redis-4.0.0Parsing documentation for redis-4.0.0Installing ri documentation for redis-4.0.0Done installing documentation for redis after 1 seconds1 gem installed
创建集群,确保开放端口

运行 redis-trib.rb 查看 集群创建帮助

Usage: redis-trib     create          host1:port1 ... hostN:portN                  --replicas   check           host:port  info            host:port  fix             host:port                  --timeout   reshard         host:port                  --from                   --to                   --slots                   --yes                  --timeout                   --pipeline   rebalance       host:port                  --weight                   --auto-weights                  --use-empty-masters                  --timeout                   --simulate                  --pipeline                   --threshold   add-node        new_host:new_port existing_host:existing_port                  --slave                  --master-id   del-node        host:port node_id  set-timeout     host:port milliseconds  call            host:port command arg arg .. arg  import          host:port                  --from                   --copy                  --replace  help            (show this help)

创建集群

#其中 --replicas 1 意思为为每个 master 分配 1 各 slave

redis-trib.rb create --replicas 1 172.17.7.11:7001 172.17.7.11:7002 172.17.7.11:7003 172.17.7.11:7004 172.17.7.11:7005 172.17.7.11:7006 172.17.7.11:7007 172.17.7.11:7008 172.17.7.11:7009 172.17.7.11:7010 172.17.7.11:7011 172.17.7.11:7012 172.17.7.25:7001 172.17.7.25:7002 172.17.7.25:7003 172.17.7.25:7004 172.17.7.25:7005 172.17.7.25:7006 172.17.7.25:7007 172.17.7.25:7008 172.17.7.25:7009 172.17.7.25:7010 172.17.7.25:7011 172.17.7.25:7012 172.17.7.26:7001 172.17.7.26:7002 172.17.7.26:7003 172.17.7.26:7004 172.17.7.26:7005 172.17.7.26:7006 172.17.7.26:7007 172.17.7.26:7008 172.17.7.26:7009 172.17.7.26:7010 172.17.7.26:7011 172.17.7.26:7012

>>> Creating cluster>>> Performing hash slots allocation on 36 nodes...Using 18 masters:172.17.7.11:7001172.17.7.25:7001172.17.7.26:7001172.17.7.11:7002172.17.7.25:7002172.17.7.26:7002172.17.7.11:7003172.17.7.25:7003172.17.7.26:7003172.17.7.11:7004172.17.7.25:7004172.17.7.26:7004172.17.7.11:7005172.17.7.25:7005172.17.7.26:7005172.17.7.11:7006172.17.7.25:7006172.17.7.26:7006Adding replica 172.17.7.25:7007 to 172.17.7.11:7001Adding replica 172.17.7.11:7007 to 172.17.7.25:7001Adding replica 172.17.7.11:7008 to 172.17.7.26:7001Adding replica 172.17.7.26:7007 to 172.17.7.11:7002Adding replica 172.17.7.26:7008 to 172.17.7.25:7002Adding replica 172.17.7.25:7008 to 172.17.7.26:7002Adding replica 172.17.7.25:7009 to 172.17.7.11:7003Adding replica 172.17.7.11:7009 to 172.17.7.25:7003Adding replica 172.17.7.11:7010 to 172.17.7.26:7003Adding replica 172.17.7.26:7009 to 172.17.7.11:7004Adding replica 172.17.7.26:7010 to 172.17.7.25:7004Adding replica 172.17.7.25:7010 to 172.17.7.26:7004Adding replica 172.17.7.25:7011 to 172.17.7.11:7005Adding replica 172.17.7.11:7011 to 172.17.7.25:7005Adding replica 172.17.7.11:7012 to 172.17.7.26:7005Adding replica 172.17.7.26:7011 to 172.17.7.11:7006Adding replica 172.17.7.26:7012 to 172.17.7.25:7006Adding replica 172.17.7.25:7012 to 172.17.7.26:7006M: 574bdafc37a1d63f9988480b1c360b71699b0d3a 172.17.7.11:7001   slots:0-909 (910 slots) masterM: 0fdc365a3360390e7ee01b6e2c5f2f293142122c 172.17.7.11:7002   slots:2731-3640 (910 slots) masterM: 8e91fca9fddbe7ecdd4d97c7de160daf6c635a6d 172.17.7.11:7003   slots:5461-6371 (911 slots) masterM: 2c38ebc64166f8bcba3c00830326d50051a7a96e 172.17.7.11:7004   slots:8192-9101 (910 slots) masterM: a841f6d12f8e8f0ed56479a596319675538fc474 172.17.7.11:7005   slots:10923-11832 (910 slots) masterM: 49e4e848e2e09e6ef743777d59574740e2e95030 172.17.7.11:7006   slots:13653-14563 (911 slots) masterS: 71f3fb407719262a844d53d1abd2c164cdca20bf 172.17.7.11:7007   replicates 32d5dfc35a27aed442b41138bb9b367507359100S: ab1fe94d17ac2f4810b277addd2dc973b5e708d3 172.17.7.11:7008   replicates a57d14b025d5c8c37fc2711e109362b5b40b4d6e。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。S: 3e773633e3f0761777e3f70ba0038c05a87a49b4 172.17.7.26:7011   replicates 49e4e848e2e09e6ef743777d59574740e2e95030S: c0c52c55b24d25f7f1cdc76fd9a68752789f3ea7 172.17.7.26:7012   replicates f59e48dcf034e3a961cc4999616e59fa03dc8a8cCan 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 clusterWaiting for the cluster to join.......>>> Performing Cluster Check (using node 172.17.7.11:7001)M: 574bdafc37a1d63f9988480b1c360b71699b0d3a 172.17.7.11:7001   slots:0-909 (910 slots) master   1 additional replica(s)S: 95cc9fab44f23e38c055bce23fddb19b813dca8a 172.17.7.11:7010   1 additional replica(s)M: 5baf8fcf69e3aaafe3a4d7eba997697335b6c3c1 172.17.7.25:7002   slots:3641-4550 (910 slots) master   1 additional replica(s)S: 71f3fb407719262a844d53d1abd2c164cdca20bf 172.17.7.11:7007   slots: (0 slots) slave。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。   slots:13653-14563 (911 slots) master   1 additional replica(s)M: 6451851e996947f9e3f906c646664efb7242d451 172.17.7.25:7004   slots:9102-10011 (910 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

测试集群

在 172.17.7.11:7001 机器上设置一个键值对 testKey--testValue ,因为绑定了 IP ,所以 h 参数不能省略

redis-cli -h 172.17.7.11 -c -p 7001
172.17.7.11:7001> set testkey testvalue-> Redirected to slot [4757] located at 172.17.7.26:7002OK172.17.7.26:7002> get testkey"testvalue"

在 72.17.7.25:7001 上获取 testKey 的值,发现自动定向到 72.17.7.26:7002,说明成功

[root@jp33e503-7-11 ~]# redis-cli -h 172.17.7.25 -c -p 7001 172.17.7.25:7001> get testkey-> Redirected to slot [4757] located at 172.17.7.26:7002"testvalue"

至此,Redis 集群搭建完成

集群操作

检查状态
redis-trib.rb check 172.17.7.11:7001>>> Performing Cluster Check (using node 172.17.7.11:7001)M: 574bdafc37a1d63f9988480b1c360b71699b0d3a 172.17.7.11:7001   slots:0-909 (910 slots) master   1 additional replica(s)S: 95cc9fab44f23e38c055bce23fddb19b813dca8a 172.17.7.11:7010   slots: (0 slots) slave   replicates 8a645dd55e4abe59c3d480ebf0fd60494eaabe4aM: 8e91fca9fddbe7ecdd4d97c7de160daf6c635a6d 172.17.7.11:7003   slots:5461-6371 (911 slots) master   1 additional replica(s)M: a841f6d12f8e8f0ed56479a596319675538fc474 172.17.7.11:7005   slots:10923-11832 (910 slots) master   1 additional replica(s)S: 8fefc4e249e0c54d326ed3fa263f10952ae94ac4 172.17.7.25:7010   slots: (0 slots) slave   replicates 4ce13bdfc044a546ec7d03a346b39acda7bb4dd0M: 62b01ec5ecf4bd4596c4c004bf1c82f1afc6a407 172.17.7.26:7002   slots:4551-5460 (910 slots) master   1 additional replica(s)M: 5baf8fcf69e3aaafe3a4d7eba997697335b6c3c1 172.17.7.25:7002   slots:3641-4550 (910 slots) master   1 additional replica(s)   slots:14564-15473 (910 slots) master   1 additional replica(s)S: 11a8e35e3c9ea4c93dd36a96094881ff29fc1856 172.17.7.11:7012...................略。。。。   slots:10012-10922 (911 slots) master   1 additional replica(s)S: 24ef0015d8a38d7aeab5dc7373262ba078f9e13d 172.17.7.26:7007   slots: (0 slots) slave   replicates 0fdc365a3360390e7ee01b6e2c5f2f293142122cM: 49e4e848e2e09e6ef743777d59574740e2e95030 172.17.7.11:7006   slots:13653-14563 (911 slots) master   1 additional replica(s)M: 6451851e996947f9e3f906c646664efb7242d451 172.17.7.25:7004   slots:9102-10011 (910 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
查看所有节点信息

redis-cli -h 172.17.7.11 -c -p 7001

172.17.7.11:7001> cluster nodes

95cc9fab44f23e38c055bce23fddb19b813dca8a 172.17.7.11:7010@17010 slave 8a645dd55e4abe59c3d480ebf0fd60494eaabe4a 0 1504596545000 27 connected8e91fca9fddbe7ecdd4d97c7de160daf6c635a6d 172.17.7.11:7003@17003 master - 0 1504596545502 3 connected 5461-6371a841f6d12f8e8f0ed56479a596319675538fc474 172.17.7.11:7005@17005 master - 0 1504596546103 5 connected 10923-11832.........................
查看集群信息

172.17.7.11:7001> CLUSTER INFO

cluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:36cluster_size:18cluster_current_epoch:36cluster_my_epoch:1cluster_stats_messages_ping_sent:26628cluster_stats_messages_pong_sent:26948cluster_stats_messages_sent:53576cluster_stats_messages_ping_received:26913cluster_stats_messages_pong_received:26628cluster_stats_messages_meet_received:35cluster_stats_messages_received:53576
节点命令

#将 ip 和 port 所在的节点添加到集群当中 cluster meet

#从集群中移除 node_id 指定的节点。 cluster forget

#将当前结点设置为 node_id 的 slave(从节点) cluster replicate

#保存节点配置文件 cluster saveconfig

槽命令

#将一个或多个槽( slot)指派( assign)给当前节点。 cluster addslots [slot ...]

#移除一个或多个槽对当前节点的指派。 cluster delslots [slot ...]

#移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 cluster flushslots

#将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。 cluster setslot node

#将本节点的槽 slot 迁移到 node_id 指定的节点中。 cluster setslot migrating

#从 node_id 指定的节点中导入槽 slot 到本节点。 cluster setslot importing

#取消对槽 slot 的导入( import)或者迁移( migrate)。 cluster setslot stable

#计算键 key 应该被放置在哪个槽上。 cluster keyslot

#返回槽 slot 目前包含的键值对数量。 cluster countkeysinslot

#返回 count 个 slot 槽中的键 。 cluster getkeysinslot

脚本

启动节点

#!/bin/bash for i in 1 2 3 4 5 6 7 8 9 do redis-server /redis_cluster/700$i/redis.conf; done

for i in 10 11 12 do redis-server /redis_cluster/70$i/redis.conf; done

启动集群
#!/bin/bashredis-trib.rb  create  --replicas  1 172.17.7.11:7001 172.17.7.11:7002 172.17.7.11:7003 172.17.7.11:7004 172.17.7.11:7005 172.17.7.11:7006 172.17.7.11:7007 172.17.7.11:7008 172.17.7.11:7009 172.17.7.11:7010 172.17.7.11:7011 172.17.7.11:7012 172.17.7.25:7001 172.17.7.25:7002 172.17.7.25:7003 172.17.7.25:7004 172.17.7.25:7005 172.17.7.25:7006 172.17.7.25:7007 172.17.7.25:7008 172.17.7.25:7009 172.17.7.25:7010 172.17.7.25:7011 172.17.7.25:7012 172.17.7.26:7001 172.17.7.26:7002 172.17.7.26:7003 172.17.7.26:7004 172.17.7.26:7005 172.17.7.26:7006 172.17.7.26:7007 172.17.7.26:7008 172.17.7.26:7009 172.17.7.26:7010 172.17.7.26:7011 172.17.7.26:7012
关闭集群
#!/bin/bashfor i in 11 25 26do        for j in 1 2 3 4 5 6 7 8 9         do                redis-cli -c -h 172.17.7.$i -p 700$j shutdown;        donedonefor i in 11 25 26do        for j in 10 11 12        do                redis-cli -c -h 172.17.7.$i -p 70$j shutdown;        donedone

首次安装,体验一下过程,官方下载的源码包里面有线程的自动化脚本,安装单个服务和创建集群的,大家可以考虑使用:

脚本位置:redis-4.0.1/utils

ls


[root@ts-100-20 utils]# ls

build-static-symbols.tcl generate-command-help.rb install_server.sh redis_init_script.tpl whatisdoing.sh

cluster_fail_time.tcl graphs lru redis-sha1.rb

corrupt_rdb.c hashtable redis-copy.rb releasetools

create-cluster hyperloglog redis_init_script speed-regression.tcl


0