Redis主從複製和高可用
阿新 • • 發佈:2018-12-18
一.Redis的安裝和主從配置
實驗環境:rhel6.5
主機:server{1..4} ip:172.25.254.{1..4}
1.在server1和server2上下載redis安裝包,解壓,編譯,安裝
tar zxf redis-4.0.8.tar.gz
yum install -y gcc
make&&make install
cd utils/
./install_server.sh #啟動指令碼
2.server1修改監聽埠
[[email protected] utils]# redis-cli #檢視安裝資訊
[ [email protected] udev]# cd /etc/redis/
[[email protected] redis]# vim 6379.conf #監聽埠改為0.0.0.0,有什麼監聽什麼
netstat檢視服務埠是否開啟
3.server2上修改配置檔案
[[email protected] redis]# vim 6379.conf [[email protected] redis]# /etc/init.d/redis_6379 restart #重啟 Stopping ... Redis stopped Starting Redis server...
4.測試,在server1上設定變數,server2上進行了同步
二.redis叢集實現高可用
1.在上一步d的基礎上在開啟兩個虛擬機器server3,用於實現redis的高可用
2.在3上安裝redis,實現主從,get name可以看到主的訊息
3.配置檔案複製到redis目錄下
[[email protected] redis-4.0.8]# cp sentinel.conf /etc/redis/
[[email protected] redis-4.0.8]# cd /etc/redis/
[[email protected] ~]# vim /etc/redis/sentinel.conf 17 protected-mode no #保護模式 21 port 26379 69 sentinel monitor mymaster 172.25.254.1 6379 2 #sentinel監聽主機的ip和6379埠,必須至少有兩個連線才能故障切換 98 sentinel down-after-milliseconds mymaster 10000 #預設30s沒有反應,認為伺服器斷開
複製到叢集的每一個結點上,scp
4.在任意一臺中可以監聽
redis的主是server1,slave為2,3
redis-server /etc/redis/sentinel.conf –sentinel #監控命令,檢視master和slave
5.手動停止server1
redis-cli
shutdown
監控檢視server1--->server2
叢集中master變為server2,server2的配置檔案也變為server2
三.叢集管理工具
1.寫redis的配置目錄,建立不同的程序,redis.conf複製到每一個700{1..6} 目錄中,
redis-server 700{1..6}/redis.conf #開啟,生效
cd /usr/local
mkdir cluster
cd cluster
mkdir 700{1..6}
cd 7001
vim redis.conf
port 7000 #每一個不一樣
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000 #超時
appendonly yes
redis-cli -p 7001 #檢視狀態
[[email protected]rver1 ~]# redis-trib.rb --help
/usr/bin/env: ruby: No such file or directory
[[email protected] ~]# yum install -y ruby
[[email protected] ~]# redis-trib.rb --help
/usr/local/bin/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from /usr/local/bin/redis-trib.rb:24
[[email protected] ~]# yum install -y rubygems-1.3.7-5.el6.noarch.rpm
[[email protected] ~]# redis-trib.rb --help
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/local/bin/redis-trib.rb:25
[[email protected] ~]# gem list
*** LOCAL GEMS ***
[[email protected] ~]# gem --help
RubyGems is a sophisticated package manager for Ruby. This is a
basic help message containing pointers to more information.
Usage:
gem -h/--help
gem -v/--version
gem command [arguments...] [options...]
Examples:
gem install rake
gem list --local
gem build package.gemspec
gem help install
Further help:
gem help commands list all 'gem' commands
gem help examples show some examples of usage
gem help platforms show information about platforms
gem help <COMMAND> show help on COMMAND
(e.g. 'gem help install')
gem server present a web page at
http://localhost:8808/
with info about installed gems
Further information:
http://rubygems.rubyforge.org
[[email protected] ~]# gem install --local redis-4.0.1.gem
ERROR: Error installing redis-4.0.1.gem:
redis requires Ruby version >= 2.2.2.
[[email protected] ~]# rpm -q ruby
ruby-1.8.7.352-12.el6_4.x86_64
[[email protected] ~]# yum install ruby-2.2.3-1.el6.x86_64.rpm libyaml-0.1.3-4.el6_6.x86_64.rpm -y
[[email protected] ~]# gem install --local redis-4.0.1.gem
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 1 seconds
1 gem installed
[[email protected] ~]# gem list
*** LOCAL GEMS ***
bigdecimal (1.2.6)
io-console (0.4.3)
json (1.8.1)
minitest (5.4.3)
power_assert (0.2.2)
psych (2.0.8)
rake (10.4.2)
rdoc (4.2.0)
redis (4.0.1)
test-unit (3.0.8)
[[email protected] ~]# redis-trib.rb create --replicas 1 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 127.0.0.1:7006
Yes確認新增
[[email protected] ~]# redis-trib.rb info 127.0.0.1:7002
[[email protected] ~]# redis-trib.rb check 127.0.0.1:7001
[[email protected] ~]# redis-cli -c -p 7001
127.0.0.1:7001> set name xue 7001上設定name等於xue
-> Redirected to slot [5798] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get name
"xue"
[[email protected] ~]# redis-cli -c -p 7006
127.0.0.1:7006> get name -> Redirected to slot [5798] located at 127.0.0.1:7002
"xue"
127.0.0.1:7002> set user westos 7006上改了名字
OK
127.0.0.1:7002> get user 7002查到改了的名字
"westos"
[[email protected] ~]# redis-cli -c -p 7004
127.0.0.1:7004> get user
-> Redirected to slot [5474] located at 127.0.0.1:7002
"westos"
127.0.0.1:7002> del user #刪除之後名字沒有了
(integer) 1
127.0.0.1:7002> get user
(nil)
127.0.0.1:700
[[email protected] ~]# redis-cli -c -p 7004 1,4互相為主從
127.0.0.1:7004> SHUTDOWN
not connected>
[[email protected] ~]# ps ax
[[email protected] ~]# redis-cli -c -p 7001
127.0.0.1:7001> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002
"xue"
127.0.0.1:7002>