Workstation Logo
产品
AI 实验室OpenAI代理Claude 代理Grok BotWorkstation CRM (WSL CRM)营销全部产品
AI 解决方案
AI 工作站AI SME Packages私有 AIGPU 集群边缘 AI企业 AI 实验室按行业分类的 AI
服务
Platform ModernisationDigital EngineeringData Foundations & AIAutonomous OperationsAI 咨询DevOps 自动化网络安全软件开发智能体构建MLOps 搭建
关于我们
合作伙伴客户案例
文章
文档
WSL ProxyRing PromoterWSL VaultJobshoutSysOps 24/7
博客
联系我们Login
Workstation

面向现代企业的 AI 工作站、AI 多智能体软件、GPU 基础设施和智能代理解决方案。

联系我们

AI 解决方案

AI 工作站AI SME Packages私有 AIGPU 集群边缘 AI企业 AI 实验室按行业分类的 AI

产品

全部产品WSL CRM 与 ERP营销OpenAI代理WSL ProxyRing PromoterWSL VaultJobshoutSysOps 24/7

公司

关于我们为什么选择Workstation合作伙伴客户案例价格联系

资源

文章文档博客搜索网站地图
英国办公室
77-79 Marlowes, Hemel Hempstead HP1 1LF路线指引 — 从 M25 外环伦敦 20 号出口驶出公司编号: 11641870周一至周五:上午 9:00 - 下午 6:00 GMT
+44 7515 356 146
比利时办公室
Workstation SRL, Rue Vanderkindere 34, 1180 Uccle, BrusselsBE 0751.518.683周一至周五:上午 9:00 - 下午 6:00 CET
+32 492 45 67 46
印度办公室
#159 Sector 9, Pocket 1, DDA Flats, 110077 Dwarka, New Delhi
+91 98881 98841

© 2026 Workstation AI。保留所有权利。

隐私Cookie服务条款网站地图

Loading blog...

Home / Blog
DatabaseKubernetesDevOpsBackend

Couchbase 在生产中的高可用性:XDCR、Kubernetes Operator 和多区域部署

具有 XDCR、自主操作员和多云部署的企业 Couchbase HA

Balinder Walia2026年4月12日24 min read

简介:为什么选择 Couchbase 实现高可用性生产工作负载

Couchbase 服务器是一款分布式、多模型 NoSQL 数据库,专为需要任何规模的一致低延迟性能的交互式应用程序而设计。与事后才添加分布式功能的数据库不同,Couchbase 从一开始就围绕无共享、点对点拓扑进行架构设计,其中每个节点都是平等的,并且使用称为vBuckets的确定性哈希机制在集群中自动对数据进行分片。这种架构选择消除了数据层的单点故障,并无需更改应用程序即可实现水平扩展。

Couchbase 在高可用性领域中脱颖而出的是其跨数据中心复制 (XDCR)— 一种内置异步复制引擎,可在地理分布的集群之间连续传输突变。结合自动故障转移、机架/区域感知和丰富的集成服务(数据、索引、查询、搜索、分析和事件),Couchbase 提供了一个统一的平台,可以充当现代应用程序的操作数据库和分析引擎。

在本综合指南中,我们将探讨在高可用性生产环境中运行 Couchbase Server 的各个方面:使 HA 成为可能的内部架构、用于多区域部署的 XDCR 配置、Kubernetes 的 Couchbase Autonomous Operator、AWS EKS、Azure AKS 和 GCP GKE 的云特定部署模式、使用 Rancher 和 Longhorn 的裸机 k3s 部署、备份和恢复策略、N1QL 查询调优、安全强化、监控和带有用于边缘部署的同步网关的 Couchbase Mobile。最后,您将拥有在任何基础设施上部署和操作生产级 Couchbase 集群的实用知识。

Couchbase 服务器架构:服务、vBuckets 和自动分片

在部署高可用性之前,了解 Couchbase 的内部架构至关重要。 Couchbase 使用多维扩展 (MDS)架构,其中不同的服务可以跨集群节点独立部署和扩展。这使运营商能够对资源分配和性能隔离进行细粒度控制。

六大核心服务

Couchbase 服务器提供六种集成服务,每种服务处理不同的工作负载类型:

  • 数据服务 (KV)— 基于内存优先架构构建的核心键值引擎。它处理 CRUD 操作、管理 vBucket 分发并充当持久层。数据存储在内存(托管缓存)中并异步持久化到磁盘。该服务必须在每个集群中的至少一个节点上运行。
  • 索引服务 (GSI)— 维护支持 N1QL 查询的全局二级索引。索引与数据分开存储,允许独立扩展。支持标准和内存优化的索引存储模式。
  • 查询服务 (N1QL)— 针对集群执行 N1QL(对于 JSON 为 SQL++)查询。无状态设计,使其易于水平扩展。与数据和索引服务协调来计划和执行查询。
  • 搜索服务 (FTS)— 提供由 Bleve 搜索引擎支持的全文搜索功能。支持模糊匹配、地理空间查询、分面搜索和自定义分析器。索引在搜索节点之间进行分区和复制。
  • 分析服务 (CBAS)— 使用基于 Apache Asterix 的并行处理引擎运行复杂的分析查询。对自己的数据副本进行操作,确保分析工作负载不会影响操作延迟。
  • 事件服务— 执行服务器端 JavaScript 功能以响应数据突变。无需外部基础设施即可实现实时数据丰富、转换、级联删除和集成触发器。
Couchbase 集群架构和服务分布Couchbase 集群(启用自动故障转移,3 秒检测)节点 1 (10.0.1.10)数据服务 (KV)vBuckets 0-341 | 256MB 缓存索引服务 (GSI)查询服务 (N1QL)vBucket 映射(活动)0-341 活跃 | 342-682 复制品自动保留到磁盘(异步)节点 2 (10.0.1.11)数据服务 (KV)vBuckets 342-682 | 256MB 缓存搜索服务 (FTS)分析服务 (CBAS)vBucket 映射(活动)342-682 有源 | 683-1023 复制品DCP 流至 Analytics节点 3 (10.0.1.12)数据服务 (KV)vBuckets 683-1023 | 256MB 缓存事件服务查询服务 (N1QL)vBucket 映射(活动)683-1023 活跃 | 0-341复制品事件 DCP 消费类自动故障转移管理器心跳监测 | 3秒检测| vBucket促销 |最多 3 个连续故障转移数据 (KV)索引/查询搜索/分析/事件集群内复制心跳

vBucket 分配和自动分片

Couchbase 使用1024 vBuckets(虚拟存储桶)在集群中分发数据。每个文档都使用文档密钥模 1024 的 CRC32 哈希映射到 vBucket。集群映射(由每个节点维护并由每个 SDK 客户端缓存)将每个 vBucket 映射到特定节点。这种确定性映射意味着客户端始终准确地知道哪个节点保存任何给定文档,从而实现亚毫秒级延迟的单跳读取和写入。

当添加或删除节点时,Couchbase 通过称为重新平衡的过程自动重新分配 vBuckets。在重新平衡期间,集群在节点之间移动 vBucket,同时保持完全运行。 重新平衡经过精心安排,以始终维持配置的副本数量,并且客户端通过集群映射更新无缝重定向到新的 vBucket 位置。

集群内复制和自动故障转移

每个 vBucket 都有一个活动副本和分布在不同节点上的最多三个副本副本。当客户端写入文档时,写入会转到负责节点上的活动 vBucket。然后,数据服务通过内部DCP(数据库更改协议)流将突变复制到其他节点上的副本 vBuckets。默认情况下,Couchbase 配置一个副本,但对于生产 HA 部署,建议配置两个副本:

