一、單機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-RELEASENOTES BUGS COPYING deps INSTALL Makefile README.md runtest-cluster sentinel.conf tests CONTRIBUTING MANIFESTO runtest runtest-sentinel src utils
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-RELEASENOTES BUGS COPYING deps INSTALL Makefile README.md runtest-cluster sentinel.conf tests bin CONTRIBUTING data etc log MANIFESTO runtest runtest-sentinel src [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-server redis-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的一主二從三哨兵架構搭建完成。