External Database Connection from OpenShift

Learn how Spring Boot applications running on OpenShift securely connect to external databases such as PostgreSQL, Oracle, MySQL, SQL Server, and cloud databases. Understand networking, Secrets, ConfigMaps, connection pooling, and production best practices.


Introduction

In real enterprise environments, databases are rarely deployed inside the same OpenShift cluster as the application.

Instead, applications running in OpenShift connect to databases hosted on:

  • Amazon RDS
  • Azure SQL Database
  • Google Cloud SQL
  • Oracle Database
  • SQL Server
  • PostgreSQL
  • MySQL
  • Enterprise Data Centers

This architecture improves:

  • Security
  • Scalability
  • High Availability
  • Disaster Recovery
  • Database Administration

In this article, you'll learn how a Spring Boot application securely connects to an external database from OpenShift.


Learning Objectives

By the end of this article, you will understand:

  • Why external databases are used
  • OpenShift networking
  • Database connection architecture
  • ConfigMaps and Secrets
  • Spring Boot datasource configuration
  • Connection pooling with HikariCP
  • Firewall and networking considerations
  • Enterprise best practices

Why External Databases?

Running databases outside the OpenShift cluster provides several advantages.

  • Independent scaling
  • Centralized database management
  • Better backup strategy
  • High availability
  • Disaster recovery
  • Enterprise security

High-Level Architecture

flowchart LR
    User[User]
    --> Route[OpenShift Route]

    Route
    --> Service[ClusterIP Service]

    Service
    --> Pod[Spring Boot Pod]

    Pod
    --> Database[(External PostgreSQL)]

The application runs inside OpenShift while the database resides outside the cluster.


Enterprise Architecture

flowchart TD
    Internet
    --> LoadBalancer

    LoadBalancer
    --> OpenShift

    OpenShift
    --> SpringBoot[Spring Boot Pods]

    SpringBoot
    --> Firewall

    Firewall
    --> PostgreSQL[(Amazon RDS)]

    PostgreSQL
    --> Backup[(Automated Backup)]

This architecture is commonly used in banking, insurance, healthcare, and retail.


Request Flow

sequenceDiagram
    participant User
    participant SpringBoot
    participant HikariCP
    participant Database

    User->>SpringBoot: REST API Request
    SpringBoot->>HikariCP: Request Connection
    HikariCP->>Database: SQL Query
    Database-->>SpringBoot: Result
    SpringBoot-->>User: JSON Response

Database Connectivity

Spring Boot requires four basic properties:

  • Database URL
  • Username
  • Password
  • JDBC Driver

Never hardcode these values.


ConfigMap

Store non-sensitive configuration.

apiVersion: v1
kind: ConfigMap

metadata:
  name: database-config

data:

  DATABASE_HOST: postgres.company.com

  DATABASE_PORT: "5432"

  DATABASE_NAME: paymentdb

Deploy

oc apply -f configmap.yaml

Secret

Store credentials securely.

apiVersion: v1
kind: Secret

metadata:
  name: database-secret

type: Opaque

stringData:

  DATABASE_USERNAME: paymentuser

  DATABASE_PASSWORD: StrongPassword@123

Deploy

oc apply -f secret.yaml

Configuration Architecture

flowchart LR
    ConfigMap
    --> Environment

    Secret
    --> Environment

    Environment
    --> SpringBoot

    SpringBoot
    --> PostgreSQL

Deployment YAML

env:

- name: DATABASE_HOST
  valueFrom:
    configMapKeyRef:
      name: database-config
      key: DATABASE_HOST

- name: DATABASE_PORT
  valueFrom:
    configMapKeyRef:
      name: database-config
      key: DATABASE_PORT

- name: DATABASE_NAME
  valueFrom:
    configMapKeyRef:
      name: database-config
      key: DATABASE_NAME

- name: DATABASE_USERNAME
  valueFrom:
    secretKeyRef:
      name: database-secret
      key: DATABASE_USERNAME

- name: DATABASE_PASSWORD
  valueFrom:
    secretKeyRef:
      name: database-secret
      key: DATABASE_PASSWORD

Spring Boot Configuration

spring.datasource.url=jdbc:postgresql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}

spring.datasource.username=${DATABASE_USERNAME}

spring.datasource.password=${DATABASE_PASSWORD}

PostgreSQL Example

spring.datasource.url=jdbc:postgresql://postgres.company.com:5432/paymentdb

spring.datasource.driver-class-name=org.postgresql.Driver

Dependency

<dependency>

    <groupId>org.postgresql</groupId>

    <artifactId>postgresql</artifactId>

</dependency>

Oracle Example

spring.datasource.url=jdbc:oracle:thin:@oracle.company.com:1521/XEPDB1

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

