codis

下载

  1. cd app/codis

安装

Codis Dashboard 集群管理(只能部署 1 个)

  • 集群管理工具,支持 codis-proxy、codis-server 的添加、删除,以及据迁移等操作。
  • 在集群状态发生改变时,codis-dashboard 维护集群下所有 codis-proxy 的状态的一致性。
  • 注意事项
    • 对于同一个业务集群而言,同一个时刻 codis-dashboard 只能有 0个或者1个;
    • 所有对集群的修改都必须通过 codis-dashboard 完成。
  1. 1. 生成/修改配置文件
  2. # 生成配置
  3. mkdir -p config
  4. ./bin/codis-dashboard --default-config > config/dashboard.toml
  5. # 编辑配置
  6. vim ./config/dashboard.toml
  7. #coordinator_name = "filesystem"
  8. #coordinator_addr = "/tmp/codis"
  9. # 外部存储类型,接受 zookeeper/etcd
  10. coordinator_name = "zookeeper"
  11. # 外部存储地址
  12. coordinator_addr = "zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181"
  13. #coordinator_auth = ""
  14. # 集群名称 Set Codis Product Name/Auth.
  15. product_name = "codis-infrastructure"
  16. # 集群密码,默认为空
  17. product_auth = "codis-123456"
  18. # RESTful API 端口
  19. # Set bind address for admin(rpc), tcp only.
  20. admin_addr = "0.0.0.0:18080"
  21. # Set arguments for data migration (only accept 'sync' & 'semi-async').
  22. migration_method = "semi-async"
  23. migration_parallel_slots = 100
  24. migration_async_maxbulks = 200
  25. migration_async_maxbytes = "32mb"
  26. migration_async_numkeys = 500
  27. migration_timeout = "30s"
  28. # Set configs for redis sentinel.
  29. sentinel_client_timeout = "10s"
  30. sentinel_quorum = 2
  31. sentinel_parallel_syncs = 1
  32. sentinel_down_after = "30s"
  33. sentinel_failover_timeout = "5m"
  34. sentinel_notification_script = ""
  35. sentinel_client_reconfig_script = ""
  36. 2. 查看命令
  37. $ ./codis-dashboard -h
  38. Usage:
  39. codis-dashboard [--ncpu=N] [--config=CONF] [--log=FILE] [--log-level=LEVEL] [--host-admin=ADDR]
  40. codis-dashboard --default-config
  41. codis-dashboard --version
  42. Options:
  43. --ncpu=N 最大使用 CPU 个数
  44. -c CONF, --config=CONF 指定启动配置文件
  45. -l FILE, --log=FILE 设置 log 输出文件
  46. --log-level=LEVEL 设置 log 输出等级:INFO,WARN,DEBUG,ERROR;默认INFO,推荐WARN
  47. 3. 启动
  48. # 创建日志目录
  49. sudo mkdir -p /opt/logs/codis
  50. sudo chown hadoop:hadoop /opt/logs/codis
  51. # 后台启动
  52. ./codis-dashboard --ncpu=1 --config=config/dashboard.toml \
  53. --log=/opt/logs/codis/dashboard.log --log-level=WARN &
  54. # 查看日志
  55. tail -f /logs/codis/dashboard.log.[日期]
  56. # 查看启动状态
  57. netstat -tunlp | grep 18080

