ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

CentOS搭建Redis集群

2022-09-13 16:00:10  阅读:248  来源: 互联网

标签:CentOS Redis make redis 192.168 cluster 集群 root localhost


#环境

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,make,redis,192.168,cluster,集群,root,localhost
来源: https://www.cnblogs.com/meideprac/p/16689417.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有