The Flag That Unplugged Your Pod From the API Server
A pod with a service account. A token, a CA, the two environment variables the kubelet sets. Everything in-cluster configuration needs, present and readable. One flag on one kubectl call, and the client stopped looking at any of it.
What the pod said#
A hook job in a namespace that denies egress. Every kubectl call in it failed the same way:
The connection to the server localhost:8080 was refused - did you specify the right host or port?
The namespace had been through a network incident the week before. A real one: a label-scoped allow rule that did not cover a new workload. So the failure read like that again. The job’s own diagnostic said so, in a confident paragraph, naming the file to fix.
It was wrong. The evidence was in the line it had just printed.
Nobody dialled#
localhost:8080 is kubectl’s compiled-in default server. It is what the client
uses when it has decided there is no configuration: no kubeconfig, no
in-cluster credentials. Reaching it means the client never tried the API
server’s address at all.
That is the one durable inference, and it rules out a whole class of theories. A NetworkPolicy is consulted when a packet leaves. No packet left for the API server. A blocked network cannot change which URL a client picks, so no rule added to any policy was going to fix this.
Establish that first. Then stop reading the firewall.
The two checks that came back green#
The usual explanation for a fallback to the default is that in-cluster
configuration was not possible: either KUBERNETES_SERVICE_HOST and
KUBERNETES_SERVICE_PORT are unset, or the projected service-account token is
not there. Both were checked, from inside the failing pod, printed to its log:
in-cluster config: KUBERNETES_SERVICE_HOST=kubernetes.default.svc KUBERNETES_SERVICE_PORT=443
token: readable, 1206 bytes; ca.crt: present
Both legs fine. Every call still went to localhost.
When the two documented preconditions are met and the behaviour is still the fallback, the thing to read is not the pod. It is the command line.
Ten seconds in a container#
The whole thing reproduces with no cluster. A container from the same image
kubectl runs in. A directory holding a self-signed ca.crt, a token in the
right shape, a namespace file, mounted where a pod would have it. The two
environment variables. No network at all, so every call fails, which does not
matter: what we read is the URL it tried.
#!/bin/sh
IMG=cr.example.com/kubectl:1.33.5
pod() {
podman run --rm --network none \
-v "$PWD/sa:/var/run/secrets/kubernetes.io/serviceaccount:ro,Z" \
-e KUBERNETES_SERVICE_HOST=kubernetes.default.svc \
-e KUBERNETES_SERVICE_PORT=443 \
"$IMG" "$@"
}
try() {
printf '%-40s -> ' "$*"
pod "$@" -v=6 2>&1 | sed -n 's/.*url="\(https\{0,1\}:\/\/[^/]*\)\/.*/\1/p' | head -n 1
}
try kubectl get pvc
try kubectl --request-timeout=10s get pvc
try kubectl --request-timeout=0 get pvc
try timeout 10s kubectl get pvc
kubectl get pvc -> https://kubernetes.default.svc:443
kubectl --request-timeout=10s get pvc -> http://localhost:8080
kubectl --request-timeout=0 get pvc -> https://kubernetes.default.svc:443
timeout 10s kubectl get pvc -> https://kubernetes.default.svc:443
One flag. The service account is still there. kubectl is not consulting it.
The verbose output is worth a second look, because it lies. With the flag,
-v=6 prints this before heading for localhost:
merged_client_builder.go:163] Using in-cluster namespace
round_trippers.go:632] "Response" verb="GET" url="http://localhost:8080/api?timeout=10s" ...
Without the flag, the same run prints two lines, and the second one is the one that matters:
merged_client_builder.go:163] Using in-cluster namespace
merged_client_builder.go:121] Using in-cluster configuration
The namespace lookup and the configuration lookup take different branches. The first succeeds either way. So a reader scanning verbose output for the word “in-cluster” finds it, and is reassured, and is wrong.
Why one flag does that#
kubectl builds its client configuration by merging whatever kubeconfig files it
can find with the overrides from the command line. Then it asks one question.
From client-go’s merged_client_builder.go, at the version in the image:
// the configuration is valid, but if this is equal to the defaults we should try
// in-cluster configuration
if !config.loader.IsDefaultConfig(mergedConfig) {
return mergedConfig, nil
}
IsDefaultConfig is a deep equality check against a sentinel: the
configuration you get from no files and no flags, whose server is
http://localhost:8080. In a pod with no kubeconfig, that is exactly what the
merge produces, the check passes, and kubectl goes on to try in-cluster
configuration.
Add --request-timeout=10s and the merged configuration carries a timeout the
sentinel does not. Not equal. Not the default. kubectl returns it as a
deliberate configuration, server and all, and never reaches the in-cluster
branch.
--request-timeout=0 is safe because zero is the documented “no timeout” and
leaves the configuration equal to the sentinel. Any other value does not.
This is not new. It is kubernetes/kubernetes issue 49343, opened in July 2017, and issue 93474, opened in July 2020 under the title “kubectl —request-timeout (or any other client configuration flag) prevents use of in-cluster configuration”. Both are open as we write this. Two pull requests to fix it have been closed without merging and a third is open. We measured it in kubectl v1.33.5. Check your own version; do not assume.
The fix was the cause#
The flag had gone in two days earlier. On purpose.
A poll loop in the same job had been failing silently when the API server was unreachable: no bound on the call, so nothing to fail. Someone added a ten-second request timeout to every kubectl call to turn that silence into a fast, loud error. Reasonable. It created a new silence instead.
The bound is still needed. It moved outside the call:
k() { timeout 10s kubectl "$@"; }
A bound on the process does not touch the client’s configuration. The rig above
shows it. One consequence to write down: timeout kills with exit status 124
and no output, so whatever classifies failures has to know that an empty error
with that status is its own cause, not an unrecognised one.
And then the bound strangled the wait#
Applying that uniformly was the next mistake. The script had one call that was supposed to be slow:
kubectl wait --for=delete pod -l "$SELECTOR" --timeout=240s
Wrapped in timeout 10s, the bound kills the wait it was meant to protect.
Exit 124, ten seconds in, right after the workload had been scaled to zero so
the volume could detach. The worst possible moment to stop.
So the bound is per call. A short one for reads and scales, and for the deliberate wait a bound larger than the wait’s own deadline:
k() { timeout 10s kubectl "$@"; } # reads, scales
kwait() { timeout 250s kubectl "$@"; } # strictly greater than --timeout=240s
The rule is simple enough to check without running anything. For every
--timeout=N in a script, find the wrapper around it and confirm its bound is
strictly greater than N.
The test suite had not caught it because the fake kubectl returned instantly
from wait. A fake that cannot be slow cannot test a timeout. The fix there
needed a pair of assertions, because either one alone is satisfied by a wrong
implementation: a wait longer than the short bound must complete, and a wait
that exceeds its own bound must fail and restore the workload. No bound at all
passes the first. A one-second bound passes the second.
The diagnostic that remembered#
Back to that confident paragraph. The failure message asserted a cause, the allow-list, and it keyed on nothing. It printed the same conclusion for every unreachable-server result regardless of what the error said. It had been written the week before, while the allow-list genuinely was the cause, and it read like hard-won experience because it was.
A diagnostic that names a cause is a claim. It needs a test that the same code reaches a different conclusion on different evidence. The errors here distinguish themselves if you let them:
| error text | what it proves |
|---|---|
localhost:8080 | the client never dialled; suspect the flags |
Forbidden | it reached the server and RBAC refused |
no such host | DNS; the cost of pinning a name |
i/o timeout, dial tcp <ip> | now the network is the suspect |
| exit 124, no output | an external timeout killed it |
| anything else | name no cause, print the evidence |
Exactly one of those headlines may appear. Naming the wrong cause is worse than naming none. It sends the reader to the wrong file with the authority of the artifact behind it.
Ten seconds versus a day#
Everything above was eventually established in a container in about ten seconds. Before that it cost a session of read-only forensics against the live cluster, three changes to add instrumentation, two confidently wrong mechanisms written into a tracking issue, and a five-minute sync cycle per hypothesis.
Worse: a rig built days earlier for the same component had already hit this exact symptom. It was written up as the rig misbehaving and worked around with an init container that handed kubectl a kubeconfig. That workaround suppressed the one reproduction on hand.
Three things we are keeping from it.
- Two falsified hypotheses is the signal to change instrument, not to add logging.
- A rig that misbehaves has reproduced something. Diagnose the divergence or record it as open; do not explain it away to get the rig green.
- The place you cannot exec is the worst place to iterate. Read-only access to the real thing is a reason to leave for a container, not a reason to instrument harder.
What to grep for tonight#
Any --request-timeout with a non-zero value in anything that runs kubectl
from inside a pod: hook jobs, cron jobs, init containers, operator shell-outs,
exec-credential wrappers. Each one is a job that will fall over to
localhost:8080 the moment it runs, and tell you the network did it.
Replace the request bound with a process bound. Then, for every --timeout=
that remains, check the bound wrapping it is larger.
The flag is ordinary hygiene. That is what makes it worth the grep.