- 安裝環境
這里使用三台服務器,每台服務器上開啟一個redis-server和redis-sentinel服務,redis-server端口為6379,redis-sentinel的端口為26379。
redis-server說明
192.168.10.3:6379 主
192.168.10.4:6379 從
192.168.10.5:6379 從
redis-sentinel說明
192.168.10.3:26379
192.168.10.4:26379
192.168.10.5:26379
目錄規划:
配置文件目錄: /data/apps/conf/redis
數據目錄: /data/apps/data/redis_6379
日志目錄: /data/apps/log/redis
pid文件目錄: /data/apps/var/redis
sentinel數據目錄: /data/apps/var/sentinel
- 安裝Redis
操作主機:192.168.10.3 192.168.10.4 192.168.10.5
1、下載redis安裝包
下載完畢后使用tar命令解壓到當前目錄。
[root@localhost ~]# wget http://download.redis.io/releases/redis-3.2.8.tar.gz [root@localhost ~]# tar zxvf redis-3.2.8.tar.gz
2、編譯redis
進入安裝目錄下,執行make命令,編譯成功后會顯示下面內容。
編譯后在目錄下生成一個src目錄,里面會有編譯好的執行文件
[root@localhost ~]# cd redis-3.2.8
[root@localhost redis-3.2.8]# make
[root@localhost redis-3.2.8]# cd src && make all
make[1]: Entering directory `/home/redis/redis-3.2.8/src' Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory `/home/redis/redis-3.2.8/src'
3、安裝redis
Redis的默認安裝路徑是 /usr/local 目錄下,進入src目錄下,使用vi編輯Makefile文件,在文件中找到“PRIFIX?=/usr/local”,可以修改為你需要安裝的目錄,這里使用局對路徑。
然后執行make install命令進行安裝,安裝完畢后會在安裝目錄下生成一些執行文件。
[root@localhost src]# make install Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install
[root@localhost ~]# cd /usr/local/bin/
[redis@localhost bin]$ ls
dump.rdb redis-check-aof redis-cli redis-server
redis-benchmark redis-check-rdb redis-sentinel
4、編輯環境變量
[root@localhost ~]# vim /etc/profile.d/redis.sh
export REDIS_HOME=/usr/local
export PATH=${REDIS_HOME}:bin:${PATH}
[root@localhost ~]# source /etc/profile
5、修改配置文件
vim /data/apps/conf/redis/redis_6379.conf bind 0.0.0.0 protected-mode no port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/data/apps/var/redis/redis_6379.pid" loglevel notice logfile "/data/apps/log/redis/redis_6379.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/apps/data/redis_6379" slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes # Generated by CONFIG REWRITE
6、 配置主從復制
192.168.10.3為master,其他兩個為slave,只需修改從庫的配置文件
操作主機:192.168.10.4、192.168.10.5
[root@localhost ~]# echo 'slaveof 192.168.10.3 6379' >>/data/apps/conf/redis_6379.conf
7 、啟動redis-server
操作主機:所有主機
[root@localhost ~]# redis-server /data/apps/conf/redis/redis_6379.conf
8、檢查
[root@localhost ~]# redis-cli -h 192.168.10.3 -p 6379
127.0.0.1:6379> info
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.10.5,port=6379,state=online,offset=14874,lag=1
slave1:ip=192.168.10.4,port=6379,state=online,offset=15160,lag=1
master_repl_offset:15303
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:15302
# 出現上面的結果說明主從復制成功
- 部署redis-sentinel
操作主機:所有主機,每個主機的配置文件相同
1、配置文件
[root@localhost ~]# vim /data/apps/config/redis/redis-sentinel-26379.conf port 26379 daemonize yes protected-mode no dir "/data/apps/var/sentinel" pidfile "/data/apps/var/redis/redis-sentinel.pid" logfile "/data/apps/log/redis/redis-sentinel.log" sentinel monitor redis_master 192.168.10.3 6379 2 sentinel down-after-milliseconds redis_master 9000 sentinel failover-timeout redis_master 9000
2、啟動sentinel
[root@localhost ~]# redis-sentinel /data/apps/conf/redis/redis-sentinel-26379.conf
3、檢查
[root@localhost ~]# ss -lnt|grep 26379
[root@localhost ~]# redis-cli -p 26379
127.0.0.1:26379> info
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=redis_master,status=ok,address=192.168.10.3:6379,slaves=2,sentinels=3
# 出現上面的情況說明配置成功
- 部署Haproxy
1、安裝Haproxy
#下載 wget http://fossies.org/linux/misc/haproxy-1.8.12.tar.gz #解壓 tar -zxvf haproxy-1.8.12.tar.gz cd haproxy-1.8.12 #安裝 make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy #參數說明 TARGET=linux26 #內核版本,使用uname -r查看內核,如:2.6.18-371.el5,此時該參數就為linux26;kernel 大於2.6.28的用:TARGET=linux2628 ARCH=x86_64 #系統位數 PREFIX=/usr/local/haprpxy #/usr/local/haprpxy為haprpxy安裝路徑
2、配置Haproxy
vim /usr/local/haproxy/haproxy.cfg #--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /usr/local/haproxy pidfile /usr/local/haproxy/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults log global mode tcp retries 3 option redispatch maxconn 20000 timeout connect 600s timeout client 600s timeout server 600s listen stats bind 0.0.0.0:18888 mode http stats uri /haproxy-status stats auth admin:admin stats hide-version stats refresh 30s frontend redis16379 bind :16379 default_backend redis_16379_backend backend redis_16379_backend option tcp-check tcp-check send PING\r\n tcp-check expect string +PONG tcp-check send INFO\ REPLICATION\r\n tcp-check expect string role:master tcp-check send INFO\ REPLICATION\r\n tcp-check expect rstring connected_slaves:[1-9] tcp-check send QUIT\r\n tcp-check expect string +OK server redis1_6379 192.168.10.3:6379 check inter 1s server redis2_6379 192.168.10.4:6379 check inter 1s server redis3_6379 192.168.10.5:6379 check inter 1s
3、檢查
[root@localhost ~]# redis-cli -p 16379 set a 2 OK [root@localhost ~]# redis-cli -p 16379 set get a OK
- 總結
上面的內容可以解決redis高可用的問題,但是Haproxy還是單點。如果服務器是物理機,Haproxy可以通過Keepalived解決單點問題,實現高可用;但是服務器是ECS的情況下不能使用Keepalived。