redis環境搭建及一主二從三哨兵模式配置
一、單機redis環境搭建
1.安裝:
OS:linux redhat6.5
下載redis 官網下載連結:https://redis.io/download
把安裝包上傳到伺服器,進行解壓
[root@master redis]# tar -xvf redis-5.0.4.tar.gz [root@master redis]# cd redis-5.0.4 [root@master redis-5.0.4]# ls 00-RELEASENOTESBUGSCOPYINGdepsINSTALLMakefileREADME.mdruntest-clustersentinel.conftests CONTRIBUTINGMANIFESTOruntestruntest-sentinelsrcutils
2.安裝gcc,如果未安裝gcc的話需要安裝gcc環境
[root@master redis-5.0.4]# yum -y install gcc*
3.進入redis-5.0.4目錄對解壓後的檔案進行編譯、安裝
[root@master redis]# cd redis-5.0.4 [root@master redis-5.0.4]#make//進行編譯 [root@master redis-5.0.4]#cd src///進入src目錄下 [root@master src]# make install //安裝redis
4.為了便於管理,在redis-5.0.4下建立bin,log,data,etc等四個目錄
[root@master redis-5.0.4]# mkdir bin [root@master redis-5.0.4]#mkdir etc [root@master redis-5.0.4]#mkdir data [root@master redis-5.0.4]#mkdir log [root@master redis-5.0.4]#ls 00-RELEASENOTESBUGSCOPYINGdepsINSTALLMakefileREADME.mdruntest-clustersentinel.conftests binCONTRIBUTINGdataetclogMANIFESTOruntestruntest-sentinelsrc [root@master redis-5.0.4]#mv redis.conf etc///將redis-5.0.4目錄下的 redis.conf 移動到 redis-5.0.4目錄下的etc資料夾下 [root@master redis-5.0.4]#mv src/mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-serverredis-sentinel bin/ //將mkreleasehdr.sh、redis-benchmark、redis-check-aof、redis-cli、redis-server 、redis-sentinel移動到/usr/local/redis-5.0.0/bin/ 目錄下
5.進入redis-5.0.4/etc下修改redis.conf配置檔案,然後啟動redis服務
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #bind 127.0.0.1//繫結的主機,註釋掉後允許所有主機登陸 protected-mode no //關閉保護模式 port 6379//埠,預設為6379 daemonize yes//開啟後臺執行模式 logfile "/backup2/redis/redis-5.0.4/log/redis.log" //redis日誌檔案路徑 dir "/backup2/redis/redis-5.0.4/data"//持久化資料檔案路徑 requirepass "password"//登陸redis資料庫的密碼認證 masterauth "password"//哨兵模式中設定主庫密碼與當前庫密碼同步,保證從庫能夠提升為主庫 appendonly yes//開啟AOF持久化模式
進入bin目錄下
[root@master bin]# ./redis-server ../etc/redis.conf //使用編輯好的配置檔案啟動redis
[root@master bin]# ./redis-cli //啟動redis客戶端 127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379> auth password//該密碼為配置檔案中設定的密碼 OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379> //如上輸出即表示redis啟動成功
6.啟動告警處理,在首次啟動redis時 有可能會有以下告警:
告警:overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to/etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
解決辦法:在/etc/sysctl.conf/檔案中新增vm.overcommit_memory=1 sysctl -p 生效
二、哨兵模式配置
由於哨兵模式包含了簡單的主從模式,所以主從模式不再進行單獨說明,普通主從模式只是可以備份資料,在主機宕機並且資料檔案損壞的情況下提供資料恢復,不能在主機掛掉之後主動升級為主節點,而哨兵模式就是通過哨兵對節點的監控,在主節點掛掉之後能通過投票的方式在從節點中選舉一個升級為主節點。
軟體架構:三臺伺服器:master,slave1,slave2,其中master為前面已經安裝好的主機
1.修改sentinel配置檔案
//在redis.conf所在目錄下新建一個sentinel.conf檔案 [root@master etc]# vi sentinel.conf protected-mode no//關閉保護模式 port 26379//預設埠 daemonize yes//允許後臺執行 pidfile "/var/run/redis-sentinel.pid"//pid檔案 預設就好 logfile "/backup2/redis/redis-5.0.4/log/sentinel.log"//sentinel日誌檔案 sentinel announce-ip 192.168.1.1 //設定本機ip #master sentinel monitor mymaster 192.168.1.1 6379 2 sentinel down-after-milliseconds mymaster1 30000 sentinel parallel-syncs mymaster 2 sentinel failover-timeout mymaster 180000 sentinel auth-pass mymaster1 yourpasswd
2.把 redis-5.0.4目錄整個打包並傳到另外兩臺伺服器,然後解壓
在每臺伺服器上分別修改redis.conf,在配置檔案中新增slaveof 192.168.1.1 6379 //配置主從關係
修改sentinel.cong,把 sentinel announce-ip 192.168.1.1 中的ip更換為本機ip,其餘配置可不用更改。
3.啟動叢集服務,按照master->slave->sentinel的啟動順序
[root@master bin]# ./redis-server ../etc/redis.conf//在每個節點執行該命令啟動redis root@master bin]# ./redis-sentinel ../etc/sentinel.conf//啟動sentinel //啟動後在主節點連線到主節點 127.0.0.1:6379> info replication # Replication role:master//角色 connected_slaves:2//節點個數 slave0:ip=192.168.1.2,port=6379,state=online,offset=84,lag=1//從節點資訊 slave1:ip=192.168.1.3,port=6379,state=online,offset=98,lag=0//從節點資訊 master_replid:27663ab908f4a8bfd198cd7ec3db924bee4fa441 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:98 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:98 //連線到從節點 127.0.0.1:6379> info replication # Replication role:slave master_host:192.168.1.1 master_port:6379 master_link_status:up//主節點狀態 up/down master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:62896 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:af5dd7a6f5e2093bbe8f01923fcfe0ed7fc8ba14 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:62896 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:62896 127.0.0.1:6379>
啟動哨兵:[root@test bin]# ./redis-sentinel ../etc/sentinel.conf
[root@master bin]#./redis-cli -h 192.168.1.1 -p 26379 10.197.11.159:26379> info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=192.168.1.1:6379,slaves=2,sentinels=3 //以上表示哨兵模式叢集正常
至此,redis的一主二從三哨兵架構搭建完成。