Dependency

<dependency>

    <groupId>com.oracle.database.jdbc</groupId>

    <artifactId>ojdbc11</artifactId>

</dependency>

MySQL Example

spring.datasource.url=jdbc:mysql://mysql.company.com:3306/paymentdb

SQL Server Example

spring.datasource.url=jdbc:sqlserver://sql.company.com:1433;databaseName=paymentdb

Spring Data JPA

@Entity
public class Payment {

    @Id
    private Long id;

    private String customer;

    private Double amount;

}

Repository

@Repository
public interface PaymentRepository
        extends JpaRepository<Payment,Long>{

}

Connection Pooling

Spring Boot uses HikariCP by default.

flowchart LR
    SpringBoot
    --> HikariCP

    HikariCP
    --> Connection1

    HikariCP
    --> Connection2

    HikariCP
    --> Connection3

    Connection1 --> Database
    Connection2 --> Database
    Connection3 --> Database

Benefits:

  • Faster performance
  • Reduced latency
  • Efficient resource utilization

HikariCP Configuration

spring.datasource.hikari.maximum-pool-size=20

spring.datasource.hikari.minimum-idle=5

spring.datasource.hikari.connection-timeout=30000

spring.datasource.hikari.idle-timeout=600000

Banking Architecture

flowchart TD
    Customers
    --> API

    API
    --> SpringBoot

    SpringBoot
    --> OracleRAC[(Oracle RAC)]

    OracleRAC
    --> Standby[(Disaster Recovery)]

AWS Architecture

flowchart LR
    OpenShift
    --> SpringBoot

    SpringBoot
    --> AmazonRDS[(Amazon RDS PostgreSQL)]

    AmazonRDS
    --> MultiAZ[(Multi-AZ Replica)]

Firewall Architecture

flowchart LR
    SpringBoot

    --> Firewall

    Firewall

    --> Database

Database administrators usually whitelist:

  • OpenShift Worker Nodes
  • Private Network
  • VPN
  • VPC Peering

Health Check

Spring Boot Actuator

management.endpoint.health.show-details=always

Verify

GET

/actuator/health

Connectivity Test

Open Pod

oc rsh payment-api-xxxxx

Test

nc -zv postgres.company.com 5432

Verify Environment Variables

env

Expected

DATABASE_HOST=postgres.company.com

DATABASE_PORT=5432

Common Problems

Cannot Connect

Possible causes:

  • Wrong hostname
  • Wrong port
  • Firewall blocked
  • VPN disconnected

Authentication Failed

Verify

  • Username
  • Password
  • Secret

Connection Timeout

Check

  • Security Groups
  • Firewall
  • Database availability

Too Many Connections

Increase HikariCP pool size carefully.

Close idle connections.


Useful Commands

View Secrets

oc get secrets

View ConfigMaps

oc get configmaps

Describe Pod

oc describe pod payment-api

Open Pod

oc rsh payment-api-xxxxx

Production Best Practices

  • Store credentials in Secrets.
  • Store hostnames in ConfigMaps.
  • Use HikariCP connection pooling.
  • Enable SSL/TLS for database connections.
  • Restrict database access using firewalls.
  • Monitor connection pool metrics.
  • Rotate database passwords regularly.
  • Use least-privilege database accounts.
  • Never expose databases to the public Internet.
  • Enable automated database backups.

Common Mistakes

❌ Hardcoding database credentials.

❌ Using the root database account.

❌ Storing passwords in ConfigMaps.

❌ Disabling connection pooling.

❌ Opening database ports publicly.

❌ Ignoring SSL encryption.


Advantages

  • Secure architecture
  • Independent database scaling
  • Better disaster recovery
  • Cloud-native deployments
  • Centralized database management
  • Improved performance with connection pooling
  • Easier maintenance
  • Enterprise ready

Summary

Connecting Spring Boot applications in OpenShift to external databases is the standard enterprise deployment model.

Key takeaways:

  • External databases improve scalability, security, and operational management.
  • Store connection details in ConfigMaps and credentials in Secrets.
  • Use HikariCP for efficient connection pooling.
  • Protect database traffic with firewalls and SSL/TLS.
  • Monitor database connectivity and pool health in production.

Interview Questions

  1. Why do enterprises use external databases instead of databases inside OpenShift?
  2. Where should database credentials be stored?
  3. What is the difference between ConfigMaps and Secrets?
  4. What is HikariCP, and why is it used?
  5. How do you configure Spring Boot to connect to PostgreSQL?
  6. How do you test database connectivity from a Pod?
  7. Why should databases not be publicly accessible?
  8. How do you secure database communication?
  9. What causes connection timeout errors?
  10. What are the best practices for connecting Spring Boot applications to external databases?