github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/user_docs/connect_database/connect-database-in-production-environment.md (about)

     1  ---
     2  title: Connect database in production environment
     3  description: How to connect to a database in production environment
     4  keywords: [connect to a database, production environment]
     5  sidebar_position: 3
     6  sidebar_label: Production environment
     7  ---
     8  
     9  # Connect database in production environment
    10  
    11  In the production environment, it is normal to connect a database with CLI and SDK clients. There are three scenarios.
    12  
    13  - Scenario 1: Client1 and the database are in the same Kubernetes cluster. To connect client1 and the database, see [Procedure 3](#procedure-3-connect-database-in-the-same-kubernetes-cluster).
    14  - Scenario 2: Client2 is outside the Kubernetes cluster, but it is in the same VPC as the database. To connect client2 and the database, see [Procedure 5](#procedure-5-client-outside-the-kubernetes-cluster-but-in-the-same-vpc-as-the-kubernetes-cluster).
    15  - Scenario 3: Client3 and the database are in different VPCs, such as other VPCs or the public network. To connect client3 and the database, see [Procedure 4](#procedure-4-connect-database-with-clients-in-other-vpcs-or-public-networks).
    16  
    17  See the figure below to get a clear image of the network location.
    18  
    19  ![Example](./../../img/connect_database_in_a_production_environment.png)
    20  
    21  ## Procedure 3. Connect database in the same Kubernetes cluster
    22  
    23  You can connect with the database ClusterIP or domain name. To check the database endpoint, use `kbcli cluster describe ${cluster-name}`.
    24  
    25  ```bash
    26  kbcli cluster describe x
    27  >
    28  Name: x         Created Time: Mar 01,2023 11:45 UTC+0800
    29  NAMESPACE   CLUSTER-DEFINITION   VERSION           STATUS    TERMINATION-POLICY
    30  default     apecloud-mysql       ac-mysql-8.0.30   Running   Delete
    31  
    32  Endpoints:
    33  COMPONENT   MODE        INTERNAL                                 EXTERNAL
    34  x           ReadWrite   x-mysql.default.svc.cluster.local:3306   <none>
    35  
    36  Topology:
    37  COMPONENT   INSTANCE    ROLE     STATUS    AZ                NODE                                                       CREATED-TIME
    38  mysql       x-mysql-0   leader   Running   cn-northwest-1b   ip-10-0-2-184.cn-northwest-1.compute.internal/10.0.2.184   Mar 01,2023 11:45 UTC+0800
    39  
    40  Resources Allocation:
    41  COMPONENT   DEDICATED   CPU(REQUEST/LIMIT)   MEMORY(REQUEST/LIMIT)   STORAGE-SIZE   STORAGE-CLASS
    42  mysql       false       1 / 1                1Gi / 1Gi               data:10Gi      <none>
    43  
    44  Images:
    45  COMPONENT   TYPE    IMAGE
    46  mysql       mysql   registry.cn-hangzhou.aliyuncs.com/apecloud/apecloud-mysql-server:8.0.30-5.alpha2.20230105.gd6b8719.2
    47  
    48  Events(last 5 warnings, see more:kbcli cluster list-events -n default x):
    49  TIME   TYPE   REASON   OBJECT   MESSAGE
    50  ```
    51  
    52  ## Procedure 4. Connect database with clients in other VPCs or public networks
    53  
    54  You can enable the External LoadBalancer of the cloud vendor.
    55  
    56  :::note
    57  
    58  The following command creates a LoadBalancer instance for the database instance, which may incur expenses from your cloud vendor.
    59  
    60  :::
    61  
    62  ```bash
    63  kbcli cluster expose ${cluster-name} --type internet --enable=true
    64  ```
    65  
    66  To disable the LoadBalancer instance, execute the following command.
    67  
    68  ```bash
    69  kbcli cluster expose ${cluster-name} --type internet --enable=false
    70  ```
    71  
    72  :::note
    73  
    74  The instance is inaccessible after you disable the LoadBalancer instance.
    75  
    76  :::
    77  
    78  ## Procedure 5. Client outside the Kubernetes cluster but in the same VPC as the Kubernetes cluster
    79  
    80  A stable domain name for long-term connections is required. An Internal LoadBalancer provided by the cloud vendor can be used for this purpose.
    81  
    82  :::note
    83  
    84  The following command creates a LoadBalancer instance for the database instance, which may incur expenses from your cloud vendor.
    85  
    86  :::
    87  
    88  ```bash
    89  kbcli cluster expose ${cluster-name} --type vpc --enable=true
    90  ```
    91  
    92  To disable the LoadBalancer instance, execute the following command.
    93  
    94  :::note
    95  
    96  Once disabled, the instance is not accessible.
    97  
    98  :::
    99  
   100  ```bash
   101  kbcli cluster expose ${cluster-name} --type vpc --enable=false
   102  ```