redis main building from the cluster environment

Redis three hosts to build three pairs of master and slave server cluster environment ready

host1:192.168.1.9:6379
                    192.168.1.9:6380
host2:192.168.1.106:6379
                    192.168.1.106:6380
host3:192.168.1.110:6379
                    192.168.1.110:6380
注意:
(1)在建立redis的cluster环境时必须清空所有redis服务的所有key-value数据,没有任何数据
(2)每个 redis node 节点采用相同的硬件配置、相同的密码 

1. The three hosts are open two ports of 6379 and 6380, redis need to configure two separate profiles, for example to host1

1) to redis configured listening port 6379

     [root@localhost ~]# vim /app/redis/etc/redis.conf 
        ……
     bind 192.168.1.9  #绑定ip
        ……
     port 6379   #绑定6379端口
     ……
    cluster-enabled yes   #开启redis的集群功能
     ……
    cluster-config-file nodes-6379.conf  #开启自动创建集群配置文件
     ……

2) to redis configured listening port 6380

    [root@localhost ~]# cp/app/redis/etc/redis.conf   /app/redis/etc/redis.6380.conf 
    [root@localhost ~]# vim /app/redis/etc/redis.6380.conf
    ……
     bind 192.168.1.9  #绑定ip
        ……
     port 6380   #绑定6380端口
     ……
    cluster-enabled yes   #开启redis的集群功能
     ……
    cluster-config-file nodes-6379.conf  #开启自动创建集群配置文件
     ……
    :%s/6379/6380/g   #配置文件底部实现全局替换,将所有6379替换为6380

2. The three hosts redis profile is set to finish before starting redis service

1) open simultaneously process two redis service

[root@localhost ~]#  redis-server /app/redis/etc/redis.6380.conf && redis-server /app/redis/etc/redis.conf

2) redis View of 6379 and 6380 the two ports is in listening state

[root@localhost ~]# ss -tnlp
State      Recv-Q Send-Q                  Local Address:Port                      Peer Address:Port              
LISTEN     0      511                            192.168.1.9:6379                             *:*                  
 users:(("redis-server",pid=7492,fd=6))
LISTEN     0      511                             192.168.1.9:6380                             *:*                   
users:(("redis-server",pid=7487,fd=6))
LISTEN     0      511                             192.168.1.9:16379                           *:*                  
 users:(("redis-server",pid=7492,fd=8))
LISTEN     0      511                             192.168.1.9:16380                           *:*                   
users:(("redis-server",pid=7487,fd=8))

3. Copy command to create the cluster redis-trib.rb to under / usr / bin, this command is temporarily unavailable, the need to compile and install ruby ​​and install redis module

[root@localhost ruby-2.5.5]# cp redis-trib.rb /usr/bin

4. For the ruby ​​compiler compiler environment Toolkit

yum ×××tall -y vim lrzsz tree screen psmisc lsof tcpdump wget  ntpdate  gcc gcc-c++ glibc glibc-devel 
pcre pcre-devel openssl  openssl-devel systemd-devel net-tools iotop bc  zip unzip zlib-devel bash-completion
 nfs-utils automake libxml2  libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed

5. Compile the installation kit ruby ​​(ruby because yum installed version is too low, can not meet the ruby ​​gem version Installation modules depend redis)

[root@localhost ~]# cd /data/ruby/
[root@localhost ruby]# tar xf ruby-2.5.5.tar.gz
[root@localhost ruby]# cd ruby-2.5.5/
[root@localhost ruby-2.5.5]#  ./configure && make –j 4 && make ×××tall 

6. Installation package rubygems

[root@localhost ruby-2.5.5]# yum ×××tall -y rubygems

7. create an environment variable paths are connected to flexible command and ruby ​​gem command

[root@localhost ruby-2.5.5]# ln -sv /data/ruby/ruby-2.5.5/bin/gem /usr/bin/
[root@localhost ruby-2.5.5]# ln -sv/data/ruby/ruby-2.5.5/ruby /usr/bin/

8.gem install redis module

    [root@localhost ruby-2.5.5]# gem ×××tall redis -y    

9. Modify cluster environment redis module login password

[root@localhost ~]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.2/lib/redis/client.rb
#frozen_string_literal: true

require_relative "errors"
require "socket"
require "cgi"