# Configure bucket with 2 replicas via CLI
/opt/couchbase/bin/couchbase-cli bucket-create \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --bucket production-data \
  --bucket-type couchbase \
  --bucket-ramsize 4096 \
  --bucket-replica 2 \
  --bucket-priority high \
  --bucket-eviction-policy valueOnly \
  --enable-flush 0 \
  --compression-mode active \
  --max-ttl 0 \
  --durability-min-level majorityAndPersistActive

自动故障转移是 Couchbase 自动检测节点故障并从中恢复的机制。当节点变得无响应时,集群编排器会等待可配置的超时(最少 5 秒,建议生产环境为 30 秒),然后将幸存节点上的副本 vBuckets 提升为活动状态。这种情况无需任何应用程序端干预即可发生 - SDK 客户端收到更新的集群映射并立即将请求路由到新的活动 vBuckets。

# Configure auto-failover settings
/opt/couchbase/bin/couchbase-cli setting-autofailover \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --enable-auto-failover 1 \
  --auto-failover-timeout 30 \
  --max-failovers 3 \
  --enable-failover-of-server-groups 1 \
  --failover-on-data-disk-issues 1 \
  --failover-data-disk-period 120 \
  --can-abort-rebalance 1

关键自动故障转移参数:

  • 自动故障转移超时— 触发故障转移之前等待的秒数。较低的值会减少停机时间,但会增加误报风险。 30 秒是建议的生产设置。
  • 最大故障转移数— 需要手动干预之前连续自动故障转移的最大数量。对于 5 节点集群,设置为 3(以维持仲裁)。
  • 启用服务器组故障转移— 启用整个服务器组(机架/区域)的故障转移,这对于区域感知部署至关重要。
  • 数据磁盘问题故障转移— 当数据服务检测到永久性磁盘 I/O 错误时触发故障转移。

XDCR:跨数据中心复制

XDCR是Couchbase的旗舰多区域复制技术。与传统 RDBMS 系统中的数据库级复制不同,XDCR 在存储桶级别上运行,并在独立的 Couchbase 集群之间传输单个文档突变。每个集群都保持完全自治 - 它可以独立接受读取和写入,这使得 XDCR 非常适合用户需要从任何地理位置进行低延迟访问的主动-主动多区域部署。

单向与双向 XDCR

单向 XDCR将突变从源簇单向复制到目标簇。这适用于灾难恢复场景、远程区域的只读副本或将数据从操作集群馈送到分析集群。

双向 XDCR在两个集群之间创建双向复制链接,从而实现两个集群都接受写入的主动-主动部署。这是最强大的配置,但需要仔细的冲突解决规划。

XDCR 多区域双向复制美国东部 (AWS EKS)集群:cb-us-east5 个节点 |数据+索引+查询桶:应用程序桶:用户冲突解决:LWW(时间戳)XDCR:压缩开启 | TLS 1.3写入:~15k 操作/秒欧盟-西部 (Azure AKS)集群:cb-eu-west5 个节点 |数据+索引+查询桶:应用程序桶:用户冲突解决:LWW(时间戳)XDCR:压缩开启 | TLS 1.3写入:~12k 操作/秒AP-南 (GCP GKE)集群:cb-ap-south3 个节点 |数据+索引+查询铲斗:应用程序桶:用户冲突解决:LWW(时间戳)XDCR:压缩开启 | TLS 1.3写入:~8k 操作/秒XDCRXDCRXDCR(双向)冲突解决策略LWW(最后写入以时间戳为准)|序列号 |通过合并功能自定义冲突解决(企业)XDCR 正向XDCR 反向跨区域链接AWSAzureGCP

设置 XDCR 复制

配置 XDCR 涉及创建远程集群引用,然后在存储桶级别定义复制链接。以下是完整双向设置的 CLI 命令和 REST API 调用:

# Step 1: Create remote cluster reference on the US-EAST cluster
/opt/couchbase/bin/couchbase-cli xdcr-setup \
  --cluster cb-us-east.example.com:8091 \
  --username Administrator \
  --password password \
  --create \
  --xdcr-cluster-name eu-west-cluster \
  --xdcr-hostname cb-eu-west.example.com:8091 \
  --xdcr-username Administrator \
  --xdcr-password password \
  --xdcr-demand-encryption 1 \
  --xdcr-encryption-type full \
  --xdcr-certificate /path/to/eu-west-ca.pem

# Step 2: Create replication from US-EAST to EU-WEST for the 'app' bucket
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
  --cluster cb-us-east.example.com:8091 \
  --username Administrator \
  --password password \
  --create \
  --xdcr-cluster-name eu-west-cluster \
  --xdcr-from-bucket app \
  --xdcr-to-bucket app \
  --xdcr-replication-mode xmem \
  --enable-compression 1 \
  --filter-expression "" \
  --priority high \
  --network-usage-limit 0

# Step 3: Create the reverse replication on EU-WEST cluster (bidirectional)
/opt/couchbase/bin/couchbase-cli xdcr-setup \
  --cluster cb-eu-west.example.com:8091 \
  --username Administrator \
  --password password \
  --create \
  --xdcr-cluster-name us-east-cluster \
  --xdcr-hostname cb-us-east.example.com:8091 \
  --xdcr-username Administrator \
  --xdcr-password password \
  --xdcr-demand-encryption 1 \
  --xdcr-encryption-type full \
  --xdcr-certificate /path/to/us-east-ca.pem

/opt/couchbase/bin/couchbase-cli xdcr-replicate \
  --cluster cb-eu-west.example.com:8091 \
  --username Administrator \
  --password password \
  --create \
  --xdcr-cluster-name us-east-cluster \
  --xdcr-from-bucket app \
  --xdcr-to-bucket app \
  --xdcr-replication-mode xmem \
  --enable-compression 1

冲突解决策略

在双向 XDCR 中,同一文档可以在不同集群上同时修改,从而产生冲突。 Couchbase提供多种冲突解决策略:

  • 基于时间戳(LWW — 最后写入获胜)— 具有最新时间戳的突变获胜。这是默认设置,适用于大多数用例。需要所有集群之间的 NTP 同步(偏差在 5 秒以内)。在创建存储桶时设置,以后无法更改。
  • 基于序列号的— 使用内部序列号(版本 ID)来确定获胜者。修订次数较高的突变获胜。当时间戳同步不可靠时很有用。
  • 自定义冲突解决(企业版)— Couchbase 企业版支持执行服务器端 JavaScript 的自定义合并功能,以解决与应用程序特定逻辑的冲突。 这支持合并来自不同区域的购物车项目或应用特定于域的冲突解决规则等场景。
# Create a bucket with timestamp-based conflict resolution
curl -X POST http://localhost:8091/pools/default/buckets \
  -u Administrator:password \
  -d name=app \
  -d ramQuota=4096 \
  -d replicaNumber=2 \
  -d bucketType=couchbase \
  -d conflictResolutionType=lww \
  -d compressionMode=active \
  -d durabilityMinLevel=majorityAndPersistActive

# XDCR advanced settings via REST API
curl -X POST http://localhost:8091/settings/replications/<replication-id> \
  -u Administrator:password \
  -d optimisticReplicationThreshold=256 \
  -d sourceNozzlePerNode=4 \
  -d targetNozzlePerNode=4 \
  -d checkpointInterval=600 \
  -d batchCount=500 \
  -d batchSize=2048 \
  -d failureRestartInterval=10 \
  -d docBatchSizeKb=2048 \
  -d networkUsageLimit=0 \
  -d priority=High

XDCR 滤波

XDCR 支持过滤,因此您只能复制文档的子集。过滤器对文档键使用正则表达式,还可以根据文档过期或删除进行过滤:

# Replicate only documents with keys starting with 'user::'
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --create \
  --xdcr-cluster-name remote-cluster \
  --xdcr-from-bucket app \
  --xdcr-to-bucket app-users \
  --filter-expression "^user::" \
  --filter-skip-restream 0

