将设为首页浏览此站
开启辅助访问 天气与日历 收藏本站联系我们切换到窄版

易陆发现论坛

 找回密码
 开始注册
查看: 1519|回复: 1
收起左侧

how to configure a Redis cluster on ubuntu14.04

[复制链接]
发表于 2016-9-7 15:18:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?开始注册

x
Introduction3 p; r4 O/ \$ k1 V5 E1 T5 F

5 k7 L- v2 S$ \5 bRedis is an open source key-value data store, using an in-memory storage model with optional disk writes for persistence. It features transactions, pub/sub, and automatic failover, among other functionality. It is recommended to use Redis with Linux for production environments, but the developers also mention OS X as a platform on which they develop and test. Redis has clients written in most languages, with recommended ones featured on their website.& h0 X7 \. \5 }( E2 _: q2 O/ `
4 p- ?4 a9 P  i, u
For production environments, replicating your data across at least two nodes is considered the best practice. Redundancy allows for recovery in case of environment failure, which is especially important when the user base of your application grows.
% c0 t% y9 `. w% Q* q. J' F
' Z8 M: o5 M( v% Z) D. GBy the end of this guide, we will have set up two Redis Droplets on DigitalOcean, as follows:
: U( I, R+ Y; q' L2 M
- ]  G9 m+ @+ eone Droplet for the Redis master server
$ x# b( `! K; B4 E4 Yone Droplet for the Redis slave server
. m) ~7 D$ {& z% ?( F/ P/ G0 sWe will also demonstrate how to switch to the slave server and set it up as a temporary master.& B* L6 E/ W6 P# U$ e
. V5 U, k0 N# w' V1 F2 ]) O
Feel free to set up more than one slave server.. q& }; R% E% A! o, a1 O* Q$ u

; C" A6 E' H$ ^- ]4 C9 VThis article focuses on setting up a master-slave Redis cluster; to learn more about Redis in general and its basic usage as a database, see this usage tutorial.
' j9 \0 T3 q- S! v4 e1 {7 s' e1 |) D5 t# H8 d3 p" o
Prerequisites
' r, ^& A$ \3 {! g. DWhile this may work on earlier releases and other Linux distributions, we recommend Ubuntu 14.04.  n$ X  P/ ]9 V. M( h8 F' @% a
5 v5 V3 v  P* ]% j8 K
For testing purposes, we will use small instances as there is no real workload to be handled, but production environments may require larger servers.
5 K% O% t/ t2 h: ~$ R7 F) ]- U0 N4 A- _" S! G1 I
Ubuntu 14.04 LTS
+ A$ F( w$ p( F0 S$ LTwo Droplets, of any size you need; one master and one or more slave(s)* b9 E( R; e) }
Access to your machines via SSH with a sudo non-root user as explained in Initial Server Setup with Ubuntu 14.04
! P1 B6 D( z/ u4 BStep 1 — Install Redis
4 v8 Q" L0 j# A6 D6 CStarting with the Droplet that will host our master server, our first step is to install Redis. First we need to add Chris Lea's Redis repository (as always, take extreme caution when adding third party repositories; we are using this one because its maintainer is a reputable figure):
5 r4 w4 U" l$ J1 p6 ~
6 X3 i2 _' S# {/ d8 w5 ]6 e; Q9 csudo add-apt-repository ppa:chris-lea/redis-server
' s( _# z! m; t1 n5 J  s+ h9 vPress ENTER to accept the repository.  M5 V1 F! o1 P( D2 y6 Y" T+ [

& f% r0 ]* k' {+ e: E* {Run the following command to update our packages:
% b1 R" E9 {& d1 ?  d
" u) A" S9 C9 rsudo apt-get update- H  @  G2 Y  a$ s8 C' E
Install the Redis server:
$ D1 [' H/ n6 R1 c! S! }4 D1 L4 P3 n, L2 [8 `3 k9 c( Z% q7 j- X4 s
sudo apt-get install redis-server
# apt-getinstall redis-server -y
Check that Redis is up and running:
" N3 r7 c' v0 Z$ z* ^+ U- E* w  f& Z# ^$ K$ e; e- m
redis-benchmark -q -n 1000 -c 10 -P 5+ P1 T- j  t8 V8 Y3 p& w& A
The above command is saying that we want redis-benchmark to run in quiet mode, with 1000 total requests, 10 parallel connections and pipeline 5 requests. For more information on running benchmarks for Redis, typing redis-benchmark --help in your terminal will print useful information with examples.& N1 [- u/ i: C' v: p1 u, R: i5 C

, j* t- y2 _- pLet the benchmark run. After it's finished, you should see output similar to the following:7 S( l* N8 v+ ~' B

$ O2 `. `4 f, }0 m" s* TOutput
+ _1 x5 i5 ^3 D4 w- Y1 |2 \8 YPING_INLINE: 166666.67 requests per second, w9 Y0 }+ P) r0 Q6 r  |: y- }
PING_BULK: 249999.98 requests per second" {6 f4 I0 l" `! |' r; u
SET: 249999.98 requests per second; \6 F* M/ D% m6 ]4 l
GET: 499999.97 requests per second3 s: K0 D% _7 ?* ?# i7 @
INCR: 333333.34 requests per second1 I3 W1 o+ B' a' a8 [; O
LPUSH: 499999.97 requests per second4 S+ ~2 l! ^" c- J; h
LPOP: 499999.97 requests per second/ s. X6 m& [* o6 Q* L- Z
SADD: 499999.97 requests per second
5 ~5 I4 q* D. f: ZSPOP: 499999.97 requests per second
  z! q7 o; J2 g+ TLPUSH (needed to benchmark LRANGE): 499999.97 requests per second
% n9 a, W3 v5 f/ HLRANGE_100 (first 100 elements): 111111.12 requests per second' v, L( k6 K. O& m
LRANGE_300 (first 300 elements): 27777.78 requests per second
- {$ D2 }4 P' y( c; F9 JLRANGE_500 (first 450 elements): 8333.33 requests per second8 O/ }4 k; I( w4 F. `0 W7 g. S+ Y
LRANGE_600 (first 600 elements): 6369.43 requests per second' C% T; s9 w8 X1 a$ p% t+ G
MSET (10 keys): 142857.14 requests per second0 k9 ?8 D* s8 h2 F3 B
Now repeat this section for the Redis slave server. If you are configuring more Droplets, you may set up as many slave servers as necessary.
  n& G4 _8 V: f; I8 X' S& z2 c1 t4 O/ K" T# C
