標籤:
1、安裝redis
首頁地址:http://redis.io/
:http://download.redis.io/ 下載最新的源碼包
tar -zxvf redis-stable.tar.gz -C /apps/product/cd /apps/product/redis-stable/make MALLOC=libcmake install
2、運行
載入設定檔並後台運行
redis-server /apps/product/redis-stable/redis.conf &
#redis-cli (命令列工具)
3、測試
# redis-benchmark --help 擷取協助資訊,其中包含壓力測試命令範例
實際測試舉例
[[email protected]~]# redis-benchmark -t set -c 20 -n 1000000 -r 100000000====== SET ====== 1000000 requests completed in 8.92 seconds 20 parallel clients 3 bytes payload keep alive: 1100.00% <= 0 milliseconds112095.06 requests per second
4、主從配置及測試:
主 |
gc-redis1 |
10.10.10.15 |
6379 |
從 |
gc-redis2 |
10.10.10.16 |
6379 |
從 |
gc-redis3 |
10.10.10.17 |
6379 |
#主伺服器(10.15)上做如下配置,其他預設即可:
[[email protected] redis-stable]# vim /apps/product/redis-stable/redis.confmasterauth "jrgcredispass"requirepass "jrgcredispass"
#從伺服器上(10.16,10.17)做如下配置,其他預設即可:
[[email protected] redis-stable]# vim /apps/product/redis-stable/redis.confslaveof 10.10.10.15 6379masterauth "jrgcredispass"requirepass "jrgcredispass"[[email protected] redis-stable]# vim /apps/product/redis-stable/redis.confslaveof 10.10.10.15 6379masterauth "jrgcredispass"requirepass "jrgcredispass"
#啟動主、從redis服務:
redis-server /apps/product/redis-stable/redis.conf &
#主伺服器顯示資訊:
[13997] 03 Nov 09:57:21.045 * Slave ask for synchronization[13997] 03 Nov 09:57:21.045 * Starting BGSAVE for SYNC[13997] 03 Nov 09:57:21.046 * Background saving started by pid 14002[14002] 03 Nov 09:57:21.058 * DB saved on disk[14002] 03 Nov 09:57:21.059 * RDB: 0 MB of memory used by copy-on-write[13997] 03 Nov 09:57:21.070 * Background saving terminated with success[13997] 03 Nov 09:57:21.070 * Synchronization with slave succeeded
#從伺服器顯示資訊:
[3496] 03 Nov 09:56:41.953 * Connecting to MASTER... [3496] 03 Nov 09:56:41.953 * MASTER <-> SLAVE sync started [3496] 03 Nov 09:56:41.954 * Non blocking connect for SYNC fired the event. [3496] 03 Nov 09:56:41.954 * Master replied to PING, replication can continue... [3496] 03 Nov 09:56:42.055 * MASTER <-> SLAVE sync: receiving 18 bytes from master [3496] 03 Nov 09:56:42.055 * MASTER <-> SLAVE sync: Loading DB in memory [3496] 03 Nov 09:56:42.055 * MASTER <-> SLAVE sync: Finished with success [3499] 03 Nov 09:56:42.056 * SYNC append only file rewrite performed [3499] 03 Nov 09:56:42.057 * AOF rewrite: 0 MB of memory used by copy-on-write [3496] 03 Nov 09:56:42.057 * Background append only file rewriting started by pid 3499 [3496] 03 Nov 09:56:42.154 * Background AOF rewrite terminated with success [3496] 03 Nov 09:56:42.154 * Parent diff successfully flushed to the rewritten AOF (0 bytes) [3496] 03 Nov 09:56:42.154 * Background AOF rewrite finished successfully
#主伺服器查看主從資訊
[[email protected] ~]# redis-cli -h 127.0.0.1 -a redispass info replication# Replicationrole:masterconnected_slaves:2slave0:ip=10.10.10.16,port=6379,state=online,offset=168812144,lag=1slave1:ip=10.10.10.17,port=6379,state=online,offset=168812144,lag=0master_repl_offset:168812281repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:167763706repl_backlog_histlen:1048576
#從伺服器查看主從資訊
[[email protected] ~]# redis-cli -h 127.0.0.1 -a redispass info replication# Replicationrole:slavemaster_host:10.10.10.15master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:168809239slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0
#寫同步測試
#主伺服器
[[email protected] ~]# redis-cli -h 127.0.0.1 -a jrgcredispass redis 127.0.0.1:6379> set a 123OKredis 127.0.0.1:6379> get a"123"
#從伺服器
[[email protected]~]# redis-cli -h 127.0.0.1 -a jrgcredispassredis 127.0.0.1:6379> get a "123"[[email protected]~]# redis-cli -h 127.0.0.1 -a jrgcredispassredis 127.0.0.1:6379> get a "123" redis 127.0.0.1:6379> set b 234 (error) READONLY You can‘t write against a read only slave. (開起了唯讀模式,所以從將不能寫入資料,可以保證資料只從主伺服器同步至從伺服器)
#故障測試#從伺服器宕機將自動從主伺服器的主從關係中解除#主伺服器宕機,從伺服器顯示,並且不能自動切換
[15932] 03 Nov 09:46:25.465 * Connecting to MASTER...[15932] 03 Nov 09:46:25.465 * MASTER <-> SLAVE sync started[15932] 03 Nov 09:46:25.466 # Error condition on socket for SYNC: Connection refused
5、Redis sentinel配置
首頁地址:http://redis.io/topics/sentinel
sentinel主要功能就是為Redis M-S(master,slaves)叢集提供了
1)master存活檢測
2)叢集中M-S服務監控
3) 自動容錯移轉,M-S角色轉換等能力,從一個方面說是提高了redis叢集的可用性.
#添加並編輯設定檔/etc/sentinel.conf,新增內容如下,也可在安裝檔案中負責sentinel設定檔並作適當修改:
[[email protected] ~]# vim /apps/product/redis-stable/sentinel.confport 26379sentinel monitor mymaster 10.10.10.15 6379 2sentinel failover-timeout mymaster 900000sentinel auth-pass mymaster jrgcredispasssentinel config-epoch mymaster 2#sentinel can-failover mymaster yessentinel leader-epoch mymaster 2-----------------------------------------------------------------------------------------------[[email protected] ~]# vim /apps/product/redis-stable/sentinel.confport 26380sentinel monitor mymaster 10.10.10.15 6379 2sentinel failover-timeout mymaster 900000sentinel auth-pass mymaster jrgcredispasssentinel config-epoch mymaster 2#sentinel can-failover mymaster yessentinel leader-epoch mymaster 2----------------------------------------------------------------------------------------------[[email protected] ~]# vim /apps/product/redis-stable/sentinel.confport 26381sentinel monitor mymaster 10.10.10.15 6379 2sentinel failover-timeout mymaster 900000sentinel auth-pass mymaster jrgcredispasssentinel config-epoch mymaster 2#sentinel can-failover mymaster yessentinel leader-epoch mymaster 2
#在三個伺服器中以sentinel模式啟動redis-server
啟動sentinel [[email protected] ~]#redis-server /apps/product/redis-stable/sentinel.conf --sentinel &[[email protected] ~]#redis-server /apps/product/redis-stable/sentinel.conf --sentinel &[[email protected] ~]#redis-server /apps/product/redis-stable/sentinel.conf --sentinel &
#三台伺服器sentinel輸出:(注意每個機器輸出對應的都是另外兩台機器的IP)
#關掉主gc-redis1的redis-server服務#將重新選舉主伺服器並重新設定串連到新的主伺服器:
#查看主從關係:(gc-redis2為主,gc-redis3為從,如果gc-redis1重新啟動,也將為從伺服器加入到新的叢集)
啟動redisredis-server /usr/local/redis-stable/redis.conf & 停止redisredis-cli -a jrgcredispass shutdown 啟動sentinelredis-server /usr/local/redis-stable/sentinel.conf --sentinel & 查看主備資訊:redis-cli -h 127.0.0.1 -p 6379 -a redispass info replication
.
參考文檔:http://mrcto.blog.51cto.com/1923168/1319542
Redis 安裝,主從配置及Sentinel配置自動Failover