# Replicate documents matching a complex pattern
# (orders from 2026 with specific type)
--filter-expression "REGEXP_CONTAINS(META().id, '^order::2026') AND type='premium'"

Couchbase 用于 Kubernetes 的自主操作员

Couchbase 自主操作器 (CAO)是企业级 Kubernetes 操作器,可自动执行 Couchbase 服务器集群的部署、管理、扩展和恢复。与简单的 StatefulSet 部署不同,Autonomous Operator 了解 Couchbase 的内部拓扑 - 它管理重新平衡操作、协调滚动升级、处理服务器组感知,并与 Kubernetes 调度原语集成以确保 Couchbase Pod 的最佳放置。

Couchbase Kubernetes 上的自主操作员Couchbase 自主操作员手表 CouchbaseCluster CRD |协调Couchbase集群 CRD所需状态声明TLS 的秘密通过证书管理器自动轮换服务器组:a 区(机架 1)StatefulSet:cb-prod-data-zaCB-PROD-0000数据 + 索引4 CPU | 16GiPVC:100Gi gp3CB-PROD-0001查询+搜索4 CPU | 8GiPVC:50Gi gp3PVC (EBS gp3)ClusterIP Svc反关联性:仅限 a 区节点服务器组:区域 b(机架 2)StatefulSet:cb-prod-data-zbCB-PROD-0002数据 + 索引4 CPU | 16GiPVC:100Gi gp3CB-PROD-0003查询+搜索4 CPU | 8GiPVC:50Gi gp3PVC (EBS gp3)ClusterIP Svc反关联性:仅限 zone-b 节点服务器组:zone-c(机架 3)StatefulSet:cb-prod-data-zcCB-PROD-0004数据 + 分析8 CPU | 32GiPVC:200Gi gp3CB-PROD-0005赛事2 CPU | 8GiPVC:50Gi gp3PVC (EBS gp3)ClusterIP Svc反关联性:仅限 zone-c 节点公开服务节点端口 8091 | SDK 11210 | N1QL 8093准入控制器验证 CRD | Webhook TLS备份控制器cbbackupmgr | S3/GCS/Azure操作员数据/索引/查询分析/事件PVC 存储Pod 调度安全/验证

CouchbaseCluster CRD 规格

CouchbaseCluster CRD 是声明 Couchbase 部署所需状态的中央配置。自治操作员将其协调为 StatefulSets、Services、PVC、Secrets 和 RBAC 资源。以下是可投入生产的 CRD:

apiVersion: couchbase.com/v2
kind: CouchbaseCluster
metadata:
  name: cb-production
  namespace: couchbase
spec:
  image: couchbase/server:7.6.1-enterprise
  antiAffinity: true
  platform: aws
  cluster:
    autoFailoverTimeout: 30s
    autoFailoverMaxCount: 3
    autoFailoverOnDataDiskIssues: true
    autoFailoverOnDataDiskIssuesTimePeriod: 120s
    autoFailoverServerGroup: true
    clusterName: cb-production
    dataServiceMemoryQuota: 8Gi
    indexServiceMemoryQuota: 4Gi
    searchServiceMemoryQuota: 2Gi
    analyticsServiceMemoryQuota: 4Gi
    eventingServiceMemoryQuota: 2Gi
    indexStorageSetting: memory_optimized
    autoCompaction:
      databaseFragmentationThreshold:
        percent: 30
        size: 1Gi
      viewFragmentationThreshold:
        percent: 30
        size: 1Gi
      parallelCompaction: false
      timeWindow:
        start: "02:00"
        end: "06:00"
        abortCompactionOutsideWindow: true
  security:
    adminSecret: cb-admin-credentials
    rbac:
      managed: true
      selector:
        matchLabels:
          cluster: cb-production
    ldap:
      hosts:
      - ldap.example.com
      port: 636
      encryption: TLS
  networking:
    tls:
      static:
        serverSecret: couchbase-server-tls
        operatorSecret: couchbase-operator-tls
    exposeAdminConsole: true
    adminConsoleServices:
    - data
    adminConsoleServiceType: NodePort
    exposedFeatures:
    - client
    - xdcr
    exposedFeatureServiceType: NodePort
  buckets:
    managed: true
    selector:
      matchLabels:
        cluster: cb-production
  servers:
  - name: data-zone-a
    size: 2
    services:
    - data
    - index
    serverGroups:
    - zone-a
    pod:
      metadata:
        labels:
          couchbase-service: data-index
        annotations:
          prometheus.io/scrape: "true"
          prometheus.io/port: "9091"
      spec:
        nodeSelector:
          topology.kubernetes.io/zone: us-east-1a
        tolerations:
        - key: couchbase
          operator: Equal
          value: "true"
          effect: NoSchedule
        resources:
          requests:
            cpu: "4"
            memory: 16Gi
          limits:
            cpu: "8"
            memory: 20Gi
    volumeMounts:
      default: couchbase-data
      data: couchbase-data
      index: couchbase-index
  - name: data-zone-b
    size: 2
    services:
    - data
    - index
    serverGroups:
    - zone-b
    pod:
      spec:
        nodeSelector:
          topology.kubernetes.io/zone: us-east-1b
        tolerations:
        - key: couchbase
          operator: Equal
          value: "true"
          effect: NoSchedule
        resources:
          requests:
            cpu: "4"
            memory: 16Gi
          limits:
            cpu: "8"
            memory: 20Gi
    volumeMounts:
      default: couchbase-data
      data: couchbase-data
      index: couchbase-index
  - name: query-search
    size: 2
    services:
    - query
    - search
    serverGroups:
    - zone-a
    - zone-b
    pod:
      spec:
        resources:
          requests:
            cpu: "4"
            memory: 8Gi
          limits:
            cpu: "8"
            memory: 12Gi
    volumeMounts:
      default: couchbase-default
  - name: analytics-eventing
    size: 2
    services:
    - analytics
    - eventing
    serverGroups:
    - zone-c
    pod:
      spec:
        nodeSelector:
          topology.kubernetes.io/zone: us-east-1c
        resources:
          requests:
            cpu: "8"
            memory: 32Gi
          limits:
            cpu: "16"
            memory: 40Gi
    volumeMounts:
      default: couchbase-analytics
      analytics:
      - couchbase-analytics
  serverGroups:
  - zone-a
  - zone-b
  - zone-c
  volumeClaimTemplates:
  - metadata:
      name: couchbase-data
    spec:
      storageClassName: ebs-gp3-couchbase
      resources:
        requests:
          storage: 100Gi
  - metadata:
      name: couchbase-index
    spec:
      storageClassName: ebs-gp3-couchbase
      resources:
        requests:
          storage: 50Gi
  - metadata:
      name: couchbase-default
    spec:
      storageClassName: ebs-gp3-couchbase
      resources:
        requests:
          storage: 20Gi
  - metadata:
      name: couchbase-analytics
    spec:
      storageClassName: ebs-gp3-couchbase
      resources:
        requests:
          storage: 200Gi
通过 Helm

操作员安装

# Add Couchbase Helm repository
helm repo add couchbase https://couchbase-partners.github.io/helm-charts/
helm repo update

# Install the Couchbase Autonomous Operator
helm install couchbase-operator couchbase/couchbase-operator \
  --namespace couchbase \
  --create-namespace \
  --set operator.image.repository=couchbase/operator \
  --set operator.image.tag=2.7.1 \
  --set admissionController.enabled=true

# Create the admin credentials secret
kubectl create secret generic cb-admin-credentials \
  --namespace couchbase \
  --from-literal=username=Administrator \
  --from-literal=password=$(openssl rand -base64 24)

# Deploy the CouchbaseCluster CRD
kubectl apply -f couchbase-cluster.yaml

# Verify deployment
kubectl get couchbaseclusters -n couchbase
kubectl get pods -n couchbase -l app=couchbase
kubectl get svc -n couchbase

服务器组和机架/区域感知

