Your Certificate Rotated. Production Can Still Go Down.
Vault issued the certificate. cert-manager updated the Secret. The kubelet updated the files. TLS can still break hours later, because none of those steps changes what your process holds in memory.
The failure chain
Vault PKI → mounted volume → stale in-memory TLS context → failed connections
1. Vault signs a new certificate
With cert-manager and a Vault issuer:
apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: web-tls spec: secretName: web-tls duration: 24h renewBefore: 8h dnsNames: - web.prod.svc issuerRef: name: vault-issuer kind: ClusterIssuer Sixteen hours in, cert-manager sends a signing request to Vault and writes the new pair to the Secret.
2. The kubelet updates the volume
The mounted files are symlinks through a ..data directory. The kubelet writes a new directory and repoints ..data with an atomic rename, typically within a minute or two.
The exception is subPath. Those mounts do not receive Secret updates, so mount the whole directory.
3. The process keeps the old certificate
cert, err := tls.LoadX509KeyPair("/etc/tls/tls.crt", "/etc/tls/tls.key") if err != nil { log.Fatal(err) } srv.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}} log.Fatal(srv.ListenAndServeTLS("", "")) The files are read once. Replacing them later has no effect.
4. The old certificate expires
New connections from verifying clients fail:
tls: failed to verify certificate: x509: certificate has expired or is not yet valid Two things can hide the problem:
- Connections that are already open usually keep working until they reconnect.
- Kubernetes HTTPS probes skip certificate verification, so the pod can stay Ready.
Check your own service
# on disk openssl x509 -in /etc/tls/tls.crt -noout -serial -enddate # being served openssl s_client -connect localhost:8443 </dev/null 2>/dev/null \ | openssl x509 -noout -serial -enddate If the serials differ, the process has not loaded the new certificate.
Fix it in Go
Hand crypto/tls a callback instead of a fixed certificate, and re-read the files when they change:
// certReloader re-reads the key pair when the certificate file changes. type certReloader struct { certFile, keyFile string mu sync.Mutex cert *tls.Certificate modTime time.Time } func newCertReloader(certFile, keyFile string) (*certReloader, error) { r := &certReloader{certFile: certFile, keyFile: keyFile} return r, r.reload() } func (r *certReloader) reload() error { info, err := os.Stat(r.certFile) // follows the ..data symlink if err != nil { return err } if info.ModTime().Equal(r.modTime) { return nil } cert, err := tls.LoadX509KeyPair(r.certFile, r.keyFile) if err != nil { return err } r.cert, r.modTime = &cert, info.ModTime() return nil } // GetCertificate runs on each full handshake. func (r *certReloader) GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) { r.mu.Lock() defer r.mu.Unlock() if err := r.reload(); err != nil { log.Printf("keeping previous certificate: %v", err) } return r.cert, nil } Wire it up:
r, err := newCertReloader("/etc/tls/tls.crt", "/etc/tls/tls.key") if err != nil { log.Fatal(err) } srv := &http.Server{ Addr: ":8443", TLSConfig: &tls.Config{GetCertificate: r.GetCertificate}, } log.Fatal(srv.ListenAndServeTLS("", "")) If a reload fails, the server keeps the previous certificate, logs the error, and tries again on the next handshake. This version does a stat and takes a lock on each handshake. For high handshake rates, consider reloading in a background goroutine.
What a local test showed
I ran both versions in a Go test that imitates the kubelet's symlink swap, using a certificate that expires after a few seconds. It ran locally, not on a cluster.
| Check | Result |
|---|---|
| Load-once server after rotation | Still presents the old serial |
| Reloading server after rotation | Presents the new serial |
| Load-once server, new connection after expiry | Fails with the x509 error |
| Load-once server, already-open connection | Still works |
| Reloading server, new connection after expiry | Works |
If you can't change the app
- Nginx and similar: run a watcher that triggers a reload (
nginx -s reload) when the files change. - Anything else: restart the pods before the old certificate expires, either with a controller that watches Secrets or with a scheduled
kubectl rollout restart.
Also alert on the expiry of the certificate being served, not only the one in the Secret.
How does your team handle certificate reloads?
原文:https://www.a8q9.cn/en/article/your-certificate-rotated-production-can-still-go-down-d59f35.html