JDBC (Custom, Amazon RDS, GCP Cloud SQL)

The Akka Operator can provide configuration for JDBC databases for use as the event store or in projection. This can be either:

Regardless of which database you’re using you’ll need:

  • Database instance hostname

  • JDBC connection URL e.g. jdbc:postgresql://<hostname>:5432/<database name>?reWriteBatchedInserts=true

  • Username

  • Password

Create tables

To create the tables and other administrative tasks you can connect to the database from a Pod in your Kubernetes cluster.

For instance to do this for PostgreSQL you can create a temporary Pod and run psql to load the ddl sql scripts with:

kubectl run -i rds-mgmt --image=postgres \
  --restart=Never --rm --env "PGPASSWORD=<password>" -- \
  psql -h <hostname> -U postgres -t < ddl-scripts/create_tables.sql

It creates all tables needed for Akka Persistence as well as the offset store table for Akka Projection.

For an interactive psql session you can use:

kubectl run -i --tty rds-mgmt --image=postgres --restart=Never --rm -- \
  psql -h <hostname> -U <username>

JDBC configuration

To use the JDBC integration in the Akka Operator you place the connection credentials in a Secret new tab. The Secret must contain three entries:

  • connectionUrl - the JDBC connection URL

  • username - the database username

  • password - the database password

The Secret can be created with the following kubectl command, replacing the values for your database:

kubectl create secret generic \
    shopping-cart-service-jdbc-secret \
    --from-literal=username=postgres \
    --from-literal=password=tiger \
    --from-literal=connectionUrl="<connection url>"

To enable the JDBC integration you define the name of the secret in jdbc section of the deployment descriptor:

kubernetes/shopping-cart-service-cr.yml:
apiVersion: "v1"
kind: "Namespace"
metadata:
  name: "shopping"
---
apiVersion: akka.lightbend.com/v1
kind: AkkaMicroservice
metadata:
  name: shopping-cart-service
  namespace: "shopping"
spec:
  replicas: 1
  image: <docker-registry>/shopping-cart-service:<tag> (1)
  javaOptions: "-Xlog:gc -XX:InitialRAMPercentage=75 -XX:MaxRAMPercentage=75"
  resources:
    limits:
      memory: "2Gi"
    requests:
      memory: "2Gi"
      cpu: "1"
  jdbc:
    credentialsSecret: shopping-cart-service-jdbc-secret  (2)

Apply the deployment descriptor:

kubectl apply -f kubernetes/shopping-cart-service-cr.yml

The Akka Operator will automatically provide the configuration for the connection based on the Secret when the application starts the ActorSystem.

JDBC configuration details

It can be useful to have access to the database connection settings for additional application specific components in addition to the Akka Persistence journal and snapshot plugins that are automatically set up. The operator sets up the jdbc-connection-settings top level config object with the following properties based on the Secret in the config the application is started with:

jdbc-connection-settings {
  url = "connectionUrl"
  user = "username"
  password = "password"
}