服务器组是 Couchbase 的机制,用于确保活动 vBuckets 及其副本放置在不同的故障域(可用区、机架或数据中心)中。配置服务器组时,Couchbase 保证同一 vBucket 的活动和副本对不会驻留在同一服务器组中。这意味着完全的区域故障不会导致数据丢失。

Autonomous Operator 将服务器组映射到 Kubernetes 节点拓扑标签,自动在正确的区域中调度 Pod。与 Pod 反关联性规则相结合,这可确保 Couchbase Pod 分布在物理基础设施上,以实现最大的弹性。

AWS EKS 部署

Amazon EKS 需要特定配置才能实现最佳 Couchbase 性能。关键考虑因素是存储(用于吞吐量的 EBS gp3)、实例类型(用于数据节点的内存优化 r6i/r7i)和网络(用于 Pod 级网络的 VPC CNI)。

# EBS gp3 StorageClass optimized for Couchbase
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: ebs-gp3-couchbase
provisioner: ebs.csi.aws.com
parameters:
  type: gp3
  iops: "6000"
  throughput: "500"
  encrypted: "true"
  kmsKeyId: "arn:aws:kms:us-east-1:123456789:key/mrk-abcdef"
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

---
# Recommended EKS node groups for Couchbase
# Data nodes:     r6i.2xlarge (8 vCPU, 64 GiB) or r7i.2xlarge
# Index/Query:    m6i.2xlarge (8 vCPU, 32 GiB)
# Analytics:      r6i.4xlarge (16 vCPU, 128 GiB)
# Eventing:       m6i.xlarge  (4 vCPU, 16 GiB)

# EKS managed node group with taints for Couchbase
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
  name: couchbase-eks
  region: us-east-1
managedNodeGroups:
- name: cb-data
  instanceType: r6i.2xlarge
  desiredCapacity: 4
  minSize: 4
  maxSize: 8
  volumeSize: 200
  volumeType: gp3
  volumeIOPS: 6000
  volumeThroughput: 500
  availabilityZones: ["us-east-1a", "us-east-1b"]
  labels:
    workload: couchbase-data
  taints:
  - key: couchbase
    value: "true"
    effect: NoSchedule
  iam:
    attachPolicyARNs:
    - arn:aws:iam::policy/AmazonEBSCSIDriverPolicy
- name: cb-query
  instanceType: m6i.2xlarge
  desiredCapacity: 2
  minSize: 2
  maxSize: 4
  availabilityZones: ["us-east-1a", "us-east-1b"]
  labels:
    workload: couchbase-query

Azure AKS 部署

Azure AKS 使用 Premium SSD v2 或 Ultra Disk 来满足 Couchbase 的 I/O 需求,并使用 Azure Private Link 来实现区域之间的安全 XDCR 连接。

# Azure Premium SSD v2 StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: azure-premium-couchbase
provisioner: disk.csi.azure.com
parameters:
  skuName: PremiumV2_LRS
  DiskIOPSReadWrite: "6000"
  DiskMBpsReadWrite: "500"
  cachingMode: None
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

---
# Ultra Disk StorageClass for high-performance workloads
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: azure-ultra-couchbase
provisioner: disk.csi.azure.com
parameters:
  skuName: UltraSSD_LRS
  DiskIOPSReadWrite: "10000"
  DiskMBpsReadWrite: "1000"
  cachingMode: None
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

---
# AKS recommended VM sizes:
# Data nodes:      Standard_E8s_v5  (8 vCPU, 64 GiB)
# Index/Query:     Standard_D8s_v5  (8 vCPU, 32 GiB)
# Analytics:       Standard_E16s_v5 (16 vCPU, 128 GiB)

GCP GKE 部署

Google Kubernetes 引擎使用 SSD 持久磁盘和工作负载身份来安全访问 Google Cloud Storage 进行备份。

# GKE SSD Persistent Disk StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: ssd-couchbase
provisioner: pd.csi.storage.gke.io
parameters:
  type: pd-ssd
  provisioned-iops-on-create: "6000"
  provisioned-throughput-on-create: "500"
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

---
# GKE Hyperdisk Balanced for cost-effective performance
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: hyperdisk-couchbase
provisioner: pd.csi.storage.gke.io
parameters:
  type: hyperdisk-balanced
  provisioned-iops-on-create: "6000"
  provisioned-throughput-on-create: "500"
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true

---
# GKE recommended machine types:
# Data nodes:      n2-highmem-8   (8 vCPU, 64 GB)
# Index/Query:     n2-standard-8  (8 vCPU, 32 GB)
# Analytics:       n2-highmem-16  (16 vCPU, 128 GB)

裸机 k3s/带 Longhorn

的 Rancher

对于需要完全基础设施控制而又不被云供应商锁定的组织来说,具有 Rancher 管理和 Longhorn 分布式存储的裸机 k3s 为 Couchbase HA 提供了良好的基础。这种架构在受监管的行业、边缘计算场景和成本敏感的环境中很受欢迎。

k3s / Rancher 裸机 Couchbase 部署Rancher 管理控制台节点 1:k3s 服务器(机架 1)Couchbase 数据 + 索引CB-PROD-0000 | 8 个 CPU、64GBvBuckets 0-341 活跃 | SDK端口11210Couchbase 查询 (N1QL)CB-PROD-0001 |端口 8093MetalLB VIPPrometheusLonghorn 音量/dev/sdb NVMe |跨节点3 个副本反亲和性:没有并置 Couchbase 吊舱节点 2:k3s 服务器(机架 2)Couchbase 数据 + 索引CB-PROD-0002 | 8 个 CPU、64GBvBuckets 342-682 活跃 | SDK端口11210Couchbase 搜索 (FTS)CB-产品-0003 |端口 8094MetalLB VIPGrafanaLonghorn 音量/dev/sdb NVMe | /dev/sdb NVMe |跨节点3 个副本反亲和性:没有并置 Couchbase 吊舱节点 3:k3s 代理(机架 3)Couchbase 数据 + 分析CB-PROD-0004 | 16 个 CPU、128GBvBuckets 683-1023 活动 | CBASCouchbase 赛事CB-PROD-0005 | DCP消费类MetalLB VIPCAO 操作员Longhorn 音量/dev/sdb NVMe | /dev/sdb NVMe |跨节点3 个副本反亲和性:没有并置 Couchbase 吊舱MetalLB 负载均衡器VIP:192.168.1.200-210 | SDK + Web 控制台 + XDCR备份:cbbackupmgr → MinIO S3每日完整 + 每小时增量 |专用 NVMe上的 MinIO数据/索引查询/搜索分析/事件/MetalLBLonghornDCP 复制Rancher 管理
# k3s bare metal setup for Couchbase

# Install k3s on master nodes (HA with embedded etcd)
curl -sfL https://get.k3s.io | sh -s - server \
  --cluster-init \
  --disable traefik \
  --disable servicelb \
  --write-kubeconfig-mode 644 \
  --node-taint couchbase=true:NoSchedule \
  --node-label topology.kubernetes.io/zone=rack-1

# Join additional server nodes
curl -sfL https://get.k3s.io | sh -s - server \
  --server https://master-1:6443 \
  --token $(cat /var/lib/rancher/k3s/server/node-token) \
  --node-taint couchbase=true:NoSchedule \
  --node-label topology.kubernetes.io/zone=rack-2

# Join agent nodes
curl -sfL https://get.k3s.io | sh -s - agent \
  --server https://master-1:6443 \
  --token $(cat /var/lib/rancher/k3s/server/node-token) \
  --node-taint couchbase=true:NoSchedule \
  --node-label topology.kubernetes.io/zone=rack-3

