KubeBlocks
BlogsKubeBlocks Cloud
⌘K
​

Overview

Introduction
Concepts
Kubernetes and Operator 101
Supported addons
About this manual

Try out KubeBlocks with Playground

Try out KubeBlocks on cloud
Try out KubeBlocks on laptop

Installation

Prerequisite for Local Env

Prerequisite for Local Env
Install kbcli
Install KubeBlocks
Install Addons
Uninstall KubeBlocks and kbcli

Upgrade KubeBlocks

Upgrade to v0.8
Upgrade to v0.9.0
Upgrade to v0.9.3
FAQ

Connect Database

Overview
Testing environment
Production environment

Maintenance

Scale

Horizontal Scale
Vertical Scale

Backup and Restore

Introduction

Backup

Configure BackupRepo
Configure BackupPolicy
Scheduled backup
On-demand backup

Restore

Restore from backup set
PITR

In Place Update

Overview on in-place update
Enable in-place update

Resource Scheduling

Configure pod affinity for database clusters

Cross K8s Deployment

Deploy a Cluster across Multiple Kubernetes Clusters by KubeBlocks

Instance Template

Introduction of instance template
Apply instance template

Observability

Monitor database
Configure alert

User Management

Manage user accounts

Handle an Exception

Handle an exception
Full disk lock

Developer

Developer guides
Terminology

Add an add-on to KubeBlocks

Add-ons of KubeBlocks
Add an add-on
Backup and restore
Parameter template
Parameter configuration
Monitoring
Multi-component configuration
Environment variables and placeholders

External Component

Reference external component

API Reference

Cluster
Backup
Add-On
  1. Examples
    1. Default configuration
    2. Spread evenly as much as possible
    3. Forced spread evenly
    4. Deploy pods in specified nodes
    5. Deploy pods in dedicated nodes
    6. One node only for one pod
  2. Examples
    1. Default configuration
    2. Spread evenly as much as possible
    3. Forced spread evenly
    4. Deploy pods in specified nodes
    5. Deploy pods in dedicated nodes
    6. One node only for one pod

Configure pod affinity for database clusters

Affinity controls the selection logic of pod allocation on nodes. By a reasonable allocation of Kubernetes pods on different nodes, the business availability, resource usage rate, and stability are improved.

Affinity and toleration can be set by kbcli or editing the spec of a cluster or a component. Note that kbcli only supports the cluster-level configuration. If you want to realize the cluster-level and component-level configurations, you can configure the CR YAML file.

You can configure pod affinity and toleration in either the spec of a cluster or the spec of a component.

The cluster-level configuration is used as the default configuration of all components; if the pod affinity configuration exists in a component, the component-level configuration will take effect and cover the default cluster-level configuration.

spec:
  affinity:
    podAntiAffinity: Preferred
    topologyKeys:
    - kubernetes.io/hostname
    nodeLabels:
      topology.kubernetes.io/zone: us-east-1a
  tolerations:
  - key: EngineType
    operator: Equal
    value: mysql
    effect: NoSchedule
  componentSpecs:
  - name: mysql
    componentDefRef: mysql
    affinity:
      podAntiAffinity: Required
      topologyKeys:
        - kubernetes.io/hostname
...

Description of parameters in the YAML file

  • Affinity

    Parameters related to pod affinity are under the object of spec.affinity in the Cluster CR YAML file. The pod affinity configuration can be applied to the cluster or component and the component-level configuration covers the cluster-level configuration.

  • Toleration

    Parameters related to toleration are under the object of spec.tolerations in the Cluster CR YAML file and Kubernetes native semantics are used. For the toleration parameter configuration, refer to the Kubernetes official document Taints and Tolerations. Like affinity configuration, toleration also supports component-level and cluster-level configurations. The cluster-level configuration is set as default and the component-level configuration covers the cluster-level configuration.

ParameterValueDescription
podAntiAffinity- Required
- Preferred (default)
It stands for the anti-affinity level of the pod under the current component.
- Required means pods must be spread evenly in the fault domain specified by topologyKeys.
- Preferred means pods can be spread as evenly as possible in the fault domain specified by topologyKeys.
topologyKeysTopologyKey is the key of the node label. The node with the same value as this key is considered to be in the same topology, i.e. the same fault domain.
For example, if the TopologyKey is kubernetes.io/hostname, every node is a domain of this topology. If the TopologyKey is topology.kubernetes.io/zone, every available zone is a domain of this topology.
nodeLabelsNodeLabels specifies a pod can only be scheduled to the node with the specified node label.
tenancy- SharedNode (default)
- DedicatedNode
It refers to the pod tenancy type:
- SharedNode means that multiple pods share a node.
- DedicatedNode means that a node is dedicated to a pod.

Examples

The following examples show the common situations of how to use pod affinity and toleration configuration.

Default configuration

No affinity parameters are required.

Spread evenly as much as possible

You can configure the pod affinity as "Preferred" if you want the pods of the cluster to be deployed in different topological domains, but do not want deployment failure due to failing to meet distribution requirements.

The example below creates and sets a cluster to be deployed evenly across nodes as much as possible.

spec:
  affinity:
    podAntiAffinity: Preferred

