DEV Community

Cover image for Kubernetes certificate expiration “X509”
JamallMahmoudi
JamallMahmoudi

Posted on

Kubernetes certificate expiration “X509”

If you’re operating Kubernetes within your infrastructure, it’s imperative to grasp the fundamentals of certificate management to uphold the security and reliability of your cluster. This article delves into the essence of Kubernetes certificates, elucidating their significance and offering insights into their management, particularly focusing on the examination and renewal of the kube-apiserver server certificate. Let’s delve into the intricacies to safeguard the integrity of your Kubernetes cluster.

Certificates within Kubernetes are pivotal for ensuring the fortified communication across various components of the platform. They serve to establish secure connections, encrypt data during transit, and authenticate the identity of Kubernetes components. Absent proper certificate oversight, your cluster becomes susceptible to unauthorized breaches, data breaches, and assorted security vulnerabilities.

Consider a scenario where your Kubernetes cluster houses multiple applications, each containing sensitive customer data. Should the kube-apiserver server certificate, responsible for authenticating the API server, lapse without renewal, it could disrupt component communication, leaving your cluster vulnerable to exploitations. Hence, maintaining a proactive approach towards certificate management is imperative to avert potential security hazards.

Kubernetes Certificates:

Digital documents for authentication, authorization, and encryption in a Kubernetes cluster
Verify identity of nodes, users, and services within the cluster
Based on X.509 standard (PKI certificates)
Consist of 2 main components:
Private key (secret, for signing and decrypting)
Public key (shared, for verifying signatures and encrypting)

Types of Kubernetes Certificates:

Node Certificates: Authenticate nodes to the control plane, generated by the cluster’s CA.
User Certificates: Authenticate users (admins, devs) to the cluster, issued by the cluster’s CA.
Service Account Certificates: Authenticate services and apps within the cluster, created by Kubernetes for each service account.
API Server Certificates: Secure communication between API server and other components, issued by the cluster’s CA.
Etcd Certificates: Secure communication between etcd nodes and other components, generated by the cluster’s CA.
Each type of certificate serves a specific purpose in a Kubernetes cluster.

Why Are Kubernetes Certificates Important?

Kubernetes certificates are crucial for:

Securing data in transit: Encrypting data to prevent unauthorized access.
Verifying component identity: Ensuring components are who they claim to be, preventing impersonation attacks.
Ensuring cluster security: Establishing secure connections to prevent attacks that could compromise the entire cluster.
In short, Kubernetes certificates are essential for maintaining the security and integrity of a Kubernetes cluster.

Checking Certificate Expiration

You can check the expiration date of the kube-apiserver server certificate using:

`mahmoudi@master2:~$ openssl x509 -noout -enddate -in /etc/kubernetes/pki/apiserver.crt

mahmoudi@master2:~$ echo | openssl s_client -showcerts -connect :6443 -servername api 2>/dev/null | openssl x509 -noout -enddate

This command extracts the certificate and displays its expiration date, e.g.:

notAfter=Mar 8 12:50:57 2024 GMT

This shows the certificate expires on March 8, 2024, at 12:50:57 GMT.`

`---------------------------------------------------------------------------------
mahmoudi@master1:~$ sudo kubeadm certs check-expiration
[check-expiration] Reading configuration from the cluster...
[check-expiration] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
W0506 05:02:36.776204 22422 utils.go:69] The recommended value for "clusterDNS" in "KubeletConfiguration" is: [10.233.0.10]; the provided value is: [169.254.25.10]

CERTIFICATE EXPIRES RESIDUAL TIME CERTIFICATE AUTHORITY EXTERNALLY MANAGED
admin.conf May 27, 2024 05:49 UTC 21d ca no

apiserver May 27, 2024 05:40 UTC 21d ca no

apiserver-kubelet-client May 27, 2024 05:40 UTC 21d ca no

controller-manager.conf May 27, 2024 05:49 UTC 21d ca no

front-proxy-client May 27, 2024 05:40 UTC 21d front-proxy-ca no

scheduler.conf May 27, 2024 05:49 UTC 21d ca no

CERTIFICATE AUTHORITY EXPIRES RESIDUAL TIME EXTERNALLY MANAGED
ca Apr 08, 2033 07:05 UTC 8y no

front-proxy-ca Apr 08, 2033 07:05 UTC 8y no

--------------------------------------------------------------------------------------`
Renewing the Certificate

