Kubeadm证书过期问题修复方法之一:通过修改kubeadm源码

2,357 阅读5分钟

       虽然kubeadm已经是达到GA,而且ca证书默认是10年,但是经过其他ca签发的证书默认只有1年时间,虽然目前还没有遇到问题,但是需要未雨绸缪,提前验证证书,本文也是参考大神的文章,经过本人验证。

环境信息

kubeadmin 1.13.3 

一.验证不同时间编译的二进制的文件是否一致

1.yum安装的kubeadm

[root@host60 ~]# kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.3", GitCommit:"721bfa751924da8d1680787490c54b9179b1fed0", GitTreeState:"clean", BuildDate:"2019-02-01T20:05:53Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
[root@host60 ~]# md5sum /usr/bin/kubeadm 
7e7e0d0245cbcb9ce74b11c745ecc8f8  /usr/bin/kubeadm

2.源码编译时间节点A

[root@localhost kubernetes]# ./_output/dockerized/bin/linux/amd64/kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.3", GitCommit:"721bfa751924da8d1680787490c54b9179b1fed0", GitTreeState:"clean", BuildDate:"2019-02-25T01:32:35Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
[root@localhost kubernetes]# md5sum ./_output/dockerized/bin/linux/amd64/kubeadm
789c964f5a76a78059b22e55dc1b13b1  ./_output/dockerized/bin/linux/amd64/kubeadm

3.源码编译时间节点B

[root@localhost kubernetes]# ./_output/dockerized/bin/linux/amd64/kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.3", GitCommit:"721bfa751924da8d1680787490c54b9179b1fed0", GitTreeState:"clean", BuildDate:"2019-02-25T01:44:22Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
[root@localhost kubernetes]# md5sum ./_output/dockerized/bin/linux/amd64/kubeadm
9232aa9541f068e823ddbee217136705  ./_output/dockerized/bin/linux/amd64/kubeadm

       由此可以得出一个结论,虽然他们都是基于一套代码,甚至编译环境一样的,只是在不同时间节点编译出来的文件都不是一个文件,但是应该不会影响使用,所以我才能放心修改源码进行重新编译,并且可以基于他们的文件进行修改。

二.重新编译kubeadm源码

       本文是购买阿里云美国服务器来验证,避免墙的问题,在执行下面操作之前已经编译过2次,就是上面的上次,如果是初次可能输出略有不同

1.修改源码,查看改动的代码

本文修改过2次,一次是增加10,另外一次是从10改成99

[root@localhost kubernetes]# git diff
diff --git a/staging/src/k8s.io/client-go/util/cert/cert.go b/staging/src/k8s.io/client-go/util/cert/cert.go
index 3429c82..7bc8141 100644
--- a/staging/src/k8s.io/client-go/util/cert/cert.go
+++ b/staging/src/k8s.io/client-go/util/cert/cert.go
@@ -74,7 +74,7 @@ func NewSelfSignedCACert(cfg Config, key crypto.Signer) (*x509.Certificate, erro
                        Organization: cfg.Organization,
                },
                NotBefore:             now.UTC(),
-               NotAfter:              now.Add(duration365d * 10).UTC(),
+               NotAfter:              now.Add(duration365d * 99).UTC(),
                KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
                BasicConstraintsValid: true,
                IsCA:                  true,
@@ -109,7 +109,7 @@ func NewSignedCert(cfg Config, key crypto.Signer, caCert *x509.Certificate, caKe
                IPAddresses:  cfg.AltNames.IPs,
                SerialNumber: serial,
                NotBefore:    caCert.NotBefore,
-               NotAfter:     time.Now().Add(duration365d).UTC(),
+               NotAfter:     time.Now().Add(duration365d * 10).UTC(),
                KeyUsage:     x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
                ExtKeyUsage:  cfg.Usages,
        }

2.重新编译