# Install Longhorn for distributed storage
helm repo add longhorn https://charts.longhorn.io
helm install longhorn longhorn/longhorn \
  --namespace longhorn-system \
  --create-namespace \
  --set defaultSettings.defaultReplicaCount=3 \
  --set defaultSettings.defaultDataPath=/mnt/longhorn \
  --set defaultSettings.guaranteedInstanceManagerCPU=12

# Create Longhorn StorageClass for Couchbase
kubectl apply -f - <

使用 cbbackupmgr 进行备份和恢复

Couchbase 提供cbbackupmgr,这是一款企业备份工具,支持完整备份、增量备份和差异备份,并具有可选的压缩和加密功能。对于生产 HA 部署,强大的备份策略将 Couchbase 级备份与云快照功能相结合。

备份配置

# Initialize a backup repository
/opt/couchbase/bin/cbbackupmgr config \
  --archive /backup/couchbase \
  --repo production-backup \
  --include-data production-data \
  --include-data user-profiles \
  --exclude-data _system

# Run a full backup
/opt/couchbase/bin/cbbackupmgr backup \
  --archive /backup/couchbase \
  --repo production-backup \
  --cluster couchbase://localhost \
  --username Administrator \
  --password "$CB_PASSWORD" \
  --threads 4 \
  --no-progress-bar

# Run an incremental backup (only mutations since last backup)
/opt/couchbase/bin/cbbackupmgr backup \
  --archive /backup/couchbase \
  --repo production-backup \
  --cluster couchbase://localhost \
  --username Administrator \
  --password "$CB_PASSWORD" \
  --threads 4

# List available backups
/opt/couchbase/bin/cbbackupmgr list \
  --archive /backup/couchbase \
  --repo production-backup

# Restore from a specific backup
/opt/couchbase/bin/cbbackupmgr restore \
  --archive /backup/couchbase \
  --repo production-backup \
  --cluster couchbase://target-cluster:8091 \
  --username Administrator \
  --password "$CB_PASSWORD" \
  --start 2026-04-12T00_00_00 \
  --end 2026-04-12T14_30_00 \
  --threads 4

Kubernetes

自动备份脚本
#!/bin/bash
# couchbase-backup.sh — Automated backup to S3-compatible storage

set -euo pipefail

CLUSTER_HOST="cb-production-srv.couchbase.svc.cluster.local"
BACKUP_DIR="/backup/couchbase"
REPO_NAME="prod-$(date +%Y%m%d)"
S3_BUCKET="s3://couchbase-backups/production"
RETENTION_DAYS=14

log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"; }

log "Starting Couchbase backup for cluster: $CLUSTER_HOST"

if [ ! -d "$BACKUP_DIR/$REPO_NAME" ]; then
  log "Configuring new backup repository: $REPO_NAME"
  cbbackupmgr config \
    --archive "$BACKUP_DIR" \
    --repo "$REPO_NAME" \
    --include-data production-data \
    --include-data user-profiles
fi

log "Running incremental backup..."
cbbackupmgr backup \
  --archive "$BACKUP_DIR" \
  --repo "$REPO_NAME" \
  --cluster "couchbase://$CLUSTER_HOST" \
  --username "$CB_USERNAME" \
  --password "$CB_PASSWORD" \
  --threads 4 \
  --no-progress-bar

BACKUP_SIZE=$(du -sh "$BACKUP_DIR/$REPO_NAME" | cut -f1)
log "Backup complete. Size: $BACKUP_SIZE"

log "Syncing to S3: $S3_BUCKET/$REPO_NAME"
aws s3 sync "$BACKUP_DIR/$REPO_NAME" "$S3_BUCKET/$REPO_NAME" \
  --storage-class STANDARD_IA \
  --sse aws:kms

log "Cleaning up backups older than $RETENTION_DAYS days..."
find "$BACKUP_DIR" -maxdepth 1 -name "prod-*" -mtime +"$RETENTION_DAYS" -exec rm -rf {} \;

log "Backup pipeline complete."

Couchbase备份 CRD(运营商管理)

自治操作员提供用于自动备份管理的 CRD:

apiVersion: couchbase.com/v2
kind: CouchbaseBackup
metadata:
  name: cb-daily-backup
  namespace: couchbase
spec:
  strategy: full_incremental
  full:
    schedule: "0 2 * * 0"   # Full backup every Sunday at 2 AM
  incremental:
    schedule: "0 2 * * 1-6" # Incremental Mon-Sat at 2 AM
  successfulJobsHistoryLimit: 5
  failedJobsHistoryLimit: 3
  backOffLimit: 3
  logRetention: 168h
  size: 100Gi
  s3bucket: s3://couchbase-backups/production
---
apiVersion: couchbase.com/v2
kind: CouchbaseBackupRestore
metadata:
  name: cb-restore-pitr
  namespace: couchbase
spec:
  backup: cb-daily-backup
  repo: "20260412"
  start:
    int: 1
  end:
    int: 5
  backOffLimit: 3

N1QL 查询性能调整

N1QL(JSON 的 SQL++)是 Couchbase 的查询语言。调整 N1QL 性能需要了解查询规划器、索引设计和服务器端优化。

索引策略:GSI 和 FTS

-- Global Secondary Index (GSI) for common query patterns

-- Composite index for user lookups
CREATE INDEX idx_users_email_status
  ON `user-profiles`(email, status)
  WHERE type = 'user'
  WITH {"num_replica": 1, "defer_build": false};

-- Covering index (includes all queried fields to avoid fetch)
CREATE INDEX idx_orders_covering
  ON `production-data`(customer_id, order_date, total_amount, status)
  WHERE type = 'order'
  WITH {"num_replica": 1};

-- Array index for nested documents
CREATE INDEX idx_order_items
  ON `production-data`(DISTINCT ARRAY item.product_id FOR item IN items END)
  WHERE type = 'order'
  WITH {"num_replica": 1};

-- Partial index for active records only
CREATE INDEX idx_active_sessions
  ON `production-data`(user_id, created_at)
  WHERE type = 'session' AND status = 'active'
  WITH {"num_replica": 1};

-- Adaptive index for dynamic query patterns
CREATE INDEX idx_adaptive_products
  ON `production-data`(DISTINCT PAIRS(self))
  WHERE type = 'product'
  WITH {"num_replica": 1};

-- Check index status
SELECT name, state, num_docs_indexed, num_docs_pending
FROM system:indexes
WHERE keyspace_id = 'production-data';

-- Analyze query execution plan
EXPLAIN SELECT u.name, u.email, COUNT(o.id) AS order_count
FROM `user-profiles` u
JOIN `production-data` o ON o.customer_id = u.id
WHERE u.status = 'active' AND o.type = 'order'
GROUP BY u.name, u.email
ORDER BY order_count DESC
LIMIT 100;

-- Use ADVISE to get index recommendations
ADVISE SELECT * FROM `production-data`
WHERE type = 'order'
  AND customer_id = 'cust-12345'
  AND order_date BETWEEN '2026-01-01' AND '2026-04-12'
ORDER BY order_date DESC;

查询优化技巧

-- Use PREPARE for frequently executed queries (cached plan)
PREPARE get_user_orders AS
SELECT o.id, o.order_date, o.total_amount, o.status
FROM `production-data` o
WHERE o.type = 'order'
  AND o.customer_id = $customer_id
ORDER BY o.order_date DESC
LIMIT $page_size OFFSET $page_offset;

-- Execute prepared statement
EXECUTE get_user_orders
USING {"customer_id": "cust-12345", "page_size": 20, "page_offset": 0};

-- Use META().id for direct key-value lookups (fastest path)
SELECT META().id, *
FROM `production-data`
USE KEYS ["order::2026-001", "order::2026-002", "order::2026-003"];

-- Correlated subquery with USE KEYS for joins
SELECT u.name,
  (SELECT o.id, o.total_amount
   FROM `production-data` o
   USE KEYS u.order_ids
   WHERE o.status = 'completed') AS completed_orders
