1、redis 主從複製安裝配置node
一、主從複製介紹redis
主從複製,當用戶往Master端寫入數據時,經過Redis Sync機制將數據文件發送至Slave,Slave也會執行相同的操做確保數據一致;且實現Redis的主從複製很是簡單,可是redis的主從複製是異步的。vim
二、redis主從複製特色緩存
同一個Master能夠擁有多個Slaves。bash
Master下的Slave還能夠接受同一架構中其它slave的連接與同步請求,實現數據的級聯複製,即Master->Slave->Slave模式;服務器
Master以非阻塞的方式同步數據至slave,這將意味着Master會繼續處理一個或多個slave的讀寫請求;cookie
Slave端同步數據也能夠修改成非阻塞式的方式,當slave在執行新的同步時,它仍能夠用舊的數據信息來提供查詢;不然,當slave與master失去聯繫時,slave會返回一個錯誤給客戶端;架構
主從複製具備可擴展性,即多個slave專門提供只讀查詢與數據的冗餘,Master端專門提供寫操做;app
經過配置禁用Master數據持久化機制,將其數據持久化操做交給Slaves完成,避免在Master中要有獨立的進程來完成此操做。less
三、主從複製原理
當啓動一個Slave進程後,它會向Master發送一個SYNC Command,請求同步鏈接。不管是第一次鏈接仍是從新鏈接,Master都會啓動一個後臺進程,將數據快照保存到數據文件中,
同時Master會記錄全部修改數據的命令並緩存在數據文件中。後臺進程完成緩存操做後,Master就發送數據文件給Slave,Slave端將數據文件保存到硬盤上,而後將其在加載到
內存中,接着Master就會將全部修改數據的操做,將其發送給Slave端。若Slave出現故障致使宕機,恢復正常後會自動從新鏈接,Master收到Slave的鏈接後,將其完整的數據
文件發送給Slave,若是Mater同時收到多個Slave發來的同步請求,Master只會在後臺啓動一個進程保存數據文件,而後將其發送給全部的Slave,確保Slave正常。
四、主從架構介紹
Centos 6.6 x86_64 redis 3.0.7 master IP: 10.0.18.145 redis port 6379 slave1 IP: 10.0.18.146 redis port 6379
配置主從以前先編譯安裝redis,優化內核參數,參數以下:
#vi /etc/sysctl.conf #添加以下到文件末尾 vm.overcommit_memory = 1 net.core.somaxconn = 511 vm.swappiness = 0 net.ipv4.neigh.default.gc_stale_time=120 net.ipv4.conf.all.rp_filter=0 net.ipv4.conf.default.rp_filter=0 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.all.arp_announce=2 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_syn_backlog = 1024 net.ipv4.tcp_synack_retries = 2 net.ipv4.conf.lo.arp_announce=2 ##修改如下參數爲never #echo never > /sys/kernel/mm/transparent_hugepage/enabled 修改打開最大文件數 #vim /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 * soft nproc 10240 * hard nproc 10240
五、開始配置主從
爲master端提供redis配置文件:
#cp /root/redis-3.0.7/redis.conf /usr/local/redis/conf/ #cat redis.conf | egrep -v "^#|^$" #註釋都是修改的,其餘默認,若是有其餘需求能夠根據狀況修改 daemonize yes #以daemon後臺模式運行 pidfile "/var/run/redis.pid" port 6379 tcp-backlog 511 bind 10.0.18.145 #綁定master的地址 timeout 0 tcp-keepalive 0 loglevel notice logfile "/usr/local/redis/log/redis.log" #redis日誌路徑 databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error no #修改成no,若是對數據要求比較高,不容許服務器出任何錯誤,保持默認yes。 rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/usr/local/redis" #rdb文件存放目錄 masterauth "abcd123" #master驗證密碼 slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 80 #slave優先級別,數字越小優先級別越高,當master宕機時優先選取slave,若是單純是主從,能夠不配置此項 #我這裏配置了,是爲了後面配置redisHA作準備! requirepass "abcd123" #redis的認證密碼,若是設置進入redis必須經過密碼認證才能夠操做 appendonly yes #改成yes,aof持久化,增量寫disk,比aof慢,但更可靠.企業的默認選擇,有的會二者均開啓 appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
爲slave1提供redis配置文件:
#cp /root/redis-3.0.7/redis.conf /usr/local/redis/conf/ #cat redis.conf #slave1的配置文件參數其餘都和master端的同樣,只有下面幾項須要修改: bind 10.0.18.146 #綁定slave1的地址 slaveof 10.0.18.146 6379 #此選項是主從配置的關鍵,指向master的ip和redis端口 slave-priority 90 #slave1服務器的優先級 而後啓動主從端的redis服務! #nohup /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf & #在後臺運行 補充:這裏之因此從redis源碼目錄中拷貝redis.conf文件,而不直接建redis.conf而且寫入參數是由於遇到了一個坑,後面會描述詳情!
六、日誌查看
在master上查看日誌,以下:
#tail -f redis.log …………………… 10370:M 12 Oct 11:02:25.148 # Server started, Redis version 3.0.7 10370:M 12 Oct 11:02:25.148 * DB loaded from append only file: 0.000 seconds 10370:M 12 Oct 11:02:25.148 * The server is now ready to accept connections on port 6379 10370:M 12 Oct 11:02:45.243 * Slave 10.0.18.146:6379 asks for synchronization 10370:M 12 Oct 11:02:45.243 * Full resync requested by slave 10.0.18.146:6379 10370:M 12 Oct 11:02:45.243 * Starting BGSAVE for SYNC with target: disk 10370:M 12 Oct 11:02:45.244 * Background saving started by pid 10384 10384:C 12 Oct 11:02:45.254 * DB saved on disk 10384:C 12 Oct 11:02:45.255 * RDB: 0 MB of memory used by copy-on-write 10370:M 12 Oct 11:02:45.303 * Background saving terminated with success 10370:M 12 Oct 11:02:45.303 * Synchronization with slave 10.0.18.146:6379 succeeded 在slave 上查看日誌,以下: #tail -f redis.log ……………… 20825:S 12 Oct 11:02:44.278 * DB loaded from append only file: 0.000 seconds 20825:S 12 Oct 11:02:44.278 * The server is now ready to accept connections on port 6379 20825:S 12 Oct 11:02:45.278 * Connecting to MASTER 10.0.18.145:6379 20825:S 12 Oct 11:02:45.278 * MASTER <-> SLAVE sync started 20825:S 12 Oct 11:02:45.279 * Non blocking connect for SYNC fired the event. 20825:S 12 Oct 11:02:45.280 * Master replied to PING, replication can continue... 20825:S 12 Oct 11:02:45.282 * Partial resynchronization not possible (no cached master) 20825:S 12 Oct 11:02:45.284 * Full resync from master: 80508d50892115ab7d158c643c6ee68cb9282c76:1 20825:S 12 Oct 11:02:45.343 * MASTER <-> SLAVE sync: receiving 46 bytes from master 20825:S 12 Oct 11:02:45.344 * MASTER <-> SLAVE sync: Flushing old data 20825:S 12 Oct 11:02:45.344 * MASTER <-> SLAVE sync: Loading DB in memory 20825:S 12 Oct 11:02:45.344 * MASTER <-> SLAVE sync: Finished with success 20825:S 12 Oct 11:02:45.345 * Background append only file rewriting started by pid 20828 20825:S 12 Oct 11:02:45.370 * AOF rewrite child asks to stop sending diffs. 20828:C 12 Oct 11:02:45.370 * Parent agreed to stop sending diffs. Finalizing AOF... 20828:C 12 Oct 11:02:45.370 * Concatenating 0.00 MB of AOF diff received from parent. 20828:C 12 Oct 11:02:45.371 * SYNC append only file rewrite performed 20828:C 12 Oct 11:02:45.371 * AOF rewrite: 0 MB of memory used by copy-on-write 20825:S 12 Oct 11:02:45.379 * Background AOF rewrite terminated with success 20825:S 12 Oct 11:02:45.379 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB) 20825:S 12 Oct 11:02:45.379 * Background AOF rewrite finished successfully 若是均可以正常啓動,測試數據能夠複製,表示主從複製就配置OK了! 注意:進入redis以後,須要認證後才能進去操做 #./redis-cli -h 10.0.18.145 10.0.18.145:6379> info NOAUTH Authentication required. 10.0.18.145:6379> auth abcd123 #輸入密碼 ok
七、查看複製信息
在master查看:
#./redis-cli -h 10.0.18.145 10.0.18.145:6379> auth abcd123 OK 10.0.18.145:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=10.0.18.146,port=6379,state=online,offset=14967,lag=0 master_repl_offset:14967 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:742
在slave查看
#./redis-cli -h 10.0.18.146 10.0.18.146:6379> auth abcd123 OK 10.0.18.146:6379> info replication # Replication role:slave master_host:10.0.18.145 master_port:6379 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_repl_offset:14967 slave_priority:90 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
注意:若是主從複製正常運行master_repl_offset:14967和slave_repl_offset:14967值是相同的,master_last_io_seconds_ago:的值小於等於10秒!
主從複製配置好以後,寫操做是在master實現的,而後複製到slave服務器,因此是不能再slave上進行寫操做的,或報錯,以下:
10.0.18.146:6379> set class num1 (error) READONLY You can't write against a read only slave.
若是從服務器宕機, 將自動從主服務器的主從關係中解除;可是若是是主服務器宕機了,從服務器沒法工做也不能自動切換!!!
八、主從複製的健康檢查
Slave按照repl-ping-slave-period的間隔(默認10秒),向Master發送ping。
若是主從間的連接中斷後,再次鏈接的時候,2.8之前按照full sync再同期。2.8之後,由於有backlog的設定,backlog存在master的內存裏,從新鏈接以前,若是redis沒有重啓,而且offset在backlog保存的範圍內,能夠實現從斷開地方同期,不符合這個條件,仍是full sync。
用monitor命令,能夠看到slave在發送ping
10.0.18.146:6379> monitor OK 1476252338.841551 [0 10.0.18.145:6379] "PING" 1476252348.871064 [0 10.0.18.145:6379] "PING" 1476252358.896442 [0 10.0.18.145:6379] "PING" 1476252368.923831 [0 10.0.18.145:6379] "PING" ……………………
到此,主從複製配置完成,其實挺簡單!
2、redis的級聯複製
所謂級聯級複製是master-slave1-slave2,對於slave1來講,master是主;對於slave2來講,slave1就是主,具體方式以下:
假如ip地址以下: master 192.168.8.7 slave1 192.168.8.8 slave2 192.168.8.9 在slave1上操做 #vim redis.conf # slaveof <masterip><masterport> slaveof 192.168.8.7 6379 --指定master的IP和端口 在slave2上操做 #vim redis.conf # slaveof <masterip><masterport> slaveof 192.168.8.8 6379 --指定slave1的IP和端口 而後啓動redis-server進程 #nohup /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf & 以上就實現了redis級聯級複製!
不足之處,請多多指出!