Search…
Adding Database Containers to Application Templates
Best practices for running your application with databases running in a container
Adding a database to your application for durability is a common use case and this document should cover the basic use cases for adding a database to your application. There are a few caveats that come with running databases in containers which we'll cover here:
  • Containers and pods in Kubernetes can be restarted and deleted and moved at any time, so they should not be relied upon for any critical services
  • Because containers are ephemeral and can move around, it is vital to use a persistent storage volume so that restarting a container does not erase the database!
  • Kubernetes can start a container and assume it's running but the database may not be completely initialized or ready yet. Therefore, you should use readiness probes to ensure the deployments wait until the database is fully ready to receive traffic.
  • Passwords can be randomized and the database connections should be kept internal only so that external people cannot access the data or store their own data in your tables, even if it is a test database.

Postgres

Example Postgres Application Template

services:
- name: db
image: public.ecr.aws/lts/postgres:12-20.04_edge
volumes:
- type: persistent
name: db_data
mount_path: "/var/lib/postgresql/data"
ports:
- type: container_port
port: '5432'
readiness_probe:
exec:
command:
- psql
- "-h"
- localhost
- "-U"
- postgres
- "-c"
- SELECT 1
period_seconds: 2
timeout_seconds: 2
failure_threshold: 30
storage:
size: 10Gi

Example Postgres Environment Template

mapping:
DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db/${POSTGRES_DB}"
services:
db:
- key: POSTGRES_PASSWORD
value: secretPassword
secret: true
- key: POSTGRES_USER
value: postgres
- key: POSTGRES_DB
value: postgres
- key: POSTGRES_HOST_AUTH_METHOD
value: trust

MySQL

Example MySQL Appliation Template

- name: db
image: mysql:5.7
volumes:
- type: persistent
name: db_data
mount_path: "/var/lib/mysql/data"
ports:
- type: container_port
port: '5432'
liveness_probe:
exec:
command: ["mysqladmin", "ping"]
initial_delay_seconds: 30
period_seconds: 10
timeout_seconds: 5
failure_threshold: 10
readiness_probe:
exec:
# Check we can execute queries over TCP (skip-networking is off).
command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
initial_delay_seconds: 5
period_seconds: 2
timeout_seconds: 1
failure_threshold: 30
storage:
size: 10Gi

Example MySQL Environment Template

services:
db:
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "1"

Notes

Copy link
On this page
Postgres
MySQL
Notes