FROM `user-profiles` u
WHERE META(u).id = 'user::12345';

-- Use INFER to understand document schema
INFER `production-data` WITH {"sample_size": 10000, "similarity_metric": 0.6};

内存管理和存储桶配置

Couchbase 的内存优先架构意味着 RAM 分配直接影响性能。每个服务都有自己的内存配额,桶共享数据服务配额。适当的大小可以防止缓存驱逐而降低延迟。

# Configure cluster-level memory quotas
/opt/couchbase/bin/couchbase-cli setting-cluster \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --cluster-ramsize 8192 \
  --cluster-index-ramsize 4096 \
  --cluster-fts-ramsize 2048 \
  --cluster-eventing-ramsize 2048 \
  --cluster-analytics-ramsize 4096

# Memory allocation guidelines:
# Data Service:      60% of available node RAM
# Index Service:     20% of available node RAM
# Search Service:    10% of available node RAM
# OS/overhead:       10% reserved

# Bucket memory sizing formula:
# Required RAM = (avg_doc_size * num_docs * 2.5) / num_data_nodes
# The 2.5 multiplier accounts for:
#   - Metadata overhead (~56 bytes per document)
#   - Internal fragmentation
#   - Replica copies in memory

# Create an optimized production bucket
curl -X POST http://localhost:8091/pools/default/buckets \
  -u Administrator:password \
  -d name=production-data \
  -d ramQuota=4096 \
  -d bucketType=couchbase \
  -d replicaNumber=2 \
  -d threadsNumber=8 \
  -d evictionPolicy=valueOnly \
  -d compressionMode=active \
  -d maxTTL=0 \
  -d conflictResolutionType=lww \
  -d flushEnabled=0 \
  -d durabilityMinLevel=majorityAndPersistActive

# Eviction policies:
# valueOnly  - Evicts document values but keeps metadata in RAM
#              Best for workloads where key access patterns are predictable
# fullEviction - Evicts both values and metadata
#                Best for very large datasets that exceed available RAM
# noEviction - (Ephemeral buckets only) Rejects writes when RAM is full
#              Best for caching use cases

TLS 加密和 RBAC

在生产中保护 Couchbase 需要对传输中的数据进行加密 (TLS)、基于角色的细粒度访问控制 (RBAC) 和审核日志记录。

# Enable TLS for all Couchbase services
/opt/couchbase/bin/couchbase-cli ssl-manage \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --set-node-certificate

# Enforce minimum TLS version
/opt/couchbase/bin/couchbase-cli setting-security \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --set \
  --tls-min-version tlsv1.2 \
  --tls-honor-cipher-order 1 \
  --hsts-max-age 31536000 \
  --hsts-preload-enabled 1

# Create application-specific RBAC users
/opt/couchbase/bin/couchbase-cli user-manage \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --set \
  --rbac-username app-service \
  --rbac-password "$(openssl rand -base64 32)" \
  --rbac-name "Application Service Account" \
  --roles 'data_reader[production-data],data_writer[production-data],query_select[production-data],query_insert[production-data],query_update[production-data],query_delete[production-data]' \
  --auth-domain local

# Create a read-only analytics user
/opt/couchbase/bin/couchbase-cli user-manage \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --set \
  --rbac-username analytics-reader \
  --rbac-password "$(openssl rand -base64 32)" \
  --roles 'data_reader[production-data],query_select[production-data],analytics_reader[production-data]' \
  --auth-domain local

# Enable audit logging
/opt/couchbase/bin/couchbase-cli setting-audit \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --set \
  --audit-enabled 1 \
  --audit-log-path /opt/couchbase/var/lib/couchbase/logs \
  --audit-log-rotate-interval 86400 \
  --audit-log-rotate-size 20971520

Kubernetes TLS 与证书管理器

# Certificate for Couchbase server TLS
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: couchbase-server-tls
  namespace: couchbase
spec:
  secretName: couchbase-server-tls
  duration: 8760h   # 1 year
  renewBefore: 720h  # 30 days before expiry
  privateKey:
    algorithm: RSA
    size: 4096
  usages:
  - server auth
  - client auth
  dnsNames:
  - "*.cb-production.couchbase.svc.cluster.local"
  - "*.cb-production.couchbase.svc"
  - "cb-production-srv.couchbase.svc.cluster.local"
  - "localhost"
  issuerRef:
    name: couchbase-ca-issuer
    kind: ClusterIssuer

使用 Prometheus 导出器进行监控

Couchbase 通过其 REST API 公开丰富的指标。couchbase-exporter将这些转换为 Prometheus 格式以进行全面监控。

# Deploy Couchbase Prometheus Exporter
apiVersion: apps/v1
kind: Deployment
metadata:
  name: couchbase-exporter
  namespace: couchbase
spec:
  replicas: 1
  selector:
    matchLabels:
      app: couchbase-exporter
  template:
    metadata:
      labels:
        app: couchbase-exporter
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9091"
    spec:
      containers:
      - name: exporter
        image: couchbase/exporter:1.0.9
        args:
        - --couchbase-address=cb-production-srv.couchbase.svc.cluster.local
        - --couchbase-port=8091
        - --couchbase-username=$(CB_USERNAME)
        - --couchbase-password=$(CB_PASSWORD)
        - --server-address=0.0.0.0:9091
        - --per-node-refresh=5
        env:
        - name: CB_USERNAME
          valueFrom:
            secretKeyRef:
              name: cb-admin-credentials
              key: username
        - name: CB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: cb-admin-credentials
              key: password
        ports:
        - containerPort: 9091
          name: metrics
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 500m
            memory: 256Mi
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: couchbase-monitor
  namespace: couchbase
spec:
  selector:
    matchLabels:
      app: couchbase-exporter
  endpoints:
  - port: metrics
    interval: 15s
    path: /metrics

用于监控

的关键 Couchbase 指标
  • cb_bucket_ops_per_sec— 每个存储桶每秒的总操作数。确定正常吞吐量的基线并对异常情况发出警报。
  • cb_bucket_mem_used_bytes— 存储桶内存使用情况。当接近 RAM 配额时发出警报,以防止驱逐。
  • cb_bucket_cache_miss_ratio— 未命中缓存且需要磁盘获取的请求比率。应保持在 2% 以下以获得最佳性能。
  • cb_bucket_disk_queue_items— 磁盘写入队列深度。队列不断增长表明磁盘 I/O 无法跟上写入吞吐量。
  • cb_xdcr_changes_left— 等待 XDCR 复制的突变数量。表示跨区域复制滞后。
  • cb_xdcr_docs_writing— 每秒通过 XDCR 复制的文档。
  • cb_node_cpu_utilization_percent— 每个节点 CPU 使用情况。 Couchbase 是 CPU 的压缩和索引密集型产品。
  • cb_bucket_vbucket_active_num— 每个节点的活动 vBucket 数量。数据节点之间应该大致均匀。
  • cb_index_num_docs_pending— 文档等待索引更新。表示索引构建滞后。
  • cb_n1ql_requests_per_sec— N1QL 查询吞吐量。结合平均延迟,可以识别查询性能问题。

Prometheus 警报规则

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: couchbase-alerts
  namespace: couchbase
spec:
  groups:
  - name: couchbase.rules
    rules:
    - alert: CouchbaseNodeDown
      expr: cb_node_healthy == 0
      for: 1m
      labels:
        severity: critical
      annotations:
        summary: "Couchbase node {{ $labels.node }} is unhealthy"
    - alert: CouchbaseHighCacheMissRate
      expr: cb_bucket_cache_miss_ratio > 0.05
      for: 5m
      labels:
        severity: warning
      annotations:
        summary: "Cache miss rate {{ $value | humanizePercentage }} on bucket {{ $labels.bucket }}"
    - alert: CouchbaseXDCRLag
      expr: cb_xdcr_changes_left > 10000
      for: 10m
      labels:
        severity: warning
      annotations:
        summary: "XDCR replication lag: {{ $value }} pending mutations"
    - alert: CouchbaseDiskQueueGrowing
      expr: rate(cb_bucket_disk_queue_items[5m]) > 100
      for: 10m
      labels:
        severity: warning
      annotations:
        summary: "Disk queue growing on bucket {{ $labels.bucket }}"
    - alert: CouchbaseMemoryPressure
      expr: cb_bucket_mem_used_bytes / cb_bucket_mem_quota_bytes > 0.9
      for: 5m
      labels:
        severity: critical
      annotations:
        summary: "Memory usage at {{ $value | humanizePercentage }} for bucket {{ $labels.bucket }}"
