Redis + Twemproxy(六)之 HAProxy + Keepalived

HAProxy 集成 Keepalived,可避免 HAProxy 单点故障。

一、部署 Redis

之前的文章:Redis + Twemproxy(四)之 Sentinel 集群 ,所有 Redis 整合在一个集群中:

  • 优点:
    Master 与所有 Slave 数据一致,当增减 Redis 节点时,不会出现文章:Redis + Twemproxy(三)之新增 Redis 节点 所出现的数据丢失。
  • 缺点:
    因为只有一个 Master,所以 Twemproxy 中只能配置一个节点,从而导致 Twemproxy 分片功能缺失。

将 Redis 改造成2个集群,其中:

  • Master_01 -> Slave_01
  • Master_02 -> Slave_02
主机名称 IP地址 描述
server01 192.168.255.128 Master_01:192.168.255.128:7000,Slave_02:192.168.255.128:7001
server02 192.168.177.128 Slave_01:192.168.177.128:7003,Master_02:192.168.177.128:7004

修改 Redis + Twemproxy(四)之 Sentinel 集群 中 Sentinel 配置,同时监控两个 Redis 集群。

Sentinel_01:

port 26379
daemonize yes
logfile "sentinel_26379.log"
dir /root/redis_cluster/sentinel_26379

sentinel monitor mymaster_01 192.168.255.128 7000 2
sentinel monitor mymaster_02 192.168.177.128 7004 2

sentinel auth-pass mymaster_01 www.wave.com
sentinel auth-pass mymaster_02 www.wave.com

sentinel down-after-milliseconds mymaster_01 30000
sentinel down-after-milliseconds mymaster_02 30000

sentinel parallel-syncs mymaster_01 1
sentinel parallel-syncs mymaster_02 1

sentinel failover-timeout mymaster_01 180000
sentinel failover-timeout mymaster_02 180000

sentinel client-reconfig-script mymaster_01 /root/twemproxy/reconfig.sh
sentinel client-reconfig-script mymaster_02 /root/twemproxy/reconfig.sh

Sentinel_02:

port 26380
daemonize yes
logfile "sentinel_26380.log"
dir /root/redis_cluster/sentinel_26380

sentinel monitor mymaster_01 192.168.255.128 7000 2
sentinel monitor mymaster_02 192.168.177.128 7004 2

sentinel auth-pass mymaster_01 www.wave.com
sentinel auth-pass mymaster_02 www.wave.com

sentinel down-after-milliseconds mymaster_01 30000
sentinel down-after-milliseconds mymaster_02 30000

sentinel parallel-syncs mymaster_01 1
sentinel parallel-syncs mymaster_02 1

sentinel failover-timeout mymaster_01 180000
sentinel failover-timeout mymaster_02 180000

Sentinel_03:

port 26381
daemonize yes
logfile "sentinel_26381.log"
dir /root/redis_cluster/sentinel_26381

sentinel monitor mymaster_01 192.168.255.128 7000 2
sentinel monitor mymaster_02 192.168.177.128 7004 2

sentinel auth-pass mymaster_01 www.wave.com
sentinel auth-pass mymaster_02 www.wave.com

sentinel down-after-milliseconds mymaster_01 30000
sentinel down-after-milliseconds mymaster_02 30000

sentinel parallel-syncs mymaster_01 1
sentinel parallel-syncs mymaster_02 1

sentinel failover-timeout mymaster_01 180000
sentinel failover-timeout mymaster_02 180000

sentinel client-reconfig-script mymaster_01 /root/twemproxy/reconfig.sh
sentinel client-reconfig-script mymaster_02 /root/twemproxy/reconfig.sh

启动后,查看 Sentinel 信息:

./redis-cli -h 192.168.255.128 -p 26379 -a www.wave.com info Sentinel

Redis + Twemproxy(六)之 HAProxy + Keepalived_第1张图片

二、部署 Twemproxy

参照之前的文章,部署两台 Twemproxy:

主机名称 IP地址 描述
server01 192.168.255.128 Twemproxy_01:192.168.255.128:22121
server02 192.168.177.128 Twemproxy_02:192.168.177.128:22121

两台 Twemproxy 配置保持一致:/root/twemproxy/conf/redis_master.conf

