---
title: "OceanBase 以及应用在 K8s 中部署的示例 | OceanBase 文档中心"
description: OceanBase 以及应用在 K8s 中部署的示例 本文介绍在实际环境中 OceanBase 和应用程序在 K8s 中部署的典型架构，通过真实示例展示如何在 K8s 中部署 OceanBase 和相关组件以及应用程序。 前提条件 在您开始之前，确保您已经在 k8s 集群当中部署了， cert-manager 、 l…
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

文档反馈![](https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*_Ef7QIYUi3gAAAAAAAAAAAAADiGDAQ/original) ob-operatorV 2.3.0 社区版

# OceanBase 以及应用在 K8s 中部署的示例

更新时间：2026-04-13 16:46:35

[编辑](https://github.com/oceanbase/ob-operator-doc/edit/V2.3.0/zh-CN/900.appendix/100.example.md)  

本文介绍在实际环境中 OceanBase 和应用程序在 K8s 中部署的典型架构，通过真实示例展示如何在 K8s 中部署 OceanBase 和相关组件以及应用程序。

## 前提条件

在您开始之前，确保您已经在 k8s 集群当中部署了，[cert-manager](https://cert-manager.io/docs/) 、[local-path-provisioner](https://github.com/rancher/local-path-provisioner) 和 [ob-operator](https://github.com/oceanbase/ob-operator)。

本文涉及以下组件：

- [OceanBase Database](https://github.com/oceanbase/oceanbase)，OceanBase 数据库。
 - [ob-configserver](https://github.com/oceanbase/oceanbase/tree/master/tools/ob-configserver)，用于注册 OceanBase 数据库的 rootservice 地址信息。
 - [Oceanbase Database Proxy (ODP)](https://github.com/oceanbase/obproxy)，OceanBase 数据库代理程序。
 - [OceanBase Todo List](https://github.com/oceanbase/ob-operator/tree/master/distribution/oceanbase-todo)。一个较为简单的 web 应用程序，用于演示如何在 Kubernetes 集群中部署 web 应用程序并使用 OceanBase 集群作为后端数据库。
 - [Prometheus](https://prometheus.io/)，监控告警系统，通过 Prometheus 采集和计算 OceanBase 监控指标。
 - [Grafana](https://grafana.com/)，数据可视化系统。与 Prometheus 对接可以可视化展示 OceanBase 的监控数据。

## 部署 OceanBase 以及相关组件

### 部署前准备

创建 namespace。

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/namespace.yaml

```

使用以下命令查看创建的 namespace：

```shell
kubectl get namespace oceanbase

```

输出如下结果表示创建成功：

```shell
NAME        STATUS   AGE
oceanbase   Active   98s

```

创建集群和租户的 secret：

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/secret.yaml

```

通过以下命令查看创建的 secret：

```shell
kubectl get secret -n oceanbase

```

```shell
NAME                      TYPE                                  DATA   AGE
sc-metatenant-root        Opaque                                1      11s
sc-metatenant-standbyro   Opaque                                1      11s
sc-sys-monitor            Opaque                                1      11s
sc-sys-operator           Opaque                                1      11s
sc-sys-proxyro            Opaque                                1      11s
sc-sys-root               Opaque                                1      11s

```

### 部署 ob-configserver

ob-configserver 是提供 OceanBase rootservice 信息注册和查询的服务，元数据存储支持 MySQL 和 Sqlite3。这里为了简单使用了 Sqlite3。 使用如下命令部署 ob-configserver 以及创建对应的 service：

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/configserver.yaml

```

检查 pod 状态：

```shell
kubectl get pods -n oceanbase | grep ob-configserver

# desired output
ob-configserver-856bf5d865-dlwxr   1/1     Running   0          16s

```

检查 svc 状态：

```shell
kubectl get svc svc-ob-configserver -n oceanbase

# desired output
NAME                  TYPE       CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
svc-ob-configserver   NodePort   10.96.3.39   <none>        8080:30080/TCP   98s

```

### 部署 OceanBase 集群

部署 OceanBase 集群时增加环境变量，使用 ob-configserver 的服务地址作为系统参数 obconfig_url 的值，OceanBase 会将 rootservice 信息注册到 ob-configserver 中。 使用如下命令部署 OceanBase 集群：

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/obcluster.yaml

```

轮询使用如下命令检查 obcluster 状态，直到集群变成 running 状态。

```shell
kubectl get obclusters.oceanbase.oceanbase.com metadb -n oceanbase

# desired output
NAME     STATUS    AGE
metadb   running   3m21s

```

### 部署 ObProxy

ObProxy 支持使用 ob-configserver 或者直接指定 rs_list 的形式启动，为了能充分利用 obproxy 的能力，推荐使用 ob-configserver 的形式连接集群

使用如下命令部署 ObProxy 以及创建 service：

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/obproxy.yaml

```

查看 ObProxy 的 pod 状态，会有两个 obproxy 的 pod。

```shell
kubectl get pod -A | grep obproxy

# desired output
oceanbase            obproxy-5cb8f4d975-pmr59                          1/1     Running   0          21s
oceanbase            obproxy-5cb8f4d975-xlvjp                          1/1     Running   0          21s

```

查看 ObProxy 的 service：

```shell
kubectl get svc svc-obproxy -n oceanbase

# desired output
NAME          TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)             AGE
svc-obproxy   ClusterIP   10.96.2.46   <none>        2883/TCP,2884/TCP   2m26s

```

通过 ObProxy 服务地址连接 OceanBase 集群：

```shell
mysql -h${obproxy-service-address} -P2883 -uroot@sys#metadb -p

```

连接成功表示 obproxy 服务正常：

![connection](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/observer/V4.2.0/ob-operator-1.png)

如果提示 `cluster not exist` 表示 OceanBase 集群尚未把集群元信息注册到 ob-configserver，请稍作等待后重试连接。您可以使用 `curl "http://127.0.0.1:30080/services?Action=ObRootServiceInfo&ObCluster=metadb"` 查看注册结果。如果返回的结果 RsList 参数不为空，说明已经注册完成。

## 部署应用

### 创建租户

为业务单独创建一个租户，可以更好的做到资源的隔离，本文中为了简单，仅创建一个租户。

使用如下命令创建租户：

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/tenant.yaml

```

创建后轮询租户的资源状态, 当变成 running 时表示租户以及创建完成了

```shell
kubectl get obtenants.oceanbase.oceanbase.com metatenant -n oceanbase
NAME         STATUS    TENANTNAME   TENANTROLE   CLUSTERNAME   AGE
metatenant   running   metatenant   PRIMARY      metadb        106s

```

使用如下命令连接租户进行验证：

```shell
mysql -h${obproxy-service-address} -P2883 -uroot@metatenant#metadb -p

```

连接成功表示租户可以正常使用。

### 部署应用程序

[OceanBase Todo List](https://github.com/oceanbase/ob-operator/tree/master/distribution/oceanbase-todo)。一个较为简单的 web 应用程序，用于演示如何在 Kubernetes 集群中部署 web 应用程序并使用 OceanBase 集群作为后端数据库。

在启动应用之前，首先使用如下命令创建 database：

```shell
# Connect to the tenant
mysql -h${obproxy-service-address} -P2883 -uroot@metatenant#metadb -p

# Create dev database
create database dev;

```

使用如下命令部署应用：

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/oceanbase-todo.yaml

```

部署成功之后，可以通过如下命令进行查看部署的状态：

```shell
# Check the pod
kubectl get pods -n oceanbase | grep oceanbase-todo
oceanbase-todo-746c7ff78f-49dxv       1/1     Running   0             12m
oceanbase-todo-746c7ff78f-4875t       1/1     Running   0             12m

# Check service
kubectl get svc svc-oceanbase-todo -n oceanbase
NAME                  TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)           AGE
svc-oceanbase-todo    NodePort   10.43.39.231   <none>        20031:32080/TCP   12m

```

部署成功之后需要一段时间才可以正常提供服务，可以通过服务地址进行访问。

```shell
# Check service with the following command:
curl 'http://${service_ip}:${service_port}'

# Take Cluster IP 10.43.39.231 as an example
$ curl http://10.43.39.231:20031
# Desired output is as follows:
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/png" href="/logo.png" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>OceanBase Todo List</title>
    <script type="module" crossorigin src="/assets/index-DHbyEFSo.js"></script>
    <link rel="stylesheet" crossorigin href="/assets/index-B8po_uIp.css">
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

```

如果您想从互联网访问应用程序，可以使用 NodePort 类型的服务将应用的端口暴露在 K8s 节点的某端口上给外界访问。在本例中 NodePort 为 `32080`。您可以通过地址 `http://${node_ip}:32080` 访问应用程序。

## 部署监控系统

### 部署 prometheus

部署 OceanBase 集群的同时，在每个 pod 中创建了 obagent 的 sidecar 容器，可以提供 prometheus 协议的监控数据，同时也创建了一个 service，配合服务发现，可以做到自动识别 obagent 的地址去采集数据。

使用如下命令部署 prometheus：

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/prometheus.yaml

```

使用如下命令查看部署状态：

```shell
# check pod status
kubectl get pods -n oceanbase | grep prometheus
prometheus-576d7757b9-jsvfh        1/1     Running   0          3m17s

# check service status
kubectl get svc svc-prometheus -n oceanbase
NAME             TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
svc-prometheus   NodePort   10.96.1.212   <none>        9090:30090/TCP   3m45s

```

### 部署 grafana

grafana 可以使用 prometheus 作为数据源，进行 OceanBase 指标的展示。 使用如下命令部署 grafana：

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.3.0_release/example/webapp/grafana.yaml

```

```shell
# check pod status
kubectl get pods -n oceanbase | grep grafana
grafana-b7c6c6ccb-dkv57            1/1     Running   0          2m

# check service status
kubectl get svc svc-grafana -n oceanbase
NAME          TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
svc-grafana   NodePort   10.96.2.145   <none>        3000:30030/TCP   2m

```

打开浏览器访问服务地址，查看 OceanBase 的监控指标：

![grafana](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/observer/V4.2.0/ob-operator-2.png)

## 总结

本文介绍了典型的 OceanBase 部署模式，包括 OceanBase 及相关组件（oceanbase、obproxy 和 ob-configserver），应用以及监控系统的部署，其他应用的部署可以以此文档作为参考。

## 说明

本文中的配置文件均可在 [webapp 配置文件](https://github.com/oceanbase/ob-operator/tree/2.3.0_release/example/webapp) 目录中找到。

 上一篇 下一篇 ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
