1、前提,請認真閱讀我上篇博文《consul 1.0 server cluster集羣配置全解密》node
http://412166174.blog.51cto.com/3102369/1975822 redis
本文是上面文章的續集數據庫
安裝consul client consul 客戶端檢腳本 ============================================================================================= 在192.168.56.122服務器上 ============================================================================================= 一、安裝redis yum intstall redis -y 二、配置redis 爲主 cat >/etc/redis.conf<<EOF bind 192.168.56.122 127.0.0.1 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/redis.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 /var/lib/redis 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 requirepass password 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 EOF 三、啓動redis service redis start 四、配置consul服務 [root@MySQL-slave /etc/consul]# cat >redis-master.json<<EOF { "service": [ { "name": "6379-redis-m", "tags": ["master"], "address": "192.168.56.132", "port": 6379, "checks": [ { "args": ["/data/consul/check_master.sh","0"], "interval": "5s" } ] } ] } EOF [root@MySQL-slave /etc/consul]# cat >redis-slave.json<<EOF { "service": [ { "name": "6379-redis-s", "tags": ["slave"], "address": "192.168.56.132", "port": 6379, "checks": [ { "args": ["/data/consul/check_slave.sh","0"], "interval": "5s" } ] } ] } EOF 五、主從檢測腳本 [root@tomcat103 17:30:04 consul]/#cat >/data/consul/check_master.sh<<EOF #!/bin/bash /usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a password info | grep role:master a=$? if [ $a == 1 ];then exit 2 fi EOF [root@tomcat103 17:30:18 consul]/#cat >/data/consul/check_slave.sh<<EOF #!/bin/bash /usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a password info | grep role:slave a=$? if [ $a == 1 ];then exit 2 fi EOF 更改啓動命令 nohup /usr/local/bin/consul agent -config-dir=/etc/consul -rejoin -enable-script-checks=true & ==================================================================== 在192.168.56.122服務器上 ==================================================================== 一、安裝redis yum intstall redis -y 二、配置192.168.56.132上爲192.168.56.122 redis的從 cat >/etc/redis.conf<<EOF bind 192.168.56.132 127.0.0.1 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis/redis.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 /var/lib/redis slaveof 192.168.56.122 6379 masterauth password 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 requirepass password 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 EOF 三、配置consul服務 主從檢測腳本 [root@tomcat103 17:30:04 consul]/#cat >/data/consul/check_master.sh<<EOF #!/bin/bash /usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a password info | grep role:master a=$? if [ $a == 1 ];then exit 2 fi EOF [root@tomcat103 17:30:18 consul]/#cat >/data/consul/check_slave.sh<<EOF #!/bin/bash /usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a password info | grep role:slave a=$? if [ $a == 1 ];then exit 2 fi EOF 四、consul配置文件 [root@MySQL-slave /etc/consul]# cat >/etc/consul/consul_config.json<<EOF { "advertise_addr": "192.168.56.132", "bind_addr": "192.168.56.132", "domain": "consul", "datacenter": "consul-cluster", "data_dir": "/data/consul/data", "enable_syslog": true, "performance": { "raft_multiplier": 1 }, "dns_config": { "allow_stale": true, "max_stale": "15s" }, "retry_join": [ "192.168.56.120", "192.168.56.121", "192.168.56.122" ], "retry_interval": "10s", "skip_leave_on_interrupt": true, "leave_on_terminate": false, "ports": { "dns": 53, "http": 80 }, "rejoin_after_leave": true, "addresses": { "http": "0.0.0.0", "dns": "0.0.0.0" } } EOF [root@MySQL-slave /etc/consul]# cat >/etc/consul/redis-master.json<<EOF { "service": [ { "name": "6379-redis-m", "tags": ["master"], "address": "192.168.56.132", "port": 6379, "checks": [ { "args": ["/data/consul/check_master.sh","0"], "interval": "5s" } ] } ] } EOF [root@MySQL-slave /etc/consul]# cat >/etc/consul/redis-slave.json<<EOF { "service": [ { "name": "6379-redis-s", "tags": ["slave"], "address": "192.168.56.132", "port": 6379, "checks": [ { "args": ["/data/consul/check_slave.sh","0"], "interval": "5s" } ] } ] } EOF 五、啓動consul客戶端服務 nohup consul agent -config-dir /etc/consul/ -data-dir /data/consul/data -enable-script-checks=true -ui & 六、 在122服務器上ping 域名 測試高可用性 ping 6379-redis-m.service.consul ping 6379-redis-s.service.consul (推薦使用dig @192.168.56.120 -p 53 6379-redis-m.service.consul 命令測試 )
================================================================================== 更改兩個服務器redis配置 互換主從角色 在122上打開着兩行註釋 slaveof 192.168.56.132 6379 masterauth password 重啓redis service redis restart 在132上 #slaveof 192.168.56.122 6379 #masterauth password 重啓服務 service redis restart 從新測試域名對應的數據庫主從地址 ping 6379-redis-m.service.consul ping 6379-redis-s.service.consul
至此consul經過腳本自動發現redis主從高可用完成json
特別建議使用sentinel 或者redis cluster 實現故障切換,本文利用主從結構只是爲了達到實驗目的tomcat