1. 程式人生 > >CentOS 安裝 Redis (高可用)

CentOS 安裝 Redis (高可用)

原文:https://www.sunjianhua.cn/archives/centos-redis.html

 

下載地址: http://download.redis.io/releases/

官方文件: https://redis.io/documentation

一、Redis單機版安裝

$ wget http://download.redis.io/releases/redis-4.0.11.tar.gz
$ tar xzf redis-4.0.11.tar.gz
$ cd redis-4.0.11 $ make 

二、Redis Sentine主從複製

方案: 1 master,2 slave,3 sentinel

注:在以下配置為單機偽叢集搭建,生產建議多臺機器搭建.
redis-sentinel.png

redis-sentinel-app.png

2.1配置master

a.將安裝好的單機版,移動到/usr/local/ 下
$ mv redis-4.0.11 /usr/local/redis

b.編輯master配置檔案,主要修改以下內容
$ vi /usr/local/redis/redis.conf bind 192.168.20.250 # dir "/usr/local/redis/" pidfile /var/run/redis_6379.pid logfile "/usr/local/redis/redis.log" databases 51 masterauth redis1234 requirepass redis1234 c.啟動master服務 $ /usr/local/redis/src/redis-server /usr/local/redis/redis.conf & d.檢視日誌 $ tail -f /usr/local/redis/redis.log 

2.2配置兩個slave

a. 建立目錄
$ mkdir /usr/local/redis_slave1  /usr/local/redis_slave2

b.複製server,cli,conf等檔案    
$ cp /usr/local/redis/src/redis-server /usr/local/redis_slave1 $ cp /usr/local/redis/src/redis-server /usr/local/redis_slave2  $ cp /usr/local/redis/src/redis-cli /usr/local/redis_slave1 $ cp /usr/local/redis/src/redis-cli /usr/local/redis_slave2  $ cp /usr/local/redis/redis.conf /usr/local/redis_slave1 $ cp /usr/local/redis/redis.conf /usr/local/redis_slave2 c.編輯slave配置檔案,主要修改以下內容 $ vi /usr/local/redis_slave1/redis.conf port 6380 bind 127.0.0.1 daemonize no pidfile "/var/run/redis_6380.pid" logfile "/usr/local/redis_slave1/redis_slave1.log" databases 51 dir "/usr/local/redis_slave1" masterauth "redis1234" requirepass "redis1234" slaveof 127.0.0.1 6379  $ vi /usr/local/redis_slave2/redis.conf port 6381 bind 127.0.0.1 daemonize no pidfile "/var/run/redis_6381.pid" logfile "/usr/local/redis_slave2/redis_slave2.log" databases 51 dir "/usr/local/redis_slave2" masterauth "redis1234" requirepass "redis1234" slaveof 127.0.0.1 6379 d.啟動兩個slave $ /usr/local/redis_slave1/redis-server /usr/local/redis_slave1/redis.conf & $ /usr/local/redis_slave2/redis-server /usr/local/redis_slave2/redis.conf & e.檢視兩個slave日誌 $ tail -f /usr/local/redis_slave1/redis_slave1.log $ tail -f /usr/local/redis_slave2/redis_slave2.log 

2.3配置三個sentinel

a.建立三個sentinel目錄
$ mkdir /usr/local/redis_sentinel1 /usr/local/redis_sentinel2 /usr/local/redis_sentinel3 b.拷貝sentinel,conf等檔案 $ cp /usr/local/redis/src/redis-sentinel /usr/local/redis_sentinel1 $ cp /usr/local/redis/src/redis-sentinel /usr/local/redis_sentinel2 $ cp /usr/local/redis/src/redis-sentinel /usr/local/redis_sentinel3  $ cp /usr/local/redis/sentinel.conf /usr/local/redis_sentinel1 $ cp /usr/local/redis/sentinel.conf /usr/local/redis_sentinel2 $ cp /usr/local/redis/sentinel.conf /usr/local/redis_sentinel3 c.編輯sentinel配置檔案,主要一下配置 $ vi /usr/local/redis_sentinel1/sentinel.conf port 26379 protected-mode no dir "/usr/local/redis_sentinel1" logfile "/usr/local/redis_sentinel1/redis_sentinel1.log" sentinel auth-pass mymaster redis sentinel monitor mymaster 127.0.0.1 6379 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000  $ vi /usr/local/redis_sentinel2/sentinel.conf port 26380 protected-mode no dir "/usr/local/redis_sentinel2" logfile "/usr/local/redis_sentinel2/redis_sentinel2.log" sentinel auth-pass mymaster redis1234 sentinel monitor mymaster 127.0.0.1 6379 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000  $ vi /usr/local/redis_sentinel3/sentinel.conf port 26381 protected-mode no dir "/usr/local/redis_sentinel3" logfile "/usr/local/redis_sentinel3/redis_sentinel3.log" sentinel auth-pass mymaster redis1234 sentinel monitor mymaster 127.0.0.1 6379 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 

2.4測試master和slave

a.啟動master和2個slave服務!
檢視上述啟動方法

b.用master或者slave中的任意cli連線master節點進行操作
$ /usr/local/redis_slave1/redis-cli -p 6379
$ auth redis1234 $ select 2 $ set name sunjianhua $ info Replication 

redis-slave-setvalue.jpg
redis-slave-setvalue1.jpg
redis-slave-setvalue2.jpg

c.連線slave節點查詢資料是否存在
$ /usr/local/redis_slave1/redis-cli -p 6381
$ auth redis1234 $ select 2 $ select name 

redis-slave-getvalue.jpg

2.5測試sentinel

a.先啟動主從,後啟動sentinel服務
主從啟動看上面
$ /usr/local/redis_sentinel1/redis-sentinel  /usr/local/redis_sentinel1/sentinel.conf &
$ /usr/local/redis_sentinel2/redis-sentinel /usr/local/redis_sentinel2/sentinel.conf & $ /usr/local/redis_sentinel3/redis-sentinel /usr/local/redis_sentinel3/sentinel.conf & b.檢視sentinel日誌 $ tail -f /usr/local/redis_sentinel1/redis-sentinel1.log 

redis-sentinel-test.png

c.關閉master,檢視sentinel控制檯

redis-sentinel-test1.png
redis-sentinel-test2.png

d.連線之前的slave服務,檢視info replication,兩個slave 節點之一變為了master

redis-sentinel-test3.png