HA的

SDK 连接字符串配置

Couchbase SDK 具有拓扑感知能力 - 它们维护内部集群映射并将操作直接路由到正确的节点。正确的 SDK 配置对于 HA 至关重要,可确保快速故障转移检测和针对瞬态错误的自动重试。

// Node.js SDK configuration for HA
const couchbase = require('couchbase');

const clusterConnStr = 'couchbases://cb-node1.example.com,cb-node2.example.com,cb-node3.example.com';

const cluster = await couchbase.connect(clusterConnStr, {
  username: process.env.CB_USERNAME,
  password: process.env.CB_PASSWORD,
  timeouts: {
    kvTimeout: 2500,           // Key-value operation timeout (ms)
    kvDurableTimeout: 10000,   // Durable write timeout
    queryTimeout: 75000,       // N1QL query timeout
    searchTimeout: 75000,      // FTS search timeout
    analyticsTimeout: 75000,   // Analytics query timeout
    connectTimeout: 10000,     // Initial connection timeout
    managementTimeout: 75000   // Management API timeout
  },
  security: {
    trustStorePath: '/etc/couchbase/ca.pem'
  },
  transactions: {
    durabilityLevel: couchbase.DurabilityLevel.MajorityAndPersistToActive,
    timeout: 15000
  }
});

const bucket = cluster.bucket('production-data');
const collection = bucket.defaultCollection();

// Durable write with observe-based durability
await collection.upsert('order::2026-001', orderDocument, {
  durabilityLevel: couchbase.DurabilityLevel.MajorityAndPersistToActive,
  timeout: 10000
});

// Read with replica fallback for HA
try {
  const result = await collection.get('user::12345');
} catch (err) {
  if (err instanceof couchbase.errors.TimeoutError) {
    const replicaResult = await collection.getAnyReplica('user::12345');
  }
}
# Java SDK configuration for HA
import com.couchbase.client.java.*;
import com.couchbase.client.java.env.*;
import java.time.Duration;

ClusterEnvironment env = ClusterEnvironment.builder()
    .timeoutConfig(TimeoutConfig.builder()
        .kvTimeout(Duration.ofMillis(2500))
        .kvDurableTimeout(Duration.ofSeconds(10))
        .queryTimeout(Duration.ofSeconds(75))
        .connectTimeout(Duration.ofSeconds(10))
        .build())
    .ioConfig(IoConfig.builder()
        .numKvConnections(4)
        .enableMutationTokens(true)
        .enableDnsSrv(true)
        .build())
    .securityConfig(SecurityConfig.builder()
        .enableTls(true)
        .trustCertificate(Paths.get("/etc/couchbase/ca.pem"))
        .build())
    .build();

Cluster cluster = Cluster.connect(
    "couchbases://cb-node1.example.com,cb-node2.example.com",
    ClusterOptions.clusterOptions("username", "password")
        .environment(env)
);

Kubernetes 用于 SDK 连接的 DNS 服务

# When using the Autonomous Operator, connect via the headless service:
# couchbase://cb-production-srv.couchbase.svc.cluster.local
#
# The operator creates these services:
# cb-production-srv      - Headless service for SDK auto-discovery
# cb-production-ui       - Web Console (port 8091/18091)
# cb-production-cloud    - External connectivity (NodePort/LoadBalancer)
#
# For external SDK access (outside Kubernetes), use:
# - NodePort with explicit node addresses
# - LoadBalancer with MetalLB (bare metal)
# - Ingress with TCP passthrough for port 11210 (SDK) and 11207 (SDK TLS)

Couchbase 用于边缘部署的移动和同步网关

Couchbase Mobile 将 Couchbase 生态系统扩展到边缘设备和移动应用。Couchbase Lite嵌入在iOS、Android和IoT设备上运行,而同步网关充当Couchbase Lite和Couchbase服务器之间的同步中间件。

// Sync Gateway configuration for production
{
  "interface": ":4984",
  "adminInterface": "127.0.0.1:4985",
  "logging": {
    "console": {
      "log_level": "info",
      "log_keys": ["HTTP", "Sync", "Auth", "Changes"]
    }
  },
  "databases": {
    "mobile-app": {
      "server": "couchbases://cb-production-srv.couchbase.svc.cluster.local",
      "bucket": "production-data",
      "username": "sync-gateway",
      "password": "${SG_PASSWORD}",
      "enable_shared_bucket_access": true,
      "import_docs": true,
      "num_index_replicas": 1,
      "delta_sync": {
        "enabled": true,
        "rev_max_age_seconds": 86400
      },
      "cache": {
        "channel_cache": {
          "max_number": 50000,
          "compact_high_watermark_pct": 80,
          "compact_low_watermark_pct": 60
        },
        "rev_cache": {
          "size": 5000,
          "shard_count": 16
        }
      },
      "users": {
        "GUEST": {"disabled": true}
      },
      "sync": "function(doc, oldDoc) { if (doc.type === 'user-data') { channel(doc.channels); requireAccess(doc.channels); } else { channel('public'); } }"
    }
  }
}

# Deploy Sync Gateway on Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sync-gateway
  namespace: couchbase
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sync-gateway
  template:
    metadata:
      labels:
        app: sync-gateway
    spec:
      containers:
      - name: sync-gateway
        image: couchbase/sync-gateway:3.1.4-enterprise
        args: ["/etc/sync-gateway/config.json"]
        ports:
        - containerPort: 4984
          name: public
        - containerPort: 4985
          name: admin
        resources:
          requests:
            cpu: "2"
            memory: 4Gi
          limits:
            cpu: "4"
            memory: 8Gi
        volumeMounts:
        - name: config
          mountPath: /etc/sync-gateway
      volumes:
      - name: config
        configMap:
          name: sync-gateway-config

容量规划和规模调整

正确的容量规划对于 Couchbase 性能和成本优化至关重要。 下表提供了基于工作负载层的规模调整指南:

并置
工作负载层数据节点索引/查询每个节点的 RAM存储吞吐量
开发1(所有服务)与4 GB20 GB SSD<1k ops/s
小批量生产3 数据2 查询+索引16 GB100 GB SSD10k 操作/秒
中等量产5 数据3 查询+索引32 GB500 GB SSD50k 操作/秒
大批量生产7-10 数据4+ 查询+索引64 GB1 TB NVMe200k+ 操作/秒
企业/全球10+ 数据(多区域)6+ 查询+索引128 GB2+ TB NVMe500k+ 操作/秒

尺码公式

# Data Service RAM sizing
# Required RAM per node = (num_documents * (doc_metadata_size + avg_value_size)) / num_data_nodes * (1 + num_replicas)
# doc_metadata_size = 56 bytes (fixed overhead per document)
# Include 25% headroom for fragmentation and growth

# Example: 100M documents, 1KB avg size, 3 data nodes, 1 replica
# RAM = (100,000,000 * (56 + 1024)) / 3 * 2 = ~72 GB per node
# With 25% headroom: ~90 GB per node

# Index Service RAM sizing (memory-optimized)
# RAM = total_index_size * 3 (for build/merge overhead)
# Use system:indexes to check current index sizes

