├── README.md ├── config ├── create-replica-user.sh ├── create_configmap.sh ├── master.conf ├── pg_hba.conf ├── postgres.conf ├── replica.conf └── secret.yml ├── service.yml ├── statefulset-master.yml └── statefulset-replica.yml /README.md: -------------------------------------------------------------------------------- 1 | # postgres-statefulset 2 | This is an example of using Kubernetes StatefulSets to get a Postgres instance running with replication enabled. This also uses the [standard Postgres container](https://github.com/docker-library/postgres). Blog article [here](https://stacksoft.io/blog/postgres-statefulset/) 3 | 4 | The work here is based off the official documentation here https://wiki.postgresql.org/wiki/Streaming_Replication 5 | 6 | ## Configuration 7 | 8 | 1. Edit `config/secret.yml` with the Postgres database password and the replication password 9 | 2. Run `kubectl apply -f config/secret.yml` and then `cd config && ./create_configmap.sh` 10 | 11 | Note, replication password is used to connect to the master and stream updates to the replica. It just needs to be a random password. 12 | 13 | ## Running 14 | 15 | Running this example is easy! 16 | 17 | ### Start Master servers 18 | 19 | Run `kubectl apply -f statefulset-master.yml` and wait for Master to be running 20 | 21 | ### Start Master service (IMPORTANT or replica cannot find the master) 22 | Run `kubectl apply -f service.yml` 23 | 24 | ### Start Replica server 25 | 26 | Run `kubectl apply -f statefulset-replica.yml` and wait for Replica to be running 27 | 28 | If you run `kubectl logs -f postgres-replica-0`, you can see in the logs that it starts replication: 29 | 30 | ``` 31 | 2019-01-08 05:07:01.035 UTC [24] LOG: started streaming WAL from primary at 0/6000000 on timeline 1 32 | ``` 33 | 34 | That's it, you have a full Postgres master + replicating server that's ready to use in production. 35 | 36 | #### Multiple Replicas 37 | 38 | You can also set replicas to more than 1 if you want N replicas. 39 | 40 | ### Motivation 41 | 42 | I was using Helm to manage Postgres, but they switched to `bitnami/postgresql` instead of the standard `postgres`. Also, upon upgrading, I could not use my existing helm setup to get replication working. 43 | 44 | Lately I've realized it's just easier to write my own definitions instead of messing around with Helm charts. 45 | -------------------------------------------------------------------------------- /config/create-replica-user.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL 5 | CREATE ROLE replication WITH REPLICATION PASSWORD '$REPLICATION_PASSWORD' LOGIN 6 | EOSQL 7 | -------------------------------------------------------------------------------- /config/create_configmap.sh: -------------------------------------------------------------------------------- 1 | kubectl create configmap postgres --from-file=postgres.conf --from-file=master.conf --from-file=replica.conf --from-file=pg_hba.conf --from-file=create-replica-user.sh 2 | -------------------------------------------------------------------------------- /config/master.conf: -------------------------------------------------------------------------------- 1 | # To enable read-only queries on a standby server, wal_level must be set to 2 | # "hot_standby". But you can choose "archive" if you never connect to the 3 | # server in standby mode. 4 | wal_level = hot_standby 5 | 6 | # Set the maximum number of concurrent connections from the standby servers. 7 | max_wal_senders = 5 8 | 9 | # To prevent the primary server from removing the WAL segments required for 10 | # the standby server before shipping them, set the minimum number of segments 11 | # retained in the pg_xlog directory. At least wal_keep_segments should be 12 | # larger than the number of segments generated between the beginning of 13 | # online-backup and the startup of streaming replication. If you enable WAL 14 | # archiving to an archive directory accessible from the standby, this may 15 | # not be necessary. 16 | wal_keep_segments = 32 17 | 18 | # Enable WAL archiving on the primary to an archive directory accessible from 19 | # the standby. If wal_keep_segments is a high enough number to retain the WAL 20 | # segments required for the standby server, this is not necessary. 21 | #archive_mode = on 22 | #archive_command = 'cp %p /path_to/archive/%f' 23 | -------------------------------------------------------------------------------- /config/pg_hba.conf: -------------------------------------------------------------------------------- 1 | # PostgreSQL Client Authentication Configuration File 2 | # =================================================== 3 | # 4 | # Refer to the "Client Authentication" section in the PostgreSQL 5 | # documentation for a complete description of this file. A short 6 | # synopsis follows. 7 | # 8 | # This file controls: which hosts are allowed to connect, how clients 9 | # are authenticated, which PostgreSQL user names they can use, which 10 | # databases they can access. Records take one of these forms: 11 | # 12 | # local DATABASE USER METHOD [OPTIONS] 13 | # host DATABASE USER ADDRESS METHOD [OPTIONS] 14 | # hostssl DATABASE USER ADDRESS METHOD [OPTIONS] 15 | # hostnossl DATABASE USER ADDRESS METHOD [OPTIONS] 16 | # 17 | # (The uppercase items must be replaced by actual values.) 18 | # 19 | # The first field is the connection type: "local" is a Unix-domain 20 | # socket, "host" is either a plain or SSL-encrypted TCP/IP socket, 21 | # "hostssl" is an SSL-encrypted TCP/IP socket, and "hostnossl" is a 22 | # plain TCP/IP socket. 23 | # 24 | # DATABASE can be "all", "sameuser", "samerole", "replication", a 25 | # database name, or a comma-separated list thereof. The "all" 26 | # keyword does not match "replication". Access to replication 27 | # must be enabled in a separate record (see example below). 28 | # 29 | # USER can be "all", a user name, a group name prefixed with "+", or a 30 | # comma-separated list thereof. In both the DATABASE and USER fields 31 | # you can also write a file name prefixed with "@" to include names 32 | # from a separate file. 33 | # 34 | # ADDRESS specifies the set of hosts the record matches. It can be a 35 | # host name, or it is made up of an IP address and a CIDR mask that is 36 | # an integer (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that 37 | # specifies the number of significant bits in the mask. A host name 38 | # that starts with a dot (.) matches a suffix of the actual host name. 39 | # Alternatively, you can write an IP address and netmask in separate 40 | # columns to specify the set of hosts. Instead of a CIDR-address, you 41 | # can write "samehost" to match any of the server's own IP addresses, 42 | # or "samenet" to match any address in any subnet that the server is 43 | # directly connected to. 44 | # 45 | # METHOD can be "trust", "reject", "md5", "password", "scram-sha-256", 46 | # "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert". 47 | # Note that "password" sends passwords in clear text; "md5" or 48 | # "scram-sha-256" are preferred since they send encrypted passwords. 49 | # 50 | # OPTIONS are a set of options for the authentication in the format 51 | # NAME=VALUE. The available options depend on the different 52 | # authentication methods -- refer to the "Client Authentication" 53 | # section in the documentation for a list of which options are 54 | # available for which authentication methods. 55 | # 56 | # Database and user names containing spaces, commas, quotes and other 57 | # special characters must be quoted. Quoting one of the keywords 58 | # "all", "sameuser", "samerole" or "replication" makes the name lose 59 | # its special character, and just match a database or username with 60 | # that name. 61 | # 62 | # This file is read on server startup and when the server receives a 63 | # SIGHUP signal. If you edit the file on a running system, you have to 64 | # SIGHUP the server for the changes to take effect, run "pg_ctl reload", 65 | # or execute "SELECT pg_reload_conf()". 66 | # 67 | # Put your actual configuration here 68 | # ---------------------------------- 69 | # 70 | # If you want to allow non-local connections, you need to add more 71 | # "host" records. In that case you will also need to make PostgreSQL 72 | # listen on a non-local interface via the listen_addresses 73 | # configuration parameter, or via the -i or -h command line switches. 74 | 75 | # CAUTION: Configuring the system for local "trust" authentication 76 | # allows any local user to connect as any PostgreSQL user, including 77 | # the database superuser. If you do not trust all your local users, 78 | # use another authentication method. 79 | 80 | 81 | # TYPE DATABASE USER ADDRESS METHOD 82 | 83 | # "local" is for Unix domain socket connections only 84 | local all all trust 85 | # IPv4 local connections: 86 | host all all 127.0.0.1/32 trust 87 | # IPv6 local connections: 88 | host all all ::1/128 trust 89 | # Allow replication connections from localhost, by a user with the 90 | # replication privilege. 91 | local replication all trust 92 | host replication all 127.0.0.1/32 trust 93 | host replication all ::1/128 trust 94 | 95 | host replication replication all md5 96 | host all all all md5 97 | -------------------------------------------------------------------------------- /config/postgres.conf: -------------------------------------------------------------------------------- 1 | # ----------------------------- 2 | # PostgreSQL configuration file 3 | # ----------------------------- 4 | # 5 | # This file consists of lines of the form: 6 | # 7 | # name = value 8 | # 9 | # (The "=" is optional.) Whitespace may be used. Comments are introduced with 10 | # "#" anywhere on a line. The complete list of parameter names and allowed 11 | # values can be found in the PostgreSQL documentation. 12 | # 13 | # The commented-out settings shown in this file represent the default values. 14 | # Re-commenting a setting is NOT sufficient to revert it to the default value; 15 | # you need to reload the server. 16 | # 17 | # This file is read on server startup and when the server receives a SIGHUP 18 | # signal. If you edit the file on a running system, you have to SIGHUP the 19 | # server for the changes to take effect, run "pg_ctl reload", or execute 20 | # "SELECT pg_reload_conf()". Some parameters, which are marked below, 21 | # require a server shutdown and restart to take effect. 22 | # 23 | # Any parameter can also be given as a command-line option to the server, e.g., 24 | # "postgres -c log_connections=on". Some parameters can be changed at run time 25 | # with the "SET" SQL command. 26 | # 27 | # Memory units: kB = kilobytes Time units: ms = milliseconds 28 | # MB = megabytes s = seconds 29 | # GB = gigabytes min = minutes 30 | # TB = terabytes h = hours 31 | # d = days 32 | 33 | 34 | #------------------------------------------------------------------------------ 35 | # FILE LOCATIONS 36 | #------------------------------------------------------------------------------ 37 | 38 | # The default values of these variables are driven from the -D command-line 39 | # option or PGDATA environment variable, represented here as ConfigDir. 40 | 41 | #data_directory = 'ConfigDir' # use data in another directory 42 | # (change requires restart) 43 | #hba_file = 'ConfigDir/pg_hba.conf' # host-based authentication file 44 | # (change requires restart) 45 | #ident_file = 'ConfigDir/pg_ident.conf' # ident configuration file 46 | # (change requires restart) 47 | 48 | # If external_pid_file is not explicitly set, no extra PID file is written. 49 | #external_pid_file = '' # write an extra PID file 50 | # (change requires restart) 51 | 52 | 53 | #------------------------------------------------------------------------------ 54 | # CONNECTIONS AND AUTHENTICATION 55 | #------------------------------------------------------------------------------ 56 | 57 | # - Connection Settings - 58 | 59 | listen_addresses = '*' 60 | # comma-separated list of addresses; 61 | # defaults to 'localhost'; use '*' for all 62 | # (change requires restart) 63 | #port = 5432 # (change requires restart) 64 | max_connections = 100 # (change requires restart) 65 | #superuser_reserved_connections = 3 # (change requires restart) 66 | #unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories 67 | # (change requires restart) 68 | #unix_socket_group = '' # (change requires restart) 69 | #unix_socket_permissions = 0777 # begin with 0 to use octal notation 70 | # (change requires restart) 71 | #bonjour = off # advertise server via Bonjour 72 | # (change requires restart) 73 | #bonjour_name = '' # defaults to the computer name 74 | # (change requires restart) 75 | 76 | # - Security and Authentication - 77 | 78 | #authentication_timeout = 1min # 1s-600s 79 | #ssl = off 80 | #ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers 81 | #ssl_prefer_server_ciphers = on 82 | #ssl_ecdh_curve = 'prime256v1' 83 | #ssl_dh_params_file = '' 84 | #ssl_cert_file = 'server.crt' 85 | #ssl_key_file = 'server.key' 86 | #ssl_ca_file = '' 87 | #ssl_crl_file = '' 88 | #password_encryption = md5 # md5 or scram-sha-256 89 | #db_user_namespace = off 90 | #row_security = on 91 | 92 | # GSSAPI using Kerberos 93 | #krb_server_keyfile = '' 94 | #krb_caseins_users = off 95 | 96 | # - TCP Keepalives - 97 | # see "man 7 tcp" for details 98 | 99 | #tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds; 100 | # 0 selects the system default 101 | #tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds; 102 | # 0 selects the system default 103 | #tcp_keepalives_count = 0 # TCP_KEEPCNT; 104 | # 0 selects the system default 105 | 106 | 107 | #------------------------------------------------------------------------------ 108 | # RESOURCE USAGE (except WAL) 109 | #------------------------------------------------------------------------------ 110 | 111 | # - Memory - 112 | 113 | shared_buffers = 128MB # min 128kB 114 | # (change requires restart) 115 | #huge_pages = try # on, off, or try 116 | # (change requires restart) 117 | #temp_buffers = 8MB # min 800kB 118 | #max_prepared_transactions = 0 # zero disables the feature 119 | # (change requires restart) 120 | # Caution: it is not advisable to set max_prepared_transactions nonzero unless 121 | # you actively intend to use prepared transactions. 122 | #work_mem = 4MB # min 64kB 123 | #maintenance_work_mem = 64MB # min 1MB 124 | #replacement_sort_tuples = 150000 # limits use of replacement selection sort 125 | #autovacuum_work_mem = -1 # min 1MB, or -1 to use maintenance_work_mem 126 | #max_stack_depth = 2MB # min 100kB 127 | dynamic_shared_memory_type = posix # the default is the first option 128 | # supported by the operating system: 129 | # posix 130 | # sysv 131 | # windows 132 | # mmap 133 | # use none to disable dynamic shared memory 134 | # (change requires restart) 135 | 136 | # - Disk - 137 | 138 | #temp_file_limit = -1 # limits per-process temp file space 139 | # in kB, or -1 for no limit 140 | 141 | # - Kernel Resource Usage - 142 | 143 | #max_files_per_process = 1000 # min 25 144 | # (change requires restart) 145 | #shared_preload_libraries = '' # (change requires restart) 146 | 147 | # - Cost-Based Vacuum Delay - 148 | 149 | #vacuum_cost_delay = 0 # 0-100 milliseconds 150 | #vacuum_cost_page_hit = 1 # 0-10000 credits 151 | #vacuum_cost_page_miss = 10 # 0-10000 credits 152 | #vacuum_cost_page_dirty = 20 # 0-10000 credits 153 | #vacuum_cost_limit = 200 # 1-10000 credits 154 | 155 | # - Background Writer - 156 | 157 | #bgwriter_delay = 200ms # 10-10000ms between rounds 158 | #bgwriter_lru_maxpages = 100 # 0-1000 max buffers written/round 159 | #bgwriter_lru_multiplier = 2.0 # 0-10.0 multiplier on buffers scanned/round 160 | #bgwriter_flush_after = 512kB # measured in pages, 0 disables 161 | 162 | # - Asynchronous Behavior - 163 | 164 | #effective_io_concurrency = 1 # 1-1000; 0 disables prefetching 165 | #max_worker_processes = 8 # (change requires restart) 166 | #max_parallel_workers_per_gather = 2 # taken from max_parallel_workers 167 | #max_parallel_workers = 8 # maximum number of max_worker_processes that 168 | # can be used in parallel queries 169 | #old_snapshot_threshold = -1 # 1min-60d; -1 disables; 0 is immediate 170 | # (change requires restart) 171 | #backend_flush_after = 0 # measured in pages, 0 disables 172 | 173 | 174 | #------------------------------------------------------------------------------ 175 | # WRITE AHEAD LOG 176 | #------------------------------------------------------------------------------ 177 | 178 | # - Settings - 179 | 180 | #wal_level = replica # minimal, replica, or logical 181 | # (change requires restart) 182 | #fsync = on # flush data to disk for crash safety 183 | # (turning this off can cause 184 | # unrecoverable data corruption) 185 | #synchronous_commit = on # synchronization level; 186 | # off, local, remote_write, remote_apply, or on 187 | #wal_sync_method = fsync # the default is the first option 188 | # supported by the operating system: 189 | # open_datasync 190 | # fdatasync (default on Linux) 191 | # fsync 192 | # fsync_writethrough 193 | # open_sync 194 | #full_page_writes = on # recover from partial page writes 195 | #wal_compression = off # enable compression of full-page writes 196 | #wal_log_hints = off # also do full page writes of non-critical updates 197 | # (change requires restart) 198 | #wal_buffers = -1 # min 32kB, -1 sets based on shared_buffers 199 | # (change requires restart) 200 | #wal_writer_delay = 200ms # 1-10000 milliseconds 201 | #wal_writer_flush_after = 1MB # measured in pages, 0 disables 202 | 203 | #commit_delay = 0 # range 0-100000, in microseconds 204 | #commit_siblings = 5 # range 1-1000 205 | 206 | # - Checkpoints - 207 | 208 | #checkpoint_timeout = 5min # range 30s-1d 209 | #max_wal_size = 1GB 210 | #min_wal_size = 80MB 211 | #checkpoint_completion_target = 0.5 # checkpoint target duration, 0.0 - 1.0 212 | #checkpoint_flush_after = 256kB # measured in pages, 0 disables 213 | #checkpoint_warning = 30s # 0 disables 214 | 215 | # - Archiving - 216 | 217 | #archive_mode = off # enables archiving; off, on, or always 218 | # (change requires restart) 219 | #archive_command = '' # command to use to archive a logfile segment 220 | # placeholders: %p = path of file to archive 221 | # %f = file name only 222 | # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f' 223 | #archive_timeout = 0 # force a logfile segment switch after this 224 | # number of seconds; 0 disables 225 | 226 | 227 | #------------------------------------------------------------------------------ 228 | # REPLICATION 229 | #------------------------------------------------------------------------------ 230 | 231 | # - Sending Server(s) - 232 | 233 | # Set these on the master and on any standby that will send replication data. 234 | 235 | #max_wal_senders = 10 # max number of walsender processes 236 | # (change requires restart) 237 | #wal_keep_segments = 0 # in logfile segments, 16MB each; 0 disables 238 | #wal_sender_timeout = 60s # in milliseconds; 0 disables 239 | 240 | #max_replication_slots = 10 # max number of replication slots 241 | # (change requires restart) 242 | #track_commit_timestamp = off # collect timestamp of transaction commit 243 | # (change requires restart) 244 | 245 | # - Master Server - 246 | 247 | # These settings are ignored on a standby server. 248 | 249 | #synchronous_standby_names = '' # standby servers that provide sync rep 250 | # method to choose sync standbys, number of sync standbys, 251 | # and comma-separated list of application_name 252 | # from standby(s); '*' = all 253 | #vacuum_defer_cleanup_age = 0 # number of xacts by which cleanup is delayed 254 | 255 | # - Standby Servers - 256 | 257 | # These settings are ignored on a master server. 258 | 259 | #hot_standby = on # "off" disallows queries during recovery 260 | # (change requires restart) 261 | #max_standby_archive_delay = 30s # max delay before canceling queries 262 | # when reading WAL from archive; 263 | # -1 allows indefinite delay 264 | #max_standby_streaming_delay = 30s # max delay before canceling queries 265 | # when reading streaming WAL; 266 | # -1 allows indefinite delay 267 | #wal_receiver_status_interval = 10s # send replies at least this often 268 | # 0 disables 269 | #hot_standby_feedback = off # send info from standby to prevent 270 | # query conflicts 271 | #wal_receiver_timeout = 60s # time that receiver waits for 272 | # communication from master 273 | # in milliseconds; 0 disables 274 | #wal_retrieve_retry_interval = 5s # time to wait before retrying to 275 | # retrieve WAL after a failed attempt 276 | 277 | # - Subscribers - 278 | 279 | # These settings are ignored on a publisher. 280 | 281 | #max_logical_replication_workers = 4 # taken from max_worker_processes 282 | # (change requires restart) 283 | #max_sync_workers_per_subscription = 2 # taken from max_logical_replication_workers 284 | 285 | 286 | #------------------------------------------------------------------------------ 287 | # QUERY TUNING 288 | #------------------------------------------------------------------------------ 289 | 290 | # - Planner Method Configuration - 291 | 292 | #enable_bitmapscan = on 293 | #enable_hashagg = on 294 | #enable_hashjoin = on 295 | #enable_indexscan = on 296 | #enable_indexonlyscan = on 297 | #enable_material = on 298 | #enable_mergejoin = on 299 | #enable_nestloop = on 300 | #enable_seqscan = on 301 | #enable_sort = on 302 | #enable_tidscan = on 303 | 304 | # - Planner Cost Constants - 305 | 306 | #seq_page_cost = 1.0 # measured on an arbitrary scale 307 | #random_page_cost = 4.0 # same scale as above 308 | #cpu_tuple_cost = 0.01 # same scale as above 309 | #cpu_index_tuple_cost = 0.005 # same scale as above 310 | #cpu_operator_cost = 0.0025 # same scale as above 311 | #parallel_tuple_cost = 0.1 # same scale as above 312 | #parallel_setup_cost = 1000.0 # same scale as above 313 | #min_parallel_table_scan_size = 8MB 314 | #min_parallel_index_scan_size = 512kB 315 | #effective_cache_size = 4GB 316 | 317 | # - Genetic Query Optimizer - 318 | 319 | #geqo = on 320 | #geqo_threshold = 12 321 | #geqo_effort = 5 # range 1-10 322 | #geqo_pool_size = 0 # selects default based on effort 323 | #geqo_generations = 0 # selects default based on effort 324 | #geqo_selection_bias = 2.0 # range 1.5-2.0 325 | #geqo_seed = 0.0 # range 0.0-1.0 326 | 327 | # - Other Planner Options - 328 | 329 | #default_statistics_target = 100 # range 1-10000 330 | #constraint_exclusion = partition # on, off, or partition 331 | #cursor_tuple_fraction = 0.1 # range 0.0-1.0 332 | #from_collapse_limit = 8 333 | #join_collapse_limit = 8 # 1 disables collapsing of explicit 334 | # JOIN clauses 335 | #force_parallel_mode = off 336 | 337 | 338 | #------------------------------------------------------------------------------ 339 | # ERROR REPORTING AND LOGGING 340 | #------------------------------------------------------------------------------ 341 | 342 | # - Where to Log - 343 | 344 | #log_destination = 'stderr' # Valid values are combinations of 345 | # stderr, csvlog, syslog, and eventlog, 346 | # depending on platform. csvlog 347 | # requires logging_collector to be on. 348 | 349 | # This is used when logging to stderr: 350 | #logging_collector = off # Enable capturing of stderr and csvlog 351 | # into log files. Required to be on for 352 | # csvlogs. 353 | # (change requires restart) 354 | 355 | # These are only used if logging_collector is on: 356 | #log_directory = 'log' # directory where log files are written, 357 | # can be absolute or relative to PGDATA 358 | #log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern, 359 | # can include strftime() escapes 360 | #log_file_mode = 0600 # creation mode for log files, 361 | # begin with 0 to use octal notation 362 | #log_truncate_on_rotation = off # If on, an existing log file with the 363 | # same name as the new log file will be 364 | # truncated rather than appended to. 365 | # But such truncation only occurs on 366 | # time-driven rotation, not on restarts 367 | # or size-driven rotation. Default is 368 | # off, meaning append to existing files 369 | # in all cases. 370 | #log_rotation_age = 1d # Automatic rotation of logfiles will 371 | # happen after that time. 0 disables. 372 | #log_rotation_size = 10MB # Automatic rotation of logfiles will 373 | # happen after that much log output. 374 | # 0 disables. 375 | 376 | # These are relevant when logging to syslog: 377 | #syslog_facility = 'LOCAL0' 378 | #syslog_ident = 'postgres' 379 | #syslog_sequence_numbers = on 380 | #syslog_split_messages = on 381 | 382 | # This is only relevant when logging to eventlog (win32): 383 | # (change requires restart) 384 | #event_source = 'PostgreSQL' 385 | 386 | # - When to Log - 387 | 388 | #client_min_messages = notice # values in order of decreasing detail: 389 | # debug5 390 | # debug4 391 | # debug3 392 | # debug2 393 | # debug1 394 | # log 395 | # notice 396 | # warning 397 | # error 398 | 399 | #log_min_messages = warning # values in order of decreasing detail: 400 | # debug5 401 | # debug4 402 | # debug3 403 | # debug2 404 | # debug1 405 | # info 406 | # notice 407 | # warning 408 | # error 409 | # log 410 | # fatal 411 | # panic 412 | 413 | #log_min_error_statement = error # values in order of decreasing detail: 414 | # debug5 415 | # debug4 416 | # debug3 417 | # debug2 418 | # debug1 419 | # info 420 | # notice 421 | # warning 422 | # error 423 | # log 424 | # fatal 425 | # panic (effectively off) 426 | 427 | #log_min_duration_statement = -1 # -1 is disabled, 0 logs all statements 428 | # and their durations, > 0 logs only 429 | # statements running at least this number 430 | # of milliseconds 431 | 432 | 433 | # - What to Log - 434 | 435 | #debug_print_parse = off 436 | #debug_print_rewritten = off 437 | #debug_print_plan = off 438 | #debug_pretty_print = on 439 | #log_checkpoints = off 440 | #log_connections = off 441 | #log_disconnections = off 442 | #log_duration = off 443 | #log_error_verbosity = default # terse, default, or verbose messages 444 | #log_hostname = off 445 | #log_line_prefix = '%m [%p] ' # special values: 446 | # %a = application name 447 | # %u = user name 448 | # %d = database name 449 | # %r = remote host and port 450 | # %h = remote host 451 | # %p = process ID 452 | # %t = timestamp without milliseconds 453 | # %m = timestamp with milliseconds 454 | # %n = timestamp with milliseconds (as a Unix epoch) 455 | # %i = command tag 456 | # %e = SQL state 457 | # %c = session ID 458 | # %l = session line number 459 | # %s = session start timestamp 460 | # %v = virtual transaction ID 461 | # %x = transaction ID (0 if none) 462 | # %q = stop here in non-session 463 | # processes 464 | # %% = '%' 465 | # e.g. '<%u%%%d> ' 466 | #log_lock_waits = off # log lock waits >= deadlock_timeout 467 | #log_statement = 'none' # none, ddl, mod, all 468 | #log_replication_commands = off 469 | #log_temp_files = -1 # log temporary files equal or larger 470 | # than the specified size in kilobytes; 471 | # -1 disables, 0 logs all temp files 472 | log_timezone = 'UTC' 473 | 474 | 475 | # - Process Title - 476 | 477 | #cluster_name = '' # added to process titles if nonempty 478 | # (change requires restart) 479 | #update_process_title = on 480 | 481 | 482 | #------------------------------------------------------------------------------ 483 | # RUNTIME STATISTICS 484 | #------------------------------------------------------------------------------ 485 | 486 | # - Query/Index Statistics Collector - 487 | 488 | #track_activities = on 489 | #track_counts = on 490 | #track_io_timing = off 491 | #track_functions = none # none, pl, all 492 | #track_activity_query_size = 1024 # (change requires restart) 493 | #stats_temp_directory = 'pg_stat_tmp' 494 | 495 | 496 | # - Statistics Monitoring - 497 | 498 | #log_parser_stats = off 499 | #log_planner_stats = off 500 | #log_executor_stats = off 501 | #log_statement_stats = off 502 | 503 | 504 | #------------------------------------------------------------------------------ 505 | # AUTOVACUUM PARAMETERS 506 | #------------------------------------------------------------------------------ 507 | 508 | #autovacuum = on # Enable autovacuum subprocess? 'on' 509 | # requires track_counts to also be on. 510 | #log_autovacuum_min_duration = -1 # -1 disables, 0 logs all actions and 511 | # their durations, > 0 logs only 512 | # actions running at least this number 513 | # of milliseconds. 514 | #autovacuum_max_workers = 3 # max number of autovacuum subprocesses 515 | # (change requires restart) 516 | #autovacuum_naptime = 1min # time between autovacuum runs 517 | #autovacuum_vacuum_threshold = 50 # min number of row updates before 518 | # vacuum 519 | #autovacuum_analyze_threshold = 50 # min number of row updates before 520 | # analyze 521 | #autovacuum_vacuum_scale_factor = 0.2 # fraction of table size before vacuum 522 | #autovacuum_analyze_scale_factor = 0.1 # fraction of table size before analyze 523 | #autovacuum_freeze_max_age = 200000000 # maximum XID age before forced vacuum 524 | # (change requires restart) 525 | #autovacuum_multixact_freeze_max_age = 400000000 # maximum multixact age 526 | # before forced vacuum 527 | # (change requires restart) 528 | #autovacuum_vacuum_cost_delay = 20ms # default vacuum cost delay for 529 | # autovacuum, in milliseconds; 530 | # -1 means use vacuum_cost_delay 531 | #autovacuum_vacuum_cost_limit = -1 # default vacuum cost limit for 532 | # autovacuum, -1 means use 533 | # vacuum_cost_limit 534 | 535 | 536 | #------------------------------------------------------------------------------ 537 | # CLIENT CONNECTION DEFAULTS 538 | #------------------------------------------------------------------------------ 539 | 540 | # - Statement Behavior - 541 | 542 | #search_path = '"$user", public' # schema names 543 | #default_tablespace = '' # a tablespace name, '' uses the default 544 | #temp_tablespaces = '' # a list of tablespace names, '' uses 545 | # only default tablespace 546 | #check_function_bodies = on 547 | #default_transaction_isolation = 'read committed' 548 | #default_transaction_read_only = off 549 | #default_transaction_deferrable = off 550 | #session_replication_role = 'origin' 551 | #statement_timeout = 0 # in milliseconds, 0 is disabled 552 | #lock_timeout = 0 # in milliseconds, 0 is disabled 553 | #idle_in_transaction_session_timeout = 0 # in milliseconds, 0 is disabled 554 | #vacuum_freeze_min_age = 50000000 555 | #vacuum_freeze_table_age = 150000000 556 | #vacuum_multixact_freeze_min_age = 5000000 557 | #vacuum_multixact_freeze_table_age = 150000000 558 | #bytea_output = 'hex' # hex, escape 559 | #xmlbinary = 'base64' 560 | #xmloption = 'content' 561 | #gin_fuzzy_search_limit = 0 562 | #gin_pending_list_limit = 4MB 563 | 564 | # - Locale and Formatting - 565 | 566 | datestyle = 'iso, mdy' 567 | #intervalstyle = 'postgres' 568 | timezone = 'UTC' 569 | #timezone_abbreviations = 'Default' # Select the set of available time zone 570 | # abbreviations. Currently, there are 571 | # Default 572 | # Australia (historical usage) 573 | # India 574 | # You can create your own file in 575 | # share/timezonesets/. 576 | #extra_float_digits = 0 # min -15, max 3 577 | #client_encoding = sql_ascii # actually, defaults to database 578 | # encoding 579 | 580 | # These settings are initialized by initdb, but they can be changed. 581 | lc_messages = 'en_US.utf8' # locale for system error message 582 | # strings 583 | lc_monetary = 'en_US.utf8' # locale for monetary formatting 584 | lc_numeric = 'en_US.utf8' # locale for number formatting 585 | lc_time = 'en_US.utf8' # locale for time formatting 586 | 587 | # default configuration for text search 588 | default_text_search_config = 'pg_catalog.english' 589 | 590 | # - Other Defaults - 591 | 592 | #dynamic_library_path = '$libdir' 593 | #local_preload_libraries = '' 594 | #session_preload_libraries = '' 595 | 596 | 597 | #------------------------------------------------------------------------------ 598 | # LOCK MANAGEMENT 599 | #------------------------------------------------------------------------------ 600 | 601 | #deadlock_timeout = 1s 602 | #max_locks_per_transaction = 64 # min 10 603 | # (change requires restart) 604 | #max_pred_locks_per_transaction = 64 # min 10 605 | # (change requires restart) 606 | #max_pred_locks_per_relation = -2 # negative values mean 607 | # (max_pred_locks_per_transaction 608 | # / -max_pred_locks_per_relation) - 1 609 | #max_pred_locks_per_page = 2 # min 0 610 | 611 | 612 | #------------------------------------------------------------------------------ 613 | # VERSION/PLATFORM COMPATIBILITY 614 | #------------------------------------------------------------------------------ 615 | 616 | # - Previous PostgreSQL Versions - 617 | 618 | #array_nulls = on 619 | #backslash_quote = safe_encoding # on, off, or safe_encoding 620 | #default_with_oids = off 621 | #escape_string_warning = on 622 | #lo_compat_privileges = off 623 | #operator_precedence_warning = off 624 | #quote_all_identifiers = off 625 | #standard_conforming_strings = on 626 | #synchronize_seqscans = on 627 | 628 | # - Other Platforms and Clients - 629 | 630 | #transform_null_equals = off 631 | 632 | 633 | #------------------------------------------------------------------------------ 634 | # ERROR HANDLING 635 | #------------------------------------------------------------------------------ 636 | 637 | #exit_on_error = off # terminate session on any error? 638 | #restart_after_crash = on # reinitialize after backend crash? 639 | 640 | 641 | #------------------------------------------------------------------------------ 642 | # CONFIG FILE INCLUDES 643 | #------------------------------------------------------------------------------ 644 | 645 | # These options allow settings to be loaded from files other than the 646 | # default postgresql.conf. 647 | 648 | #include_dir = 'conf.d' # include files ending in '.conf' from 649 | # directory 'conf.d' 650 | #include_if_exists = 'exists.conf' # include file only if it exists 651 | #include = 'special.conf' # include file 652 | 653 | 654 | #------------------------------------------------------------------------------ 655 | # CUSTOMIZED OPTIONS 656 | #------------------------------------------------------------------------------ 657 | 658 | # Add settings for extensions here 659 | include_if_exists = 'master.conf' 660 | include_if_exists = 'replica.conf' -------------------------------------------------------------------------------- /config/replica.conf: -------------------------------------------------------------------------------- 1 | hot_standby = on -------------------------------------------------------------------------------- /config/secret.yml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Secret 3 | metadata: 4 | name: postgres 5 | type: Opaque 6 | stringData: 7 | password: master-password 8 | replicaPassword: replica-password 9 | 10 | -------------------------------------------------------------------------------- /service.yml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | app: postgres 6 | name: postgres 7 | spec: 8 | type: ClusterIP 9 | ports: 10 | - name: postgres 11 | port: 5432 12 | protocol: TCP 13 | targetPort: 5432 14 | selector: 15 | app: postgres 16 | 17 | --- 18 | 19 | apiVersion: v1 20 | kind: Service 21 | metadata: 22 | labels: 23 | app: postgres-replica 24 | name: postgres-replica 25 | 26 | spec: 27 | type: ClusterIP 28 | ports: 29 | - name: postgres-replica 30 | port: 5432 31 | protocol: TCP 32 | targetPort: 5432 33 | selector: 34 | app: postgres-replica 35 | 36 | --- 37 | -------------------------------------------------------------------------------- /statefulset-master.yml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: StatefulSet 3 | metadata: 4 | name: postgres 5 | spec: 6 | updateStrategy: 7 | type: RollingUpdate 8 | 9 | selector: 10 | matchLabels: 11 | app: postgres 12 | 13 | serviceName: postgres 14 | replicas: 1 15 | template: 16 | metadata: 17 | labels: 18 | app: postgres 19 | spec: 20 | volumes: 21 | - name: postgres-config 22 | configMap: 23 | name: postgres 24 | 25 | terminationGracePeriodSeconds: 10 26 | 27 | containers: 28 | - name: postgres 29 | image: postgres:10.5 30 | args: ['-c', 'config_file=/etc/postgres.conf', '-c', 'hba_file=/etc/pg_hba.conf'] 31 | 32 | imagePullPolicy: IfNotPresent 33 | 34 | ports: 35 | - name: postgres 36 | containerPort: 5432 37 | protocol: TCP 38 | 39 | resources: 40 | requests: 41 | cpu: 100m 42 | memory: 256Mi 43 | 44 | env: 45 | - name: POSTGRES_USER 46 | value: postgres 47 | 48 | - name: PGUSER 49 | value: postgres 50 | 51 | - name: POSTGRES_DB 52 | value: postgres 53 | 54 | - name: PGDATA 55 | value: /var/lib/postgresql/data/pgdata 56 | 57 | - name: POSTGRES_PASSWORD 58 | valueFrom: 59 | secretKeyRef: 60 | key: password 61 | name: postgres 62 | 63 | - name: REPLICATION_PASSWORD 64 | valueFrom: 65 | secretKeyRef: 66 | key: replicaPassword 67 | name: postgres 68 | 69 | - name: POD_IP 70 | valueFrom: 71 | fieldRef: 72 | apiVersion: v1 73 | fieldPath: status.podIP 74 | 75 | livenessProbe: 76 | exec: 77 | command: 78 | - sh 79 | - -c 80 | - exec pg_isready --host $POD_IP 81 | failureThreshold: 6 82 | initialDelaySeconds: 60 83 | periodSeconds: 10 84 | successThreshold: 1 85 | timeoutSeconds: 5 86 | 87 | readinessProbe: 88 | exec: 89 | command: 90 | - sh 91 | - -c 92 | - exec pg_isready --host $POD_IP 93 | failureThreshold: 3 94 | initialDelaySeconds: 5 95 | periodSeconds: 5 96 | successThreshold: 1 97 | timeoutSeconds: 3 98 | 99 | volumeMounts: 100 | - mountPath: /var/lib/postgresql/data/pgdata 101 | name: postgres 102 | subPath: postgres-db 103 | 104 | - name: postgres-config 105 | mountPath: /etc/postgres.conf 106 | subPath: postgres.conf 107 | 108 | - name: postgres-config 109 | mountPath: /etc/master.conf 110 | subPath: master.conf 111 | 112 | - name: postgres-config 113 | mountPath: /etc/pg_hba.conf 114 | subPath: pg_hba.conf 115 | 116 | - name: postgres-config 117 | mountPath: /docker-entrypoint-initdb.d/create-replica-user.sh 118 | subPath: create-replica-user.sh 119 | 120 | volumeClaimTemplates: 121 | - metadata: 122 | name: postgres 123 | spec: 124 | accessModes: ["ReadWriteOnce"] 125 | resources: 126 | requests: 127 | storage: 1Gi 128 | -------------------------------------------------------------------------------- /statefulset-replica.yml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: StatefulSet 3 | metadata: 4 | name: postgres-replica 5 | spec: 6 | updateStrategy: 7 | type: RollingUpdate 8 | 9 | selector: 10 | matchLabels: 11 | app: postgres-replica 12 | 13 | serviceName: postgres-replica 14 | replicas: 1 15 | template: 16 | metadata: 17 | labels: 18 | app: postgres-replica 19 | spec: 20 | volumes: 21 | - name: postgres-config 22 | configMap: 23 | name: postgres 24 | 25 | terminationGracePeriodSeconds: 10 26 | 27 | initContainers: 28 | - name: setup-replica-data-directory 29 | image: postgres:10.5 30 | 31 | env: 32 | - name: PGPASSWORD 33 | valueFrom: 34 | secretKeyRef: 35 | key: replicaPassword 36 | name: postgres 37 | 38 | command: 39 | - sh 40 | - -c 41 | - | 42 | if [ -z "$(ls -A /var/lib/postgresql/data/pgdata)" ]; then 43 | echo "Running pg_basebackup to catch up replication server..."; 44 | pg_basebackup -R -h postgres -D /var/lib/postgresql/data/pgdata -P -U replication; 45 | chown -R postgres:postgres $PGDATA; 46 | else 47 | echo "Skipping pg_basebackup because directory is not empty"; 48 | fi 49 | 50 | volumeMounts: 51 | - mountPath: /var/lib/postgresql/data/pgdata 52 | name: postgres-replica 53 | subPath: postgres-db 54 | 55 | containers: 56 | - name: postgres-replica 57 | image: postgres:10.5 58 | args: ['-c', 'config_file=/etc/postgres.conf'] 59 | 60 | imagePullPolicy: IfNotPresent 61 | 62 | ports: 63 | - name: postgres-rep 64 | containerPort: 5432 65 | protocol: TCP 66 | 67 | resources: 68 | requests: 69 | cpu: 100m 70 | memory: 256Mi 71 | 72 | env: 73 | - name: POSTGRES_USER 74 | value: postgres 75 | 76 | - name: PGUSER 77 | value: postgres 78 | 79 | - name: POSTGRES_DB 80 | value: postgres 81 | 82 | - name: PGDATA 83 | value: /var/lib/postgresql/data/pgdata 84 | 85 | - name: POSTGRES_PASSWORD 86 | valueFrom: 87 | secretKeyRef: 88 | key: password 89 | name: postgres 90 | 91 | - name: POD_IP 92 | valueFrom: 93 | fieldRef: 94 | apiVersion: v1 95 | fieldPath: status.podIP 96 | 97 | livenessProbe: 98 | exec: 99 | command: 100 | - sh 101 | - -c 102 | - exec pg_isready --host $POD_IP 103 | failureThreshold: 6 104 | initialDelaySeconds: 60 105 | periodSeconds: 10 106 | successThreshold: 1 107 | timeoutSeconds: 5 108 | 109 | readinessProbe: 110 | exec: 111 | command: 112 | - sh 113 | - -c 114 | - exec pg_isready --host $POD_IP 115 | failureThreshold: 3 116 | initialDelaySeconds: 5 117 | periodSeconds: 5 118 | successThreshold: 1 119 | timeoutSeconds: 3 120 | 121 | volumeMounts: 122 | - mountPath: /var/lib/postgresql/data/pgdata 123 | name: postgres-replica 124 | subPath: postgres-db 125 | 126 | - name: postgres-config 127 | mountPath: /etc/postgres.conf 128 | subPath: postgres.conf 129 | 130 | - name: postgres-config 131 | mountPath: /etc/replica.conf 132 | subPath: replica.conf 133 | 134 | 135 | 136 | volumeClaimTemplates: 137 | - metadata: 138 | name: postgres-replica 139 | spec: 140 | accessModes: ["ReadWriteOnce"] 141 | resources: 142 | requests: 143 | storage: 1Gi 144 | --------------------------------------------------------------------------------