k3s: x509: certificate has expired or is not yet valid
Environmental Info: K3s Version: v1.22.6+k3s1 and v1.23.4+k3s1
Node(s) CPU architecture, OS, and Version: ubuntu 1804
Cluster Configuration: 1 server
Describe the bug:
Cannot rotate k3s-serving certificate after restarting k3s
Steps To Reproduce:
- Installed K3s: curl -sfL https://get.k3s.io | sh -
- Make sure all pods are up and running
root@ip-172-31-15-171:~# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-96cc4f57d-xpppw 1/1 Running 0 70s
kube-system local-path-provisioner-84bb864455-lkc65 1/1 Running 0 70s
kube-system helm-install-traefik-crd--1-6mw65 0/1 Completed 0 70s
kube-system helm-install-traefik--1-qbr25 0/1 Completed 1 70s
kube-system svclb-traefik-hxggr 2/2 Running 0 40s
kube-system metrics-server-ff9dbcb6c-txhfq 1/1 Running 0 70s
kube-system traefik-55fdc6d984-c28rn 1/1 Running 0 40s
- current date is:
Fri Feb 25 02:37:07 UTC 2022 - Change the os date to after the certificate expires:
root@ip-172-31-15-171:~# timedatectl set-ntp no
root@ip-172-31-15-171:~# date -s 20230303
Fri Mar 3 00:00:00 UTC 2023
root@ip-172-31-15-171:~# date
Fri Mar 3 00:00:03 UTC 2023
- Restart K3s
systemctl restart k3s
- After a few minutes, query the k3s-serving expiration time
root@ip-172-31-15-171:~# kubectl --insecure-skip-tls-verify get secret -n kube-system k3s-serving -o jsonpath='{.data.tls\.crt}' | base64 -d | openssl x509 -noout -text | grep Not
Not Before: Feb 25 02:34:03 2022 GMT
Not After : Feb 25 02:34:04 2023 GMT
- At this point, kubectl cannot be used due to an expired certificate
root@ip-172-31-15-171:~# kubectl get nodes
Unable to connect to the server: x509: certificate has expired or is not yet valid: current time 2023-03-03T00:04:06Z is after 2023-02-25T02:34:04Z
Expected behavior:
Restart k3s to automatically rotate certificates
Actual behavior:
Restart k3s, k3s-serving does not automatically rotate.
But I can manually rotate the k3s-serving certificate by:
kubectl --insecure-skip-tls-verify delete secret k3s-serving -n kube-system
rm -rf /var/lib/rancher/k3s/server/tls/dynamic-cert.json
systemctl restart k3s
Additional context / logs:
Backporting
- Needs backporting to older releases
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 34 (20 by maintainers)
I use this workaround to get the
k3s-servingcertificate renew.Validated on version
v1.24.4-rc1+k3s1Environment Details
Infrastructure
Node(s) CPU architecture, OS, and Version:
Cluster Configuration:
Testing Steps
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.24.4-rc1+k3s1 INSTALL_K3S_EXEC="server" sh -Validation Results: Message
Unable to connect to the server: x509: certificate has expired or is not yet valid:...is not longer presentProblem exists on 1.23.9+k3s1 as well.
The workaround works:
I see a potential fix (https://github.com/k3s-io/k3s/pull/5951) has been merged to 1.23. When do you think a release with this fix in place will be made please?
Also is there a way I can install the master 1.23.x version so I can test and report back it’s fixed on a VM?
@xzycn please see the steps at https://github.com/k3s-io/k3s/issues/5163#issuecomment-1189750770
@LarsBingBong no, what you’re reporting does not have anything to do with the dynamic certificate expiring. Please open a new issue.
Ah, I see. The locally cached secret is used initially, but when the apiserver comes up, the datastore secret is merged into it, and replaces it - which is what we want in general, to ensure that the datastore certificate is used in favor of the one from the local node. However, we shouldn’t do this if the one from the datastore is expired.