• Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Postgres vertical autoscaling in k8s

Autoscaling

Vertical autoscaling for a fleet of postgres instances running in a Kubernetes cluster.

Quick access

Images are available as:

Component name Image name
scheduler (and plugin) neondatabase/autoscale-scheduler
autoscaler-agent neondatabase/autoscaler-agent

The deployment files and a vm-builder binary are attached to each release.

For information on inter-version compatibility, see pkg/api/VERSIONING.md.

For now, the currently deployed configuration on staging is manually replicated in the staging branch.

Overview

We want to dynamically change the amount of CPUs and memory of running postgres instances, without breaking TCP connections to postgres.

This relatively easy when there's already spare resources on the physical (Kubernetes) node, but it takes careful coordination to move postgres instances from one node to another when the original node doesn't have the room.

We've tried a bunch of existing tools and settled on the following:

  • Use VM live migration to move running postgres instances between physical nodes
  • QEMU is used as our hypervisor
  • NeonVM orchestrates NeonVM VMs as custom resources in K8s, and is responsible for scaling allocated resources (CPU and memory slots)
  • A modified K8s scheduler ensures that we don't overcommit resources and triggers migrations when demand is above a pre-configured threshold
  • Each K8s node has an autoscaler-agent pod that triggers scaling decisions and makes resource requests to the K8s scheduler on the VMs' behalf to reserve additional resources for them
  • Each compute node runs the _VM monitor binary, which communicates to the autoscaler-agent so that it can immediately respond to memory pressure by allocating more (among other things).

Networking is preserved across migrations by giving each VM an additional IP address on a bridge network spanning the cluster with a flat topology; the L2 network figures out "by itself" where to send the packets after migration.

For more information, refer to ARCHITECTURE.md.

Building and running

Build NeonVM Linux kernel (it takes time, can be run only once)

make kernel

Build docker images:

make docker-build

Start local cluster with kind or k3d:

make kind-setup # or make k3d-setup

Deploy NeonVM and Autoscaling components

make deploy

Build and load the test VM:

make pg14-disk-test

Start the test VM:

kubectl apply -f vm-deploy.yaml

Running pgbench

Broadly, the run-bench.sh script just exists to be expensive on CPU, so that more vCPU will be allocated to the vm. You can run it with:

scripts/run-bench.sh
# or:
VM_NAME=postgres14-disk-test scripts/run-bench.sh

Running allocate-loop

To test on-demand memory reservation, the allocate-loop binary is built into the test VM, and can be used to slowly increasing memory allocations of arbitrary size. For example:

# After ssh-ing into the VM:
cgexec -g memory:neon-test allocate-loop 256 2280
#^^^^^^^^^^^^^^^^^^^^^^^^^               ^^^ ^^^^
# run it in the neon-test cgroup  ;  use 256 <-> 2280 MiB

Testing

To run e2e tests you need to install dependencies:

You can either download them from their websites or install using Homebrew: brew install kubectl kind k3d kuttl

make kind-setup # or make k3d-setup, if you'd like to use k3d
make kernel
make deploy
make example-vms
make e2e

More Repositories

1

neon

Neon: Serverless Postgres. We separated storage and compute to offer autoscaling, code-like database branching, and scale to zero.
Rust
13,985
star
2

pg_embedding

Hierarchical Navigable Small World (HNSW) algorithm for vector similarity search in PostgreSQL
C
557
star
3

serverless

Connect to Neon PostgreSQL from serverless/worker/edge functions
TypeScript
324
star
4

website

Official docs and website for Neon.
JavaScript
191
star
5

yc-idea-matcher

Submit your idea and get a list of similar ideas that YCombinator has invested in in the past.
TypeScript
145
star
6

wsproxy

Go
115
star
7

ask-neon

Chatbot: Search your own knowledge base by semantic similarity
TypeScript
54
star
8

neonctl

Neon CLI tool. The Neon CLI is a command-line interface that lets you manage Neon Serverless Postgres directly from the terminal.
TypeScript
49
star
9

drizzle-overview

Demo Drizzle ORM, Hono & Neon API
TypeScript
49
star
10

postgres-ai-playground

TypeScript
33
star
11

cloudflare-drizzle-neon

Example API using Cloudflare Workers, Drizzle ORM and Neon
TypeScript
33
star
12

helm-charts

neondatabase helm charts
Smarty
32
star
13

psql-describe

psql's \d (describe) family of commands ported to JavaScript
JavaScript
31
star
14

create-branch-action

GitHub Action to create a new Neon branch
30
star
15

preview-branches-with-vercel

Example project that shows how you can create a branch for every preview deployment on Vercel using GitHub actions
TypeScript
27
star
16

serverless-cfworker-demo

Demo app for @neondatabase/serverless โ€”ย details at https://blog.cloudflare.com/neon-postgres-database-from-workers/
HTML
23
star
17

postgres

PostgreSQL in Neon
C
21
star
18

examples

Examples and code snippets demonstrating common ways of integrating Neon with various frameworks and languages.
TypeScript
19
star
19

postgres-sample-dbs

A collection of sample Postgres databases for learning, testing, and development.
PLpgSQL
18
star
20

naturesnap

TypeScript
18
star
21

neonvm