Use kubeadm to renew the kube-apiserver server certificate.
NOTE:
backup_all_file /etc/kubernetes/* ~/backup_kube.

cp /etc/kubernetes /home/user/backups

kubeadm certs renew — help # for more options.

Command: kubeadm certs renew apiserver
This will update the certificate with a new expiration date.
By renewing the certificate before it expires in kubernetes cluster , you can ensure continuous security and smooth operation of your Kubernetes cluster.

In essence, Kubernetes certificates serve as pivotal components in fortifying the security of your Kubernetes cluster. By comprehending their significance, understanding their management intricacies, and adhering to best practices, you can uphold the security of your cluster, safeguarding your applications and data against potential threats. Regularly monitoring and renewing the kube-apiserver server certificate stands as a paramount practice to sustain the ongoing security of your cluster. Thus, prioritize certificate management to ensure the safety of your Kubernetes environment.
Note:
Utilize OpenSSL or CFSSL to routinely verify the expiration date of the kube-apiserver server certificate.
Employ the kubeadm command to renew the certificate proactively before its expiration.
Maintain meticulous records of certificate expiration dates across your Kubernetes cluster and execute timely renewals.
Stay abreast of Kubernetes security best practices and adhere to them diligently to fortify your cluster’s defenses.
Continuously evaluate and enhance your Kubernetes cluster security protocols to preempt potential security vulnerabilities.
This article aims to furnish you with invaluable insights into Kubernetes certificates and their pivotal role in fortifying your cluster’s security. Remember, adopting a proactive stance towards certificate management is indispensable for upholding the security and resilience of your Kubernetes ecosystem. Remain vigilant and ensure the safety of your cluster at all times!
Kubernetes certificates are crucial for securing a Kubernetes cluster. To keep the cluster secure, it’s essential to regularly check and renew the kube-apiserver server certificate, as well as track and renew all certificates in the cluster. Additionally, staying updated with best practices for Kubernetes security and regularly reviewing and updating security measures can help prevent potential security threats. Proactive certificate management is key to maintaining the security and integrity of a Kubernetes environment.

mahmoudi@master1:~$ sudo kubeadm certs renew all

Done renewing certificates. You must restart the kube-apiserver, kube-controller-manager, kube-scheduler and etcd, so that they can use the new certificates.
You must now make sure that you have restarted the following legs and you must do this for each master node.

NOTE:
If you have encountered such an error

mahmoudi@master1:~$ sudo kubectl get pods -o wide
Unable to connect to the server: x509: certificate has expired or is not yet valid

mahmoudi@master2:~$ sudo kubeadm certs check-expiration
mahmoudi@master2:~$ ls -latr /etc/kubernetes/pki/
mahmoudi@master2:~$ cd /etc/kubernetes/
mahmoudi@master2:/etc/kubernetes$ ls -ltra *.conf
-rw------- 1 root root 5638 Apr 16 05:03 admin.conf
-rw------- 1 root root 1989 Apr 16 06:08 kubelet.conf
-rw------- 1 root root 5622 Apr 21 07:09 scheduler.conf
-rw------- 1 root root 5674 May 3 16:08 controller-manager.conf

Steps to fix

mahmoudi@master2:~$ mkdir ~/kubernetes_dir_backup/
mahmoudi@master2:~$ cp -pr /etc/kubernetes ~/kubernetes_dir_backup/

mahmoudi@master2:/etc/kubernetes$ sudo sudo crictl pods
POD ID CREATED STATE NAME NAMESPACE ATTEMPT RUNTIME
3c77dba957da2 2 days ago Ready coredns-68868dc95b-4hnj9 kube-system 0 (default)
acd074c7a060b 2 days ago Ready calico-kube-controllers-685cc55b76-rnd8z kube-system 0 (default)
7d76765d06d1a 2 weeks ago Ready node-exporter-rc6t2 lens-metrics 6 (default)
2d74875eb2086 2 weeks ago Ready nodelocaldns-8l7vt kube-system 18 (default)
e329ae3d3f935 2 weeks ago Ready calico-node-g6kl9 kube-system 18 (default)
5a7051fe472e6 2 weeks ago Ready kube-proxy-gp48l kube-system 5 (default)
6328b43001d94 2 weeks ago Ready prometheus-prometheus-node-exporter-w8lzw monitoring 4 (default)
9b4e3ea90ffea 2 weeks ago Ready kube-controller-manager-master2 kube-system 4 (default)
3d7993468deba 2 weeks ago Ready kube-apiserver-master2 kube-system 4 (default)
2801dbb83835d 2 weeks ago Ready kube-scheduler-master2 kube-system 4 (default)

NOTE:
Now restart pods
mahmoudi@master2:$ cd /etc/kubernetes/manifests
mahmoudi@master2:/etc/kubernetes/manifests$ ls
kube-apiserver.yaml kube-controller-manager.yaml kube-scheduler.yaml
mahmoudi@master2:/etc/kubernetes/manifests$ mv kube-apiserver.yaml /tmp/
mahmoudi@master2:/etc/kubernetes/manifests$ crictl pods

mahmoudi@master2:/etc/kubernetes/manifests$ crictl rmp
mahmoudi@master2:/etc/kubernetes/manifests$ mv /tmp/kube-apiserver.yaml .
mahmoudi@master2:/etc/kubernetes/manifests$ crictl pods

Important note:
Be sure to execute these commands after executing the Renew command

mahmoudi@master2:$ rm -rf $HOME/.kube || true
mahmoudi@master2:$ mkdir -p $HOME/.kube
mahmoudi@master2:$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
mahmoudi@master2:$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

And at the end of the work:

mahmoudi@master2:$ systemctl restart kubelet ; systemctl status kubelet

Top comments (0)