---
title: "在 Kubernetes 环境中部署 OceanBase 集群 | OceanBase 文档中心"
description: 在 Kubernetes 环境中部署 OceanBase 集群 本文结合示例介绍如何通过 ob-operator 在 Kubernetes 环境中部署 OceanBase 集群。 前提条件 在开始之前，请确保您已满足以下条件： 您有可用的 Kubernetes 集群且至少有 9 个可用 CPU，33 GB 可用内存 …
---
切换语言

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

文档反馈![](https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*P8CuR4UJ_FkAAAAAAAAAAAAADiGDAQ/original) OceanBase 数据库分布式版 - V 4.0.0 社区版

# 在 Kubernetes 环境中部署 OceanBase 集群

更新时间：2026-05-27 16:46:40

[编辑](https://github.com/oceanbase/oceanbase-doc/edit/V4.0.0/zh-CN/400.installation-and-deployment/300.deploy-in-the-k8s-cluster.md)  

本文结合示例介绍如何通过 ob-operator 在 Kubernetes 环境中部署 OceanBase 集群。

## 前提条件

在开始之前，请确保您已满足以下条件：

- 您有可用的 Kubernetes 集群且至少有 9 个可用 CPU，33 GB 可用内存 和 360 GB 的可用存储空间。
 - ob-operator 依赖 cert-manager，请确保您已安装 cert-manager。cert-manager 的安装方法请参考对应的[安装文档](https://cert-manager.io/docs/installation/)。
 - 连接 OceanBase 集群时，您需已安装 MySQL 客户端或 OBClient。

#### 说明

本文以 ob-operator V2.2.0 为例介绍操作步骤，其他版本可能略有不同。不同版本的操作步骤可参见 [ob-operator 手册](https://www.oceanbase.com/docs/community-ob-operator-doc-1000000000659334) 中对应版本内容。

## 部署 ob-operator

使用 ob-operator 可以简化 OceanBase 数据库在 Kubernetes 中的部署和运维，您可通过以下两种方式部署 ob-operator。

### 使用 Helm 部署

您可执行以下命令部署 ob-operator。

```shell
helm repo add ob-operator https://oceanbase.github.io/ob-operator/
helm install ob-operator ob-operator/ob-operator --namespace=oceanbase-system --create-namespace --version=2.2.0

```

参数说明：

- `--namespace`：命名空间，可根据需要自定义，建议使用 `oceanbase-system`。
 - `--version`：ob-operator 版本号，建议使用最新的版本。

### 使用配置文件部署

您可执行如下命令使用配置文件部署 ob-operator。

```shell
kubectl apply -f https://raw.githubusercontent.com/oceanbase/ob-operator/2.2.0_release/deploy/operator.yaml

```

#### ob-operator 自定义

如果您需要对 ob-operator 进行自定义修改，可执行如下命令下载配置文件。

```shell
wget https://raw.githubusercontent.com/oceanbase/ob-operator/2.2.0_release/deploy/operator.yaml

```

根据自身需求修改配置文件后，执行如下命令进行自定义部署。配置文件中的配置项的详细含义可参见 [配置 ob-operator](https://www.oceanbase.com/docs/community-ob-operator-doc-1000000000659330)。

```shell
kubectl apply -f operator.yaml

```

## 部署 OceanBase 集群

1. 创建 PVC

   ob-operator 在部署 OceanBase 集群时需要创建 PVC 作为 OceanBase 集群的存储。本文中使用 local-path-provisioner 来管理 PVC。部署命令如下：

   ```shell
   kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/v0.0.24/deploy/local-path-storage.yaml

   ```

   更多信息可以参考 [local-path-provisioner](https://github.com/rancher/local-path-provisioner)。
 2. 创建 Namespace

   执行如下命令创建部署 OceanBase 集群使用的 Namespace。

   ```shell
   kubectl create namespace oceanbase

   ```

   创建后可执行 `kubectl get namespace oceanbase` 命令查看是否创建成功（状态为 `Active`）。
 3. 创建默认用户的 Secret

   创建 OceanBase 集群之前，您需要先执行如下命令创建好若干 Secret 来存储 OceanBase 集群中的特定用户。

   ```shell
   kubectl create secret -n oceanbase generic root-password --from-literal=password='<root_password>'
   kubectl create secret -n oceanbase generic proxyro-password --from-literal=password='<proxyro_password>'

   ```

   `<root_password>` 和 `<proxyro_password>` 分别表示 root@sys 用户和 proxyro@sys 用户的密码，您需修改为想要配置的密码。创建后可执行 `kubectl get secret -n oceanbase` 命令查看 Secret 资源是否创建成功。
 4. 定义 OceanBase 集群

   OceanBase 集群通过 yaml 配置文件进行定义，您可执行如下命令创建配置文件，此处以配置文件名为 `obcluster.yaml` 为例。

   ```shell
   vi obcluster.yaml

   ```

   配置文件内容示例如下。

   ```yaml
   apiVersion: oceanbase.oceanbase.com/v1alpha1
   kind: OBCluster
   metadata:
     name: obcluster
     namespace: oceanbase
   spec:
     clusterName: obcluster
     clusterId: 1
     userSecrets:
       root: root-password
       proxyro: proxyro-password
     topology:
       - zone: zone1
         replica: 1
       - zone: zone2
         replica: 1
       - zone: zone3
         replica: 1
     observer:
       image: oceanbase/oceanbase-cloud-native:4.0.0.0-103000022023011215
       resource:
         cpu: 2
         memory: 10Gi
       storage:
         dataStorage:
           storageClass: local-path
           size: 50Gi
         redoLogStorage:
           storageClass: local-path
           size: 50Gi
         logStorage:
           storageClass: local-path
           size: 20Gi
     monitor:
       image: oceanbase/obagent:4.2.1-100000092023101717
       resource:
         cpu: 1
         memory: 1Gi

   ```

   主要配置介绍如下，全面的配置项介绍可参见 [集群创建](https://www.oceanbase.com/docs/community-ob-operator-doc-1000000000659352)。

   | 配置项 | 是否必填 | 说明 |
   | --- | --- | --- |
   | metadata.name | 必填 | 集群名。K8s 中资源的名字。 |
   | metadata.namespace | 必填 | 集群所在的命名空间。 |
   | spec.clusterName | 必填 | OceanBase 集群名。 |
   | spec.clusterId | 必填 | OceanBase 集群 ID。 |
   | spec.userSecrets | 必填 | OceanBase 集群默认用户的 Secret。 |
   | spec.userSecrets.root | 必填 | OceanBase 集群 root@sys 用户的 Secret 名称，Secret 中需要包含 password 字段。 |
   | spec.userSecrets.proxyro | 选填 | OceanBase 集群 proxyro@sys 用户的 Secret 名称，Secret 中需要包含 password 字段。 |
   | spec.topology | 必填 | OceanBase 集群部署 topo 的定义，包含对各个 Zone 的定义。 |
   | spec.topology[i].zone | 必填 | OceanBase 集群中 Zone 的名字。 |
   | spec.topology[i].replica | 必填 | OceanBase 集群中 Zone 下包含的 OBServer 节点数。 |
   | spec.observer.image | 必填 | 所用的 OceanBase 数据库镜像。 |
   | spec.observer.resource | 必填 | OceanBase 集群中 OBServer 节点的资源配置。 |
   | spec.observer.resource.cpu | 必填 | OceanBase 集群中 OBServer 节点的 CPU 资源配置。建议配置为大于 2 的整数，小于 2 会引发系统异常。 |
   | spec.observer.resource.memory | 必填 | OceanBase 集群中 OBServer 节点的 Memory 资源配置。建议配置值大于 10Gi，小于 10Gi 会引发系统异常。 |
   | spec.observer.storage | 必填 | OceanBase 集群中 OBServer 节点的存储配置。 |
   | spec.observer.storage.dataStorage | 必填 | OceanBase 集群中 OBServer 节点的数据存储配置，建议至少配置为 `memory` 大小的 3 倍以上。 |
   | spec.observer.storage.redoLogStorage | 必填 | OceanBase 集群中 OBServer 节点的 Clog 存储配置，建议至少配置为 `memory` 大小的 3 倍以上。 |
   | spec.observer.storage.logStorage | 必填 | OceanBase 集群中 OBServer 节点的运行日志存储配置，建议大小至少配置为 10Gi 以上。 |
   | spec.observer.storage.*.storageClass | 必填 | 对于存储配置生效，用于定义创建 pvc 使用的 storageClass。 |
   | spec.observer.storage.*.size | 必填 | 对于存储配置生效，用于定义创建 pvc 的容量。 |
   | spec.monitor | 选填 | 监控配置，建议开启。ob-operator 会使用 OBAgent 来做监控数据采集，通过对接 Prometheus 可以实现对 OceanBase 集群的状态监控。 |
   | spec.monitor.image | 必填 | OBAgent 所使用的镜像。 |
   | spec.monitor.resource | 必填 | 监控容器使用的资源。 |
   | spec.monitor.resource.cpu | 必填 | 监控容器使用的 CPU 资源。 |
   | spec.monitor.resource.memory | 必填 | 监控容器使用的 memory 资源。 |
 5. 部署 OceanBase 集群

   ```shell
   kubectl apply -f obcluster.yaml

   ```

   执行如下命令查看 OceanBase 集群状态，当状态变成 `running` 时即表示 OceanBase 集群已成功部署并完成初始化。该过程一般需要几分钟时间，主要耗时过程为镜像拉取和集群初始化。

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

   ```

### 直连 OceanBase 集群

部署 OceanBase 集群后，您可参考本节步骤直连 OceanBase 集群。推荐部署 ODP 后通过 ODP 连接 OceanBase 集群，部署 ODP 的操作可参见下文 [部署 ODP](#部署%20ODP)。

1. 获取 OceanBase 集群 Pod 地址

   ```shell
   kubectl get pods -n oceanbase -l ref-obcluster=obcluster -o wide

   ```

   示例中的 `oceanbase` 对应 metadata.namespace 的值，`obcluster` 对应 metadata.name 的值，您需根据实际配置进行替换。

   输出如下：

   ```shell
   NAME                                READY   STATUS    RESTARTS   AGE    IP            NODE     NOMINATED NODE   READINESS GATES
   obcluster-1-zone2-c76d303299a9      2/2     Running   0          4m     10.10.10.1    node-x   <none>           <none>
   obcluster-1-zone3-2cdf3cd8a05e      2/2     Running   0          4m     10.10.10.2    node-x   <none>           <none>
   obcluster-1-zone1-94904330202f      2/2     Running   0          4m     10.10.10.3    node-x   <none>           <none>

   ```
 2. 连接 OceanBase 集群

   您可以使用任一节点的 IP 连接到 OceanBase 集群，对应的连接命令如下。

   ```shell
   mysql -h10.10.10.1 -P2881 -uroot@sys -p oceanbase -A -c

   ```

## 部署 ODP

ODP 通过 yaml 配置文件进行定义，详细部署步骤如下。

1. 创建 ODP 的配置文件

   ```shell
   vi obproxy.yaml

   ```

   此处以配置文件名为 `obproxy.yaml` 为例，文件内容如下：

   ```yaml
   apiVersion: v1
   kind: Service
   metadata:
     name: svc-obproxy
     namespace: oceanbase
   spec:
     type: ClusterIP
     selector:
       app: obproxy
     ports:
       - name: "sql"
         port: 2883
         targetPort: 2883
       - name: "prometheus"
         port: 2884
         targetPort: 2884

   ---
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: obproxy
     namespace: oceanbase
   spec:
     selector:
       matchLabels:
         app: obproxy
     replicas: 2
     template:
       metadata:
         labels:
           app: obproxy
       spec:
         containers:
           - name: obproxy
             image: oceanbase/obproxy-ce:4.0.0-5
             ports:
               - containerPort: 2883
                 name: "sql"
               - containerPort: 2884
                 name: "prometheus"
             env:
               - name: APP_NAME
                 value: helloworld
               - name: OB_CLUSTER
                 value: obcluster
               - name: RS_LIST
                 value: ${RS_LIST}
               - name: PROXYRO_PASSWORD
                 valueFrom:
                   secretKeyRef:
                     name: proxyro-password
                     key: password
             resources:
               limits:
                 memory: 2Gi
                 cpu: "1"
               requests:
                 memory: 200Mi
                 cpu: 200m

   ```

   主要环境变量配置说明：

      - `APP_NAME`：ODP 的应用名称。
      - `OB_CLUSTER`：ODP 连接的 OceanBase 集群名。
      - `RS_LIST`：OceanBase 集群的 rs_list，格式为 `${ip1}:${sql_port1};${ip2}:${sql_port2};${ip3}:${sql_port3}`，需要根据实际部署情况来进行替换。您可直连到 OceanBase 集群中执行 `SELECT GROUP_CONCAT(CONCAT(SVR_IP, ':', SQL_PORT) SEPARATOR ';') AS RS_LIST FROM oceanbase.DBA_OB_SERVERS;` 命令查看，直连 OceanBase 集群的操作可参见 [直连 OceanBase 集群](#直连%20OceanBase%20集群)。
      - `PROXYRO_PASSWORD`：需要配置 `name` 为上文创建的 proxyro@sys 用户的 Secret 名称，Secret 中需要包含 password 字段。
 2. 部署 ODP

   ```shell
   kubectl apply -f obproxy.yaml

   ```
 3. 验证是否部署成功

   执行如下命令查看 ODP 的 pod 状态。

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

   ```

   输出如下，有两个 obproxy 的 pod。

   ```shell
   oceanbase            obproxy-5cb8f4d975-pmr59                          1/1     Running   0          21s
   oceanbase            obproxy-5cb8f4d975-xlvjp                          1/1     Running   0          21s

   ```

   执行如下命令查看 ODP 的服务。

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

   ```

   输出如下：

   ```shell
   NAME          TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)             AGE
   svc-obproxy   ClusterIP   10.10.10.1   <none>        2883/TCP,2884/TCP   2m26s

   ```

### 通过 ODP 连接 OceanBase 集群

推荐通过 ODP 来连接 OceanBase 集群，在部署好 OceanBase 数据库和 ODP 后，您可通过以下步骤连接 OceanBase 集群。

1. 获取 ODP 的服务连接地址

   ```shell
   kubectl get svc ${servicename} -n ${namespace}

   # for example
   kubectl get svc svc-obproxy -n oceanbase

   ```

   输出如下：

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

   您可以通过 CLUSTER-IP 和 PORT 的方式进行连接，对应的连接命令如下。

   ```shell
   mysql -h10.10.10.1 -P2883 -uroot@sys#obcluster -p oceanbase -A -c

   ```

## 监控 OceanBase 集群

您可使用 OceanBase Dashboard 工具监控 OceanBase 集群。OceanBase Dashboard 是与 ob-operator 配套的白屏运维工具，目前最新版本为 V0.2.1，具有集群管理、租户管理、备份管理、性能监控和终端连接等功能，是在 K8s 环境中配合 ob-operator 监控 OceanBase 集群性能指标的首选工具。

### 部署 OceanBase Dashboard

推荐使用 Kubernetes 包管理器 Helm 安装 OceanBase Dashboard，确认已经安装了 Helm 之后，执行下面三条命令即可在默认命名空间安装 OceanBase Dashboard。

```shell
helm repo add ob-operator https://oceanbase.github.io/ob-operator/
helm repo update ob-operator
helm install oceanbase-dashboard ob-operator/oceanbase-dashboard --version=0.2.1

```

若要在其他命名空间安装 OceanBase Dashboard，可将最后一条安装命令替换为如下命令。

```shell
helm install oceanbase-dashboard ob-operator/oceanbase-dashboard --version=0.2.1 -n <namespace> --create-namespace

```

其中，`<namespace>` 需设置为待安装 OceanBase Dashboard 的目标命名空间，若目标命名空间不存在，可配置 `--create-namespace` 进行创建。若您的集群支持 LoadBalancer 服务，也可在安装时配置 `--set service.type=LoadBalancer`，指定创建 LoadBalancer 类型的服务。

执行命令后输出如下，则表示 OceanBase Dashboard 部署成功。

```shell
NAME: oceanbase-dashboard
LAST DEPLOYED: Wed May  8 11:04:49 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Welcome to OceanBase dashboard

1. After installing the dashboard chart, you can use `port-forward` to expose the dashboard outside like:

  > kubectl port-forward -n default services/oceanbase-dashboard-oceanbase-dashboard 18081:80 --address 0.0.0.0

then you can visit the dashboard on http://$YOUR_SERVER_IP:18081

2. Use the following command to get password for default admin user

  > echo $(kubectl get -n default secret oceanbase-dashboard-user-credentials -o jsonpath='{.data.admin}' | base64 -d)

Log in as default account:
Username: admin
Password: <Get from the above command>

```

部署 OceanBase Dashboard 后 K8s 集群需要一段时间用于拉取所需镜像，可通过如下命令查询 OceanBase Dashboard 是否完成安装。

```shell
kubectl get deployment oceanbase-dashboard-oceanbase-dashboard

```

输出如下，`READY` 列显示 `1/1` 即表示已经安装完成，可进行后续操作。

```shell
NAME                                      READY   UP-TO-DATE   AVAILABLE   AGE
oceanbase-dashboard-oceanbase-dashboard   1/1     1            1           2m10s

```

### 访问 OceanBase Dashboard

OceanBase Dashboard 默认创建的登录账号为 `admin`，通过输出中的第二条命令可获取默认账号的密码，命令如下。

```shell
echo $(kubectl get -n default secret oceanbase-dashboard-user-credentials -o jsonpath='{.data.admin}' | base64 -d)

```

您有如下三种方法访问 OceanBase Dashboard，可根据实际情况选择合适的访问方法。

- 通过 NodePort 访问：OceanBase Dashboard 默认创建 NodePort 类型的服务，您可通过 NodePort 访问 OceanBase Dashboard。
 - 通过 LoadBalancer 访问：若您的集群支持 LoadBalancer 服务，可通过 LoadBalancer 访问 OceanBase Dashboard。
 - 通过 Port Forward 临时访问：若您的集群节点端口不可访问，无法使用上述两种服务，可通过 Port Forward 临时访问 OceanBase Dashboard。

    通过 NodePort 访问   通过 LoadBalancer 访问   通过 Port Forward 临时访问

OceanBase Dashboard 默认创建 NodePort 类型的服务，您可通过执行如下命令获取服务在节点上暴露的端口。需要注意的是，服务的名称会根据您指定的 Helm Chart 名称变化而变化，具体可参考安装 OceanBase Dashboard 时输出中的第一条指令中的服务名称，本文示例中为 `oceanbase-dashboard-oceanbase-dashboard`。

```shell
kubectl get svc oceanbase-dashboard-oceanbase-dashboard

```

输出如下：

```shell
NAME                                      TYPE       CLUSTER-IP   EXTERNAL-IP  PORT(S)        AGE
oceanbase-dashboard-oceanbase-dashboard   NodePort   10.10.10.1   <none>       80:30176/TCP   13m

```

通过浏览器访问该 K8s 节点的 `PORT` 端口即可打开 OceanBase Dashboard 登录界面。服务的端口号由 K8s 动态分配，节点 IP 可通过执行 `kubectl get nodes -o wide` 命令查看。

您可在安装 OceanBase Dashboard 指定创建 LoadBalancer 类型的服务，也可在安装 OceanBase Dashboard 后，执行如下命令将服务类型修改为 LoadBalancer。

```shell
kubectl patch -n oceanbase-dashboard svc oceanbase-dashboard-oceanbase-dashboard --type=merge --patch='{"spec": {"type": "LoadBalancer"}}'

```

修改完成后集群将为 OceanBase Dashboard 服务分配外部 IP，通过该外部 IP 即可访问 OceanBase Dashboard 的页面。修改后等待一段时间再次查看服务信息会看到 External IP 字段已被赋值。

```

输出如下：

```shell
NAME                                      TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
oceanbase-dashboard-oceanbase-dashboard   LoadBalancer   10.10.10.1      10.10.10.2     80:18082/TCP   1d5h

```

在浏览器中输入 `http://10.10.10.2:18082` 即可访问 OceanBase Dashboard，这里仅以 `EXTERNAL-IP` 为 `10.10.10.2`，`PORT` 为 `18082` 为例，您需根据实际输出进行替换。

若您集群节点的端口不可访问，无法使用 NodePort 类型的服务暴露 OceanBase Dashboard，也不支持 LoadBalancer 的服务，可以使用 `kubectl port-forward` 命令将 OceanBase Dashboard 暴露到当前机器指定端口进行临时访问，例如执行下面的命令可将 OceanBase Dashboard 暴露在您当前机器（执行这条命令的机器）的 `18081` 端口。

```shell
kubectl port-forward -n default services/oceanbase-dashboard-oceanbase-dashboard 18081:80 --address 0.0.0.0

```

通过其他电脑的浏览器访问该机器的 `18081` 端口，可以打开登录界面。若执行上述命令的机器是您的个人电脑，打开浏览器访问 `http://127.0.0.1:18081` 即可访问到服务。

### 查看监控指标

访问到 OceanBase Dashboard 页面后，您可单击左侧的 **集群** 和 **租户**，选择查看集群或租户的监控信息。页面展示示例如下。

#### 说明

除了针对集群和租户的性能指标监控功能外，OceanBase Dashboard 还提供了集群管理、租户管理、备份管理和终端连接等方便用户运维 OceanBase 集群的功能。

集群监控指标如下：

![集群监控指标](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/observer-enterprise/V4.3.0/400.deploy/500.community/300.deploy-in-the-k8s-cluster-01.png)

租户监控指标如下：

![租户监控指标](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/observer-enterprise/V4.3.0/400.deploy/500.community/300.deploy-in-the-k8s-cluster-02.png)

## 相关文档

- [创建租户](https://www.oceanbase.com/docs/community-ob-operator-doc-1000000000659342)
 - [GitHub 文档](https://oceanbase.github.io/ob-operator/zh-Hans/)
 - [GitHub 代码仓库](https://github.com/oceanbase/ob-operator)

 上一篇 下一篇 ![有帮助](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) 咨询热线
