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

     1  ---
     2  title: Create and connect to a PostgreSQL Cluster
     3  description: How to create and connect to a PostgreSQL cluster
     4  keywords: [postgresql, create a postgresql cluster, connect to a postgresql cluster]
     5  sidebar_position: 1
     6  sidebar_label: Create and connect
     7  ---
     8  
     9  import Tabs from '@theme/Tabs';
    10  import TabItem from '@theme/TabItem';
    11  
    12  # Create and connect to a PostgreSQL cluster
    13  
    14  This tutorial shows how to create and connect to a PostgreSQL cluster.
    15  
    16  ## Create a PostgreSQL cluster
    17  
    18  ### Before you start
    19  
    20  * [Install kbcli](./../../installation/install-with-kbcli/install-kbcli.md) if you want to create and connect a cluster by kbcli.
    21  * Install KubeBlocks: You can install KubeBlocks by [kbcli](./../../installation/install-with-kbcli/install-kubeblocks-with-kbcli.md) or by [Helm](./../../installation/install-with-helm/install-kubeblocks-with-helm.md).
    22  * Make sure the PostgreSQL add-on is enabled.
    23    
    24    <Tabs>
    25  
    26    <TabItem value="kbcli" label="kbcli" default>
    27    
    28    ```bash
    29    kbcli addon list
    30    >
    31    NAME                       TYPE   STATUS     EXTRAS         AUTO-INSTALL   INSTALLABLE-SELECTOR
    32    ...
    33    postgresql                 Helm   Enabled                   true
    34    ...
    35    ```
    36  
    37    </TabItem>
    38  
    39    <TabItem value="kubectl" label="kubectl">
    40  
    41    ```bash
    42    kubectl get addons.extensions.kubeblocks.io postgresql
    43    >
    44    NAME         TYPE   STATUS    AGE
    45    postgresql   Helm   Enabled   23m
    46    ```
    47  
    48    </TabItem>
    49    </Tabs>
    50  
    51  * View all the database types and versions available for creating a cluster.
    52  
    53    <Tabs>
    54  
    55    <TabItem value="kbcli" label="kbcli" default>
    56  
    57    ```bash
    58    kbcli clusterdefinition list
    59  
    60    kbcli clusterversion list
    61    ```
    62  
    63    </TabItem>
    64  
    65    <TabItem value="kubectl" label="kubectl">
    66    
    67    Make sure the `postgresql` cluster definition is installed with `kubectl get clusterdefinitions postgresql`.
    68  
    69    ```bash
    70    kubectl get clusterdefinition postgresql
    71    >
    72    NAME         MAIN-COMPONENT-NAME   STATUS      AGE
    73    postgresql   postgresql            Available   25m
    74    ```
    75  
    76    View all available versions for creating a cluster
    77  
    78    ```bash
    79    kubectl get clusterversions -l clusterdefinition.kubeblocks.io/name=postgresql
    80    ```
    81  
    82    </TabItem>
    83  
    84    </Tabs>
    85  
    86  * To keep things isolated, create a separate namespace called `demo` throughout this tutorial.
    87  
    88    ```bash
    89    kubectl create namespace demo
    90    ```
    91  
    92  ### Create a cluster
    93  
    94  KubeBlocks supports creating two types of PostgreSQL clusters: Standalone and Replication Cluster. Standalone only supports one replica and can be used in scenarios with lower requirements for availability. For scenarios with high availability requirements, it is recommended to create a Replication Cluster, which creates a cluster with a Replication Cluster to support automatic failover. And to ensure high availability, Primary and Secondary are distributed on different nodes by default.
    95  
    96  <Tabs>
    97  
    98  <TabItem value="kbcli" label="kbcli" default>
    99  
   100  Create a Standalone.
   101  
   102  ```bash
   103  kbcli cluster create postgresql <clustername>
   104  ```
   105  
   106  Create a Replication Cluster.
   107  
   108  ```bash
   109  kbcli cluster create postgresql --mode replication <clustername>
   110  ```
   111  
   112  If you only have one node for deploying a Replication, set the `availability-policy` as `none` when creating a Replication Cluster.
   113  
   114  ```bash
   115  kbcli cluster create postgresql --mode replication --availability-policy none <clustername>
   116  ```
   117  
   118  :::note
   119  
   120  * In the production environment, it is not recommended to deploy all replicas on one node, which may decrease cluster availability.
   121  * Run the command below to view the flags for creating a PostgreSQL cluster and the default values.
   122    
   123    ```bash
   124    kbcli cluster create postgresql -h
   125    ```
   126  
   127  :::
   128  
   129  </TabItem>
   130  
   131  <TabItem value="kubectl" label="kubectl">
   132  
   133  KubeBlocks implements a `Cluster` CRD to define a cluster. Here is an example of creating a Standalone.
   134  
   135    ```bash
   136    cat <<EOF | kubectl apply -f -
   137    apiVersion: apps.kubeblocks.io/v1alpha1
   138    kind: Cluster
   139    metadata:
   140      name: pg-cluster
   141      namespace: demo
   142      labels: 
   143        helm.sh/chart: postgresql-cluster-0.6.0-alpha.36
   144        app.kubernetes.io/version: "14.8.0"
   145        app.kubernetes.io/instance: pg
   146    spec:
   147      clusterVersionRef: postgresql-14.8.0
   148      terminationPolicy: Delete  
   149      affinity:
   150        podAntiAffinity: Preferred
   151        topologyKeys:
   152          - kubernetes.io/hostname
   153        tenancy: SharedNode
   154      clusterDefinitionRef: postgresql
   155      componentSpecs:
   156        - name: postgresql
   157          componentDefRef: postgresql      
   158          monitor: false      
   159          replicas: 1
   160          enabledLogs:
   161            - running
   162          serviceAccountName: kb-pg
   163          switchPolicy:
   164            type: Noop      
   165          resources:
   166            limits:
   167              cpu: "0.5"
   168              memory: "0.5Gi"
   169            requests:
   170              cpu: "0.5"
   171              memory: "0.5Gi"      
   172          volumeClaimTemplates:
   173            - name: data # ref clusterDefinition components.containers.volumeMounts.name
   174              spec:
   175                accessModes:
   176                  - ReadWriteOnce
   177                resources:
   178                  requests:
   179                    storage: 20Gi      
   180          services:
   181    EOF
   182    ```
   183  
   184  * `spec.clusterDefinitionRef` is the name of the cluster definition CRD that defines the cluster components.
   185  * `spec.clusterVersionRef` is the name of the cluster version CRD that defines the cluster version.
   186  * `spec.componentSpecs` is the list of components that define the cluster components.
   187  * `spec.componentSpecs.componentDefRef` is the name of the component definition that is defined in the cluster definition, you can get the component definition names with `kubectl get clusterdefinition postgresql -o json | jq '.spec.componentDefs[].name'`.
   188  * `spec.componentSpecs.name` is the name of the component.
   189  * `spec.componentSpecs.replicas` is the number of replicas of the component.
   190  * `spec.componentSpecs.resources` is the resource requirements of the component.
   191  * `spec.componentSpecs.volumeClaimTemplates` is the list of volume claim templates that define the volume claim templates for the component.
   192  * `spec.terminationPolicy` is the policy of cluster termination. The default value is `Delete`. Valid values are `DoNotTerminate`, `Halt`, `Delete`, `WipeOut`. `DoNotTerminate` blocks delete operation. `Halt` deletes workload resources such as statefulset and deployment workloads but keep PVCs. `Delete` is based on Halt and deletes PVCs. `WipeOut` is based on Delete and wipe out all volume snapshots and snapshot data from the backup storage location.
   193  
   194  KubeBlocks operator watches for the `Cluster` CRD and creates the cluster and all dependent resources. You can get all the resources created by the cluster with `kubectl get all,secret,rolebinding,serviceaccount -l app.kubernetes.io/instance=pg-cluster -n demo`.
   195  
   196  ```bash
   197  kubectl get all,secret,rolebinding,serviceaccount -l app.kubernetes.io/instance=pg-cluster -n demo
   198  ```
   199  
   200  Run the following command to see the created PostgreSQL cluster object:
   201  
   202  ```bash
   203  kubectl get cluster pg-cluster -n demo -o yaml
   204  ```
   205  
   206  <details>
   207  
   208  <summary>Output</summary>
   209  
   210  ```yaml
   211  apiVersion: apps.kubeblocks.io/v1alpha1
   212  kind: Cluster
   213  metadata:
   214    annotations:
   215      kubectl.kubernetes.io/last-applied-configuration: |
   216        {"apiVersion":"apps.kubeblocks.io/v1alpha1","kind":"Cluster","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"pg","app.kubernetes.io/version":"14.8.0","helm.sh/chart":"postgresql-cluster-0.6.0-alpha.36"},"name":"pg-cluster","namespace":"demo"},"spec":{"affinity":{"podAntiAffinity":"Preferred","tenancy":"SharedNode","topologyKeys":["kubernetes.io/hostname"]},"clusterDefinitionRef":"postgresql","clusterVersionRef":"postgresql-14.8.0","componentSpecs":[{"componentDefRef":"postgresql","enabledLogs":["running"],"monitor":false,"name":"postgresql","replicas":1,"resources":{"limits":{"cpu":"0.5","memory":"0.5Gi"},"requests":{"cpu":"0.5","memory":"0.5Gi"}},"serviceAccountName":"kb-pg","services":null,"switchPolicy":{"type":"Noop"},"volumeClaimTemplates":[{"name":"data","spec":{"accessModes":["ReadWriteOnce"],"resources":{"requests":{"storage":"20Gi"}}}}]}],"terminationPolicy":"Delete"}}
   217    creationTimestamp: "2023-07-19T07:53:07Z"
   218    finalizers:
   219    - cluster.kubeblocks.io/finalizer
   220    generation: 1
   221    labels:
   222      app.kubernetes.io/instance: pg
   223      app.kubernetes.io/version: 14.8.0
   224      clusterdefinition.kubeblocks.io/name: postgresql
   225      clusterversion.kubeblocks.io/name: postgresql-14.8.0
   226      helm.sh/chart: postgresql-cluster-0.6.0-alpha.36
   227    name: pg-cluster
   228    namespace: demo
   229    resourceVersion: "8618"
   230    uid: c9f73d21-b79b-4956-aad0-a4e677cb8ba1
   231  spec:
   232    affinity:
   233      podAntiAffinity: Preferred
   234      tenancy: SharedNode
   235      topologyKeys:
   236      - kubernetes.io/hostname
   237    clusterDefinitionRef: postgresql
   238    clusterVersionRef: postgresql-14.8.0
   239    componentSpecs:
   240    - componentDefRef: postgresql
   241      enabledLogs:
   242      - running
   243      monitor: false
   244      name: postgresql
   245      noCreatePDB: false
   246      replicas: 1
   247      resources:
   248        limits:
   249          cpu: "0.5"
   250          memory: 0.5Gi
   251        requests:
   252          cpu: "0.5"
   253          memory: 0.5Gi
   254      serviceAccountName: kb-pg
   255      switchPolicy:
   256        type: Noop
   257      volumeClaimTemplates:
   258      - name: data
   259        spec:
   260          accessModes:
   261          - ReadWriteOnce
   262          resources:
   263            requests:
   264              storage: 20Gi
   265    terminationPolicy: Delete
   266  status:
   267    clusterDefGeneration: 2
   268    components:
   269      postgresql:
   270        phase: Running
   271        podsReady: true
   272        podsReadyTime: "2023-07-19T07:53:43Z"
   273        replicationSetStatus:
   274          primary:
   275            pod: pg-cluster-postgresql-0
   276    conditions:
   277    - lastTransitionTime: "2023-07-19T07:53:07Z"
   278      message: 'The operator has started the provisioning of Cluster: pg-cluster'
   279      observedGeneration: 1
   280      reason: PreCheckSucceed
   281      status: "True"
   282      type: ProvisioningStarted
   283    - lastTransitionTime: "2023-07-19T07:53:07Z"
   284      message: Successfully applied for resources
   285      observedGeneration: 1
   286      reason: ApplyResourcesSucceed
   287      status: "True"
   288      type: ApplyResources
   289    - lastTransitionTime: "2023-07-19T07:53:43Z"
   290      message: all pods of components are ready, waiting for the probe detection successful
   291      reason: AllReplicasReady
   292      status: "True"
   293      type: ReplicasReady
   294    - lastTransitionTime: "2023-07-19T07:53:43Z"
   295      message: 'Cluster: pg-cluster is ready, current phase is Running'
   296      reason: ClusterReady
   297      status: "True"
   298      type: Ready
   299    observedGeneration: 1
   300    phase: Running
   301  ```
   302  
   303  </details>
   304  
   305  </TabItem>
   306  
   307  </Tabs>
   308  
   309  ## Connect to a PostgreSQL Cluster
   310  
   311  <Tabs>
   312  
   313  <TabItem value="kbcli" label="kbcli" default>
   314  
   315  ```bash
   316  kbcli cluster connect <clustername>  --namespace <name>
   317  ```
   318  
   319  </TabItem>
   320  
   321  <TabItem value="kubectl" label="kubectl">
   322  
   323  You can use `kubectl exec` to exec into a Pod and connect to a database.
   324  
   325  KubeBlocks operator has created a new Secret called `pg-cluster-conn-credential` to store the connection credential of the `pg-cluster` cluster. This secret contains following keys:
   326  
   327  * `username`: the root username of the PostgreSQL cluster.
   328  * `password`: the password of root user.
   329  * `port`: the port of the PostgreSQL cluster.
   330  * `host`: the host of the PostgreSQL cluster.
   331  * `endpoint`: the endpoint of the PostgreSQL cluster and it is the same as `host:port`.
   332  
   333  1. Run the command below to get the `username` and `password` for the `kubectl exec` command.
   334  
   335     ```bash
   336     kubectl get secrets -n demo pg-cluster-conn-credential -o jsonpath='{.data.\username}' | base64 -d
   337     >
   338     postgres
   339  
   340     kubectl get secrets -n demo pg-cluster-conn-credential -o jsonpath='{.data.\password}' | base64 -d
   341     >
   342     h62rg2kl
   343     ```
   344  
   345  2. Exec into the Pod `pg-cluster-postgresql-0` and connect to the database using username and password.
   346  
   347     ```bash
   348     kubectl exec -ti -n demo pg-cluster-postgresql-0 -- bash
   349  
   350     root@pg-cluster-postgresql-0:/home/postgres# psql -U postgres -W
   351     Password: h62rg2kl
   352     ```
   353  
   354  </TabItem>
   355  
   356  <TabItem value="port-forward" label="port-forward">
   357  
   358  You can also port forward the service to connect to the database from your local machine.
   359  
   360  1. Run the following command to port forward the service.
   361  
   362     ```bash
   363     kubectl port-forward -n demo svc/pg-cluster-postgresql 5432:5432 
   364     ```
   365  
   366  2. Open a new terminal and run the following command to connect to the database.
   367  
   368     ```bash
   369     root@pg-cluster-postgresql-0:/home/postgres# psql -U postgres -W
   370     Password: h62rg2kl
   371     ```
   372  
   373  </TabItem>
   374  
   375  </Tabs>
   376  
   377  For the detailed database connection guide, refer to [Connect database](./../../connect_database/overview-of-database-connection.md).