One primary, multiple replicas, a custom read-only PgBouncer endpoint, and a Kubernetes Service that apparently believed IP addresses were permanent.
A Kubernetes Pod’s IP address is ephemeral.
I knew that.
I’d explained it to other engineers. I’d probably said it with unreasonable confidence during architecture discussions.
And yet, there I was, manually updating PostgreSQL replica IP addresses in a Kubernetes Service because our read-only database connections had stopped working.
Again.
The PostgreSQL cluster was healthy. Patroni knew which instance was the primary. Kubernetes knew where the replacement Pods were running.
PgBouncer, unfortunately, was still trying to reach Pods that no longer existed.
The first time it happened, updating the Endpoints seemed reasonable.
The second time, I realized we hadn’t fixed the problem.
We’d merely reset the countdown.
This is the story of how a relatively simple read-only PostgreSQL endpoint turned into a recurring problem involving Crunchy Postgres Operator, Kubernetes service discovery, and the consequences of treating ephemeral infrastructure as permanent.
And, eventually, why we started building a controller to reconcile the whole thing automatically.
1. The architecture that seemed reasonable
In my previous article about running PostgreSQL on Kubernetes, I wrote about the challenges of operating stateful workloads using Crunchy Postgres Operator (PGO).
One of the configurations I worked with on Azure Kubernetes Service (AKS) had four PostgreSQL instances:
- One primary responsible for writes.
- One synchronous standby.
- Two asynchronous replicas intended for read workloads.
Crunchy PGO managed the PostgreSQL cluster, with Patroni responsible for coordinating high availability.
The operator also provided PgBouncer for connection pooling.
For applications performing writes, the architecture was relatively straightforward.
Application connections would go through the operator-managed PgBouncer endpoint, which ultimately directed traffic toward the writable PostgreSQL primary.
But we also wanted to distribute read workloads.
More specifically, we wanted to direct read-only application traffic toward the asynchronous replicas, keeping that workload away from the primary and the synchronous standby.
Why exclude the synchronous standby?
Because in our design, that instance had a specific job: participating in the database’s durability and high-availability strategy.
We didn’t want the read workload competing unnecessarily with that responsibility.
This was an architectural choice for our environment, not a universal rule that synchronous standbys should never serve reads.
The intended traffic pattern was simple:
Application
|
+---------+---------+
| |
WRITES READS
| |
v v
RW PgBouncer RO PgBouncer
| |
v v
PostgreSQL Async-Replica
Primary Service
|
+-----+-----+
| |
v v
Async-1 Async-2
The problem wasn’t PostgreSQL replication.
It was how to maintain that read-only routing path without depending on a manually maintained list of replica addresses.
At the time, the implementation seemed reasonable.
In hindsight, the missing piece was rather important.
2. The read-only PgBouncer problem
Crunchy PGO gave us a managed PgBouncer deployment for the PostgreSQL cluster.
However, the particular configuration we needed for a separate asynchronous-replica pool wasn’t available through the supported CRD fields in the operator version we were using.
I remember attempting to configure additional database routing behavior through the operator’s PgBouncer configuration.
Kubernetes rejected the configuration because the proposed field wasn’t recognized by the installed schema.
Fair enough.
The API server wasn’t going to accept a configuration simply because I thought it should exist.
So we explored a separate PgBouncer deployment for read-only workloads.
That introduced an interesting requirement.
PgBouncer needed a stable address representing the currently eligible asynchronous PostgreSQL replicas.
We couldn’t simply hardcode the primary or use the existing read-write endpoint.
And we couldn’t blindly select every PostgreSQL instance, because that could include the primary or synchronous standby.
We needed a Service that represented a dynamic subset of the cluster.
Our solution involved a separate Kubernetes Service:
app-pg-replicas-async
And a separate read-only pooler:
app-pg-pgbouncer-ro
The idea was that the Service would expose the eligible asynchronous replicas, while PgBouncer would connect through that Service rather than directly using a single PostgreSQL Pod address.
So far, perfectly reasonable.
Except for one tiny detail.
The Service didn’t have a Pod selector capable of automatically maintaining its backend membership.
Its backing Endpoints were maintained separately.
And those addresses were about to become stale.
3. The day the replicas changed addresses
On January 6, 2026, we encountered a failure that initially looked like a PostgreSQL read-only connectivity issue.
The database Pods were running.
The replicas existed.
But connections through the read-only PgBouncer path were failing.
The natural first assumption was that PgBouncer had a problem.
Perhaps a connection-pool configuration issue.
Perhaps PostgreSQL authentication.
Perhaps replica availability.
But the investigation led somewhere else.
The Kubernetes Service backing our read-only pooler still referenced Pod IP addresses that were no longer assigned to the current PostgreSQL replicas.
The old addresses included:
10.224.0.11
10.224.0.170
The replacement replica Pods were using addresses including:
10.224.0.74
10.224.0.154
10.224.0.109
The important detail wasn’t the exact IP addresses.
It was that the Service’s declared backend addresses no longer matched the actual PostgreSQL Pods.
The infrastructure had changed.
Our routing configuration hadn’t.
Kubernetes had done nothing fundamentally wrong by assigning new Pod IP addresses. A recreated Pod is a new network endpoint.
Our mistake was depending on static endpoint membership without anything continuously maintaining it.
Once we updated the Endpoints to reference the correct replica addresses, connectivity returned.
Problem solved.
Or so we thought.
4. The second incident was considerably less funny
On January 21, 2026, the problem came back.
AKS node replacement had recreated PostgreSQL replicas, and those Pods received new IP addresses.
The read-only PgBouncer endpoint was once again attempting to reach stale backend addresses.
Approximately three hours after the underlying replacement event, we were investigating another failure of the read path.
The symptoms were familiar.
PostgreSQL replicas existed.
The read-only Service existed.
PgBouncer existed.
The applications still couldn’t reliably establish read-only database connections.
And the Endpoints were pointing at the wrong addresses.
Again.
We updated the Endpoints and restarted PgBouncer as part of restoring the read path.
But this time, I wasn’t particularly interested in celebrating the successful repair.
Because we’d just demonstrated that the failure wasn’t random.
It was reproducible through normal infrastructure lifecycle events.
Kubernetes didn’t have to experience a catastrophic failure to break our setup.
A node replacement or PostgreSQL Pod recreation was enough.
Think about that for a moment.
We’d built a highly available PostgreSQL topology with multiple replicas and automated failover capabilities.
But a normal Kubernetes rescheduling event could leave the application’s read-only connectivity pointing at nonexistent backends.
The database was designed to tolerate infrastructure changes. Our service-discovery layer wasn’t.
That was the actual bug.
5. Why Kubernetes didn’t update the Service automatically
This is where the incident became much more interesting from an SRE perspective.
In a typical Kubernetes Service, a selector identifies the Pods that should receive traffic.
For example:
apiVersion: v1
kind: Service
metadata:
name: example-replicas
spec:
selector:
app: postgres
role: replica
ports:
- name: postgres
port: 5432
targetPort: 5432
This is an illustrative Service, not our production manifest.
When Pods matching the selector are created, deleted, or change readiness, Kubernetes controllers maintain the corresponding EndpointSlices.
The Service doesn’t need to remember individual Pod IP addresses.
It selects eligible Pods.
But Kubernetes also supports Services without selectors.
That is useful when the backends cannot be represented by a simple selector, or when an external controller is responsible for choosing them.
And that’s where the operational contract changes.
For a selectorless Service, Kubernetes doesn’t automatically discover which Pods belong behind it.
Someone has to create and maintain the associated EndpointSlices.
Initially, our implementation depended on manually maintained Endpoints, with the legacy Endpoints-to-EndpointSlice mirroring mechanism relevant to how those addresses reached the Service’s routing path.
That distinction matters because the older Endpoints API and the newer EndpointSlice API have different responsibilities and lifecycle behavior.
A selectorless Service is not inherently broken.
Our implementation was missing the continuous reconciliation required to keep it correct.
The Service had a stable name.
But a stable Service name is not the same thing as stable backend membership.
And the backend membership was precisely what we weren’t maintaining.
6. Why couldn’t we just use a label selector?
This is the obvious question.
And it’s a fair one.
Kubernetes is already good at maintaining endpoint membership when Pods match a Service selector.
Why write custom infrastructure when the platform has a built-in mechanism?
The difficulty was that our desired membership wasn’t simply:
“All PostgreSQL Pods.”
Or even:
“All PostgreSQL replicas.”
It was:
Only the PostgreSQL instances currently eligible to serve asynchronous read traffic.
That eligibility depended on the database topology and replication roles.
A PostgreSQL instance could be:
- The current primary.
- The synchronous standby.
- An asynchronous standby.
- A replica that was unhealthy or otherwise unsuitable for new traffic.
And those roles weren’t necessarily permanent.
Patroni could change the cluster topology during failover or recovery.
An instance that was asynchronous yesterday might have a different role today.
A Pod that was previously eligible might no longer belong in the read pool.
This meant we couldn’t simply identify replicas once and assume the result would remain correct.
We needed a mechanism to continuously answer:
Which PostgreSQL Pods are currently eligible for asynchronous read traffic?
If trustworthy operator-managed labels express that exact condition and update with role transitions, a native selector-based Service can be a simpler solution.
But if those labels don’t express the complete routing policy, a custom controller may be justified.
The important thing is to avoid replacing a dynamic database-role problem with a static network-address configuration.
Which, unfortunately, is exactly what we’d done.
7. Debugging the problem properly
The recurring incident changed how I investigated this class of failure.
Instead of immediately restarting PgBouncer or PostgreSQL, I started comparing three separate views of the system.
First: what Kubernetes says is running.
kubectl get pods -n pg -o wide
This shows the current Pod IP addresses and the nodes hosting them.
Second: what the read-only Service believes its backends are.
kubectl get svc app-pg-replicas-async \
-n pg -o yaml
Followed by:
kubectl get endpoints app-pg-replicas-async \
-n pg -o yaml
And, importantly:
kubectl get endpointslices -n pg \
-l kubernetes.io/service-name=app-pg-replicas-async \
-o yaml
The EndpointSlice inspection matters because that’s where Kubernetes maintains the backend addresses used by modern Service networking.
When investigating a Service problem, you should examine all EndpointSlices associated with it rather than assuming a single slice represents the entire backend set.
Third: what PostgreSQL says about its replication topology.
From the primary, a useful starting point is:
SELECT
application_name,
client_addr,
state,
sync_state,
replay_lsn
FROM pg_stat_replication;
This exposes the connected standbys visible to the primary, including their synchronization state.
It’s useful evidence, although it isn’t a complete replacement for checking Patroni’s current topology and PostgreSQL readiness.
The goal was to compare:
Actual PostgreSQL replicas
|
v
Current Kubernetes Pod IPs
|
v
Service EndpointSlice addresses
|
v
PgBouncer backend connectivity
A mismatch between the current Pod IPs and the Service’s published endpoints was the crucial clue in our incidents.
Suddenly, the problem stopped looking like a mysterious PgBouncer failure.
It was a broken mapping between database roles and network destinations.
And that is a much more actionable problem.
8. The custom controller approach
After the problem repeated, manually updating Endpoints was no longer an acceptable long-term strategy.
We started working on a custom controller to keep the read-only Service synchronized with the actual PostgreSQL topology.
The idea was straightforward.
Observe the PostgreSQL cluster.
Determine which instances are eligible asynchronous replicas.
Publish their current Pod IP addresses to the Service’s EndpointSlice.
Repeat whenever the state changes.
Conceptually:
Kubernetes API
|
v
PostgreSQL Pod Events
|
v
Async Replica Controller
|
+------+------+
| |
v v
Pod Health Database Role
| |
+------+------+
|
v
Eligible Async Pods
|
v
Reconcile EndpointSlice
|
v
Read-Only Service
|
v
PgBouncer
The controller design had several responsibilities.
Discover eligible replicas
It needed to identify PostgreSQL Pods belonging to the correct cluster.
Then exclude the primary and synchronous standby.
A Pod merely existing wasn’t sufficient proof that it should receive traffic.
Its current role and readiness mattered.
Reconcile backend addresses
The controller needed to compare the desired set of eligible Pod addresses with the addresses currently published for the read-only Service.
If they differed, it should update the EndpointSlice.
A correct reconciliation loop should be idempotent.
Running it repeatedly against unchanged state shouldn’t cause unnecessary modifications.
React to changes
Pod creation, deletion, readiness changes, and database-role changes could all affect eligibility.
The design therefore needed event-driven reconciliation, with periodic checks as a safety net.
A periodic reconciliation interval alone is not a guarantee of application recovery time.
But it can help repair drift when an event is missed.
Avoid publishing unhealthy backends
Removing stale IPs was only half the problem.
We also had to avoid introducing new failures by publishing Pods that weren’t ready or weren’t eligible for the requested read workload.
The safe default is to publish only endpoints that have been positively identified as eligible.
And to expose enough metrics and logs that an empty backend set is immediately visible rather than silently turning into another mystery.
The controller was an attempt to turn an error-prone manual procedure into a continuously enforced invariant.
The read-only Service should contain the current eligible asynchronous replicas, and nothing else.
I don’t want to claim that our initial controller design immediately solved every edge case.
It didn’t.
There was another lesson coming.
9. The EndpointSlice had another surprise waiting
By February 2026, we encountered another read-routing problem involving EndpointSlice state.
This time, the issue wasn’t simply the original static list of stale Pod IP addresses.
We investigated an empty EndpointSlice and problems in the handling and reconciliation of endpoint state.
The read-only path was once again affected despite the PostgreSQL replicas being available.
We recovered connectivity by correcting the stale slice state and recreating the expected backend membership.
The important point was that introducing automation doesn’t automatically make the resulting system reliable.
A controller has its own failure modes.
What happens if it publishes an empty EndpointSlice?
What happens if a Kubernetes API update fails?
What if its cached view of Pods is stale?
What if two reconciliation paths attempt to update the same resource?
What if the primary changes before the controller has updated the read-only membership?
And perhaps most importantly:
What happens if the controller itself stops running?
A controller doesn’t eliminate the need for operational thinking.
It moves some of that responsibility into code.
That’s still a worthwhile tradeoff when the code can enforce an important invariant more reliably than manual intervention.
But it must be designed and tested like any other production component.
10. What I’d build differently today
Looking back, I would approach this problem differently from the beginning.
First, check whether native selectors are sufficient
Before introducing a custom controller, I’d inspect the labels and role information actually maintained by the PostgreSQL operator.
If a stable, operator-supported selector can identify the exact eligible replica set, use it.
Don’t write a controller to reproduce behavior Kubernetes already provides.
But verify that the labels remain correct through failover, promotion, and rescheduling.
Second, define endpoint eligibility explicitly
I would express the routing policy in terms of database roles and health rather than Pod names or IP addresses.
For our architecture, eligibility meant belonging to the correct PostgreSQL cluster, being ready, and currently serving as an asynchronous replica.
The policy should be explicit enough to test.
Third, use EndpointSlices deliberately
For a selectorless Service managed by custom code, I’d use the EndpointSlice API directly rather than depending on manually maintained legacy Endpoints and mirroring behavior.
I’d also give controller-managed EndpointSlices a unique endpointslice.kubernetes.io/managed-by value.
Ownership needs to be clear.
And when reading endpoint membership, the controller and diagnostic tooling must account for all slices associated with the Service.
Fourth, reconcile desired state, not individual events
A Pod deletion event shouldn’t trigger a one-off patch based only on that event.
It should trigger a fresh calculation of the complete desired endpoint set.
That way, missed or duplicated events don’t necessarily cause lasting drift.
And regular reconciliation can correct state that changed while the controller was unavailable.
Fifth, observe the routing layer
I would monitor at least:
- Number of eligible asynchronous replicas.
- Number of published Service endpoints.
- Age of the last successful reconciliation.
- Endpoint update failures.
- PgBouncer backend connection failures.
- End-to-end read-only query success.
The difference between eligible and published endpoints is particularly valuable.
If PostgreSQL reports healthy asynchronous replicas but the Service has no usable backends, that deserves immediate attention.
Finally, test the failure conditions deliberately
I’d run controlled tests involving Pod recreation, node replacement, replica promotion, temporary API failures, and controller restarts.
For each test, I’d verify both endpoint membership and actual application read queries through PgBouncer.
Because a correct-looking EndpointSlice doesn’t prove that the connection path is working.
And an apparently successful controller reconcile doesn’t prove the application has recovered.
11. The bigger lesson wasn’t about IP addresses
It’s easy to describe this as a problem caused by PostgreSQL Pod IPs changing.
But that framing misses the important part.
Pod IPs are supposed to be replaceable.
Kubernetes has spent years building abstractions around that fact.
The failure occurred because we introduced a dependency that treated those addresses as stable, without implementing the lifecycle management required to keep the dependency correct.
We had multiple systems, each with a different view of reality.
Kubernetes knew where the Pods were.
Patroni knew the PostgreSQL roles.
The read-only Service knew where it thought the backends were.
PgBouncer depended on that Service to establish database connections.
And the applications depended on PgBouncer.
The problem was the missing synchronization between those views.
Every individual component could appear healthy while the end-to-end system was broken.
That’s what made the incident frustrating.
And that’s also what made it such a valuable lesson in site reliability engineering.
Closing thoughts
I still remember the frustration of discovering that a PostgreSQL read-only connectivity problem was caused by old IP addresses sitting inside a Kubernetes resource.
Not because changing the addresses was difficult.
It wasn’t.
But because the entire point of our architecture was to make PostgreSQL resilient to infrastructure changes.
And we’d accidentally built a routing dependency that wasn’t.
We fixed the immediate incident.
Then the incident happened again.
Then we started addressing the underlying service-discovery problem.
And even that introduced new things to test and debug.
If there’s one thing this experience reinforced, it’s that reliability isn’t about how many replicas you have, how many controllers are running, or whether Kubernetes reports everything as healthy.
It’s about whether the assumptions connecting those components remain true when the system changes.
A stable Service name means very little if nobody is keeping its backends correct.
The first time I manually updated those IP addresses, it felt like fixing an outage.
The second time, it felt like a warning.
By the time we were debugging EndpointSlice reconciliation, I had a much better appreciation for why distributed systems engineers spend so much time talking about desired state, ownership, and eventual consistency.
And why a seemingly harmless piece of manually maintained infrastructure can become everybody’s problem at the worst possible time.
The PostgreSQL replicas weren’t the problem.
Our assumptions about service discovery were.
And Kubernetes, as usual, was doing exactly what we’d told it to do.
Which somehow made the whole thing even more annoying.
I write about PostgreSQL, Kubernetes, reliability engineering, and infrastructure decisions that seemed perfectly reasonable until production disagreed.
Read my previous article: I Tried Running PostgreSQL on Kubernetes. The Database Was the Easy Part.
More engineering notes at advaitkeskar.tech.