Codis Proxy 代理服务

  • 客户端连接的 Redis 代理服务, 实现了 Redis 协议。
  1. 1. 生成/修改配置
  2. ./codis-proxy --default-config > config/proxy.toml
  3. vim config/proxy.toml
  4. ## 集群名称,参考 dashboard 参数说明
  5. # Set Codis Product Name/Auth.
  6. product_name = "codis-infrastructure"
  7. # 集群密码,默认为空
  8. product_auth = "codis-123456"
  9. # Set auth for client session
  10. # 1. product_auth is used for auth validation among codis-dashboard,
  11. # codis-proxy and codis-server.
  12. # 2. session_auth is different from product_auth, it requires clients
  13. # to issue AUTH <PASSWORD> before processing any other commands.
  14. session_auth = ""
  15. # RESTful API 端口, Set bind address for admin(rpc), tcp only.
  16. admin_addr = "0.0.0.0:11080"
  17. ## 设置代理端口类型,接受 tcp/tcp4/tcp6/unix/unixpacket
  18. # Set bind address for proxy, proto_type can be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".
  19. proto_type = "tcp4"
  20. # 设置代理绑定的端口地址或者路径
  21. proxy_addr = "0.0.0.0:19000"
  22. # Set jodis address & session timeout
  23. # 1. jodis_name is short for jodis_coordinator_name, only accept "zookeeper" & "etcd".
  24. # 2. jodis_addr is short for jodis_coordinator_addr
  25. # 3. jodis_auth is short for jodis_coordinator_auth, for zookeeper/etcd, "user:password" is accepted.
  26. # 4. proxy will be registered as node:
  27. # if jodis_compatible = true (not suggested):
  28. # /zk/codis/db_{PRODUCT_NAME}/proxy-{HASHID} (compatible with Codis2.0)
  29. # or else
  30. # /jodis/{PRODUCT_NAME}/proxy-{HASHID}
  31. ## 设置 jodis 地址和会话超时
  32. # Jodis 外部存储类型,接受 zookeeper/etcd
  33. jodis_name = "zookeeper"
  34. # Jodis 注册 zookeeper 地址
  35. jodis_addr = "zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181"
  36. jodis_auth = ""
  37. # Jodis 注册 session timeout 时间,单位 second
  38. jodis_timeout = "20s"
  39. jodis_compatible = false
  40. # Set datacenter of proxy.
  41. proxy_datacenter = "Codis Dashboard"
  42. # 设置最大会话数
  43. # Set max number of alive sessions.
  44. proxy_max_clients = 1000
  45. # 设置最大内存
  46. # Set max offheap memory size. (0 to disable)
  47. proxy_max_offheap_size = "10240mb"
  48. # 设置堆占位符以降低 GC 频率。
  49. # Set heap placeholder to reduce GC frequency.
  50. proxy_heap_placeholder = "256mb"
  51. # 代理将在预定义的时间间隔内 ping 后端 redis(并清除'MASTERDOWN'状态)。 (0表示禁用)
  52. # Proxy will ping backend redis (and clear 'MASTERDOWN' state) in a predefined interval. (0 to disable)
  53. backend_ping_period = "5s"
  54. # 设置后端recv缓冲区大小和超时。
  55. # Set backend recv buffer size & timeout.
  56. backend_recv_bufsize = "128kb"
  57. backend_recv_timeout = "30s"
  58. # 设置后端发送缓冲区和超时。
  59. # Set backend send buffer & timeout.
  60. backend_send_bufsize = "128kb"
  61. backend_send_timeout = "30s"
  62. # 设置后端管道缓冲区大小。
  63. # Set backend pipeline buffer size.
  64. backend_max_pipeline = 20480
  65. # 设置后端从不读取副本组,默认为 false
  66. # Set backend never read replica groups, default is false
  67. backend_primary_only = false
  68. # 为每个服务器设置后端并行连接
  69. # Set backend parallel connections per server
  70. backend_primary_parallel = 1
  71. backend_replica_parallel = 1
  72. # 设置后端 tcp keepalive 周期。(0禁用)
  73. # Set backend tcp keepalive period. (0 to disable)
  74. backend_keepalive_period = "75s"
  75. # 设置后台数据库数量
  76. # Set number of databases of backend.
  77. backend_number_databases = 16
  78. # 如果长时间没有来自客户端的请求,连接将被关闭。(0禁用)
  79. # If there is no request from client for a long time, the connection will be closed. (0 to disable)
  80. # Set session recv buffer size & timeout.
  81. session_recv_bufsize = "128kb"
  82. session_recv_timeout = "30m"
  83. # 设置会话发送缓冲区大小和超时。
  84. # Set session send buffer size & timeout.
  85. session_send_bufsize = "64kb"
  86. session_send_timeout = "30s"
  87. # 确保这高于每个管道请求的最大请求数量,否则您的客户机可能被阻塞。
  88. # Make sure this is higher than the max number of requests for each pipeline request, or your client may be blocked.
  89. # Set session pipeline buffer size.
  90. session_max_pipeline = 10000
  91. # 设置会话 tcp keepalive 周期。(0禁用)
  92. # Set session tcp keepalive period. (0 to disable)
  93. session_keepalive_period = "75s"
  94. # 将会话设置为对失败敏感。默认为false,代理将向客户端发送错误响应,而不是关闭套接字。
  95. # Set session to be sensitive to failures. Default is false, instead of closing socket, proxy will send an error response to client.
  96. session_break_on_failure = false
  97. # 设置 metrics server, proxy 将在预定义的时间内向指定的服务器报告json格式的度量。
  98. # Set metrics server (such as http://localhost:28000), proxy will report json formatted metrics to specified server in a predefined period.
  99. metrics_report_server = ""
  100. metrics_report_period = "1s"
  101. # Set influxdb server (such as http://localhost:8086), proxy will report metrics to influxdb.
  102. metrics_report_influxdb_server = ""
  103. metrics_report_influxdb_period = "1s"
  104. metrics_report_influxdb_username = ""
  105. metrics_report_influxdb_password = ""
  106. metrics_report_influxdb_database = ""
  107. # Set statsd server (such as localhost:8125), proxy will report metrics to statsd.
  108. metrics_report_statsd_server = ""
  109. metrics_report_statsd_period = "1s"
  110. metrics_report_statsd_prefix = ""
  111. 2. 查看命令
  112. $ ./codis-proxy -h
  113. Usage:
  114. codis-proxy [--ncpu=N] [--config=CONF] [--log=FILE] [--log-level=LEVEL] [--host-admin=ADDR] [--host-proxy=ADDR] [--ulimit=NLIMIT]
  115. codis-proxy --default-config
  116. codis-proxy --version
  117. Options:
  118. --ncpu=N 最大使用 CPU 个数
  119. -c CONF, --config=CONF 指定启动配置文件
  120. -l FILE, --log=FILE 设置 log 输出文件
  121. --log-level=LEVEL 设置 log 输出等级:INFO,WARN,DEBUG,ERROR;默认INFO,推荐WARN
  122. --ulimit=NLIMIT 检查 ulimit -n 的结果,确保运行时最大文件描述不少于 NLIMIT
  123. 3. 启动服务
  124. # 后台启动
  125. nohup ./codis-proxy --ncpu=4 --config=config/proxy.toml \
  126. --log=/opt/logs/codis/proxy.log --log-level=WARN &
  127. # 查看日志
  128. tail -f /opt/logs/codis/proxy.log.[日期]
  129. # 查看启动状态
  130. netstat -tunlp | grep 19000