redis_master:
  listen: 0.0.0.0:22121
  hash: fnv1a_64
  distribution: ketama
  auto_eject_hosts: true
  redis: true
  redis_auth: www.wave.com
  server_retry_timeout: 30000
  server_failure_limit: 0
  servers:
   - 192.168.255.128:7000:1
   - 192.168.177.128:7004:1

./redis-cli -h 192.168.255.128 -p 22121 -a www.wave.com

三、部署 HAProxy

参照之前的文章,部署两台 HAProxy:

主机名称 IP地址 描述
server01 192.168.255.128 HAProxy_01:192.168.255.128:16001
server02 192.168.177.128 HAProxy_02:192.168.177.128:16001

不过,我们希望每台 HAProxy 既能够保持对 keepalived + nginx 高可用集群 这篇文章中四台 web 测试工具的负载均衡,又能保持对前面部署的两台 Twemproxy 的负载均衡。

只需在 HAProxy 配置文件中新增一项代理配置即可,两台 HAProxy 配置保持一致。

vim /etc/haproxy/haproxy.cfg

global
	log /dev/log	local0
	log /dev/log	local1 notice
	chroot /var/lib/haproxy
	stats socket /run/haproxy/admin.sock mode 660 level admin
	stats timeout 30s
	user haproxy
	group haproxy
	daemon

	# Default SSL material locations
	ca-base /etc/ssl/certs
	crt-base /etc/ssl/private

	# Default ciphers to use on SSL-enabled listening sockets.
	# For more information, see ciphers(1SSL). This list is from:
	#  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
	# An alternative list with additional directives can be obtained from
	#  https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
	ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
	ssl-default-bind-options no-sslv3

defaults
	log	global
	mode	http
	option	httplog
	option	dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
	errorfile 400 /etc/haproxy/errors/400.http
	errorfile 403 /etc/haproxy/errors/403.http
	errorfile 408 /etc/haproxy/errors/408.http
	errorfile 500 /etc/haproxy/errors/500.http
	errorfile 502 /etc/haproxy/errors/502.http
	errorfile 503 /etc/haproxy/errors/503.http
	errorfile 504 /etc/haproxy/errors/504.http


listen  appli1-rewrite
        bind 0.0.0.0:10001
        cookie  SERVERID rewrite
        balance roundrobin
        option  abortonclose
        option  redispatch
        retries 3
        maxconn 2000
        timeout connect 5000
        timeout client  50000
        timeout server  50000

listen  proxy_tw
        bind :16001
        mode tcp
        balance roundrobin
        option  redispatch
        server tw_proxy_1 192.168.255.128:22121 check inter 10s
        server tw_proxy_2 192.168.177.128:22121 check inter 10s

listen  proxy_web
        bind :16002
        mode tcp
        balance roundrobin
        option forwardfor
        timeout server 15s
        timeout connect 15s
		option httpclose
        server web_proxy_1 192.168.255.128:8571 check inter 10s
        server web_proxy_2 192.168.255.128:8572 check inter 10s
		server web_proxy_3 192.168.177.128:8571 check inter 10s
        server web_proxy_4 192.168.177.128:8572 check inter 10s

frontend admin_stats
        bind :7777
        mode http
        stats enable
        option httplog
        maxconn 10
        stats refresh 30s
        stats uri /admin
        stats auth admin:123456
        stats hide-version
        stats admin if TRUE

然而,在反复刷新访问:http://192.168.177.128:16002/ 时,发现 web 始终定位某一台 real server,而不会轮询。
解决方法:将 proxy_web 下的 “mode tcp” 改成 “mode http”。

mode { tcp | http | health }
功能:用于设置实例的运行模式或协议。

  • tcp:实例运行于纯 TCP 模式,在客户端和服务器端之间将建立一个全双工的连接,且不会对7层报文做任何类型的检查;通常用于 SSL、SSH、SMTP 等应用;
  • http:实例运行于 HTTP 模式,客户端请求在转发至后端服务器之前将被深度分析,所有不与 RFC 格式兼容的请求都会被拒绝,此为默认模式;
  • health:实例工作于 health 模式,其对入站请求仅响应 “OK” 信息并关闭连接,且不会记录任何日志信息,此模式将用于响应外部组件的健康状态检查请求。目前来讲,此模式已经废弃,因为 tcp 或 http 模式中的 monitor 关键字可完成类似功能。

四、部署 Keepalived

Keepalived 沿用 keepalived 双机互为主备配置 这篇文章的部署与配置。

你可能感兴趣的:(redis)