# Disk sizing
# Disk = (num_documents * avg_doc_size * (1 + num_replicas)) * 3 (compaction headroom)
# Use SSD/NVMe with provisioned IOPS for predictable performance

灾难恢复和故障转移程序

全面的灾难恢复计划可确保基础设施故障超出自动故障转移范围时的业务连续性。

单节点故障(自动)

# Auto-failover handles single node failures automatically.
# Verify failover occurred:
/opt/couchbase/bin/couchbase-cli server-list \
  --cluster localhost:8091 \
  --username Administrator \
  --password password

# After replacing the failed node, add and rebalance:
/opt/couchbase/bin/couchbase-cli server-add \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --server-add new-node.example.com:8091 \
  --server-add-username Administrator \
  --server-add-password password \
  --services data,index

/opt/couchbase/bin/couchbase-cli rebalance \
  --cluster localhost:8091 \
  --username Administrator \
  --password password

完全集群故障(手动)

# Scenario: Primary region (US-EAST) completely lost

# Step 1: Verify XDCR target cluster (EU-WEST) has latest data
# Check XDCR replication status before failure
curl -s http://eu-west-node:8091/pools/default/remoteClusters \
  -u Administrator:password | jq .

# Step 2: Pause XDCR replications pointing to the failed cluster
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
  --cluster cb-eu-west.example.com:8091 \
  --username Administrator \
  --password password \
  --pause \
  --xdcr-replicator <replication-id>

# Step 3: Update application connection strings to EU-WEST cluster
# (via DNS update, service mesh, or environment variable change)

# Step 4: Scale up EU-WEST cluster if needed to handle full production load
# In Kubernetes, update the CouchbaseCluster CRD:
kubectl patch couchbasecluster cb-eu-west -n couchbase --type merge \
  -p '{"spec":{"servers":[{"name":"data-zone-a","size":4}]}}'

# Step 5: After US-EAST cluster is restored, re-establish XDCR
# and perform a full resync from EU-WEST back to US-EAST

平稳的故障转移和恢复

# Graceful failover (for maintenance, drains data before removal)
/opt/couchbase/bin/couchbase-cli failover \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --server-failover node-to-remove.example.com:8091

# Recovery (re-add the node after maintenance)
/opt/couchbase/bin/couchbase-cli recovery \
  --cluster localhost:8091 \
  --username Administrator \
  --password password \
  --server-recovery node-to-recover.example.com:8091 \
  --recovery-type delta

# Delta recovery re-synchronizes only the changed data,
# which is much faster than full recovery.
# Full recovery rebuilds the node from scratch.

# Rebalance to complete the recovery
/opt/couchbase/bin/couchbase-cli rebalance \
  --cluster localhost:8091 \
  --username Administrator \
  --password password

Helm 完整生产部署的价值

下面是一个全面的 Helm 值文件,用于在生产环境中使用 Autonomous Operator 部署 Couchbase:

# helm-values-production.yaml
couchbase-operator:
  operator:
    image:
      repository: couchbase/operator
      tag: 2.7.1
    resources:
      requests:
        cpu: 500m
        memory: 512Mi
      limits:
        cpu: "1"
        memory: 1Gi
  admissionController:
    enabled: true
    resources:
      requests:
        cpu: 100m
        memory: 128Mi

cluster:
  image: couchbase/server:7.6.1-enterprise
  antiAffinity: true
  autoFailoverTimeout: 30s
  autoFailoverMaxCount: 3
  autoFailoverOnDataDiskIssues: true
  autoFailoverServerGroup: true
  security:
    adminSecret: cb-admin-credentials
  networking:
    tls:
      static:
        serverSecret: couchbase-server-tls
        operatorSecret: couchbase-operator-tls
    exposeAdminConsole: true
    adminConsoleServiceType: NodePort
  buckets:
    managed: true
  servers:
    data:
      size: 4
      services:
      - data
      - index
      serverGroups:
      - zone-a
      - zone-b
      resources:
        requests:
          cpu: "4"
          memory: 16Gi
        limits:
          cpu: "8"
          memory: 20Gi
      volumeMounts:
        default: couchbase-data
        data: couchbase-data
        index: couchbase-index
    query:
      size: 2
      services:
      - query
      - search
      resources:
        requests:
          cpu: "4"
          memory: 8Gi
        limits:
          cpu: "8"
          memory: 12Gi
      volumeMounts:
        default: couchbase-default
    analytics:
      size: 2
      services:
      - analytics
      - eventing
      serverGroups:
      - zone-c
      resources:
        requests:
          cpu: "8"
          memory: 32Gi
        limits:
          cpu: "16"
          memory: 40Gi
      volumeMounts:
        default: couchbase-analytics
        analytics:
        - couchbase-analytics
  serverGroups:
  - zone-a
  - zone-b
  - zone-c
  volumeClaimTemplates:
  - metadata:
      name: couchbase-data
    spec:
      storageClassName: ebs-gp3-couchbase
      resources:
        requests:
          storage: 100Gi
  - metadata:
      name: couchbase-index
    spec:
      storageClassName: ebs-gp3-couchbase
      resources:
        requests:
          storage: 50Gi
  - metadata:
      name: couchbase-default
    spec:
      storageClassName: ebs-gp3-couchbase
      resources:
        requests:
          storage: 20Gi
  - metadata:
      name: couchbase-analytics
    spec:
      storageClassName: ebs-gp3-couchbase
      resources:
        requests:
          storage: 200Gi

结论

Couchbase 服务器的架构围绕基于 vBucket 的分片、内存优先数据访问和集成多模型服务构建,为高可用性生产部署提供了独特的强大基础。集群内复制与自动故障转移的结合可确保透明地处理单节点故障,而 XDCR 将这种弹性扩展到全球应用程序的跨地理区域。

Kubernetes 的 Couchbase 自主操作员将复杂的手动操作转变为声明式、自我修复部署。服务器组提供机架/区域感知,操作员在扩展事件期间管理重新平衡操作,集成备份 CRD 自动执行灾难恢复准备。

本指南的要点:

  • 利用多维扩展— 将数据、索引、查询、搜索、分析和事件服务分离到专用节点池上,以实现独立扩展和资源隔离。
  • 配置 XDCR 以实现多区域弹性— 具有基于时间戳的冲突解决功能的双向 XDCR 支持跨 AWS、Azure 和 GCP 的主动-主动部署。始终确保 NTP 同步。
  • 使用服务器组进行区域感知— 将服务器组映射到可用区域或机架,以保证活动 vBucket 和副本 vBucket 位于不同的故障域中。
  • 仔细调整内存大小— Couchbase 的性能与 RAM 中容纳的工作集大小直接相关。使用大小调整公式并监控缓存未命中率。
  • 实施全面监控— 从第一天起就部署 Prometheus 导出器。 XDCR 复制延迟、缓存未命中率、磁盘队列深度和节点运行状况是您的关键信号。
  • 使用 cbbackupmgr 自动备份— 将完整备份和增量备份与云快照相结合。定期测试恢复过程。
  • 通过 TLS 和 RBAC 实现安全— 启用节点到节点和客户端到节点 TLS 加密。为每个应用程序服务帐户使用细粒度的 RBAC 角色。
  • 为 HA配置 SDK — 使用多个引导节点、配置适当的超时、实施副本读取作为回退,并利用关键数据的持久写入。
  • 灾难恢复规划— 记录并演练单节点、多节点和完整集群故障场景的故障转移过程。 XDCR 备用集群应随时准备好升级。

有了这个全面的基础,您就可以在任何基础设施的高可用性生产环境中部署和操作 Couchbase 服务器——从 AWS、Azure 和 GCP 上的托管 Kubernetes 到 Rancher 管理的裸机 k3s 集群。 Couchbase 的本机分布式架构与 Kubernetes 编排相结合,提供了一个满足现代全球分布式应用程序需求的数据库平台。