使用keydb 简化redis openresty 集成

openresty 支持redis 的链接管理以及api 集成能力是一个很不错的功能,基于keydb 的多活模式可以简化redis 的维护
同时结合haproxy 可以解决负载的问题

参考玩法

 

 


简单说明:
keydb 基于Active-Replication 模式,可以同时读写,对于openresty 集成redis 的,基于haproxy lb keydb ,为了方便haproxy 与openresty 部署在一起(类似sidecar 模式)

参考配置

  • haproxy
 
global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats timeout 30s
    user haproxy
    group haproxy
 
    # The lines below enable multithreading. This should correlate to number of threads available you want to use.
    nbthread 4
    cpu-map auto:1/1-4 0-3
 
    # 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
    maxconn 40000
 
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
 
frontend  main
    bind :6379
    maxconn 40000 
    mode tcp
    option tcplog
    default_backend  app
 
backend app
    balance first
    option tcp-check
    server keydb3 db:6379 maxconn 20000 check inter 1s
    server keydb2 db2:6379 maxconn 20000 check inter 1s
  • nginx 集成
user root; 
master_process off;
worker_processes 1;
events {
    worker_connections  65536;
}
error_log /opt/app.log debug;
http {
    include       mime.types;
    default_type  text/html;
    lua_code_cache on;
    lua_package_path '/opt/lua/?.lua;;';
    real_ip_header     X-Forwarded-For;
    resolver 127.0.0.11;
    server {
       listen 80;
       charset utf-8;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_buffering off;
       proxy_cache off;
       proxy_set_header Connection '';
       proxy_http_version 1.1;
       chunked_transfer_encoding off;
       default_type text/html;
        location /test {
            default_type text/html;
            content_by_lua_block {
                local redis = require "resty.redis"
                local red = redis:new()
                local ok, err = red:connect("haproxy", 6379)
                red:auth("dalong")
                local res, err = red:get("demoapp")
                if not res then
                    ngx.say("failed to getdemoapp: ", err)
                    return
                end
                ngx.say(res)
            }
        }
    }
}

说明

keydb 是一个很不错的redis 兼容替换方案,维护上可以简化不少部署问题,同时可以完整兼容redis,基于多线程也提供了不错的性能

参考资料

https://docs.keydb.dev/docs/haproxy
https://github.com/rongfengliang/keydb-nchan
https://docs.keydb.dev/docs/active-rep

posted on 2023-03-19 21:21  荣锋亮  阅读(93)  评论(0编辑  收藏  举报

导航