Updating a Postgres Instance Configuration

This topic describes how to update CPU, memory, and storage configuration of an existing Postgres instance.

To update an existing Postgres instance for high availability or backup, see Configuring High Availability in Tanzu Postgres, and Backing Up and Restoring Tanzu Postgres.

Prerequisites

The steps in this topic require:

  • the kubectl command line tool installed on a client that accesses the Kubernetes cluster.
  • appropriate access permissions to the Kubernetes cluster project and namespace where the Postgres instances reside.
  • access permissions to the running Postgres instances to be updated.
  • access permissions to the Kubernetes storageclass.

Modifying Memory and CPU

The memory and CPU allocation are specified in the instance yaml manifest file created during instance deployment. Edit the file to make the required changes. Before increasing any values, ensure that the Kubernetes cluster does not have any limiting resource quotas. See the Kubernetes Resource Quotas documenation for more information.

  1. Move to the Tanzu Postgres workspace directory with the Postgres instance Kubernetes manifest file.

    $ cd ./postgres-for-kubernetes-v<version>
    
  2. Edit the manifest yaml file you used to deploy the instance; in this example the file is called pg-instance-example.yaml. Set new values for the memory and cpu attributes.

    For example:

    apiVersion: sql.tanzu.vmware.com/v1
    kind: Postgres
    metadata:
      name: pg-instance-example
    spec:
      memory: 2G
      cpu: "1.5"
      storageClassName: standard
      storageSize: 10G
      pgConfig:
         dbname: pg-instance-example
         username: pgadmin
         serviceType: LoadBalancer
      highAvailability:
         enabled: false
      backupLocationSecret:
         name:
    

    Note: You cannot alter the name, storageClassName, dbname, or username of an existing instance.

  3. Execute the kubectl apply command, specifying the manifest file you edited. For example:

    $ kubectl apply -f ./pg-instance-example.yaml --wait=false
    
    sql.tanzu.vmware.com "pg-instance-example" configured
    

    If the manifest file contains any incorrectly formatted values or unrecognized field names, an error message is displayed identifying the issue. Edit the manifest to correct the error and run the command again.

  4. Verify the updated configuration by specifying the memory and cpu fields of the instance object.

    $ kubectl get postgres/pg-instance-example -o jsonpath='{.spec.memory}'
    
    1G
    
    $ kubectl get postgresinstance/pg-instance-example -o jsonpath='{.spec.cpu}'
    
    1.0
    

Modifying Storage Volume Size

To expand a Postgres instance storage volume, verify that the storage volume is expandable and then update the instance yaml file.

The Postgres operator sets the sizes of the Postgres data volume at instance initialization. The actual size of the expanded volumes may be greater than your specified value if the storage manager allocates space in fixed increments.

Note: Kubernetes does not support shrinking a volume size.

Verifying Volume Expansion

To expand the PV volumes, review the storage class object and check if the allowVolumeExpansion field is set to true. If the attribute does not exist, you may add it to the storage class, and then expand the storage volumes. The following steps describe this process.

Note: Minikube does not support volume expansion. If you set allowVolumeExpansion to true in Minikube and request a larger volume size, it fails with an error message.

  1. Show the current Persistent Volume(s) (PVs).

    $ kubectl get pv
    
    NAME                                       CAPACITY  ACCESS MODES    RECLAIM POLICY   STATUS   CLAIM                                             STORAGECLASS      AGE
    pvc-30a235f8-6360-4a34-a3bb-fd469bfc8c51   5G        RWO             Delete           Bound    default/pg-instance-example-pgdata-pg-instance-example-0            standard          64s
    pvc-85cb368d-632b-402d-9f57-0256673a2fde   1G        RWO             Delete           Bound    default/pg-instance-example-monitor-pg-instance-example-monitor-0   standard          64s
    
  2. Check the standard storage class’s allowVolumeExpansion attribute value:

    $ kubectl get storageclass standard
    
    NAME                 PROVISIONER                RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
    standard (default)   k8s.io/minikube-hostpath   Delete          Immediate           false                  20h
    
  3. Modify the storage class after checking the permissions:

    kubectl auth can-i update storageclass
    
    yes
    
  4. Amend the storage class configuration after saving it to a local yaml file:

    $ kubectl get storageclass standard -o yaml > storagesize.yaml
    
  5. Edit the saved file and change the allowVolumeExpansion attribute to true, or add the attribute if it is not already present.

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
       name: standard
    provisioner: kubernetes.io/aws-ebs # AWS specific
    reclaimPolicy: Retain
    allowVolumeExpansion: true
    mountOptions:
      - debug
    volumeBindingMode: Immediate
    

    For more information about expanding volumes, see the Kubernetes Allow Volume Expansion documentation.

  6. Apply the change.

    $ kubectl apply -f storagesize.yaml
    
  7. Verify the change.

    $ kubectl get storageclass standard --output=jsonpath='{.allowVolumeExpansion}'
    
    true
    

See the Kubernetes Documentation for more information about storage classes and persistent volumes.

Increasing Volume Size

  1. Edit the manifest file that was used to deploy the instance. Set the value for the storageSize attribute to the desired size:

    apiVersion: sql.tanzu.vmware.com/v1
    kind: Postgres
    metadata:
      name: pg-instance-example
    spec:
      memory: 2G
      cpu: "1.5"
      storageClassName: standard
      storageSize: 2Gi
      pgConfig:
         dbname: pg-instance-example
         username: pgadmin
         serviceType: LoadBalancer
      highAvailability:
         enabled: false
      backupLocationSecret:
         name:
    
  2. Apply the edited manifest to the Postgres instance.

    $ kubectl apply -f pg-instance-example.yaml
    
    postgres.sql.tanzu.vmware.com/pg-instance-example configured
    

    If the manifest file contains any incorrectly formatted values or unrecognized field names, an error message is displayed identifying the issue. Edit the manifest to correct the error and run the command again.

  3. Verify that the persistent volume size has increased.

    $ watch kubectl get pv
    
    NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                          STORAGECLASS   REASON   AGE
    pvc-8117e02b-a886-4c6c-92c9-e30cd21562c7   2Gi        RWO            Delete           Bound    default/pg-instance-example-pgdata-pg-instance-example-0       standard                42m
    

    If the storage class does not have the allowVolumeExpansion attribute set to true, the persistent volumes will not be expanded. No message is immediately displayed, but errors are written to the Postgres operator logs. View the logs with commands like the following.

    $ kubectl get pods
    
    NAME                                 READY   STATUS    RESTARTS   AGE
    pg-instance-example-0                        1/1     Running   0          15m
    postgres-operator-54fb679bc5-p8lps   1/1     Running   0          30m
    
    $ kubectl logs postgres-operator-54fb679bc5-p8lps
    
    INFO    controllers.PersistentVolumeClaims Reconciler    Error updating PVC resources: persistentvolumeclaims "pg-instance-example-pgbackrest-pg-instance-example-0" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize
    ERROR    controllers.PostgresInstance    found error reconciling backup persistent volume claim    {"error": "persistentvolumeclaims \"pg-instance-example-pgbackrest-pg-instance-example-0\" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize"}
    

    If the persistent volumes could not be resized, edit the StorageSize attribute in the manifest to match the actual size.