-
自定义基于kubeadm创建的k8s集群证书
在 Kubernetes 集群的运维过程中,证书管理是非常重要的一部分。尤其是自定义证书的过期时间和定期的证书检查与替换。本文将详细介绍如何自定义基于kubeadm创建的k8s集群证书。
- 确认当前 Kubernetes版本
首先,确认当前 Kubernetes版本:
Bash
kubectl version
WARNING: This version information is deprecated and will be replaced with the output from kubectl version –short. Use –output=yaml|json to get the full version.
Client Version: version.Info{Major:”1″, Minor:”27″, GitVersion:”v1.27.6″, GitCommit:”741c8db18a52787d734cbe4795f0b4ad860906d6″, GitTreeState:”clean”, BuildDate:”2023-09-13T09:21:34Z”, GoVersion:”go1.20.8″, Compiler:”gc”, Platform:”linux/amd64″}
Kustomize Version: v5.0.1
Server Version: version.Info{Major:”1″, Minor:”27″, GitVersion:”v1.27.6″, GitCommit:”741c8db18a52787d734cbe4795f0b4ad860906d6″, GitTreeState:”clean”, BuildDate:”2023-09-13T09:14:09Z”, GoVersion:”go1.20.8″, Compiler:”gc”, Platform:”linux/amd64″}
# 这里查看的go环境版本为:GoVersion:”go1.20.8″
# Kubernetes源码版本为:GitVersion:”v1.27.6″2.下载对应源码和编译环境
- 下载对应源码
根据Kubernetes版本信息下载对应版本的Kubernetes源码,下载地址:https://github.com/kubernetes/kubernetes/releases,这里是v1.27.6版本
- 下载对应go环境
根据Kubernetes版本信息下载对应版本的go环境,下载地址:https://go.dev/dl/,这里是1.20.8版本
- 证书备份
为确保安全,先进行证书备份:
Bash
# master
cp -r /etc/kubernetes/pki /etc/kubernetes/pki-backup- 证书检查
Kubernetes 提供了 kubeadm 命令来检测集群证书的过期时间。执行以下命令检查证书状态:
Bash
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’CERTIFICATE EXPIRES RESIDUAL TIME CERTIFICATE AUTHORITY EXTERNALLY MANAGED
admin.conf Feb 14, 2026 05:26 UTC 364d ca no
apiserver Feb 14, 2026 05:26 UTC 364d ca no
apiserver-etcd-client Feb 14, 2026 05:26 UTC 364d etcd-ca no
apiserver-kubelet-client Feb 14, 2026 05:26 UTC 364d ca no
controller-manager.conf Feb 14, 2026 05:26 UTC 364d ca no
etcd-healthcheck-client Feb 14, 2026 05:26 UTC 364d etcd-ca no
etcd-peer Feb 14, 2026 05:26 UTC 364d etcd-ca no
etcd-server Feb 14, 2026 05:26 UTC 364d etcd-ca no
front-proxy-client Feb 14, 2026 05:26 UTC 364d front-proxy-ca no
scheduler.conf Feb 14, 2026 05:26 UTC 364d ca no此命令会列出集群中所有证书的过期信息,当前证书过期时间为1年。
- 自定义证书时间并替换
按照以下步骤进行替换。
1.准备编译环境
在 master节点准备编译环境:
Bash
# master
tar xf go1.20.8.linux-amd64.tar.gz -C /usr/local/
echo “export PATH=$PATH:/usr/local/go/bin” >>/etc/profile
source /etc/profile
go version- 源码编译和替换
然后,编译 kubeadm 并替换原有版本:
Bash
# master
tar xf kubernetes-1.27.6.tar.gz
cd kubernetes-1.27.6/
vi cmd/kubeadm/app/constants/constants.go修改证书有效期为 10 年(3650 天):
Bash
CertificateValidity = time.Hour * 24 * 365 * 10编译 kubeadm:
Bash
make WHAT=cmd/kubeadm GOFLAGS=-v
ll _output/bin/
mv /usr/bin/kubeadm /usr/bin/kubeadm.bak
cp _output/bin/kubeadm /usr/bin/- 更新证书
执行以下命令更新所有证书:
Bash
# master
kubeadm certs renew all
# 如不需要更新所有证书,可以按需更新以下证书
kubeadm certs renew admin.conf
kubeadm certs renew apiserver
kubeadm certs renew apiserver-kubelet-client
kubeadm certs renew controller-manager.conf
kubeadm certs renew front-proxy-client
kubeadm certs renew scheduler.conf- 同步文件至其他 Master 节点
将更新后的 kubeadm 二进制文件和证书同步到其他 Master 节点:
Bash
scp /usr/bin/kubeadm master02:/usr/bin
scp /usr/bin/kubeadm master03:/usr/bin
scp -r /etc/kubernetes/pki master02:/etc/kubernetes
scp -r /etc/kubernetes/pki master03:/etc/kubernetes- 重启相关组件
最后,重启相关的 Kubernetes 组件,使证书生效:
Bash
kubectl -n kube-system delete po -l ‘component=kube-apiserver’
kubectl -n kube-system delete po -l ‘component=kube-controller-manager’
kubectl -n kube-system delete po -l ‘component=kube-scheduler’通过执行这些命令,集群中的证书将会更新,并且相关的组件会自动重启,应用新证书。
- 证书检查
执行以下命令检查证书状态:
Bash
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’CERTIFICATE EXPIRES RESIDUAL TIME CERTIFICATE AUTHORITY EXTERNALLY MANAGED
admin.conf Feb 12, 2035 05:48 UTC 9y ca no
apiserver Feb 12, 2035 05:48 UTC 9y ca no
apiserver-etcd-client Feb 12, 2035 05:48 UTC 9y etcd-ca no
apiserver-kubelet-client Feb 12, 2035 05:48 UTC 9y ca no
controller-manager.conf Feb 12, 2035 05:48 UTC 9y ca no
etcd-healthcheck-client Feb 12, 2035 05:48 UTC 9y etcd-ca no
etcd-peer Feb 12, 2035 05:48 UTC 9y etcd-ca no
etcd-server Feb 12, 2035 05:48 UTC 9y etcd-ca no
front-proxy-client Feb 12, 2035 05:48 UTC 9y front-proxy-ca no
scheduler.conf Feb 12, 2035 05:48 UTC 9y ca no可以看到,现在所有证书过期时间均已变成10年。
总结
通过重新编译kubeadm源码,可以自定义生成的证书过期时间,对于测试和生产环境都能提供极大的便利。