At this point, Redis is installed and running on our two nodes. If the output of any node is not similar to what is shown above, repeat the setup process carefully and check that all prerequisites are met9 \1 `' q4 ^1 L* R0 W

9 L% P0 L* o6 Y) y! YStep 2 — Configure Redis Master
* I# o6 S: U: E6 nNow that Redis is up and running on our two-Droplet cluster, we have to edit their configuration files. As we will see, there are minor differences between configuring the master server and the slave.
" f4 K) E( q9 X3 b) C3 F
; K5 I! l/ Q& }, q) R& YLet's first start with our master.
0 _/ l, _7 @8 k
% ]/ i) M6 Y) s: UOpen /etc/redis/redis.conf with your favorite text editor:
9 n0 n4 W% \3 _- I) ^5 x2 `- s
. x3 y* [5 x1 D# n2 csudo nano /etc/redis/redis.conf
' i/ `* u6 P5 Q9 q, N( fEdit the following lines.& K3 q* A% i- `1 H
- |0 d' `  a3 b" B$ d2 p3 K
Set a sensible value to the keepalive timer for TCP:$ n7 d% K5 }, z: q

0 J$ q' B& D7 _2 b, |9 n6 [/etc/redis/redis.conf* k; L7 B; l$ A5 i5 x. v
tcp-keepalive 60
  [0 I0 \2 L: ~' r2 y. z5 [Make the server accessible to anyone on the web by commenting out this line:
. F2 v& @1 n4 Y  m1 {$ h1 e/ ^8 S! v7 V
/etc/redis/redis.conf* P% g3 S  U/ n* v
#bind 127.0.0.10 u& T3 V* m# U' s% V8 v6 ?. f$ y
Given the nature of Redis, and its very high speeds, an attacker may brute force the password without many issues. That is why we recommend uncommenting the requirepass line and adding a complex password (or a complex passphrase, preferably):
" ?$ m3 F4 p2 e# R) H1 ^& G, E2 l2 x6 A4 R+ A3 m6 L3 A1 ^
/etc/redis/redis.conf& X7 O& m- u5 Y  x: c( X& G, J6 g/ E
requirepass your_redis_master_password
2 b  [5 {% B2 p; _: h9 y" IDepending on your usage scenario, you may change the following line or not. For the purpose of this tutorial, we assume that no key deletion must occur. Uncomment this line and set it as follows:5 G& ]# s3 M7 F: U" p
) y' U. A: m- L1 a& w
/etc/redis/redis.conf+ @8 V2 w0 E: Y. a+ m
maxmemory-policy noeviction
; e2 F0 M# n$ `3 C/ IFinally, we want to make the following changes, required for backing up data. Uncomment and/or set these lines as shown:
, z5 o/ z# m; e- o% S2 A5 J
+ K/ u) Z+ l0 ]% B  n/etc/redis/redis.conf
+ m1 c/ u8 v& p' R( z! y/ oappendonly yes
7 i0 l2 O6 ?9 U' n3 f; ?appendfilename redis-staging-ao.aof
  e  Z9 [3 E; pSave your changes.
% |7 k, i5 a) ~* {* Z" @. ^- ~/ D9 K6 o- @, @/ s6 _
Restart the Redis service to reload our configuration changes:- {2 ~' {# p0 h! v! V6 b! W0 E0 L3 J

7 k% ~/ e6 M2 N2 f5 Asudo service redis-server restart
$ ?$ n% \1 N, o* q  i1 vIf you want to go the extra mile, you can add some unique content to the master database by following the Redis Operations sections in this tutorial, so we can later see how it gets replicated to the slave server.; o# @8 Y0 O% I, Q
* x2 Z. @6 l) I0 Z5 {" V4 ~
Now that we have the master server ready, let's move on to our slave machine.4 S5 z, z; j2 K1 p2 }# N

# D, @5 W# l" e; R9 m/ ^Step 3 — Configure Redis Slave; g1 M5 [% _1 y" H. @
We need to make some changes that allow our slave server to connect to our master instance:
& C+ B. d6 |2 _+ T/ J. }3 P
; O7 ^" `8 F% W+ H: wOpen /etc/redis/redis.conf with your favorite text editor:
! B2 n; F7 f1 b" o/ w, Y+ K2 a& z* g/ s! K% h
sudo nano /etc/redis/redis.conf
  H( H4 ^! s6 i3 F" j& D$ xEdit the following lines; some settings will be similar to the master's.3 H, X8 h( K. ]
. S# R" `7 i4 S  X- X
Make the server accessible to anyone on the web by commenting out this line:2 H( T- d& a9 @. O5 j

+ `7 r- s, G7 m/etc/redis/redis.conf
4 X% k' c- v6 L0 k#bind 127.0.0.1
% Y& u* c* z. VThe slave server needs a password as well so we can give it commands (such as INFO). Uncomment this line and set a server password:
4 A6 a" f. m4 L( G9 M2 E) x1 j
; i4 u# x8 Y/ u. m/etc/redis/redis.conf4 x5 T3 Z. R' \+ Y
requirepass your_redis_slave_password6 m" ^  U" v& w0 n! R" U# [
Uncomment this line and indicate the IP address where the master server can be reached, followed by the port set on that machine. By default, the port is 6379:% c1 r1 j8 M! D
1 K3 ~. z8 n4 T& T( K
/etc/redis/redis.conf0 c  r# n& n. n; X% m, E. O
slaveof your_redis_master_ip 6379/ T* }# m" T7 \) b4 J3 _
Uncomment the masterauth line and provide the password/passphrase you set up earlier on the master server:& Z+ z  c% j8 i) p. N
$ y/ |$ K8 g0 r6 y( P
/etc/redis/redis.conf
2 p+ X: {" n; e  L8 g" C$ Bmasterauth your_redis_master_password# p4 P; a1 C9 t0 G, I* i6 c# z8 g
Now save these changes, and exit the file. Next, restart the service like we did on our master server:: f* T. ~) U; J% G" @$ z* n
: ~/ y  P- O* Q6 y' P
sudo service redis-server restart
2 r3 C1 ~; u0 W8 zThis will reinitialize Redis and load our modified files.
: {, M' \3 y( j& n; S7 Z
/ {. h6 n# [  X* S9 g2 HConnect to Redis:' o. R. m! @  l) o2 v3 e
: ^$ C' f* M. r
redis-cli -h 127.0.0.1 -p 6379 5 M) h2 c) m: o9 I3 Z) v  E
Authorize with the slave server's password:) t. X+ K3 \* G) s
: _% Z& ]- J" t, \& I8 R0 {
AUTH your_redis_slave_password4 s# y9 z$ }2 B: S( n
At this point we are running a functional master-slave Redis cluster, with both machines properly configured.
+ b+ S  T* U8 w+ G' }6 n  E; T8 G) Q# a7 {
Step 4 — Verify the Master-Slave Replication
' w' A# @! k, e) W- STesting our setup will allow us to better understand the behavior of our Redis Droplets, once we want to start scripting failover behavior. What we want to do now is make sure that our configuration is working correctly, and our master is talking with the slave Redis instances.7 s2 ]! R/ n9 ]- p
0 K# c" t! `7 }- M1 j$ p
First, we connect to Redis via our terminal, on the master server:
3 q# ]; _" g% v* I0 ]. |* Y9 p0 Q2 r) Y$ v/ ?2 g
First connect to the local instance, running by default on port 6379. In case you've changed the port, modify the command accordingly.
2 N: H( {! r' ?+ K6 I2 A- U8 {0 Z: [1 x- M) S) G- ^
redis-cli -h 127.0.0.1 -p 63794 a+ C/ F& i6 H1 [( }5 R( a
Now authenticate with Redis with the password you set when configuring the master:
  {! L' v8 l3 ?/ K
, p$ @* J' S" N! k& {7 JAUTH your_redis_master_password
- Y- M4 f+ N6 g! j: Q% CAnd you should get an OK as a response. Now, you only have to run:
1 P* C: A. v+ I
: [8 n6 d" [/ U' o. IINFO4 p4 B5 ?1 E: w6 m
You will see everything you need to know about the master Redis server. We are especially interested in the #Replication section, which should look like the following output:
6 E3 ^9 g& |$ `( r: a' m* X+ C- _) B. q: L% o7 u5 Q' C
Output
+ G- ]$ w9 X- w& ?/ y8 N. . .' y# `5 h* X& s. h

6 \: U5 @: X/ @3 Q" D# Replication; {2 d3 ?/ ?) G! [% p# J* B
role:master
1 J! X$ a1 F' p3 d5 n: Wconnected_slaves:15 s: I$ {7 y& c$ \9 |$ M2 O& M
slave0:ip=111.111.111.222,port=6379,state=online,offset=407,lag=1! T/ q2 h4 X4 }  C* \/ R+ {: O- E( T
master_repl_offset:407
* ?. Q% K; Q8 e3 [4 Arepl_backlog_active:1- R/ [- N( R$ e+ f
repl_backlog_size:1048576
2 X/ u' q9 s% V) \- grepl_backlog_first_byte_offset:2! m/ J3 A! p! C5 G+ `- L+ T% C
repl_backlog_histlen:406' |3 h) v: x/ _& s3 _' ^

3 v8 C# \3 @. B# m+ Y; H. . .
: X: a# L; J& I. E$ GNotice the connected_slaves:1 line, which indicates our other instance is talking with the master Droplet. You can also see that we get the slave IP address, along with port, state, and other info.( S: f, m6 u6 P

. t2 L# H; M3 U0 O( r9 h! ALet's now take a look at the #Replication section on our slave machine. The process is the same as for our master server. Log in to the Redis instance, issue the INFO command, and view the output:3 L. a( j' x" o8 u7 D0 _% x

+ N1 V  n7 ^6 kOutput+ k* j4 k2 E# I9 Z7 D) T
. . .; ^5 d$ O9 j3 m$ h% [

- m, i9 T  U0 t# G4 T0 ^, \0 S# Replication' z3 O; v# C/ R4 n# [
role:slave
' L% ]7 |  ~6 \- A( q9 M/ Kmaster_host:111.111.111.111! K# L# @2 P1 M' t2 X
master_port:63799 H' S" b0 b- C. y
master_link_status:up( {5 a* K) s) U0 E6 r2 ?; |
master_last_io_seconds_ago:3
) M# r. S- [( [( F  U( t/ \9 u$ jmaster_sync_in_progress:0
  p; h  O. ^2 K4 ~' w( }slave_repl_offset:1401
  f7 O; k8 S6 \0 [* x: b  Qslave_priority:100. ?- H3 K( V0 }: M
slave_read_only:1
: c! p* |! r6 |& c- @( Wconnected_slaves:0
2 y8 S7 B# R( |0 g$ i3 G/ Hmaster_repl_offset:0
' [: g8 |& I  zrepl_backlog_active:0
7 s9 R& S* x1 U. G' |repl_backlog_size:1048576. ^6 E9 ?- @% b  r" g
repl_backlog_first_byte_offset:0* d. B2 u: [5 J9 U/ R% f
repl_backlog_histlen:02 d* T  }0 Q. D

- \. e0 b: P- k" J% i. . ./ p) |+ p4 c7 @
We can see that this machine has the role of slave, is communicating with the master Redis server, and has no slaves of its own.5 ?3 O! D0 x' e6 Z' S8 o
$ X" ]2 Z, F' X  b  i% l( b
Step 5 — Switch to the Slave
" {5 j( n3 e; OBuilding this architecture means that we also want failures to be handled in such a way that we ensure data integrity and as little downtime as possible for our application. Any slave can be promoted to be a master. First, let's test switching manually.
' v: k0 V( `- S
8 i1 P6 U- Z6 v. g1 V0 ?$ }On a slave machine, we should connect to the Redis instance:, j- H3 n. g9 o! C. e4 k! }/ M

$ V; U5 L" y$ ^redis-cli -h 127.0.0.1 -p 6379
, w, ^+ S6 _: o' G- s% b  E# xNow authenticate with Redis with the password you set when configuring the slave0 a( u! n$ }4 R6 O% ?# m+ k' ^) {
% w9 T* ]/ x# p) @# D, c3 x8 p
AUTH your_redis_slave_password
' \( Y' J# v, u# QTurn off slave behavior:7 z% m, s8 G( Y, p/ [

( N" }: v& n! J. p4 ~1 G3 _SLAVEOF NO ONE) \0 S; Q! C8 A) P6 r/ A
The response should be OK. Now type:# `+ h. C4 }9 C& q

, L6 z" ~, g; Q) P6 S# z5 W3 P8 |/ _INFO
- T& E. O: m) r% ^9 p! ELook for the # Replication section to find the following output:
" @% `; M' `& t- t8 v
4 [3 g! Q  q3 X4 JOutput3 F7 H* ]* a0 F5 V9 J0 M2 b
. . .4 A, U. T$ \5 Q& J

% }* x( y, f& \2 h# Replication
) f9 ^, G9 C8 m, Z6 \* lrole:master+ H9 d, b& m' I1 M
connected_slaves:0
( f, b! t% `% e% ?6 Qmaster_repl_offset:1737  e5 \: e4 O9 P1 J2 r* L
repl_backlog_active:05 f" u1 |$ b, F3 D
repl_backlog_size:1048576
: h7 J$ ?& q9 b, U& Brepl_backlog_first_byte_offset:0
" y0 K  U7 ]. Lrepl_backlog_histlen:0
1 }; n3 k7 T, _% F9 F% H! U6 _1 C( F' f
. . .. t) b# b0 e. y+ v
As we expected, the slave has turned into a master, and is now ready to accept connections from other machines (if any). We can use it as a temporary backup while we debug our main master server.6 b; O4 b3 j6 c2 A9 v' `3 E
0 O) T4 [, E& G% s; Z4 Q
If you have multiple slaves that depended on the initial master, they all have to be pointed towards the newly promoted master.4 O  L- [- C4 z+ _2 Y- x

6 c- y4 l2 X; Y5 Y5 _- ]This can be scripted easily, with the following steps needing to be implemented once a failure is detected:
  D, p: p$ r7 p3 J0 a. E8 d/ f
& s$ J; _) ^1 ~4 j; NFrom the application, send all requests for Redis to a slave machine
( K3 R9 H. a% t; q+ K! i6 DOn that slave, execute the SLAVEOF NO ONE command. Starting with Redis version 1.0.0, this command tells the slave to stop replicating data, and start acting as a master server
/ }8 L% ^, G! @$ R. r4 f  `6 zOn all remaining slaves (if any), running SLAVEOF hostname port will instruct them to stop replicating from the old master, discard the now deprecated data completely, and start replicating from the new master. Make sure to replace hostname and port with the correct values, from your newly promoted master+ q6 y; ^! X& G7 ]! r7 n+ J
After analyzing the issue, you may return to having your initial server as master, if your particular setup requires it
! _) Y8 x2 P4 r- vThere are many ways of accomplishing the steps explained above. However, it is up to you to implement an adequate solution for your environment, and make sure to test it thoroughly before any actual failures occur.
; I$ I2 V) V' ~6 t$ t% H
9 ~0 X7 y( A) D( B" o& ]- eStep 6 — Reconnect to the Master$ f5 Q9 [1 _4 m( P$ y- E) h
Let's reconnect to the original master server. On the slave server, log in to Redis and execute the following:0 t7 |' R: Z2 s

" I( C6 v  r5 F# g$ l$ ASLAVEOF your_redis_master_ip 6379
$ P$ K) K; |" }5 s0 sIf you run the INFO command again, you should see we have returned to the original setup./ L) w, ?$ |; u
/ R1 R& X1 `$ S
Conclusion
* w! o5 }) a, t) EWe have properly set up an enviroment consisting of two servers, one acting as Redis master, and the other replicating data as a slave. This way, if the master server ever goes offline or loses our data, we know how to switch to one of our slaves for recovery until the issue is taken care of.2 h$ E, Q/ z& W- t2 `

# `$ D4 k2 Y! s8 N" B! U4 W! K" cNext steps might include scripting the automated failover procedure, or ensuring secure communications between all your Droplets by the use of VPN solutions such as OpenVPN or Tinc. Also, testing procedures and scripts are vital for validating your configurations.
1 d' [% O& m- l9 Q  _6 \% r
6 Y1 |$ z9 |2 x( A6 v' XAdditionally, you should take precautions when deploying this kind of setup in production environments. The Redis Documentation page should be studied and you must have a clear understanding of what security model is adequate for your application. We often use Redis as a session store, and the information it contains can be valuable to an attacker. Common practice is to have these machines accessible only via private network, and place them behind multiple layers of security.
. `' _  y# E! T% R1 E
$ u$ \( d6 }* l/ C  [This is a simple starting point on which your data store may be built; by no means an exhaustive guide on setting up Redis to use master-slave architecture. If there is anything that you consider this guide should cover, please leave comments below. For more information and help on this topic, the DigitalOcean Q&A is a good place to start.& H( v/ Q; _; k& H  P' e  X
 楼主| 发表于 2016-10-11 11:41:39 | 显示全部楼层
other place didn't change  ,only list changes
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

如有购买积分卡请联系497906712

QQ|返回首页|Archiver|手机版|小黑屋|易陆发现 点击这里给我发消息

GMT+8, 2020-6-1 06:25 , Processed in 0.236746 second(s), 22 queries .

Powered by 龙睿 bbs168x X3.2

© 2001-2020 Comsenz Inc.

快速回复 返回顶部 返回列表