The maturation of containerization platforms has changed how people think about creating development environments and has eliminated many inefficiencies for deploying applications. These concept and technologies have made its way into the PostgreSQL ecosystem as well, and tools such as Docker and Kubernetes have enabled teams to run their own “database-as-a-service” on the infrastructure of their choosing.
In this talk, we will cover the following:
- Why containers are important and what they mean for PostgreSQL
- Setting up and managing a PostgreSQL container
- Extending your setup with a pgadmin4 container
- Container orchestration: What this means, and how to use Kubernetes to leverage database-as-a-service with PostgreSQL
- Trends in the container world and how it will affect PostgreSQL
An Introduction to Using PostgreSQL with Docker & Kubernetes
1. An Introduction to Using
PostgreSQL with
Docker & Kubernetes
Jonathan S. Katz
DC PostgreSQL User Group
April 2, 2018
1
2. About Crunchy Data
• Leading provider of trusted
open source PostgreSQL and
PostgreSQL related
technologies, support and
training to enterprises.
• We're Hiring!
• crunchydata.com
2
3. About Me
• Director of Communications &
Customer Success, Crunchy Data
• Longtime PostgreSQL community
contributor
• Director, PgUS
• Co-Organizer, NYCPUG
• @postgresql + .org content
• Conference organization +
speaking galore!
• @jkatz05
!3
4. Outline
• Containers: A Brief History
• Containers + PostgreSQL
• Setting up PostgreSQL with Containers
• Deploying! - Container Orchestration
• Look Ahead: Trends in the Container World
!4
5. What are Containers?
• Containers are processes that
contain all the requirements to
execute an application
• Similar to virtual machines,
Sandbox for applications
similar to a virtual machine but
with increased density on a
single host
5
Source: Docker
6. Container Glossary
• Image
• The base filesystem that defines what to load in a
container runtime. This is static.
• Container
• The runtime loads an image and instructions on how to
set up the run time environment. File changes can be
persisted to external mounts
https://docs.docker.com/glossary/
!6
7. Why Containers?
• Lightweight
• Compared to VMs, use less disk, RAM, CPU
• Ability to sandbox
• Runtime is in an isolated environment
• Portability
• Can run containers on different platforms
!7
8. Container Runtimes
• Open Containers Initiative (OCI)
• Developing standards for how containers should run
and interface with platforms
• containerd, Docker, runC, rkt
!8
9. Container Example:
Web Application
Create Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /path/to/app/code/
WORKDIR /path/to/app/code/
ADD requirements.txt /path/to/app/code/
RUN pip install -r requirements.txt
ADD . /path/to/app/code/
!9
1.11.11
7.4
13. Containers & PostgreSQL
• Containers provide several advantages to running
PostgreSQL:
• Setup & distribution for developer environments
• Ease of packaging extensions & minor upgrades
• Separate out secondary applications (monitoring,
administration)
• Automation and scale for provisioning and creating
replicas, backups
Images sourced: docker.com & postgresql.org!13
14. Containers & PostgreSQL
• Containers also introduce several challenges:
• Administrator needs to understand and select appropriate
storage options
• Configuration for individual database specifications and
user access
• Managing 100s - 1000s of containers requires appropriate
orchestration (more on that later)
• However, these are challenges you will find in most database
environments
Images sourced: docker.com & postgresql.org!14
15. • We will use the Crunchy Container Suite
• PostgreSQL (+ PostGIS): our favorite database; option to add our favorite geospatial extension
• pgpool + pgbouncer: connection pooling, load balancing
• pgbackrest: terabyte-scale backup management
• Monitoring: Prometheus + export
• Failover: "crunchy-watch"
• pgadmin4: UX-driven management
• Open source!
• Apache 2.0 license
• Support for Docker 1.12+, Kubernetes 1.5+
• Actively maintained and updated
Getting Started With
Containers + PostgreSQL
https://github.com/CrunchyData/crunchy-containers!15
22. Recap So Far
• Explored what / why / how of containers
• Set up a PostgreSQL 10 instance that can be managed
with pgadmin4 in a development environment
!22
24. Kubernetes:
Container Orchestration
• "Open-source system for
automating deployment,
scaling, and management of
containerized applications."
• Manage the full lifecycle of a
container
• Pods => Services
• Nodes => Cluster
• Volumes
24
Source: https://kubernetes.io
27. When to Use Kubernetes
• Managing a few
containers is very different
than managing 100s, if
not 1000s
• Advanced “orchestration”
required to manage this
many containers
27
28. PostgreSQL in a
Kubernetes World
• Advantages for using Kubernetes with PostgreSQL
• Your own "database-as-a-service"
• Mass apply databases commands:
• Updates
• Backups / Restores
• ACL rule changes
• Scale up / down replicas, though...
!28
29. PostgreSQL in a
Kubernetes World
• Kubernetes is "turnkey" for stateless applications
• e.g. web servers
• Databases do maintain state: permanent storage
• Persistent Volumes (PV)
• Persistent Volume Claims (PVC)
!29
30. The "postgres operator"
• (aka "Crunchy PostgreSQL for Kubernetes")
• Allows an administrator to run PostgreSQL-specific
commands to manage database clusters, including:
• Creating / Deleting a cluster (your own DBaaS)
• Scaling up / down replicas
• Apply user policies to PostgreSQL instances
• REST API
!30
https://github.com/CrunchyData/postgres-operator
33. Looking Ahead
• Containers are no longer "new" - orchestration technologies have
matured
• Big debate with containers + databases: storage & management
• No different than virtual machines + databases
• Databases are still databases: need expertise to manage
• Database deployment automation flexibility
• Deploy your architecture to any number of clouds
• "Monitoring at scale"
!33
34. Conclusions
• Containers + PostgreSQL gives you:
• Easy-to-setup development environments
• Your own production database-as-a-service
• Tools to automate management of over 1000s of
instances in short-order
!34