Skip to content

OceanBase OBProxy 部署 ​

OBProxy 简介 ​

OBProxy 是 OceanBase 数据库的代理服务,提供负载均衡、故障转移、读写分离等功能,是 OceanBase 集群的统一访问入口。

OBProxy 部署准备 ​

硬件要求 ​

资源类型最低配置推荐配置
CPU2 核4 核及以上
内存4 GB8 GB 及以上
磁盘50 GB100 GB 及以上
网络千兆网卡万兆网卡

软件要求 ​

软件名称版本要求
操作系统CentOS 7.6 及以上,Ubuntu 18.04 及以上
OceanBase 版本与 OBProxy 版本匹配
依赖库libaio, libssl, libcrypto

网络要求 ​

端口类型端口号用途
监听端口2883客户端访问端口
状态端口2884监控和管理端口
集群通信端口2881-2882与 OceanBase 集群通信

OBProxy 安装方式 ​

1. 二进制安装 ​

下载 OBProxy ​

bash
# 下载 OBProxy 二进制包
wget https://mirrors.aliyun.com/oceanbase/community/stable/el/7/x86_64/obproxy-4.1.0-1.el7.x86_64.rpm

安装 OBProxy ​

bash
# 安装 OBProxy
yum install -y obproxy-4.1.0-1.el7.x86_64.rpm

# 验证安装
obproxy --version

2. Docker 安装 ​

拉取 OBProxy 镜像 ​

bash
# 拉取 OBProxy 镜像
docker pull oceanbase/obproxy:4.1.0

运行 OBProxy 容器 ​

bash
# 运行 OBProxy 容器
docker run -d -p 2883:2883 -p 2884:2884 --name obproxy oceanbase/obproxy:4.1.0

3. 源码编译安装 ​

克隆 OBProxy 源码 ​

bash
# 克隆 OBProxy 源码
git clone https://github.com/oceanbase/obproxy.git
cd obproxy

编译 OBProxy ​

bash
# 安装依赖
yum install -y gcc gcc-c++ make cmake libaio-devel openssl-devel

# 编译
mkdir build && cd build
cmake ..
make -j4

# 安装
make install

OBProxy 配置 ​

1. 配置文件说明 ​

OBProxy 的配置文件位于 /etc/obproxy/obproxy.conf,主要配置项包括:

配置项描述默认值
obproxy_sys_passwordOBProxy 系统密码无
observer_sys_passwordOceanBase 集群系统密码无
listen_portOBProxy 监听端口2883
prometheus_listen_portPrometheus 监控端口2884
enable_cluster_checkout是否启用集群自动切换true
enable_strict_kernel_release是否严格检查内核版本false

2. 初始化配置 ​

bash
# 初始化 OBProxy 配置
obproxy -r "sys@cluster_name:192.168.1.101:2881;192.168.1.102:2881;192.168.1.103:2881" -u "root@sys" -p "password" --rs_list "192.168.1.101:2882;192.168.1.102:2882;192.168.1.103:2882" --obproxy_sys_password "obproxy_sys_password"

3. 修改配置 ​

bash
# 编辑 OBProxy 配置文件
vi /etc/obproxy/obproxy.conf

# 重启 OBProxy 使配置生效
systemctl restart obproxy

OBProxy 启动和管理 ​

1. 启动 OBProxy ​

bash
# 使用 systemd 启动 OBProxy
systemctl start obproxy

# 查看 OBProxy 状态
systemctl status obproxy

# 设置 OBProxy 开机自启
systemctl enable obproxy

2. 停止 OBProxy ​

bash
# 停止 OBProxy
systemctl stop obproxy

3. 重启 OBProxy ​

bash
# 重启 OBProxy
systemctl restart obproxy

4. 查看 OBProxy 日志 ​

bash
# 查看 OBProxy 日志
cat /var/log/obproxy/obproxy.log

# 实时查看 OBProxy 日志
tail -f /var/log/obproxy/obproxy.log

OBProxy 集群管理 ​

1. 添加 OBProxy 节点 ​

bash
# 安装并配置新的 OBProxy 节点
# 确保所有 OBProxy 节点使用相同的配置

2. 移除 OBProxy 节点 ​

bash
# 停止需要移除的 OBProxy 节点
systemctl stop obproxy

# 从负载均衡中移除该节点
# 更新 DNS 记录或负载均衡配置

3. OBProxy 版本升级 ​

bash
# 备份配置文件
cp /etc/obproxy/obproxy.conf /etc/obproxy/obproxy.conf.bak

# 安装新版本 OBProxy
yum install -y obproxy-4.1.1-1.el7.x86_64.rpm

# 恢复配置文件
cp /etc/obproxy/obproxy.conf.bak /etc/obproxy/obproxy.conf

# 重启 OBProxy
systemctl restart obproxy

OBProxy 监控和维护 ​

1. 查看 OBProxy 状态 ​

bash
# 使用 obproxyadmin 查看状态
obproxyadmin sys tenant list

# 查看集群列表
obproxyadmin cluster list

# 查看节点列表
obproxyadmin server list

2. Prometheus 监控 ​

OBProxy 内置了 Prometheus 监控端点,可以通过 http://obproxy_ip:2884/metrics 访问监控指标。

配置 Prometheus ​

