Introduction to cache Database Redis---> Build master and slave and cluster

Source: Internet
Author: User
Tags redis cluster install redis

Master-Slave concept
    • A master can have multiple slave, a slave? can have multiple slave, so go on, form a strong? Multi-level server cluster architecture
    • Master used to write data, slave used to read data, statistics: the website read and write ratio is 10:1
    • Read/write separation via master/slave configuration

Master-Slave configuration configuration
    • To view the IP address of the current host: Ifconfig

    • Modify the etc/redis/redis.conf file: sudo vi redis.conf bind 192.168.26.128 (here the IP address is the native IP that was viewed in the previous step)
    • Restart Redis Services sudo service Redis stop redis-server Redis.con
Configuration from

Copy etc/redis/redis.conf file: sudo cp redis.conf./slave.conf

Modify redis/slave.conf file: sudo vi slave.conf

What to edit: Bind 192.168.26.128

Slaveof 192.168.26.128 6379

Port 6378

Start Redis service: sudo redis-server slave.conf

View Master-Slave Relationship: Redis-cli-h 192.168.26.128 info Replication

Data manipulation

in master and slave respectively, the "info" command, view output information into the main client: Redis-cli-h 192.168.26.128-p 6379

Go to client from: Redis-cli-h 192.168.26.128-p 6378

Write Data on master: set AA bb

Read data on slave: Get AA

Why do you have a cluster
    • Before we have talked about the concept of master and slave, a master can be more from, if the simultaneous traffic is too large (1000w), the main service will definitely hang up, data services hung up or a natural disaster occurred
    • Large companies will have a lot of servers (east China, Southern, central, north China, northwest, southwest, North East, Taiwan, Hong Kong and Macao Regional computer room)
The concept of clustering
    • A cluster is a set of independent computers interconnected by high-speed networks, which form a group and are managed in a single system mode. When a customer interacts with a cluster, the cluster is like a separate server. Cluster configuration is used to improve availability and scalability when requests arrive first handled by the Load Balancer server, forwarding requests to another server.
Redis Cluster
    • Classification
      • Software level
      • Hardware level
    • Software level: There is only one computer, which launches multiple Redis services on this computer.
    • Hardware level: Computers with multiple entities have a Redis or multiple Redis service on each computer.
Build a cluster
    • Currently has two hosts 172.16.179.130, 172.16.179.131, this? The IP is changed to the actual value when the
Reference reading
    • Redis Cluster Setup http://www.cnblogs.com/wuxl360/p/5920330.html
    • Python builds Redis cluster http://blog.5ibc.net/p/51020.html
Configuring the Machine 1
    • In the demo, 172.16.179.130 is the IP of the current Ubuntu machine
    • In 172.16.179.130? Desktop record, create CONF record
    • In conf, create a 7000.conf, edit the content as follows

      port 7000bind 172.16.179.130daemonize yespidfile 7000.pidcluster-enabled yescluster-config-file 7000_node.confcluster-node-timeout 15000appendonly yes
    • In conf, create a 7001.conf, edit the content as follows

      port 7001bind 172.16.179.130daemonize yespidfile 7001.pidcluster-enabled yescluster-config-file 7001_node.confcluster-node-timeout 15000appendonly yes
    • In conf, create a 7002.conf, edit the content as follows

      port 7002bind 172.16.179.130daemonize yespidfile 7002.pidcluster-enabled yescluster-config-file 7002_node.confcluster-node-timeout 15000appendonly yes
    • Summary: Three pieces of configuration differences in port, Pidfile, cluster-config-file three items

    • To configure the. piece to start the Redis service and view the process: Ps-ef|grep Redis

      redis-server 7000.confredis-server 7001.confredis-server 7002.conf

Configuring the Machine 2

    • In the demo, 172.16.179.131 is the IP of the current Ubuntu machine
    • In 172.16.179.131? Desktop record, create CONF record
    • In conf, create a 7003.conf, edit the content as follows

      port 7003bind 172.16.179.131daemonize yespidfile 7003.pidcluster-enabled yescluster-config-file 7003_node.confcluster-node-timeout 15000appendonly yes
    • In conf, create a 7004.conf, edit the content as follows

      port 7004bind 172.16.179.131daemonize yespidfile 7004.pidcluster-enabled yescluster-config-file 7004_node.confcluster-node-timeout 15000appendonly yes
    • In conf, create a 7005.conf, edit the content as follows

      port 7005bind 172.16.179.131daemonize yespidfile 7005.pidcluster-enabled yescluster-config-file 7005_node.confcluster-node-timeout 15000appendonly yes
    • Summary: Three pieces of configuration differences in port, Pidfile, cluster-config-file three items

    • To configure a piece to start the Redis service

      redis-server 7003.confredis-server 7004.confredis-server 7005.conf
