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.
The steps in this topic require:
kubectl command line tool installed on a client that accesses the Kubernetes cluster.storageclass.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 documentation for more information.
Move to the Tanzu Postgres workspace directory with the Postgres instance Kubernetes manifest file.
$ cd ./postgres-for-kubernetes-v<version>
Edit the manifest yaml file you used to deploy the instance; in this example the file is called postgres.yaml. Set new values for the memory and cpu attributes.
For example:
apiVersion: sql.tanzu.vmware.com/v1
kind: Postgres
metadata:
name: postgres-sample
spec:
storageClassName: standard
storageSize: 800M
cpu: "1.5"
memory: 2G
monitorStorageClassName: standard
monitorStorageSize: 1G
resources:
monitor:
limits:
cpu: 800m
memory: 800Mi
requests:
cpu: 800m
memory: 800Mi
metrics:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 100m
memory: 100Mi
pgConfig:
dbname: postgres-sample
username: pgadmin
serviceType: ClusterIP
Note: You cannot alter the name, storageClassName, monitorStorageClassName, dbname, or username of an existing instance.
Execute the kubectl apply command, specifying the manifest file you edited. For example:
$ kubectl apply -f ./postgres.yaml --wait=false
postgres.sql.tanzu.vmware.com "postgres-sample" 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.
Verify the updated configuration by specifying the memory and cpu fields of the instance object.
$ kubectl get postgres/postgres-sample -o jsonpath='{.spec.memory}'
2G
$ kubectl get postgresinstance/postgres-sample -o jsonpath='{.spec.cpu}'
1.5
Similarly, you can update monitor CPU and memory limits and requests too by updating cpu/memory fields in resources.monitor.limits and resources.monitor.requests
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.
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.
Show the current Persistent Volume(s) (PVs).
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-00d130f6-1fb6-49e0-b145-c58e152f76ab 5Gi RWO Delete Bound default/postgres-sample-pgdata-postgres-sample-0 standard 14s
pvc-33371405-6853-49f7-b704-f49ae8771b5b 1Gi RWO Delete Bound default/postgres-sample-monitor-postgres-sample-monitor-0 standard 34s
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
Modify the storage class after checking the permissions:
kubectl auth can-i update storageclass
yes
Amend the storage class configuration after saving it to a local yaml file:
$ kubectl get storageclass standard -o yaml > storagesize.yaml
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.
Apply the change.
$ kubectl apply -f storagesize.yaml
Verify the change.
$ kubectl get storageclass standard --output=jsonpath='{.allowVolumeExpansion}'
true
See the Kubernetes Documentation for more information about storage classes and persistent volumes.
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: postgres-sample
spec:
storageClassName: standard
storageSize: 2G
cpu: "1.5"
memory: 2G
monitorStorageClassName: standard
monitorStorageSize: 1G
resources:
monitor:
limits:
cpu: 800m
memory: 800Mi
requests:
cpu: 800m
memory: 800Mi
metrics:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 100m
memory: 100Mi
pgConfig:
dbname: postgres-sample
username: pgadmin
serviceType: ClusterIP
Apply the edited manifest to the Postgres instance.
$ kubectl apply -f postgres.yaml
postgres.sql.tanzu.vmware.com/postgres-sample 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.
Verify that the persistent volume size has increased.
$ watch kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-00d130f6-1fb6-49e0-b145-c58e152f76ab 2Gi RWO Delete Bound default/postgres-sample-pgdata-postgres-sample-0 standard 14s
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
postgres-sample-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 "postgres-sample-pgbackrest-postgres-sample-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 \"postgres-sample-pgbackrest-postgres-sample-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.
Similarly, you can expand monitor volume size by updating spec.monitorStorageSize if the monitor storage class has allowVolumeExpansion attribute set to true.