NeonVM: QEMU-based virtualization API and controller for Kubernetes
Go
17
star
22

preview-branches-with-fly

A Neon branch for every Fly Preview app
TypeScript
16
star
23

ping-thing

Ping a Neon Serverless Postgres database using a Vercel Edge Function to see the journey your request makes.
JavaScript
15
star
24

neon-vercel-kysely

Example use of Neon serverless driver on Vercel Edge Functions with Kysely and kysely-codegen
TypeScript
12
star
25

neon-api-python

a Python client for the Neon API
Python
12
star
26

tokio-epoll-uring

Use io_uring from vanilla tokio.
Rust
9
star
27

semicolons

Take a string with multiple Postgres SQL statements, separated by semicolons, and split it into its constituent statements
TypeScript
8
star
28

delete-branch-action

7
star
29

instant-postgres

TypeScript
7
star
30

neon-postgresql-expert

Input for an OpenAI GPT that can answer questions about Neon database and Postgres
Python
5
star
31

github-automations

Scripts that we use to track issues in github's (beta) projects
TypeScript
5
star
32

pg-import

A CLI tool for importing data from one PostgreSQL database to another.
JavaScript
5
star
33

fastapi-apprunner-neon

Create a serverless API using FastAPI, deployed on AWS App Runner and powered by Neon Postgres
Python
5
star
34

delete-branch-by-name-action

Delete Neon database branch by name
4
star
35

rfcs

4
star
36

neon-branches-visualizer

Visualize your Neon Postgres branches
TypeScript
4
star
37

zenith.tech

JavaScript
3
star
38

neon-vercel-rawsql

Example use of Neon serverless driver on Vercel Edge Functions with raw SQL
TypeScript
3
star
39

aws-cost-reporter

Create and share AWS Cost and Usage reports in Slack.
Go
3
star
40

pg_session_jwt

Postgres Extension for JWT Sessions
Rust
3
star
41

neon-vector-search-openai-notebooks

Jupyter Notebook for Vector Search with Neon and OpenAI
Jupyter Notebook
3
star
42

kube-previews-application

Example project that shows how to create a Neon branch for preview environments deployed on Kubernetes using Argo CD
TypeScript
3
star
43

postgresql_anonymizer

Neon fork of https://gitlab.com/dalibo/postgresql_anonymizer
PLpgSQL
3
star
44

neon_twitter

TypeScript
2
star
45

aversion

Rust
2
star
46

keycloak-example

TypeScript
2
star
47

neon-hyperdrive

Example use of Neon with Hyperdrive on Cloudflare Workers
TypeScript
2
star
48

meeting-notes

2
star
49

mastodon-fly

Dockerfile
2
star
50

regional-latency

A tool that tracks latencies between popular application hosting platforms and nearby Neon Postgres regions.
TypeScript
2
star
51

prisma-vercel-load-test

An app that tests prisma on vercel with vercel postgres
CSS
2
star
52

lambda-cdk-neon

This is an example API built using AWS Lambda, API Gateway, Secrets Manager and Neon
TypeScript
2
star
53

neon-vercel-http

Example use of Neon serverless driver's experimental HTTP feature on Vercel Edge Functions
TypeScript
1
star
54

neon-vercel-knex

Example use of Neon serverless driver on Vercel Edge Functions with Knex.js
JavaScript
1
star
55

dev-actions

JavaScript
1
star
56

vm-monitor

Rust
1
star
57

restore-neon-branch

Script to restore a Neon branch to a previous state while preserving the same endpoint
TypeScript
1
star
58

neon-google-colab-notebooks

Neon Google Colab Notebooks
Jupyter Notebook
1
star
59

guide-neon-prisma

Example application for Neon Prisma Guide
JavaScript
1
star
60

guide-neon-drizzle

Example application for Neon with Drizzle
TypeScript
1
star
61

s3-scrubber

Rust
1
star
62

zenith-coverage-data

1
star
63

.github

Public organization profile
1
star
64

latency-dashboard

TypeScript
1
star
65

neon-vercel-zapatos

Example use of Neon serverless driver on Vercel Edge Functions with Zapatos
TypeScript
1
star
66

rust_wal.experimental

Wrap a database frontend in rust based consensus
Rust
1
star
67

neon-ecto-getting-started-app

Neon Ecto Getting Started
Elixir
1
star
68

kube-previews-manifests

Example manifests used to create preview environments deployed on Kubernetes using Argo CD
Shell
1
star
69

rustls-split

Rust
1
star
70

pgvector

C
1
star
71

devdays2

Neon Developer Days Side Project
JavaScript
1
star
72

docker-images

Docker images that helps build and test Neon product
Dockerfile
1
star
73

branching-demo

Copy your database in milliseconds with Neon
TypeScript
1
star
74

zenith-perf-data

Simple collection of zenith performance test runs
HTML
1
star
75

reset-branch-action

1
star
76

proxy-bench

Benchmarking tools for Neon's Postgres Proxy
Rust
1
star
77

qovery-lifecycle-job

Shell
1
star
78

pg_neon_ai

Rust
1
star
79

guide-neon-next-clerk

How to use Clerk with Neon
TypeScript
1
star
80

guide-neon-next-auth0

How to use Auth0 with Noeon
TypeScript
1
star