两台服务器搭建redis集群

两台服务器搭建redis集群

### 两台服务器,各启动三个实例,形成三主三从
实验机器IP: 172.31.25.110 172.31.25.111

系统环境:centos 7

1、安装所需环境和工具
yum -y install wget vim tcl gcc make

2、下载redis 压缩包并解压
cd /usr/local/
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -zxvf redis-3.2.8.tar.gz

3、编译redis源文件
cd redis-3.2.8
make

cd src
make install
# 测试: make test
(如果 /usr/local/bin/ 文件夹内没有 redis-server 那几个文件,就从 /usr/local/redis-3.2.8/src/ 中拷贝过去,命令:cp redis-server redis-cli redis-sentinel redis-benchmark redis-check-aof redis-check-rdb /usr/local/bin/)


4、配置内核参数
# 配置 vm.overcommit_memory 为1,这可以避免数据被截断
sysctl -w vm.overcommit_memory=1

5、创建多实例的文件夹,用来存放不同实例的配置文件
cd /usr/local/
mkdir cluster
cd cluster
mkdir 7000 7001 7002

6、修改配置文件
vim /usr/local/redis-3.2.8/redis.conf

bind 172.31.25.110(需要不同服务器的节点连通,就不能设置为 127.0.0.1protected-mode no(需要不同服务器的节点连通,这个就要设置为 no)
daemonize yes(设置后台运行redis)
cluster-enabled yes
cluster-node-timeout 5000
appendonly yes

# 根据不同端口需要设置的地方
port 7000
pidfile /var/run/redis_7000.pid
logfile /var/log/redis/redis_7000.log
dbfilename dump_7000.rdb
appendfilename "appendonly_7000.aof"
cluster-config-file nodes_7000.conf


7、复制配置文件到各个实例文件夹,并修改相应端口号和参数
cp -f /usr/local/redis-3.2.8/redis.conf /usr/local/cluster/7000/
cp -f /usr/local/redis-3.2.8/redis.conf /usr/local/cluster/7001/
cp -f /usr/local/redis-3.2.8/redis.conf /usr/local/cluster/7002/

8、启动各个实例
cd /usr/local/redis-3.2.8/src/
./redis-server /usr/local/cluster/7000/redis.conf &
./redis-server /usr/local/cluster/7001/redis.conf &
./redis-server /usr/local/cluster/7002/redis.conf &

使用 ps -ef|grep redis 查看是否都启动成功,IP和端口号都正确

9、防火墙开通端口号策略(这里用centos7默认的firewall-cmd)
firewall-cmd --zone=public --add-port=7000-7002/tcp --permanent
firewall-cmd --zone=public --add-port=17000-17002/tcp --permanent(必须开集群总线端口,集群总线端口=端口号+10000,例:7000的集群总线端口是17000。这个集群总线端口不开放,集群的时候外部服务器的节点添加不进来)
firewall-cmd --reload


==================================================================================
====================== 1~9都是要在两台服务器中操作的 ===========================
==================================================================================

10、测试两台服务器是否都能 telnet 得通另一台的 7000~700217000~17002

11、安装 ruby 环境
Redis 3.0以上的集群方式是通过Redis安装目录下的bin/redis-trib.rb脚本搭建。

这个脚本是用Ruby编写的,尝试运行,如果打印如下,你可以跳过本文的第二部分。

复制代码
[email protected]:~/yangfan/local/redis-3.2.1/bin$ ruby redis-trib.rb 
Usage: redis-trib <command> <options> <arguments ...>

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

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
复制代码
如果执行失败,那么不幸的是你的机器没有Ruby运行的环境,那么你需要安装Ruby。进入第二部分。



2、安装ruby

下面的过程都是在root权限下完成的。

1)yum安装ruby和依赖的包。

# yum -y install ruby ruby-devel rubygems rpm-build 
一般来说,这一步是能正常完成的。



2)使用gem这个命令来安装redis接口

gem是ruby的一个工具包

# gem install redis 
安装过程出错



郁闷,看样子要升级ruby版本。



3)升级Ruby的版本

安装rvm,我不知道这是个什么东西,但是感觉像是Ruby的一个包管理器。

# curl -L get.rvm.io | bash -s stable 
WTF,又出问题了



气急败坏的照着他说的做

复制代码
# gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <[email protected]>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
复制代码
然后重新下载rvm安装,成功了。

