﻿# Proxying databases in Kubernetes via DB-Gateway

> [HTML Version](proxy-db-kubernetes.html)

One of the steps for preparing BRIX infrastructure is to configure proxying of database queries. For this purpose, you can use the DB‑Gateway tool. 

It provides access to the Redis, RabbitMQ, PostgreSQL, and MinIO databases via an entry point in the Kubernetes cluster. This simplifies the process of connecting to and working with databases, and optimizes the number of infrastructure components required to configure proxying.

DB‑Gateway installation consists of the following steps:

1. [Download the helm-chart and the configuration file](#download-chart).

2. [Fill out the configuration file](#configuration-file).

3. [Install the DB-Gateway chart using Helm in the Kubernetes cluster](#install-chart).

4. [Connect to databases](#connect-db).

## Step 1: Download the helm-chart and the configuration file

To install via the internet, obtain the \[OBJECT\] configuration file by running the command:

````
helm repo add brix365 https://charts.brix365.com  
helm repo update  
helm show values brix365/dbgateway > values-dbgateway.yaml

````
Obtaining the configuration file for installation in an isolated environment without internet access

1. On a computer with internet access, download the archive of the DB-Gateway latest chart from the BRIX repository by running the command:

````
helm repo add brix365 https://charts.brix365.com  
helm repo update  
helm pull brix365/dbgateway

2. ````
Copy the \[OBJECT\] chart archive to the server where the installation will be performed.

3. Unzip the \[OBJECT\] chart and copy the default \[OBJECT\] configuration file to \[OBJECT\]:

````
tar -xf dbgateway-X.Y.Z.tgz  
cp dbgateway/values.yaml values-dbgateway.yaml
````

## ````
Step 2: Fill out the configuration file

Fill out the \[OBJECT\] configuration file for DB‑gateway installation:

1. In the \[OBJECT\], set the domain (FQDN) or the IP address of the BRIX application.

2. In the \[OBJECT\] parameter, set the bucket name (in the database storage) where the BRIX files are stored.  
The bucket name in the database must correspond to the **DBbrix365** format.

````
Начало примера

````
Example:

- DBbrix365.

- DBbrix365-dev.

- DBbrix365-prod.

````
конец примера

3. ````
The following changes should be made to the configuration file if the BRIX application web-interface is accessible via HTTPS:

- In the \[OBJECT\] parameter, set the domain (FQDN).

- In the \[OBJECT\] parameter, set the name of the ingress controller class.

- Enable TLS support by setting the\[OBJECT\] parameter to \[OBJECT\].

- Uncomment the parameters of the \[OBJECT\] section.

- For the \[OBJECT\] parameter, set the domain (FQDN) where the BRIX application is available.

- For the \[OBJECT\] parameter, set the \[OBJECT\] name of the \[OBJECT\] type for the domain specified in \[OBJECT\].

4. If high availability is provided, set the required number of replicas in the \[OBJECT\] parameter.

5. In the configuration parameter, the HAProxy configuration is specified with which this tool will run in the Kubernetes cluster. In the \[OBJECT\] section of the HAProxy configuration, specify the list of database servers to which user traffic will be redirected. 

How to configure HAProxy to proxy connection to a database repository

  
The example considers the Redis, RabbittMQ, PostgreSQL, and MinIO databases located outside the Kubernetes cluster and deployed according to the instructions in the [High availability infrastructure](configure-system.md) section. Let's configure proxying of the user traffic for servers with these databases.

````
  
\# HAProxy settings for proxying connection to database storage  
haproxy:  
  ingress:  
    enabled: true  
  # domain (FQDN) where the system is available  
  hostname: brix365-server.your\_domain  
  # bucket name (in the DB storage) for BRIX application  
  ingressClassName: nginx  
  path: /DBbrix365  
  # enable https  
  tls: false  
  #  extraTls:  
  #  - hosts:  
  #      - brix365-server.your\_domain  
  #  secretName: brix365-server.your\_domain-tls  
  # number of replicas to ensure high availability  
  replicaCount: 2  
  containerPorts:  
    - name: http  
      containerPort: 8080  
    - name: redis  
      containerPort: 6379  
    - name: sentinel  
      containerPort: 26379  
    - name: rabbitmq  
      containerPort: 5672  
    - name: rabbitmq-stats  
      containerPort: 15672  
    - name: postgres  
      containerPort: 5000  
    - name: postgresqlro  
      containerPort: 5001  
  # haproxy configuration  
  configuration: |  
         global  
           log stdout format raw local0  
           maxconn 1024  
         defaults  
           log global  
           timeout client  60s  
           timeout connect 60s  
           timeout server 60s  
         ### S3 MinIO ###  
         frontend s3\_main  
           bind :8080  
           default\_backend s3\_main  
         backend s3\_main  
           balance leastconn  
           server s3\_1 192.168.1.241:9000 check inter 2s  
           server s3\_2 192.168.1.242:9000 check inter 2s  
           server s3\_3 192.168.1.243:9000 check inter 2s  
           server s3\_4 192.168.1.244:9000 check inter 2s  
         ### S3 MinIO ###  
         ### Redis ###  
         listen redis\_master  
            option tcplog  
            option clitcpka  
            bind :6379  
            balance first  
            mode tcp  
            timeout queue 2s  
            timeout connect 2s  
            timeout check 1s  
            option srvtcpka  
            timeout server 24h  
            server redis-server1 192.168.1.237:6379 check inter 5s  
            server redis-server2 192.168.1.238:6379 check inter 5s  
            server redis-server3 192.168.1.239:6379 check inter 5s  
          ### Redis ###  
         ### RabbitMQ ###  
         listen rabbitmq  
             bind :5672  
             mode tcp  
             balance roundrobin  
                 server rabbitmq-server1 192.168.1.234:5672 check inter 2s rise 2 fall 3  
                 server rabbitmq-server2 192.168.1.235:5672 check inter 2s rise 2 fall 3  
                 server rabbitmq-server3 192.168.1.236:5672 check inter 2s rise 2 fall 3  
    listen rabbitmq\_management  
        bind :15672  
        balance source  
        server rabbitmq-server1 192.168.1.234:15672 check inter 2s  
        server rabbitmq-server2 192.168.1.235:15672 check inter 2s  
        server rabbitmq-server3 192.168.1.236:15672 check inter 2s  
    ### RabbitMQ ###  
    ### PostgreSQL ###  
    listen postgres\_master  
        bind :5000  
        option tcplog  
        option httpchk OPTIONS /master  
        http-check expect status 200  
        default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions  
        server postgres-server1 192.168.1.251:5432 check port 8008  
        server postgres-server2 192.168.1.252:5432 check port 8008  
        server postgres-server3 192.168.1.253:5432 check port 8008  
  
    listen postgres\_replicas  
        bind :5001  
        option tcplog  
        option httpchk OPTIONS /replica  
        balance roundrobin  
        http-check expect status 200  
        default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions  
        server postgres-server1 192.168.1.251:5432 check port 8008  
        server postgres-server2 192.168.1.252:5432 check port 8008  
        server postgres-server3 192.168.1.253:5432 check port 8008  
    ### PostgreSQL ###  
...

````


6. If you install DB-Gateway chart in an isolated environment without internet access, fill out the connection parameters for the private registry.

How to fill out the connection parameters for the private registry

1. Set the address in the \[OBJECT\] parameter.

2. Set the path in the \[OBJECT\] parameter.

3. Set the secret name with the access permissions for the private registry in the \[OBJECT\] parameter (the secret must be manually created and encrypted in Base64).

````
\# HAProxy settings for proxying connection to the database storage  
haproxy:  
...  
  # Parameters for connecting to the private registry  
  image:  
    # address and path for the private registry  
    registry: registry.example.com  
    repository: /bitnami/haproxy  
    tag: 3.0.5-debian-12-r0  
    # the secret with access permissions to the private registry must be manually created and encrypted in Base64  
    pullSecrets:  
      - myRegistryKeySecretName

````


## Step 3: Install the DB-Gateway chart using Helm in the Kubernetes cluster

Install the DB-Gateway chart in the \[OBJECT\].

To install via the internet, run the command:

````
helm upgrade --install dbgateway brix365/dbgateway -f values-dbgateway.yaml -n dbgateway --create-namespace 

````
To install without internet access, run the command:

````
helm upgrade --install dbgateway ./dbgateway -f values-dbgateway.yaml -n dbgateway --create-namespace````
 

## Step 4: Connect to databases

In the \[OBJECT\] configuration file, fill out the parameters for BRIX application to connect to databases via the configured DB‑Gateway:

- Parameters for connecting to MinIO:

	- \[OBJECT\] — specify \[OBJECT\].

	- \[OBJECT\], \[OBJECT\], \[OBJECT\], \[OBJECT\], \[OBJECT\] и \[OBJECT\] — specify values according to the parameters of the DB storage for which proxying is configured.

- Parameters for connecting to Redis:

````
brix365.db.redisUrl — 'redis://:SecretPassword@redis.dbgateway.svc.cluster.local:6379/0';

- ````
Parameters for connecting to RabbitMQ:

````
brix365.db.amqpUrl — 'amqp://brix365user:SecretPassword@rabbitmq.dbgateway.svc.cluster.local:5672/0'  
brix365.db.amqpManagementUrl — 'http://rabbitmq-stats.dbgateway.svc.cluster.local:15672';

- ````
Parameters for connecting to PostgreSQL:

````
brix365.db.psqlUrl — 'postgres://brix365user:SecretPassword@postgresql.dbgateway.svc.cluster.local:5000/brix365?sslmode=disable'  
brix365.db.roPsqlUrl — 'postgresql://brix365user:SecretPassword@postgresqlro.dbgateway.svc.cluster.local:5001/brix365?sslmode=disable';

````
Read how to fill out these parameters in the [Modify BRIX parameters](change-settings-enterprise.md#changes-config-file) article.