Forced spread evenly

You can configure the pod affinity as "Required" if the pods of the cluster must be deployed in different topological domains to ensure that the cluster can be disaster-tolerant across topological domains. The example below creates and sets a cluster that must be deployed across nodes.

spec:
  affinity:
    podAntiAffinity: Required

Deploy pods in specified nodes

You can specify a node label to deploy a cluster on the specified node. The example below creates and sets a cluster to be deployed on the node with an available zone label of topology.kubernetes.io/zone=us-east-1a.

spec:
  affinity:
    nodeLabels:
      topology.kubernetes.io/zone: us-east-1a

Deploy pods in dedicated nodes

If you want to manage node groups by the taint and node labels and need the clusters to be deployed on a dedicated host group, you can set tolerations and specify a node label.

For example, you have a host group for deploying database clusters and this host is added with a taint named database=true:NoSchedule and a label database=true, then you can follow the command below to create a cluster.

spec:
  affinity:
    podAntiAffinity: Preferred
    topologyKeys:
    - kubernetes.io/hostname
    nodeLabels:
      database: true
  tolerations:
  - key: database
    operator: Equal
    value: true
    effect: NoSchedule

One node only for one pod

If you need one cluster only for the online core business and need to ensure every pod of this cluster has its own node to avoid being affected by the cluster of the cluster, you can specify tenancy as "DedicatedNode".

spec:
  affinity:
    tenancy: DedicatedNode
NOTE

This command will be performed successfully based on the prerequisite that you have added taints for these nodes. Otherwise, the business that is not managed by KubeBlocks can still be deployed on these nodes.

Run kbcli cluster create -h to view the examples and the parameter options of affinity and toleration configurations.

kbcli cluster create -h
>
Create a cluster

Examples:
  ...
  
  # Create a cluster forced to scatter by node
  kbcli cluster create --cluster-definition apecloud-mysql --topology-keys kubernetes.io/hostname --pod-anti-affinity
Required
  
  # Create a cluster in specific labels nodes
  kbcli cluster create --cluster-definition apecloud-mysql --node-labels
'"topology.kubernetes.io/zone=us-east-1a","disktype=ssd,essd"'
  
  # Create a Cluster with two tolerations
  kbcli cluster create --cluster-definition apecloud-mysql --tolerations
'"key=engineType,value=mongo,operator=Equal,effect=NoSchedule","key=diskType,value=ssd,operator=Equal,effect=NoSchedule"'
  
  # Create a cluster, with each pod runs on their own dedicated node
  kbcli cluster create --tenancy=DedicatedNode

Options:
    ...
    --node-labels=[]:
        Node label selector

    --pod-anti-affinity='Preferred':
        Pod anti-affinity type, one of: (Preferred, Required)
        
    --tenancy='SharedNode':
        Tenancy options, one of: (SharedNode, DedicatedNode)

    --tolerations=[]:
        Tolerations for cluster, such as '"key=engineType,value=mongo,operator=Equal,effect=NoSchedule"'

    --topology-keys=[]:
        Topology keys for affinity
    ...
...

Examples

The following examples use kbcli to create cluster instances and show the common situations of how to use pod affinity and toleration configuration.

Default configuration

No affinity parameters are required.

Spread evenly as much as possible

You can configure the pod affinity as "Preferred" if you want the pods of the cluster to be deployed in different topological domains, but do not want deployment failure due to failing to meet distribution requirements. The example below creates and sets a cluster to be deployed evenly across nodes as much as possible.

kbcli cluster create --topology-keys kubernetes.io/hostname --pod-anti-affinity Preferred

Forced spread evenly

You can configure the pod affinity as "Required" if the pods of the cluster must be deployed in different topological domains to ensure that the cluster can be disaster-tolerant across topological domains. The example below creates and sets a cluster that must be deployed across nodes.

kbcli cluster create --topology-keys kubernetes.io/hostname --pod-anti-affinity Required

Deploy pods in specified nodes

You can specify a node label to deploy a cluster on the specified node. The example below creates and sets a cluster to be deployed on the node with an available zone label of topology.kubernetes.io/zone=us-east-1a.

kbcli cluster create --node-labels '"topology.kubernetes.io/zone=us-east-1a"'

Deploy pods in dedicated nodes

If you want to manage node groups by the taint and node labels and need the clusters to be deployed on a dedicated host group, you can set tolerations and specify a node label.

For example, you have a host group for deploying database clusters and this host is added with a taint named database=true:NoSchedule and a label database=true, then you can follow the command below to create a cluster.

kbcli cluster create --node-labels '"databa=true"' --tolerations '"key=database,value=true,operator=Equal,effect=NoSchedule"

One node only for one pod

If you need one cluster only for the online core business and need to ensure every pod of this cluster has its own node to avoid being affected by the cluster of the cluster, you can specify tenancy as "DedicatedNode".

kbcli cluster create --tenancy=DedicatedNode
NOTE

This command will be performed successfully based on the prerequisite that you have added taints for these nodes. Otherwise, the business that is not managed by KubeBlocks can still be deployed on these nodes.

© 2025 ApeCloud PTE. Ltd.