github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/user_docs/kubeblocks-for-postgresql/cluster-management/scale-for-a-postgresql-cluster.md (about)

     1  ---
     2  title: Scale for PostgreSQL
     3  description: How to vertically scale a PostgreSQL cluster
     4  keywords: [postgresql, vertical scale]
     5  sidebar_position: 2
     6  sidebar_label: Scale
     7  ---
     8  
     9  # Scale for PostgreSQL
    10  
    11  Currently, only vertical scaling for PostgreSQL is supported.
    12  
    13  ## Vertical scaling
    14  
    15  You can vertically scale a cluster by changing resource requirements and limits (CPU and storage). For example, if you need to change the resource demand from 1C2G to 2C4G, vertical scaling is what you need.
    16  
    17  :::note
    18  
    19  During the vertical scaling process, a concurrent restart is triggered and the leader pod may change after the restarting.
    20  
    21  :::
    22  
    23  ### Before you start
    24  
    25  Check whether the cluster status is `Running`. Otherwise, the following operations may fail.
    26  
    27  ```bash
    28  kbcli cluster list <name>
    29  ```
    30  
    31  ***Example***
    32  
    33  ```bash
    34  kbcli cluster list pg-cluster
    35  >
    36  NAME         NAMESPACE   CLUSTER-DEFINITION           VERSION             TERMINATION-POLICY   STATUS    CREATED-TIME
    37  pg-cluster   default     postgresql-cluster           postgresql-14.7.0   Delete               Running   Mar 03,2023 18:00 UTC+0800
    38  ```
    39  
    40  ### Steps
    41  
    42  1. Change configuration. There are 3 ways to apply vertical scaling.
    43  
    44     **Option 1.** (**Recommended**) Use kbcli
    45  
    46     Configure the parameters `--components`, `--memory`, and `--cpu` and run the command.
    47  
    48     ***Example***
    49  
    50     ```bash
    51     kbcli cluster vscale pg-cluster \
    52     --components="pg-replication" \
    53     --memory="4Gi" --cpu="2" \
    54     ```
    55  
    56     - `--components` describes the component name ready for vertical scaling.
    57     - `--memory` describes the requested and limited size of the component memory.
    58     - `--cpu` describes the requested and limited size of the component CPU.
    59    
    60     **Option 2.** Create an OpsRequest
    61    
    62     Run the command below to apply an OpsRequest to the specified cluster. Configure the parameters according to your needs.
    63  
    64     ```bash
    65     kubectl apply -f - <<EOF
    66     apiVersion: apps.kubeblocks.io/v1alpha1
    67     kind: OpsRequest
    68     metadata:
    69       name: ops-vertical-scaling
    70     spec:
    71       clusterRef: pg-cluster
    72       type: VerticalScaling 
    73       verticalScaling:
    74       - componentName: pg-replication
    75         requests:
    76           memory: "2Gi"
    77           cpu: "1000m"
    78         limits:
    79           memory: "4Gi"
    80           cpu: "2000m"
    81     EOF
    82     ```
    83    
    84     **Option 3.** Change the YAML file of the cluster
    85  
    86     Change the configuration of `spec.components.resources` in the YAML file. `spec.components.resources` controls the requirement and limit of resources and changing them triggers a vertical scaling.
    87  
    88     ***Example***
    89  
    90     ```YAML
    91     apiVersion: apps.kubeblocks.io/v1alpha1
    92     kind: Cluster
    93     metadata:
    94       name: pg-cluster
    95       namespace: default
    96     spec:
    97       clusterDefinitionRef: postgresql-cluster
    98       clusterVersionRef: postgre-14.7.0
    99       componentSpecs:
   100       - name: pg-replication
   101         componentDefRef: postgresql
   102         replicas: 1
   103         resources: # Change the values of resources.
   104           requests:
   105             memory: "2Gi"
   106             cpu: "1000m"
   107           limits:
   108             memory: "4Gi"
   109             cpu: "2000m"
   110         volumeClaimTemplates:
   111         - name: data
   112           spec:
   113             accessModes:
   114               - ReadWriteOnce
   115             resources:
   116               requests:
   117                 storage: 1Gi
   118       terminationPolicy: Halt
   119     ```
   120  
   121  2. Validate the vertical scaling.
   122  
   123      Run the command below to check the cluster status to identify the vertical scaling status.
   124  
   125      ```bash
   126      kbcli cluster list <name>
   127      ```
   128  
   129      ***Example***
   130  
   131      ```bash
   132      kbcli cluster list pg-cluster
   133      >
   134      NAME              NAMESPACE        CLUSTER-DEFINITION            VERSION                TERMINATION-POLICY   STATUS    CREATED-TIME
   135      pg-cluster        default          postgresql-cluster            postgresql-14.7.0      Delete               Running   Mar 03,2023 18:00 UTC+0800
   136      ```
   137  
   138     - STATUS=VerticalScaling: it means the vertical scaling is in progress.
   139     - STATUS=Running: it means the vertical scaling has been applied.
   140     - STATUS=Abnormal: it means the vertical scaling is abnormal. The reason may be the normal instances number is less than the total instance number or the leader instance is running properly while others are abnormal.
   141       > To solve the problem, you can check manually to see whether resources are sufficient. If AutoScaling is supported, the system recovers when there are enough resources, otherwise, you can create enough resources and check the result with kubectl describe command.
   142  
   143  :::note
   144  
   145  Vertical scaling does not synchronize parameters related to CPU and memory and it is required to manually call the opsRequest of configuration to change parameters accordingly. Refer to [Configuration](./../configuration/configuration.md) for instructions.
   146  
   147  :::
   148  
   149  3. Check whether the corresponding resources change.
   150  
   151      ```bash
   152      kbcli cluster describe pg-cluster
   153      ```