Kubernetes證書使用年限修改方法
Kubernetes的apiservice.crt證書默認只有一年的使用期限,查看方法:
cd /etc/kubernetes/pki [root@Centos8 pki]# openssl x509 -in apiserver.crt -text -noout Certificate: Data: Version: 3 (0x2) Serial Number: 8195991627692645852 (0x71be02f60eb21ddc) Signature Algorithm: sha256WithRSAEncryption Issuer: CN = kubernetes Validity Not Before: Apr 11 10:52:03 2020 GMT Not After : Apr 11 10:52:03 2021 GMT ...
可以看到,默認的證書到2021年就會過期。
但是,並不是Kubernetes內所有的證書期限都是一年,比如下邊的ca.crt:
[root@Centos8 pki]# openssl x509 -in ca.crt -text -noout Certificate: Data: Version: 3 (0x2) Serial Number: 0 (0x0) Signature Algorithm: sha256WithRSAEncryption Issuer: CN = kubernetes Validity Not Before: Apr 11 10:52:03 2020 GMT Not After : Apr 9 10:52:03 2030 GMT ...
此證書的期限就是10年。
Kubernetes集群版本在更新時,就會自動更新apiserver.crt證書的使用期限,這可能也是k8s官方設置這一年期限的原因 —— 為了讓使用者跟上版本更新的步伐。
但是有一些公司就會選取一個穩定版本一直使用下去,這就會使用本節所介紹的內容:如何修改證書的使用年限。
證書修改流程
由於k8s是基於kubeadm安裝的,所以只需修改kubeadm源碼中的證書期限即可。 kubeadm是采用go語言編寫,所以我們要現在本機安裝go語言環境,然后將對應版本的kubeadm源碼進行修改,修改后重新編譯,再用新編譯的kubeadm生成新證書即可。
1、安裝go環境
## 下載 cd /data wget https://studygolang.com/dl/golang/go1.15.4.linux-amd64.tar.gz
## 解壓 tar zxvf go1.15.4.linux-amd64.tar.gz -C /usr/local/ ## 添加環境變量 vim /etc/bashrc ... export PATH=$PATH:/usr/local/go/bin ... ## 刷新 source /etc/bashrc ## 檢查環境變量 echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/go/bin ## 檢查go版本 [root@Centos8 ~]# go version go version go1.15.4 linux/amd64
2、下載kubernetes項目源碼
## 下載 [root@Centos8 ~]# git clone https://github.com/kubernetes/kubernetes.git
## 查看本地kubeadm版本 [root@Centos8 ~]# kubeadm version kubeadm version: &version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.1" ## 將源碼切換至v1.15.1版本 cd kubernetes git checkout -b remotes/origin/release-1.15.1 v1.15.1
3、修改kubeadm源碼包更新證書策略
vim staging/src/k8s.io/client-go/util/cert/cert.go # kubeadm 1.14版本及之前 vim cmd/kubeadm/app/util/pkiutil/pki_helpers.go # kubeadm 1.14之后,版本越高,文件可能不一樣,還需具體查看官方文檔確認 ... func NewSignedCert(cfg *certutil.Config, key crypto.Signer, caCert *x509.Certificate, caKey crypto.Signer) (*x509.Certificate, error) { const duration3650d = time.Hour * 24 * 365 * 10 # 在此模塊下添加此行,即表示10年,如需100年將10改為100即可 ## 上邊的常量添加好后,修改下邊的NotAfter參數 NotAfter: time.Now().Add(duration3650d).UTC(), ... ## 更改完成后,重新編譯kubeadm [root@Centos8 kubernetes]# make WHAT=cmd/kubeadm GOFLAGS=-v
4、替換kubeadm命令
## 編譯成功后,生成在_output/bin/kubeadm ## 移除舊命令,添加新命令 mv /usr/bin/kubeadm /usr/bin/kubeadm.bak cp _output/bin/kubeadm /usr/bin/kubeadm && chmod +x /usr/bin/kubeadm ## 備份下pki目錄,以防更新失敗 [root@Centos8 kubernetes]# cp -a pki pki.old ## 開始使用新kubeadm命令更新證書,--config指定kubeadm-config文件,目錄可能不同,根據實際情況更改 [root@Centos8 kubernetes]# kubeadm alpha certs renew all --config=/usr/local/install-k8s/core/kubeadm-config.yaml certificate embedded in the kubeconfig file for the admin to use and for kubeadm itself renewed certificate for serving the Kubernetes API renewed certificate the apiserver uses to access etcd renewed certificate for the API server to connect to kubelet renewed certificate embedded in the kubeconfig file for the controller manager to use renewed certificate for liveness probes to healtcheck etcd renewed certificate for etcd nodes to communicate with each other renewed certificate for serving etcd renewed certificate for the front proxy client renewed certificate embedded in the kubeconfig file for the scheduler manager to use renewed
5、檢查是否更新成功
[root@Centos8 pki]# openssl x509 -in apiserver.crt -text -noout Certificate: Data: Version: 3 (0x2) Serial Number: 5901742195310036191 (0x51e73342036384df) Signature Algorithm: sha256WithRSAEncryption Issuer: CN = kubernetes Validity Not Before: Apr 11 10:52:03 2020 GMT Not After : Nov 11 08:01:16 2030 GMT
可以看到,證書期限已經成功更新到10年