---
title: "集群创建 | OceanBase 文档中心"
description: 集群创建 本文介绍通过 ob-operator 创建 OceanBase 集群。 部署前准备 部署之前，您需要在 K8s 集群中部署好 ob-operator，请参考 部署 ob-operator , 并确保 K8s 集群有可用的 storage-class，推荐使用 local-path-provisioner 部…
---
切换语言

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

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

# 集群创建

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

[编辑](https://github.com/oceanbase/ob-operator-doc/edit/V2.2.0/zh-CN/500.ob-operator-user-guide/100.cluster-management-of-ob-operator/200.create-cluster.md)  

本文介绍通过 ob-operator 创建 OceanBase 集群。

## 部署前准备

部署之前，您需要在 K8s 集群中部署好 ob-operator，请参考 [部署 ob-operator](https://www.oceanbase.com/docs/community-ob-operator-doc-1000000000659336), 并确保 K8s 集群有可用的 storage-class，推荐使用 [local-path-provisioner](https://github.com/rancher/local-path-provisioner)

## 部署 OceanBase 数据库

### 创建 Namespace

创建部署 OceanBase 集群使用的 namespace。

```shell
kubectl create namespace oceanbase

```

### 创建默认用户的 Secret

创建 OceanBase 集群之前，您需要先创建好若干 secret 来存储 OceanBase 中的特定用户

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

```

### 定义 OceanBase 集群

OceanBase 集群可以通过 yaml 配置文件进行定义，您可参考如下配置文件作为基础按照实际进行需求进行修改。

```yaml
apiVersion: oceanbase.oceanbase.com/v1alpha1
kind: OBCluster
metadata:
  name: test
  namespace: oceanbase
  annotations:
    "oceanbase.oceanbase.com/independent-pvc-lifecycle": "true"
    # "oceanbase.oceanbase.com/mode": "standalone" 或 "service"
    # "oceanbase.oceanbase.com/single-pvc": "true"
spec:
  clusterName: obcluster
  clusterId: 1
  userSecrets:
    root: root-password
  topology:
    - zone: zone1
      replica: 1
      # nodeSelector:
      #   k1: v1
      # affinity:
      #   nodeAffinity:
      #   podAffinity:
      #   podAntiAffinity:
      # tolerations:
      #   - key: "obtopo"
      #     value: "zone"
      #     effect: "NoSchedule"
    - zone: zone2
      replica: 1
    - zone: zone3
      replica: 1
  observer:
    image: oceanbase/oceanbase-cloud-native:4.2.0.0-101000032023091319
    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.0-100000062023080210
    resource:
      cpu: 1
      memory: 1Gi

  # parameters:
  #   - name: system_memory
  #     value: 2G
  # backupVolume:
  #   volume:
  #     name: backup
  #     nfs:
  #       server: 1.1.1.1
  #       path: /opt/nfs
  #       readOnly: false

```

#### 配置项

配置项说明如下：

| 配置项 | 说明 |
| --- | --- |
| metadata.name | 集群名。K8s 中资源的名字；必填。 |
| metadata.namespace | 集群所在的命名空间；必填。 |
| spec.clusterName | OceanBase 集群名；必填。 |
| spec.clusterId | OceanBase 集群 ID；必填。 |
| spec.serviceAccount | 绑定到 OBServer Pod 上的 ServiceAccount；可选，默认为 `default`。 |
| spec.userSecrets | OceanBase 集群默认用户的 Secret；必填。 |
| spec.userSecrets.root | OceanBase 集群 root@sys 用户的 Secret 名称, Secret 中需要包含 password 字段；必填。 |
| spec.userSecrets.proxyro | OceanBase 集群 proxyro@sys 用户的 Secret 名称, Secret 中需要包含 password 字段；可选。 |
| spec.userSecrets.monitor | OceanBase 集群 monitor@sys 用户的 Secret 名称, Secret 中需要包含 password 字段；可选。 |
| spec.userSecrets.operator | OceanBase 集群 operator@sys 用户的 Secret 名称, Secret 中需要包含 password 字段；可选。 |
| spec.topology | OceanBase 集群部署 topo 的定义，包含对各个 zone 的定义；必填。 |
| spec.topology[i].zone | OceanBase Zone 的名字；必填。 |
| spec.topology[i].replica | OceanBase Zone 的 observer 数；必填。 |
| spec.topology[i].nodeSelector | 用于指定 OceanBase Zone 中的 observer 分布节点的选择，map 形式，需要配合节点的 label 使用；选填。 |
| spec.topology[i].affinity | 用于指定 OceanBase Zone 中的 observer 的节点亲和性, 节点亲和性可以参考 [K8s 文档](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity)；选填。 |
| spec.topology[i].tolerations | 用于指定 OceanBase Zone 中的 observer 的容忍度，节点容忍度可以参考 [K8s 文档](https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/)； 选填。 |
| spec.observer.image | OceanBase 中的 observer 的镜像； 必填。 |
| spec.observer.resource | OceanBase 中的 observer 的资源配置； 必填。 |
| spec.observer.resource.cpu | OceanBase 中的 observer 的 cpu 资源配置； 必填。 |
| spec.observer.resource.memory | OceanBase 中的 observer 的 memory 资源配置； 必填。 |
| spec.observer.storage | OceanBase 中的 observer 的存储配置； 必填。 |
| spec.observer.storage.dataStorage | OceanBase 中的 observer 的数据存储配置； 必填。 |
| spec.observer.storage.redoLogStorage | OceanBase 中的 observer 的 clog 存储配置； 必填。 |
| spec.observer.storage.logStorage | OceanBase 中的 observer 的运行日志存储配置； 必填。 |
| spec.observer.storage.*.storageClass | 对于存储配置生效，用于定义创建 pvc 使用的 storageClass； 必填。 |
| spec.observer.storage.*.size | 对于存储配置生效，用于定义创建 pvc 的容量； 必填。 |
| spec.monitor | 监控配置, 建议开启，ob-operator 会使用 obagent 来做监控数据采集，通过对接 prometheus 可以实现对 OceanBase 的状态监控； 选填。 |
| spec.monitor.image | 监控所使用的镜像； 必填。 |
| spec.monitor.resource | 监控容器使用的资源； 必填。 |
| spec.monitor.resource.cpu | 监控容器使用的 cpu 资源； 必填。 |
| spec.monitor.resource.memory | 监控容器使用的 memory 资源； 必填。 |
| spec.parameters | OceanBase 的自定义参数配置，对于集群全局生效； 选填。 |
| spec.parameters[i].name | 参数名； 必填。 |
| spec.parameters[i].value | 参数值； 必填。 |
| spec.backupVolume | OceanBase 备份使用的存储，如需开启备份功能，并且不是使用 OSS 进行备份的话，需要配置，一般配置 NFS Volume； 选填。 |

#### 注解

下面的表格展示了可选的注解，为书写简单，注解 `annotations` 表示其前面有个 `oceanbase.oceanbase.com/` 的前缀。

| 注解 | 说明 |
| --- | --- |
| `independent-pvc-lifecycle` | `true`: 可在删除集群后保留 PVC |
| `mode` | `standalone`: 使用 127.0.0.1 初始化单节点集群，无法与其他节点通信    `service`: 为每个 OBServer 创建单独的 K8s Service，用 Service 的 `ClusterIP` 作为 OBServer 的通讯 IP |
| `single-pvc` | `true`: 为每个 OBServer 的 Pod 创建并绑定一个整体的 PVC（默认创建三个） |

### 创建集群

配置文件保存好之后，使用如下命令在 K8s 中创建集群：

```shell
kubectl apply -f obcluster.yaml

```

一般创建集群需要 2 分钟左右，执行以下命令，查询集群状态，当集群状态变成 `running` 之后表示集群创建成功。

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

# desired output
NAME   STATUS    AGE
test   running   6m2s

```

## 连接集群

通过以下命令查找 observer 的 POD IP， POD 名的规则为 `{cluster_name}-{cluster_id}-{zone}-uuid`。

```shell
kubectl get pods -n oceanbase -o wide

```

通过以下命令连接：

```shell
mysql -h{POD_IP} -P2881 -uroot -proot_password oceanbase -A -c

```

## 后续操作

集群创建成功后，还需要创建租户才可以给业务使用，请参考[租户管理](https://www.oceanbase.com/docs/community-ob-operator-doc-1000000000659345)。

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