Codis Server 基于 redis-3.2.8 分支开发

  • 基于 redis-3.2.8 分支开发。增加了额外的数据结构,以支持 slot 有关的操作以及数据迁移指令。
  • Codis Server 只负责发送 slave of 指令,具体行为取决于 redis 自身

  • redis.conf 解压获取配置文件 redis.conf

  1. 1. 编辑/修改配置文件
  2. vim redis.conf
  3. # 注释掉
  4. # bind 127.0.0.1
  5. # Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程,设置为no
  6. daemonize no
  7. # 关闭保护模式
  8. protected-mode no
  9. # 配置 redies 密码
  10. requirepass codis-123456
  11. 2. 查看命令
  12. ./codis-server --help
  13. Usage: ./redis-server [/path/to/redis.conf] [options]
  14. ./redis-server - (read config from stdin)
  15. ./redis-server -v or --version
  16. ./redis-server -h or --help
  17. ./redis-server --test-memory <megabytes>
  18. Examples:
  19. ./redis-server (run the server with default conf)
  20. ./redis-server /etc/redis/6379.conf
  21. ./redis-server --port 7777
  22. ./redis-server --port 7777 --slaveof 127.0.0.1 8888
  23. ./redis-server /etc/myredis.conf --loglevel verbose
  24. Sentinel mode:
  25. ./redis-server /etc/sentinel.conf --sentinel
  26. 3. 启动
  27. # 与启动普通 redis 的方法一致。 启动完成后,可以通过 codis-fe 提供的界面或者 codis-admin 命令行工具添加到集群中。
  28. ./codis-server config/redis.conf --port 6379 &
  29. # 查看端口
  30. netstat -tunlp | grep 6379