Create a cluster
    • REDIS-TRIB.RB is included in the Redis installation package to create a cluster
    • The next operation on the 172.16.179.130 machine is motivated?
    • Copy the command so that it can be lowered in any recording? This command:

      sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/

Install the Ruby environment because REDIS-TRIB.RB is Ruby developed: sudo apt-get install ruby

At the tip of the message. Y, then go back to the installation

The following command creates a cluster:

  redis-trib.rb create --replicas 1 172.16.179.130:7000 172.16.179.130:7001 172.16.179.130:7002 172.16.179.131:7003 172.16.179.131:7004 172.16.179.131:7005
  
    • This command may be an error on some machines, mainly because the installed Ruby is not the latest version!

    • Celestial Anti-wall causes the latest version of the download, so you need to set the source of the gem

    • The solution is as follows:

      -- 先查看??的 gem 源是什么地址gem source -l -- 如果是https://rubygems.org/ 就需要更换-- 更换指令为gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/-- 通过 gem 安装 redis 的相关依赖sudo gem install redis-- 然后重新执?指令
redis-trib.rb create --replicas 1 172.16.179.130:7000 172.16.179.130:7001 172.16.179.130:7002 172.16.179.131:7003 172.16.179.131:7004 172.16.179.131:7005

Prompt the following master and slave information, after the return of the output yes? , prompt completion, cluster build success
Data validation
    • As can be seen, the current building of the main server is 7000, 7001, 7003, the corresponding slave server is 7004, 7005, 7002
    • Connect to the cluster on the 172.16.179.131 machine with 7002, plus-C

    • Redis-cli-h 172.16.179.131-c-P 7002
    • Write data: Set name Yinjiangchong
    • ? Jump to the 7003 server, and write? Data success
    • Data can be obtained at 7003 if the write data is redirected to 7000 (load balancer)

On which server to write data: CRC16
    • Redis cluster is designed to take into account the go to the middleware, that is, each node in the cluster is an equal relationship, are equivalent, each node holds the data and the state of the whole cluster. Each node is connected to all other nodes, and these connections remain active, which ensures that we only need to connect to any of the nodes in the cluster to get data to other nodes
    • The Redis cluster does not have and makes the traditional sex hash distribution data, which is the same as the hash slot (hash slot) type to allocate. Redis cluster is assigned 16,384 slots by default, and when we set a key, will it? The CRC16 algorithm takes the module to get the slot to which it belongs, and then points the key to the node of the hash slot interval, the algorithm is: CRC16 (key)% 16384. So when we see the set and get at the test, we jump directly to the 7000-terminal node.
    • The Redis cluster will present the data to a master node and then synchronize the data between this master and its corresponding salve. When the data is read, the data is also obtained from the corresponding master node based on the sexually-induced hash algorithm. Only when a master hangs up does it start? A corresponding salve node that acts as the master
    • It is important to note that 3 or more master nodes must be required, otherwise the cluster will fail when it is created, and when the number of surviving master nodes is at half the total number of nodes, the whole cluster will serve the law.

Redis clusters interacting with Python
    • The installation package is as follows

      Pip Install Redis-py-cluster

    • Redis-py-cluster Source Address Https://github.com/Grokzen/redis-py-cluster

    • Create a redis_cluster.py, sample code as follows

From RedisclusterImport *if __name__ = =' __main__ ':Try# Build all the nodes, Redis will make? CRC16 algorithm that writes keys and values to a node Startup_nodes = [{' Host ':' 192.168.26.128 ', ' Port ':  ' 7000 '}, { ' host ':  ' 192.168.26.130 ',  ' Port ':  ' 7003 '}, { ' host ':  ' 192.168.26.128 ',  ' Port ':  ' 7001 '},] # Build Strictrediscluster object Src=strictrediscluster (startup_ Nodes=startup_nodes,decode_responses=true) # set key to name, Data Result=src.set with value Itheima ( ' name ',  ' Itheima ') print (result) Span class= "Hljs-comment" ># gets the key for name name = Src.get ( ' name ') print (name) except Exception as e:print (E)       

  

  

Introduction to cache Database Redis---> Build master and slave and cluster

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.