Using Redis-sentinel+consul to enable Redis high availability

Source: Internet
Author: User
Tags redis cluster egrep

In the previous article, "Using redis-sentinel+keepalived to achieve Redis high availability" describes in detail the use of redis-sentinel+keepalived to achieve redis high-availability scenarios. The Redis-sentinel scenario in this article is the same, and it also provides a Redis single-instance service that, when a Redis (master) service unexpectedly stops or the host on which the service resides has a downtime or network failure, Another Redis service will automatically become master from slave, providing Redis read-write services. Redis-sentinel configuration can refer to the previous article, this paper omitted, only discuss consul instead of keepalived.

The keepalived provides a VIP to provide access to upper-level applications, this article is to replace keepalived with Consul, using Consul's DNS interface to provide Redis (master) IP for upper-level applications.

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M01/7C/69/wKioL1bP_qWxg1v3AAD02U9fD1w015.png" height= "473" />

Prerequisites: You need an available consul cluster, you can register the service with the consul cluster itself and the configuration file, you can register with a separate host, and you can register with the consul using the HTTP API.

In order to obtain the Redis (master) IP instead of the Redis (Slave) IP, you can use a script to check the implementation, similar to the keepalived script, except that the return value is set to 1 to 2 because the return value of 0 in Consul is no problem (passed), 1 is a warning (warning), 2 is severe (critical).

The following is an example of registering with a separate host.

The JSON configuration file is as follows:

{   "Services": [    {       "id":  " Redisnode1 ",      " name ": " Redis ",      " tags ": [        " Master "      ],        "Address":  "192.168.1.241",       "Port":  6379,       "Checks": [         {           "Script":  "/usr/local/sbin/redis-cli  -h 192.168.1.241 -p 6379 info | grep role:master | |  exit 2 ",          " Interval ": " 5s "         }      ]    },     {       "id":  "redisnode2",       "name":  "Redis",        "tags": [         "master"        ],       "Address":  "192.168.1.242",        "Port": 6379,       "checks": [         {           "Script":  "/ Usr/local/sbin/redis-cli -h 192.168.1.242 -p 6379 info | grep role: master | |  exit 2 ",          " Interval ": " 5s "         }      ]    },     {       "id":  "redisnode3",        "name":   " Redis ",      " tags ": [        " Master "      ],      " Address ": " 192.168.1.243 ",      " Port ": 6379,      " Checks ": [        {            "Script":  "/usr/local/sbin/redis-cli -h 192.168.1.243 -p 6379  info | grep role:master | |  exit 2 ",          " Interval ": " 5s "         }      ]    }   ]}

Registration is registered using the Consul agent as a client, assuming that the address of one of the servers in the consul cluster is 192.168.1.245.

mkdir -p /usr/local/consul/data /usr/local/consul/config   cat >/usr/ local/consul/config/rediscluster.json<<eof{   "Services": [    {        "id":  "redisnode1",       "name":  "Redis" ,       "tags": [         "master"       ],       "Address":  "192.168.1.241",        "Port": 6379,       "checks":  [         {           " Script ": "/usr/local/sbin/redis-cli -h 192.168.1.241 -p 6379 info |  grep role:master | |  exit 2 ",          " Interval ": " 5s "        }      ]    },     {       "id":  "redisnode2",        "name":  "Redis",       "tags": [          "Master"       ],       " Address ": " 192.168.1.242 ",      " Port ": 6379,        "Checks": [        {            "Script":  "/usr/local/sbin/redis-cli -h 192.168.1.242 -p  6379 info | grep role:master | |  exit 2 ",          " Interval ": " 5s "         }      ]    },    {       "id":  "redisnode3",        "name":  "Redis",       "tags":  [          "Master"       ],        "Address":  "192.168.1.243",       "port":  6379,        "Checks": [        {            "Script":  "/usr/local/sbin/redis-cli -h  192.168.1.243 -p 6379 info | grep role:master | |  exit 2 ",          " Interval ": " 5s "         }      ]    }   ]}eof/bin/consul agent -advertise=$ (ifconfig $ (route -n | awk  '/^0.0.0.0/ && /ug/ {print  $NF} ')  | grep inet | egrep -v  "(inet6|127.0.0.1)"  | cut -d  ":"  -f2 | cut -d  " "  -f1)  -bind=$ (ifconfig $ (route -n |  awk  '/^0.0.0.0/ && /ug/ {print  $NF} ')  | grep inet |  egrep -v  "(inet6|127.0.0.1)"  | cut -d  ":"  -f2 | cut -d   " " &NBSP;-F1)  -data-dir=/usr/local/consul/data -config-dir=/usr/local/consul/config  -join 192.168.1.245

You can add Nohup thiscommandline >/path/to/logfile 2>&1 & when you need to run the background

Note: In the Consul JSON configuration file, each Consul agent's service ID can not be duplicated, name is mandatory, must have, if the ID is omitted, then the same name, the others are optional, dispensable. However, in order to be able to use a service information, you must have IP and port, of course, port can be specified in the application. Check is preferable, otherwise you cannot unregister from consul if there is a problem.

Note: The above command line uses the same network segment IP (public IP address) as the default gateway, which requires that this address and the consul cluster address can be accessed from each other. You can determine whether this condition is met by one of the following two lines of command before starting the Consul agent:

Consul info-rpc-addr=192.168.1.245:8400 Consul Members-rpc-addr=192.168.1.245:8400

After the consul agent is started, the Redis information is displayed in consul, including UI, DNS interface, and HTTP APIs. Applications that use Redis should point DNS to the DNS address of the consul cluster in order to query the IP corresponding to the Redis service.

The "Curl Http://192.168.1.245:8500/v1/catalog/service/redis" method provided in the Consul HTTP API can only list which nodes are in the current Service:redis. However, it is not possible to differentiate which nodes are working correctly and which are problematic, but the DNS interface method "Dig @192.168.1.245-p Redis.service.dc1.consul. Any "can accurately locate the IP of the available nodes as shown in.

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M02/7C/6A/wKiom1bP_jeyh1swAABIwWfH6CM314.png" height= "602" />

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M01/7C/6A/wKiom1bP_jjAuQ4ZAABAKMJYjFc500.png" height= "342" />

For example, test the Redis cluster with REDIS-CLI.

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M02/7C/6A/wKiom1bP_jmDVIxWAAAOQh3SBEs816.png" height= "86"/ >

This article only uses Consul's DNS interface to obtain the master IP of the Redis cluster, and does not get to the port, although it is possible to pass "Dig @192.168.1.245-p redis.service.consul SRV" Can get to the port, but not very good for the upper-level application, after all, most of the application's domain name resolution is not to query the SRV record. The consul of this article is Consul v0.6.0, which currently has an updated version 0.6.3 can be used, https://www.consul.io/downloads.html, Consul there may be other useful ways to better obtain the IP and port of the service, which will be supplemented later in the process of use. For more information on Consul, refer to Consul's official website: https://www.consul.io/.

Note: There may be a problem. If the Redis master fails, the original slave becomes the new Master,dns cache, which may not be refreshed, which should be taken care of to clear the DNS cache of the upper application in the event of a fault handling.

Tag:redis cluster, Redis high availability, Redis-sentinel,consul Api,redis master-slave replication

--end--

This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1745314

Using Redis-sentinel+consul to enable Redis high availability

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.