Codis FE 集群管理界面

  1. 1. 查看命令
  2. $ ./codis-fe -h
  3. Usage:
  4. codis-fe [--ncpu=N] [--log=FILE] [--log-level=LEVEL] [--assets-dir=PATH] (--dashboard-list=FILE|--zookeeper=ADDR|--etcd=ADDR|--filesystem=ROOT) --listen=ADDR
  5. codis-fe --version
  6. Options:
  7. --ncpu=N 最大使用 CPU 个数
  8. -d LIST, --dashboard-list=LIST 配置文件,能够自动刷新
  9. -l FILE, --log=FILE 设置 log 输出文件
  10. --log-level=LEVEL 设置 log 输出等级:INFO,WARN,DEBUG,ERROR;默认INFO,推荐WARN
  11. --listen=ADDR HTTP 服务端口
  12. 2. 启动服务
  13. # 后台启动
  14. ./codis-fe --ncpu=1 --log=/opt/logs/codis/fe.log --log-level=WARN \
  15. --zookeeper=zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181 --listen=0.0.0.0:21050 &
  16. # 查看日志
  17. tail -f /opt/logs/codis/fe.log
  18. # 查看端口
  19. netstat -tunlp | grep 21050
  20. # 查看 Codis Dashboard 信息
  21. ./codis-admin --dashboard-list --zookeeper=zookeeper-1:2181,zookeeper-2:2181,zookeeper-3:2181 | tee codis.json
  22. 3. 打开 Codis FE 服务
  23. http://host-name:21050

集群管理

  1. 1. 添加组 Group
  2. 输入组 ID(1) -> 点击(New Group)
  3. 2. 添加 Codis Server(Redis) 到指定组
  4. 输入 Codis Server 实际部署地址和端口(xxx.xxx.xxx.xxx:6379) -> to -> 到指定组(1) -> 点击(Add Server)
  5. PS: 一组 Codis-Group 一般为 3 Codis Server, 1 2 从.

codis - 图1

  • 分配 slot 到组中
  1. 新增的集群 slot 状态是 offline,因此我们需要对它进行初始化(将 1024 slot 分配到各个 group),而初始化最快的方法可通过 fe 提供的 rebalance all slots 按钮来做,如下图所示,点击此按钮,我们即快速完成了一个集群的搭建。

codis - 图2

  • 上线 Codis Proxy 节点
  1. Codis Proxy 节点启动后,处于 waiting 状态,监听 proxy_addr 地址,但是不会 accept 连接,添加到集群并完成集群状态的同步,才能改变状态为 online。添加的方法有以下两种
  2. 方法 1. 通过 codis-fe 添加:通过 Add Proxy 按钮,将 admin_addr 加入到集群中(建议)
  3. 输入 Codis Proxy 配置 admin_addr 参数的地址(xxx.xxx.xxx.xxx:18080 ) -> 点击(New Proxy 添加即可)
  4. PS: Codis Proxy 的代理地址为 admin_addr 参数. 默认 11080 端口
  5. 方法 2. 通过 codis-admin 命令行工具添加:
  6. codis-admin --dashboard=127.0.0.1:18080 --create-proxy -x 127.0.0.1:11080
  7. # Codis Dashboard 地址
  8. 127.0.0.1:18080
  9. # Codis Proxy 代理的地址, 用于添加 Codis Proxy 代理. RESTful API 端口
  10. 127.0.0.1:11080
  11. 添加过程中,dashboard 会完成如下一系列动作
  12. 1. 获取 proxy 信息,对集群 name 以及 auth 进行验证,并将其信息写入到外部存储中;
  13. 2. 同步 slots 状态;
  14. 3. 标记 proxy 状态为 online,此后 proxy 开始 accept 连接并开始提供服务;

codis - 图3

  • HA
  1. 对下层的 redis 实例来说,当一个 group master 挂掉的时候,应该让管理员清楚,并手动的操作,因为这涉及到了数据一致性等问题(redis的主从同步是最终一致性的)。因此 codis 不会自动的将某个 slave 升级成 master。关于外部 codis-ha 工具(具体可以参考之前的章节),这是一个通过 codis-dashboard 开放的 RESTful API 实现自动切换主从的工具。该工具会在检测到 master 挂掉的时候主动应用主从切换策略,提升单个 slave 成为新的 master
  2. 需要注意,codis 将其中一个 slave 升级为 master 时,该组内其他 slave 实例是不会自动改变状态的,这些 slave 仍将试图从旧的 master 上同步数据,因而会导致组内新的 master 和其他 slave 之间的数据不一致。因此当出现主从切换时,需要管理员手动创建新的 sync action 来完成新 master slave 之间的数据同步(codis-ha 不提供自动操作的工具,因为这样太不安全了)。