Non relational database - redis cluster

redis cluster, experimental environment: two servers, six nodes, three network cards for each server

Six nodes, three masters, three replicas

Install redis on the primary server

[root@localhost ~]# yum install gcc gcc-c++ make -y  #Install environment package
[root@localhost ~]# mkdir /abc
[root@localhost ~]# Mount.cifs / / 192.168.100.25/redis/abc/mount
Password for root@//192.168.100.25/redis:  
[root@localhost ~]# cd /abc/
[root@localhost abc]# ls
redis-5.0.7.tar.gz
[root@localhost abc]# tar zxvf redis-5.0.7.tar.gz -C /opt/ #Unzip to opt
[root@localhost abc]# cd /opt/redis-5.0.7/
make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install #install
[root@localhost redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/ #Make a soft connection
[root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server

Slave server (the same operation is not repeated)

Primary server configuration redis

The content of the original configuration file with "ා" is convenient for writing shell scripts

[root@localhost utils]# vim /etc/redis/6379.conf 
  #70 bind 127.0.0.1
  70 #bind 127.0.0.1
  #89 protected-mode yes
  89 protected-mode no #Turn off the protection function
#833 # cluster-enabled yes
833  cluster-enabled yes  #Turn on Clustering
#841 # cluster-config-file nodes-6379.conf
841  cluster-config-file nodes-6379.conf #Open cluster feature profile
#847 # cluster-node-timeout 15000
847  cluster-node-timeout 15000 #Set the cluster timeout to 15000 seconds
 #700 appendonly no
 700 appendonly yes #Enable AOF support
[root@localhost redis]# /etc/init.d/redis_6379 restart #Start redis
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
[root@localhost utils]# cd /var/lib/redis/
[root@localhost redis]# cd 6379/
[root@localhost 6379]# ls
appendonly.aof  dump.rdb  nodes-6379.conf
#aof profile, rdb snapshot file, node profile

Same operation from server

Primary server installation rvm, ruby control cluster

gpg --keyserver hkp://Keys.gnupg.net -- recv keys 409b6b1796c275462a1703113804bb82d39dc0e3 ා import key
[root@localhost 6379]# curl -sSL https://get.rvm.io | bash -s stable ා find this address to install RVM

[root@localhost 6379]# cd /opt/
[root@localhost opt]# ls
redis-5.0.7  rh
[root@localhost opt]# vim abc.sh #rvm has a long script
[root@localhost opt]# ls
abc.sh  redis-5.0.7  rh
[root@localhost opt]# chmod +x abc.sh 
[root@localhost opt]# ls
abc.sh  redis-5.0.7  rh
[root@localhost opt]# source /etc/profile.d/rvm.sh 
[root@localhost opt]# rvm list known #View the version of the rvm installation

[root@localhost opt]# rvm install 2.4.1 #Install rvm2.4.1

[root@localhost opt]# rvm use 2.4.1 #Using rvm
Using /usr/local/rvm/gems/ruby-2.4.1
[root@localhost opt]# ruby -v #View the version of ruby
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[root@localhost opt]# gem install redis #Install redis

Install three network cards on the main server
192.168.136.229
192.168.136.232
192.168.136.231
Install three network cards from the server
192.168.136.185
192.168.136.233
192.168.136.234

Both servers restart network services

[root@localhost opt]# systemctl restart network

Two servers turn off firewalls and enhancements

[root@localhost opt]# systemctl stop firewalld.service
[root@localhost opt]# setenforce 0

Primary server, restart redis, create cluster

[root @ localhost 6379] ා / etc / init.d/redis_restart ා restart redis
redis-cli --cluster create 192.168.136.229:6379 192.168.136.232:6379 192.168.136.231:6379 192.168.136.185:6379 192.168.136.233:6379 192.168.136.234:6379 --cluster-replicas 1

Can I set the above configuration? (type 'yes' to accept): yes
 Three node master and three replica nodes correspond to each other randomly, but they are one master with one slave.

Start validation

[root@localhost opt]# redis-cli -h 192.168.136.229 -p 6379 #Logging in to a primary server 229
192.168.136.229:6379> set name zhangsan #Create key value pair
OK
192.168.136.229:6379> keys * #View all keys
1) "name"
192.168.136.229:6379> get name  #View values
"zhangsan"
192.168.136.229:6379> quit
[root@localhost opt]# redis-cli -h 192.168.136.234 -p 6379 #Log in to a slave server 234
192.168.136.234:6379> keys *
1) "name"
192.168.136.234:6379> get name #If you cannot view the data, you will be prompted to store the data in 231 server
(error) MOVED 5798 192.168.136.231:6379
[root@localhost opt]# redis-cli -h 192.168.136.231 -p 6379 #Log in to 231 server and you will see the data
192.168.136.231:6379> keys *
1) "name"
192.168.136.231:6379> get name
"zhangsan"

Establish key value pairs in hash mode, and set key deletion time

[root@master opt]# redis-cli -h 192.168.136.229 -p 6379 
192.168.136.229:6379> hset person age 20   ##Establishing key value pairs in hash mode
(integer) 1
192.168.136.229:6379> hset person name lisi
(integer) 1
192.168.136.229:6379> keys *
1) "person"
192.168.136.229:6379> hget person age   ##Get the value of the key
"20"
192.168.136.229.128:6379> expire person 5     ##Delete time of setting key 5s
(integer) 1
192.168.136.229:6379> keys *
1) "person"
192.168.136.229:6379> keys *
(empty list or set)

That's all we have. Thank you for watching

Tags: Redis network Ruby vim

Posted on Tue, 17 Dec 2019 14:54:45 -0800 by nvidia