[root@localhost kubernetes]# ./_output/dockerized/bin/linux/amd64/kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"13+", GitVersion:"v1.13.3-dirty", GitCommit:"721bfa751924da8d1680787490c54b9179b1fed0", GitTreeState:"dirty", BuildDate:"2019-02-25T02:13:07Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
[root@localhost kubernetes]# md5sum ./_output/dockerized/bin/linux/amd64/kubeadm
0e11ca6f3bbccb59a35485be8da49dab  ./_output/dockerized/bin/linux/amd64/kubeadm

对比上面的2次编译git版本出现不同,是因为修改过的代码导致的

GitVersion:"v1.13.3-dirty"

3.准备kubeadm配置初始环境

为了避免其他原因,所以就在这个服务器进行配置,主要就是安装kubelet,过程略

yum install -y kubelet

4.初始化集群

由于只是验证证书,所以并没有准备配置文件,而是直接初始化

我的初始化过程是错误的,因为这里主要是验证证书的问题,只要证书正常生产即可,其他的暂时不考虑

kubeadm init

5.验证集群证书

[root@iZrj95ing09kixersspt6gZ pki]# openssl x509 -in ca.crt -noout -dates
notBefore=Feb 25 02:27:28 2019 GMT
notAfter=Feb  1 02:27:28 2118 GMT
[root@iZrj95ing09kixersspt6gZ pki]# openssl x509 -in apiserver.crt -noout -dates
notBefore=Feb 25 02:27:28 2019 GMT
notAfter=Feb 22 02:27:29 2029 GMT
[root@iZrj95ing09kixersspt6gZ pki]# openssl x509 -in apiserver-etcd-client.crt -noout -dates
notBefore=Feb 25 02:27:27 2019 GMT
notAfter=Feb 22 02:27:28 2029 GMT
[root@iZrj95ing09kixersspt6gZ pki]# openssl x509 -in apiserver-kubelet-client.crt -noout -dates
notBefore=Feb 25 02:27:28 2019 GMT
notAfter=Feb 22 02:27:29 2029 GMT
[root@iZrj95ing09kixersspt6gZ pki]# openssl x509 -in front-proxy-client.crt -noout -dates
notBefore=Feb 25 02:27:27 2019 GMT
notAfter=Feb 22 02:27:27 2029 GMT
[root@iZrj95ing09kixersspt6gZ pki]# openssl x509 -in front-proxy-ca.crt -noout -dates
notBefore=Feb 25 02:27:27 2019 GMT
notAfter=Feb  1 02:27:27 2118 GMT
[root@iZrj95ing09kixersspt6gZ pki]# openssl x509 -in etcd/ca.e^Cnoout -dates
[root@iZrj95ing09kixersspt6gZ pki]# cd etcd
[root@iZrj95ing09kixersspt6gZ etcd]# openssl x509 -in ca.crt -noout -dates
notBefore=Feb 25 02:27:27 2019 GMT
notAfter=Feb  1 02:27:27 2118 GMT
[root@iZrj95ing09kixersspt6gZ etcd]# openssl x509 -in  healthcheck-client.crt -noout -dates
notBefore=Feb 25 02:27:27 2019 GMT
notAfter=Feb 22 02:27:28 2029 GMT
[root@iZrj95ing09kixersspt6gZ etcd]# openssl x509 -in peer.crt -noout -dates
notBefore=Feb 25 02:27:27 2019 GMT
notAfter=Feb 22 02:27:28 2029 GMT
[root@iZrj95ing09kixersspt6gZ etcd]# openssl x509 -in server.crt -noout -dates
notBefore=Feb 25 02:27:27 2019 GMT
notAfter=Feb 22 02:27:28 2029 GMT

可以看到CA证书是100年,其他经过他签发的证书是10年,但是我刚才代码的修改明明是从1改成99了,为什么签发的证书只有10年呢,这个问题就留给大家去思考,其实我也不知道,你们知道了可以告诉我!

三.百度网盘下载

1.已有版本

v1.13.3

2.下载地址

pan.baidu.com/s/1fWDDcFD_…