kubernetes: Kubelet serving CSR never created

What happened?

We are seeing an issue occasionally where the kubelet never gets the server certificate (serverTLSBootstrap: true). We have an auto-approver for the server certificates and detect this issue because we are waiting for the certificate to appear in /var/lib/kubelet/pki/kubelet-server-current.pem. When we hit it, not even the CSR is created. It started happening after upgrading to v1.32 so we believe it could be from a change that was introduced in v1.32. Restarting the kubelet does not help. It happily continues without the server certificate, even though it is configured to bootstrap it.

In healthy clusters, we see this:

$ kubectl get csr
NAME        AGE   SIGNERNAME                                    REQUESTOR                           REQUESTEDDURATION   CONDITION
csr-8dql4   29s   kubernetes.io/kubelet-serving                 system:node:lennart-kubelet-debug   <none>              Pending
csr-r8ztr   17s   kubernetes.io/kubelet-serving                 system:node:lennart-kubelet-debug   <none>              Pending
csr-xtwpf   30s   kubernetes.io/kube-apiserver-client-kubelet   system:node:lennart-kubelet-debug   <none>              Approved,Issued

In faulty clusters, there is just the client CSR. We are not sure why two CSRs are created for the server either, but it works when we approve the newer of them.

Edit: The two server CSRs happen because kubeadm restarts the kubelet as part of initializing. This is how it picks up the client certificate. After the restart, the kubelet does not “remember” its old server CSR so it creates a new. This is not an issue.

Here is the kubelet config from an affected node: kubelet-config.txt

What did you expect to happen?

When configured, the kubelet should always create the server CSR.

How can we reproduce it (as minimally and precisely as possible)?

Unfortunately we do not have any guaranteed reproduction steps. In general, this is what is needed to trigger the issue:

  1. Kubelet is configured to rotate certificates (default) and to use server certificate.
  2. Node status is stable, i.e. there are no changes of any kind that would cause the kubelet to update the status.
  3. Kubelet is restarted and the server certificate does not exist (e.g. because it was removed or it wasn’t issued yet).

In this specific case you should be able to see that the CSR is not created:

$ kubectl get csr
NAME        AGE   SIGNERNAME                                    REQUESTOR                           REQUESTEDDURATION   CONDITION
csr-2mq8h   22s   kubernetes.io/kube-apiserver-client-kubelet   system:node:lennart-kubelet-debug   <none>              Approved,Issued
$ kubectl get nodes
NAME                    STATUS   ROLES           AGE   VERSION
lennart-kubelet-debug   Ready    control-plane   28s   v1.32.1

Anything else we need to know?

I believe the culprit is https://github.com/kubernetes/kubernetes/pull/128640. Not the randomization of the interval, but the change in how the zero case for the lastStatusReportTime is handled. It used to be treated as expired and trigger an update. After the PR it is treated the opposite. This means that patchNodeStatus is never called and consequently neither is setLastObservedNodeAddresses. The latter is initializing the kubelets internal node address state (not node status). This internal state is what the server certificate manager is relying on to create the CSR. As long as it is nil, i.e. as long as the node status hasn’t been patched, it cannot progress.

Kubernetes version

$ kubectl version
Client Version: v1.32.1
Kustomize Version: v5.5.0
Server Version: v1.32.1

Cloud provider

Happens both with and without external cloud provider

OS version

# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, …) and versions (if applicable)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27 (25 by maintainers)

Most upvoted comments

After #128640, it does not do that until there is an actual change to it. Only after that does it respect the nodeStatusReportFrequency.

yep, I think you are right, that looks wrong, it should start reporting periodically

were relying on the tryUpdateNodeStatus previous behavior where it determined it like this:

I see, and kl.clock.Since(kl.lastStatusReportTime) was now - zero , that is why it got triggered

To summarize, I think we should change isUpdateStatusPeriodExperid to return true always if kl.lastStatusReportTime.IsZero() to make sure that things are initialized.

@lentzi90 before we didn’t update the node if it didn’t change

https://github.com/kubernetes/kubernetes/blame/3184eb3d1b2b07b3b68eff5240eb8a8e20bb0d07/pkg/kubelet/kubelet_node_status.go#L484-L488

	node, changed := kl.updateNode(ctx, originalNode)

	if !changed {
		// We don't do markVolumesFromNode(node) here and leave it to the regular syncNodeStatus().
		return false
	}

so that logic looks correct, if there was no update and the node didn’t change, we don’t update.

I think we need to understand what is the heuristic to detect the node change

EDIT

ups, wrong, place , there are two loops that update the node fastNodeStatusUpdate and tryUpdateNodeStatus, you are changing the later, taking a look now 👀

When we hit it, not even the CSR is created. It started happening after upgrading to v1.32 so we believe it could be from a change that was introduced in v1.32.

kubeadm has no recent changes related to the kubelet-finalize phase (minus this). that phase requires the kubelet restart to truly enable kubelet client cert rotation.

What I don’t know is what would happen if the order is different, and is it even possible for them to be different? Perhaps kubeadm is waiting on something that always happens after the client CSR? Is it possible that kubeadm restarts the kubelet during a sensitive step that could somehow make it permanently stuck?

kubeadm does the following on join:

  • writes a bootstrap-kubelet.conf
  • starts the kubelet with client cert rotation enabled (default from config)
  • waits for the kubelet to write a kubelet.conf file (that means csr was sent and tls bootstrap completed)

on init:

  • writes directly a kubelet.conf
  • starts the kubelet with client cert rotation enabled by default (from config)
  • waits for the control plane to start up
  • mutates the kubelet.conf to point to the client certs on disk and restarts the kubelet (kubelet-finalize phase)

if the kubelet knows about the requirement for server cert rotation (from config) but it never creates the csr due to restarts, that seems like a kubelet bug.

This is reproducible maybe 5-10% of the cluster creations, so I don’t think it is about wrong configuration or feature gates.

Minimal test environments seem never produce this same issue, the only reproductions we’ve found are when something goes wrong 100% of the time (like using the link-local ipv6 address. That breaking the node’s addresses shouldn’t happen either IMO, but maybe let’s discuss that in another issue.)

Checking the changelog, most of the Kubelet and CSR related changes were done in 1.31 already, where no issues have been seen. That’d further exclude #126441 and #125813 IMO, but thanks @lentzi90 for verifying that with a test too.

Kubelet is not logging basically anything on any log level related to server certs is also making it hard to debug. The whole DynamicCertificateManager code is silent, and kubelet itself is also very silent on the very few lines it has related to certs. For client certs it at least says something, but server cert path is so silent.

One further finding related to CSRs are that in k8s 1.32, the second server CSR request comes 15 seconds later than the first. In 1.31 and older, they were only few seconds apart. This also happens on minimal kubeadm init based test env. I’ve also not found a reason why this is happening and if its somehow related or not.

It would also be interesting to understand why there is always 2 server CSRs and why the first one never means anything. The contents are basically the same, same requestor, same CSR input, …