复制代码
# curl -L get.rvm.io | bash -s stable 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 24090  100 24090    0     0  18023      0  0:00:01  0:00:01 --:--:--  129k
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: Signature made Mon 11 Sep 2017 04:59:21 AM CST using RSA key ID BF04FF17
gpg: Good signature from "Michal Papis (RVM signing) <[email protected]>"
gpg:                 aka "Michal Papis <[email protected]>"
gpg:                 aka "[jpeg image of size 5015]"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3
     Subkey fingerprint: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17
GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
复制代码
接着,source环境,让rvm可用。

# source /usr/local/rvm/scripts/rvm
查看Ruby可用版本

复制代码
# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.7]
[ruby-]2.3[.4]
[ruby-]2.4[.1]
复制代码
可以看到最新的版本是2.4.1,那么我们装最新的吧。

复制代码
# rvm install 2.4.1
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/6/x86_64/ruby-2.4.1.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: libffi-devel, libyaml-devel......
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.4.1, this may take a while depending on your cpu(s)...
ruby-2.4.1 - #downloading ruby-2.4.1, this may take a while depending on your connection...

curl: (35) SSL connect error
There was an error(35).
Checking fallback: https://ftp.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11.9M  100 11.9M    0     0  1753k      0  0:00:07  0:00:07 --:--:-- 2919k
ruby-2.4.1 - #extracting ruby-2.4.1 to /usr/local/rvm/src/ruby-2.4.1....
ruby-2.4.1 - #applying patch /usr/local/rvm/patches/ruby/2.4.1/random_c_using_NR_prefix.patch.
ruby-2.4.1 - #configuring..................................................................
ruby-2.4.1 - #post-configuration..
ruby-2.4.1 - #compiling..............................................................................................
ruby-2.4.1 - #installing.........................
ruby-2.4.1 - #making binaries executable..
ruby-2.4.1 - #downloading rubygems-2.6.14
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  751k  100  751k    0     0   443k      0  0:00:01  0:00:01 --:--:--  628k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.4.1 - #extracting rubygems-2.6.14....
ruby-2.4.1 - #removing old rubygems.........
ruby-2.4.1 - #installing rubygems-2.6.14...........................
ruby-2.4.1 - #gemset created /usr/local/rvm/gems/ruby-2.4.1@global
ruby-2.4.1 - #importing gemset /usr/local/rvm/gemsets/global.gems...............................................
ruby-2.4.1 - #generating global wrappers........
ruby-2.4.1 - #gemset created /usr/local/rvm/gems/ruby-2.4.1
ruby-2.4.1 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.4.1 - #generating default wrappers........
ruby-2.4.1 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.4.1 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri
复制代码
至此,我们升级了Ruby的版本。



4)安装gem redis接口,成功!

复制代码
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
复制代码


5)安装rubygems,成功!

复制代码
# yum install -y rubygems
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
base                                                                                                                                                   | 3.7 kB     00:00     
didi_jenkins_enable                                                                                                                                    | 1.5 kB     00:00     
didi_op_toa_enable                                                                                                                                     | 1.5 kB     00:00     
didi_txjenkins_enable                                                                                                                                  | 1.5 kB     00:00     
didi_update                                                                                                                                            | 1.5 kB     00:00     
epel                                                                                                                                                   | 4.3 kB     00:00     
extras                                                                                                                                                 | 3.4 kB     00:00     
tmprepo                                                                                                                                                | 1.5 kB     00:00     
updates                                                                                                                                                | 3.4 kB     00:00     
Package rubygems-1.3.7-5.el6.noarch already installed and latest version
Nothing to do
复制代码
至此,我们的Ruby和运行redis-trib.rb需要的环境安装完成了。 

14、创建集群
cd /usr/local/redis-3.2.8/src/
./redis-trib.rb create --replicas 1 172.31.25.110:7000 172.31.25.110:7001 172.31.25.110:7002 172.31.25.111:7000 172.31.25.111:7001 172.31.25.111:7002

留意屏幕,会有一句(type 'yes' to accept),输入 yes ,回车,就是接受自动分配的三主三从
如果最后出现
[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...........
一直有 “.”出现,说明另一台服务器的端口策略没通,一直在等那边的节点加入集群,那么恭喜你,要悲剧了.....


15、验证集群节点数
cd /usr/local/redis-3.2.8/src/
./redis-cli -h 172.31.25.110 -c -p 7000
CLUSTER NOTES

猜你喜欢

转载自blog.csdn.net/u011790603/article/details/82663913