CentOS搭建Redis集群
2022/9/14 2:16:15
本文主要是介绍CentOS搭建Redis集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#环境
VMware: VMwareworkstation_full_12.1.0.2487.1453173744.exe
CentOS 7: CentOS Linux release 7.5.1804 (Core)
#redis安装
下载解压拷贝redis
[root@localhost ~]# mkdir /soft && cd /soft [root@localhost soft]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz [root@localhost soft]# tar xzf redis-4.0.10.tar.gz [root@localhost soft]# mv redis-4.0.10 /usr/local/redis-4.0.10 && cd /usr/local/redis-4.0.10
编译redis
[root@localhost redis-4.0.10]# make cd src && make all make[1]: 进入目录“/usr/local/redis-4.0.10/src” CC Makefile.dep make[1]: 离开目录“/usr/local/redis-4.0.10/src” make[1]: 进入目录“/usr/local/redis-4.0.10/src” rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark (cd ../deps && make distclean) make[2]: 进入目录“/usr/local/redis-4.0.10/deps” (cd hiredis && make clean) > /dev/null || true (cd linenoise && make clean) > /dev/null || true (cd lua && make clean) > /dev/null || true (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true (rm -f .make-*) make[2]: 离开目录“/usr/local/redis-4.0.10/deps” (rm -f .make-*) echo STD=-std=c99 -pedantic -DREDIS_STATIC='' >> .make-settings echo WARN=-Wall -W -Wno-missing-field-initializers >> .make-settings echo OPT=-O2 >> .make-settings echo MALLOC=jemalloc >> .make-settings echo CFLAGS= >> .make-settings echo LDFLAGS= >> .make-settings echo REDIS_CFLAGS= >> .make-settings echo REDIS_LDFLAGS= >> .make-settings echo PREV_FINAL_CFLAGS=-std=c99 -pedantic -DREDIS_STATIC='' -Wall -W -Wno-missing-field-initializers -O2 -g -ggdb -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src -DUSE_JEMALLOC -I../deps/jemalloc/include >> .make-settings echo PREV_FINAL_LDFLAGS= -g -ggdb -rdynamic >> .make-settings (cd ../deps && make hiredis linenoise lua jemalloc) make[2]: 进入目录“/usr/local/redis-4.0.10/deps” (cd hiredis && make clean) > /dev/null || true (cd linenoise && make clean) > /dev/null || true (cd lua && make clean) > /dev/null || true (cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true (rm -f .make-*) (echo "" > .make-cflags) (echo "" > .make-ldflags) MAKE hiredis cd hiredis && make static make[3]: 进入目录“/usr/local/redis-4.0.10/deps/hiredis” gcc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c make[3]: gcc:命令未找到 make[3]: *** [net.o] 错误 127 make[3]: 离开目录“/usr/local/redis-4.0.10/deps/hiredis” make[2]: *** [hiredis] 错误 2 make[2]: 离开目录“/usr/local/redis-4.0.10/deps” make[1]: [persist-settings] 错误 2 (忽略) CC adlist.o /bin/sh: cc: 未找到命令 make[1]: *** [adlist.o] 错误 127 make[1]: 离开目录“/usr/local/redis-4.0.10/src” make: *** [all] 错误 2
安装 gcc
[root@localhost redis-4.0.10]# yum install gcc [root@localhost redis-4.0.10]# make cd src && make all make[1]: 进入目录“/usr/local/redis-4.0.10/src” CC Makefile.dep make[1]: 离开目录“/usr/local/redis-4.0.10/src” make[1]: 进入目录“/usr/local/redis-4.0.10/src” CC adlist.o In file included from adlist.c:34:0: zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录 #include <jemalloc/jemalloc.h> ^ 编译中断。 make[1]: *** [adlist.o] 错误 1 make[1]: 离开目录“/usr/local/redis-4.0.10/src” make: *** [all] 错误 2
注:如果在make的时候不指定内存管理方式,使用
make MALLOC=libc
指令。malloc是管理内存碎片的。
[root@localhost redis-4.0.10]# make MALLOC=libc [root@localhost redis-4.0.10]# make & make install
#创建redis节点
[root@localhost redis-4.0.10]# mkdir conf-cluster && cd conf-cluster [root@localhost conf-cluster]# touch {9000..9002}.conf
修改{9000..9002}.conf文件。
port 9000 bind 192.168.1.100 daemonize yes cluster-enabled yes cluster-node-timeout 15000 appendonly yes pidfile /var/run/redis_9000.pid cluster-config-file nodes-9000.conf
启动{9000..9002}节点
[root@localhost conf-cluster]# redis-server 9000.conf [root@localhost conf-cluster]# redis-server 9001.conf [root@localhost conf-cluster]# redis-server 9002.conf
检查各redis节点启动情况
[root@localhost conf-cluster]# ps -ef | grep redis //redis节点是否启动成功 [root@localhost conf-cluster]# netstat -tnlp | grep redis //监听节点端口
#安装ruby
创建集群时需要用Ruby 运行
redis-trib.rb
[root@localhost conf-cluster]# yum -y install ruby ruby-devel rubygems rpm-build [root@localhost conf-cluster]# gem install redis ERROR: Could not find a valid gem 'redis' (>= 0), here is why: Unable to download data from https://rubygems.org/ - no such name (https://rubygems.org/latest_specs.4.8.gz)
修改 RubyGems
镜像
[root@localhost conf-cluster]# gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ https://gems.ruby-china.org/ added to sources https://rubygems.org/ removed from sources
[root@localhost conf-cluster]# gem install redis Fetching: redis-4.0.1.gem (100%) ERROR: Error installing redis: redis requires Ruby version >= 2.2.2.
Centos默认支持ruby到2.0.0,可gem 安装redis需要最低是2.2.2
解决办法是 先安装rvm,再把ruby版本提升
升级ruby
安装rvm(ruby版本控制)
参考:
https://blog.csdn.net/qq_37595946/article/details/77800147
https://ruby-china.org/wiki/rvm-guide
[root@localhost conf-cluster]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 [root@localhost conf-cluster]# curl -L get.rvm.io | bash -s stable [root@localhost conf-cluster]# source /etc/profile.d/rvm.sh [root@localhost conf-cluster]# rvm list known [root@localhost conf-cluster]# rvm install 2.5.1 --disable-binary
DNS
Checking fallback: https://ftp.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.bz2
# Generated by NetworkManager + 8.8.8.8 nameserver 192.168.1.1
切换ruby版本
[root@localhost conf-cluster]# rvm use 2.5.1 [root@localhost conf-cluster]# rvm remove 2.2.0
gem再次安装redis
[root@localhost conf-cluster]# gem install redis Fetching: redis-4.0.1.gem (100%) Successfully installed redis-4.0.1 Parsing documentation for redis-4.0.1 Installing ri documentation for redis-4.0.1 Done installing documentation for redis after 0 seconds 1 gem installed
#创建redis集群
[root@localhost conf-cluster]# /usr/local/redis-4.0.10/src/redis-trib.rb create --replicas 1 192.168.1.100:9000 192.168.1.100:9001 192.168.1.100:9002 >>> Creating cluster *** ERROR: Invalid configuration for cluster creation. *** Redis Cluster requires at least 3 master nodes. *** This is not possible with 3 nodes and 1 replicas per node. *** At least 6 nodes are required.
集群至少需要3主3从6台服务器或进程
复制虚拟机
注: 复制虚拟机,记得修改虚拟机的MAC地址
启动每台虚拟机的redis节点
[root@localhost conf-cluster]# redis-server {9000..9002}.conf
再次创建集群
[root@localhost conf-cluster]# /usr/local/redis-4.0.10/src/redis-trib.rb create --replicas 1 192.168.1.100:9000 192.168.1.100:9001 192.168.1.100:9002 192.168.1.101:9000 192.168.1.101:9001 192.168.1.101:9002 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.1.100:9000 192.168.1.101:9000 192.168.1.100:9001 Adding replica 192.168.1.101:9002 to 192.168.1.100:9000 Adding replica 192.168.1.100:9002 to 192.168.1.101:9000 Adding replica 192.168.1.101:9001 to 192.168.1.100:9001 M: 40d42fcd808b073c839d0b767eb296853a1995ed 192.168.1.100:9000 slots:0-5460 (5461 slots) master M: e1b5627a3f06d9dbf6ae75e26a3dfc3127018940 192.168.1.100:9001 slots:10923-16383 (5461 slots) master S: 8a84e184bad311182898e7ee3f21280ed094a527 192.168.1.100:9002 replicates 7d002ccb55e1fac7ec6ae57b3ee710806bbe1b0d M: 7d002ccb55e1fac7ec6ae57b3ee710806bbe1b0d 192.168.1.101:9000 slots:5461-10922 (5462 slots) master S: f77cc08cac4bc93c642820afb6ec9b42710e2f0a 192.168.1.101:9001 replicates e1b5627a3f06d9dbf6ae75e26a3dfc3127018940 S: 407848979110a89f8804cf61a61c4386779a1a85 192.168.1.101:9002 replicates 40d42fcd808b073c839d0b767eb296853a1995ed 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.1.100:9000) M: 40d42fcd808b073c839d0b767eb296853a1995ed 192.168.1.100:9000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 8a84e184bad311182898e7ee3f21280ed094a527 192.168.1.100:9002 slots: (0 slots) slave replicates 7d002ccb55e1fac7ec6ae57b3ee710806bbe1b0d S: 407848979110a89f8804cf61a61c4386779a1a85 192.168.1.101:9002 slots: (0 slots) slave replicates 40d42fcd808b073c839d0b767eb296853a1995ed S: f77cc08cac4bc93c642820afb6ec9b42710e2f0a 192.168.1.101:9001 slots: (0 slots) slave replicates e1b5627a3f06d9dbf6ae75e26a3dfc3127018940 M: e1b5627a3f06d9dbf6ae75e26a3dfc3127018940 192.168.1.100:9001 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 7d002ccb55e1fac7ec6ae57b3ee710806bbe1b0d 192.168.1.101:9000 slots:5461-10922 (5462 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.
注:
如果一直卡在Waiting for the cluster to join...
请确保每台机器防火墙开启redis客户端端口及集群总线端口。
例:
如redis端口为9000,则集群总线端口为19000;
集群总线端口为redis客户端连接的端口 + 10000
防火墙操作
[root@localhost ~]# firewall-cmd --zone=public --add-port=9000/tcp --permanent [root@localhost ~]# firewall-cmd --zone=public --add-port=19000/tcp --permanent ...
[root@localhost ~]# firewall-cmd --reload
[root@localhost conf-cluster]# firewall-cmd --list-ports 9000/tcp 9001/tcp 9002/tcp 19000/tcp 19001/tcp 19002/tcp
#连接redis集群
[root@localhost conf-cluster]# redis-cli -h 192.168.1.100 -c -p 9000 192.168.1.100:9000>
必须要加:-c 参数
添加数据测试
[root@localhost conf-cluster]# redis-cli -h 192.168.1.100 -c -p 9000 192.168.1.100:9000> set title testcluster OK
在192.168.1.101 9002节点连接,获取数据。直接Redirected 从192.168.1.100:9000 获取数据。
[root@localhost conf-cluster]# redis-cli -h 192.168.1.101 -c -p 9002 192.168.1.101:9002> get title -> Redirected to slot [2217] located at 192.168.1.100:9000 "testcluster"
redis集群环境配置成功!!!
如果重启服务器后,再次执行redis-trib.rb会报错如下错误:
[ERR] Node 192.168.1.80:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决方法:
删除配置的cluster-config-file
pidfile
和生成的dump.rdb
,然后重启redis服务,最后启动集群
关闭redis节点端口
[root@localhost conf-cluster]# redis-cli -h 192.168.1.100 -c -p 9000 shutdown
#参考
http://shift-alt-ctrl.iteye.com/blog/2284890
https://www.cnblogs.com/subendong/p/7417601.html
这篇关于CentOS搭建Redis集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-07Redis高并发入门详解
- 2024-12-07Redis缓存入门:新手必读指南
- 2024-12-07Redis缓存入门:新手必读教程
- 2024-12-07Redis入门:新手必备的简单教程
- 2024-12-07Redis入门:新手必读的简单教程
- 2024-12-06Redis入门教程:从安装到基本操作
- 2024-12-06Redis缓存入门教程:轻松掌握缓存技巧
- 2024-12-04Redis入门:简单教程详解
- 2024-11-29Redis开发入门教程:从零开始学习Redis
- 2024-11-27Redis入门指南:快速掌握Redis基础操作