# redis-6379.conf 文件, 寫入下面數據: port 6379 daemonize yes pidfile /data/6379/redis.pid loglevel notice logfile "/data/6379/redis.log" dbfilename dump.rdb dir /data/6379 protected-mode no
sed "s/6379/6380/g" redis-6379.conf > redis-6380.conf sed "s/6379/6381/g" redis-6379.conf > redis-6381.conf # 把6379配置成主庫,6380和6381配置成從庫, 須要在6380和6381的conf文件下寫入: slaveof 127.0.0.1 6379
redis-server redis-6379.conf redis-server redis-6380.conf redis-server redis-6381.conf
127.0.0.1:6379> info Replication # 身份是master 127.0.0.1:6380> info Replication # 身份是slave 127.0.0.1:6381> info Replication # 身份是slave
# 演示: 1. 手動檢查進程, 殺死主庫. (6379) 2.手動切換到其中的一個從庫(6380),去掉conf文件中的slave身份 3.切換到另外一個從庫(6381), 把conf文件中的slave指向改爲 slaveof 127.0.0.1 6380
# 哨兵(redis-sentinel)集羣自動切換原理簡述: 給數據庫配上幾個哨兵,讓他們監控着數據庫, 不時的去問主, 從數據庫時候還活着,若是主庫在規定時間內沒有回回復哨兵信息,證實掛了,
哨兵會告訴其餘的哨兵主庫掛啦,而後其餘的哨兵去驗證一下,若是發現真的掛了,
那麼哨兵會在從庫中選舉出一個新的主庫,而後把其餘的從庫slaveof 執行這個新的主庫.
# redis-6379.conf port 6379 daemonize yes logfile "6379.log" dbfilename "dump-6379.rdb" dir "/opt/redis/data/" # 另外兩個以下 sed "s/6379/6380/g" redis-6379.conf > redis-6380.conf sed "s/6379/6381/g" redis-6379.conf > redis-6381.conf # 而後分別在這兩個文件下寫上: slaveof 127.0.0.1 6379
redis-server redis-6379.conf redis-server redis-6380.conf redis-server redis-6381.conf
redis-cli -p 6379 info Replication # master 主庫 redis-cli -p 6380 info Replication # slave 從庫 redis-cli -p 6380 info Replication # slave 從庫
# redis-26379.conf 寫入下面數據: port 26379 dir /var/redis/data/ logfile "26379.log" # 當前Sentinel節點監控 127.0.0.1:6379 這個主節點 # 2表明判斷主節點失敗至少須要2個Sentinel節點節點贊成,少數服從多數 # s18ms是主節點的別名 sentinel monitor s18ms 127.0.0.1 6379 2 # 每一個Sentinel節點都要按期PING命令來判斷Redis數據節點和其他Sentinel節點是否可達,若是超過30000毫秒30s且沒有回覆,則斷定不可達 sentinel down-after-milliseconds s18ms 30000 # 當Sentinel節點集合對主節點故障斷定達成一致時,Sentinel領導者節點會作故障轉移操做,選出新的主節點, # 原來的從節點會向新的主節點發起復制操做,限制每次向新的主節點發起復制操做的從節點個數爲1 sentinel parallel-syncs s18ms 1 # 故障轉移超時時間爲180000毫秒 sentinel failover-timeout s18ms 180000 daemonize yes # 另外兩個配置信息也只有端口不一樣
[root@localhost s18msredis]# redis-sentinel redis-26379.conf [root@localhost s18msredis]# redis-sentinel redis-26380.conf [root@localhost s18msredis]# redis-sentinel redis-26381.conf
[root@localhost s18msredis]# redis-cli -p 26379 info sentinel [root@localhost s18msredis]# redis-cli -p 26380 info sentinel [root@localhost s18msredis]# redis-cli -p 26381 info sentinel
# 這三個哨兵檢測這一主兩從 1.幹掉(主庫)6379,查看6380和6381這兩個的身份信息 2.發現其中一個從庫(如:6380)變成了主庫, 另外一個從庫變成了6380的從庫 # 這個選主庫是隨機的. 3.當6379復活後,會自動變成6380的從庫 # 原理其實就是把6380裏面的slave移除啦,而後把6381的slaveof指向了6381, 6379復活後slaveof也指向了6381
集羣搭建的做用其實就是把一個很大的數據切片分攤, 就比如1噸重物放到一個馬車上,馬會拉不動的,
可是分開放到6輛馬車上,每輛馬車分得重量就會變小,這樣馬就能拉動啦!
虛擬槽分區巧妙地使用了哈希空間,使用分散度良好的哈希函數把全部的數據映射到一個固定範圍內的整數集合,整數定義爲槽(slot)。 Redis Cluster槽的範圍是0 ~ 16383。 槽是集羣內數據管理和遷移的基本單位。採用大範圍的槽的主要目的是爲了方便數據的拆分和集羣的擴展, 每一個節點負責必定數量的槽。
準備節點(服務端) 服務端運輸數據,分配16384個槽位,管理數據 ruby腳本自動幫你分配槽位
# 建立redis-7000.conf 文件並寫入以下數據: port 7000 daemonize yes dir "/opt/redis/data" logfile "7000.log" dbfilename "dump-7000.rdb" cluster-enabled yes #開啓集羣模式 cluster-config-file nodes-7000.conf #集羣內部的配置文件 cluster-require-full-coverage no #redis cluster須要16384個slot都正常的時候才能對外提供服務,
換句話說,只要任何一個slot異常那麼整個cluster不對外提供服務。 所以生產環境通常爲no # 咱們一共須要6個節點,也就是須要6個配置文件,3主3從,最少6個才能保證高可用,這6個配置文件僅僅是端口不一樣
sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf redis-7001.conf redis-7002.conf redis-7003.conf redis-7004.conf redis-7005.conf
[root@localhost s18cluster]# redis-server redis-7000.conf [root@localhost s18cluster]# redis-server redis-7001.conf [root@localhost s18cluster]# redis-server redis-7002.conf [root@localhost s18cluster]# redis-server redis-7003.conf [root@localhost s18cluster]# redis-server redis-7004.conf [root@localhost s18cluster]# redis-server redis-7005.conf
#安裝準備ruby語言的環境,用於自動化建立redis集羣 1.下載ruby wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz 2.解壓縮 tar -zxvf ruby-2.3.1.tar.gz ./configure --prefix=/opt/ruby/ 3.安裝 make && make install
wget http://rubygems.org/downloads/redis-3.3.0.gem
gem install -l redis-3.3.0.gem
[root@localhost s18cluster]# find /opt -name redis-trib.rb /opt/redis-4.0.10/src/redis-trib.rb
/opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 127.0.0.1:7000
127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 # --replicas 1 表示每一個主節點有一個從節點
redis-cli -p 7000 cluster info redis-cli -p 7000 cluster nodes #等同於查看nodes-7000.conf文件節點信息 集羣主節點狀態 redis-cli -p 7000 cluster nodes | grep master 集羣從節點狀態 redis-cli -p 7000 cluster nodes | grep slave
redis-cli -p 7000 -c # -c 開啓集羣模式
!!! 當你在其中一個節點中,建立redis的key,只要redis的key通過了重定向,分配到不一樣的節點中,表明集羣搭建ok 重定向以後, 只要在集羣裏有數據 不管在哪一個port裏面,均可以get出來,好比set name attila 被分配到7001 你登陸7000也能get到name