yaml
# prometheus.yml
scrape_configs:
  - job_name: 'obproxy'
    static_configs:
      - targets: ['192.168.1.104:2884', '192.168.1.105:2884']

3. OBProxy 常见问题排查 ​

连接失败 ​

bash
# 检查 OBProxy 是否启动
systemctl status obproxy

# 检查端口是否开放
netstat -tuln | grep 2883

# 检查防火墙设置
firewall-cmd --list-ports

# 检查 OBProxy 日志
cat /var/log/obproxy/obproxy.log

性能问题 ​

bash
# 查看 OBProxy 监控指标
curl http://obproxy_ip:2884/metrics

# 分析 OBProxy 日志
cat /var/log/obproxy/obproxy.log | grep -i error

# 检查 OceanBase 集群状态
mysql -h oceanbase_ip -P 2881 -u root -p -e "SELECT * FROM __all_server;"

OBProxy 最佳实践 ​

1. 部署架构 ​

  • 建议至少部署 2 个 OBProxy 节点,实现高可用
  • 使用负载均衡(如 F5、LVS、Nginx)将流量分发到多个 OBProxy 节点
  • OBProxy 节点与 OceanBase 集群节点应部署在同一机房,减少网络延迟

2. 配置优化 ​

bash
# 优化 OBProxy 配置
cat >> /etc/obproxy/obproxy.conf << EOF
# 增加连接池大小
obproxy_connect_timeout_ms=10000
obproxy_read_timeout_ms=30000
obproxy_write_timeout_ms=60000

# 调整负载均衡策略
lb_policy=roundrobin

# 开启连接复用
enable_connection_reuse=true
max_connection_reuse_count=1000

# 开启慢查询日志
enable_slow_log=true
slow_query_time=1000
EOF

# 重启 OBProxy 使配置生效
systemctl restart obproxy

3. 安全配置 ​

bash
# 设置强密码
obproxyadmin sys password set -p new_password

# 配置防火墙规则
firewall-cmd --permanent --add-port=2883/tcp
firewall-cmd --permanent --add-port=2884/tcp
firewall-cmd --reload

# 限制访问 IP
cat >> /etc/obproxy/obproxy.conf << EOF
# 限制客户端访问 IP
allow_client_ips=192.168.1.0/24
EOF

常见问题(FAQ) ​

Q1: OBProxy 与 OceanBase 版本如何匹配? ​

A1: OBProxy 版本需要与 OceanBase 集群版本匹配,建议使用相同大版本号的 OBProxy 和 OceanBase。例如,OceanBase 4.1.0 集群应使用 OBProxy 4.1.0 版本。

Q2: 如何配置 OBProxy 连接多个 OceanBase 集群? ​

A2: 可以在 OBProxy 的初始化配置中指定多个集群,或者使用 obproxyadmin 命令添加集群:

bash
# 添加集群
obproxyadmin cluster add -c cluster_name -s "192.168.1.101:2881;192.168.1.102:2881"

Q3: OBProxy 如何实现读写分离? ​

A3: OBProxy 可以通过配置读写分离规则实现读写分离。可以在租户级别或 SQL 级别配置读写分离策略。

Q4: 如何查看 OBProxy 的连接数? ​

A4: 可以通过以下方式查看 OBProxy 的连接数:

bash
# 使用 obproxyadmin 查看连接数
obproxyadmin connection list

# 查看 Prometheus 监控指标
curl http://obproxy_ip:2884/metrics | grep obproxy_connection_count

Q5: OBProxy 出现故障如何处理? ​

A5: 如果单个 OBProxy 节点出现故障,负载均衡会自动将流量分发到其他健康的 OBProxy 节点。需要及时排查故障原因,修复后重新加入集群。

Q6: 如何优化 OBProxy 的性能? ​

A6: 可以通过以下方式优化 OBProxy 性能:

  • 增加 OBProxy 节点数量,实现负载均衡
  • 优化 OBProxy 配置参数,如连接超时、连接池大小等
  • 调整负载均衡策略,根据业务场景选择合适的策略
  • 确保 OBProxy 与 OceanBase 集群之间的网络延迟较低

Q7: OBProxy 支持哪些负载均衡策略? ​

A7: OBProxy 支持多种负载均衡策略,包括:

  • roundrobin:轮询
  • weight_roundrobin:加权轮询
  • least_conn:最少连接数
  • random:随机
  • ip_hash:IP 哈希

Q8: 如何配置 OBProxy 的慢查询日志? ​

A8: 可以通过修改 OBProxy 配置文件启用慢查询日志:

bash
# 启用慢查询日志
enable_slow_log=true
slow_query_time=1000

Q9: OBProxy 如何处理 OceanBase 节点故障? ​

A9: OBProxy 会定期检查 OceanBase 节点的状态,当发现节点故障时,会自动将流量路由到其他健康节点。同时,OBProxy 会参与 OceanBase 集群的故障转移过程,确保业务连续性。

Q10: 如何迁移 OBProxy 服务? ​

A10: 迁移 OBProxy 服务的步骤:

  1. 在新节点上安装并配置 OBProxy
  2. 确保新节点的配置与旧节点一致
  3. 将新节点加入负载均衡
  4. 逐步减少旧节点的流量
  5. 确认新节点运行正常后,停止旧节点服务