class Redis
    class Client

        DEFAULTS = {
            :url => lambda { ENV["REDIS_URL"] },
            :scheme => "redis",
            :host => "127.0.0.1",
            :port => 6379,
            :path => nil,
            :timeout => 5.0,
            :password => 123456, #登录密码改为123456
            :db => 0,
            :driver => nil,
             ……

10. Create a cluster environment using redis-trib.rb command automatically assigned from the main service

[root@localhost ~]# redis-trib.rb create --replicas 1 192.168.1.9:6379  192.168.1.9:6380  192.168.1.106:6379  
192.168.1.106:6380  192.168.1.110:6379  192.168.1.110:6380

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.9:6379
192.168.1.106:6379
192.168.1.110:6379
Adding replica 192.168.1.106:6380 to 192.168.1.9:6379
Adding replica 192.168.1.110:6380 to 192.168.1.106:6379
Adding replica 192.168.1.9:6380 to 192.168.1.110:6379
M: eed2e22136cbdca6770a46bbb2e137ab693dd16b 192.168.1.9:6379
     slots:0-5460 (5461 slots) master
#主服务:M
#主服务id:eed2e22136cbdca6770a46bbb2e137ab693dd16b
#主服务ip和端口:192.168.1.9:6379
#分得的槽位区间:0-5460
#一共分得的槽:5460

S: 8efbcf7fdd4675c77199a2a1206f0209ac2255f3 192.168.1.9:6380
     replicates e387a0ba7c95d0c27d7e28a9b57d23117711eadc
#从服务:S
#从服务id:8efbcf7fdd4675c77199a2a1206f0209ac2255f3
#从服务ip和端口:192.168.1.9:6380
#从服务所属的主服务id:e387a0ba7c95d0c27d7e28a9b57d23117711eadc

M: c922b4bf56f0086609fd4fb23d987df0a77bec22 192.168.1.106:6379
     slots:5461-10922 (5462 slots) master
S: a4fd89d79cdd27698bc394134b2df25b63ddb4c5 192.168.1.106:6380
     replicates eed2e22136cbdca6770a46bbb2e137ab693dd16b
M: e387a0ba7c95d0c27d7e28a9b57d23117711eadc 192.168.1.110:6379
     slots:10923-16383 (5461 slots) master
S: 34549b777963b16e65125def8d9a8e50e27ed2a4 192.168.1.110:6380
     replicates c922b4bf56f0086609fd4fb23d987df0a77bec22
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.9:6379)
M: eed2e22136cbdca6770a46bbb2e137ab693dd16b 192.168.1.9:6379
     slots:0-5460 (5461 slots) master
     1 additional replica(s)
S: a4fd89d79cdd27698bc394134b2df25b63ddb4c5 192.168.1.106:6380
     slots: (0 slots) slave
     replicates eed2e22136cbdca6770a46bbb2e137ab693dd16b
S: 8efbcf7fdd4675c77199a2a1206f0209ac2255f3 192.168.1.9:6380
     slots: (0 slots) slave
     replicates e387a0ba7c95d0c27d7e28a9b57d23117711eadc
M: c922b4bf56f0086609fd4fb23d987df0a77bec22 192.168.1.106:6379
     slots:5461-10922 (5462 slots) master
     1 additional replica(s)
M: e387a0ba7c95d0c27d7e28a9b57d23117711eadc 192.168.1.110:6379
     slots:10923-16383 (5461 slots) master
     1 additional replica(s)
S: 34549b777963b16e65125def8d9a8e50e27ed2a4 192.168.1.110:6380
     slots: (0 slots) slave
     replicates c922b4bf56f0086609fd4fb23d987df0a77bec22
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

11. Review the slave from the connection status of the service can be seen from the main connection has not been established

[root@localhost ~]# redis-cli -h 192.168.1.106 -p 6380
192.168.1.106:6380> auth 123456
OK
192.168.1.106:6380> info  replication
#Replication
role:slave
master_host:192.168.1.9
master_port:6379
master_link_status:down  #未与主服务建立连接
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1560643385
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:84865c623e15576c50c80a46ee16845b80b872d8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

11. salve from three main services are manually inputted from the connection authentication password

1) Enter the authentication password

192.168.1.106:6380> config set masterauth 123456
OK

2) Check salve service connection status again, you can see up to indicate successful connection from master

192.168.1.106:6380> info  replication
#Replication
role:slave
master_host:192.168.1.9
master_port:6379
master_link_status:up  #主从服务已建立连接
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:df9da60e308938e7a817ca08b20b58c248ad409d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0

12. Experimental summary

1) to achieve the three hosts set up three pairs from the master server cluster environment, the available redundancy and higher concurrency;

2) to allow one of the hosts down, the shoot down of a primary service will be replaced from its service, the service is promoted to a new main service, which does not affect the normal work of the other two host servers, data is not lost;

3) reduce the number of hosts cost savings.

Reproduced in: https: //blog.51cto.com/14234542/2409596

Guess you like

Origin blog.csdn.net/weixin_33734785/article/details/93034392