首页 > 其他分享 >k8s修改证书有效期

k8s修改证书有效期

时间:2023-07-23 19:12:29浏览次数:45  
标签:UTC 有效期 证书 ca no etcd k8s 08 10

k8s各组件非常依赖证书

而默认情况下ca证书是十年,而其他证书都只有一年

Kubernetes中的如果证书过期了,会导致集群中的许多组件无法正常通信,从而影响整个集群的功能。

所以在生产环境下,这是非常大的坑

查看证书有效期

方法一

cd /etc/kubernetes/pki

for i in $(ls *.crt); do echo "====================== $i ========"; openssl x509 -in $i -text -noout | grep -A 3 'Validity' ; done

====================== apiserver.crt ========
        Validity
            Not Before: Jul 23 10:14:59 2023 GMT
            Not After : Jul 22 10:14:59 2024 GMT
        Subject: CN = kube-apiserver
====================== apiserver-etcd-client.crt ========
        Validity
            Not Before: Jul 23 10:14:59 2023 GMT
            Not After : Jul 22 10:15:00 2024 GMT
        Subject: O = system:masters, CN = kube-apiserver-etcd-client
====================== apiserver-kubelet-client.crt ========
        Validity
            Not Before: Jul 23 10:14:59 2023 GMT
            Not After : Jul 22 10:14:59 2024 GMT
        Subject: O = system:masters, CN = kube-apiserver-kubelet-client
====================== ca.crt ========
        Validity
            Not Before: Jul 23 10:14:59 2023 GMT
            Not After : Jul 20 10:14:59 2033 GMT
        Subject: CN = kubernetes
====================== front-proxy-ca.crt ========
        Validity
            Not Before: Jul 23 10:14:59 2023 GMT
            Not After : Jul 20 10:14:59 2033 GMT
        Subject: CN = front-proxy-ca
====================== front-proxy-client.crt ========
        Validity
            Not Before: Jul 23 10:14:59 2023 GMT
            Not After : Jul 22 10:14:59 2024 GMT
        Subject: CN = front-proxy-client

方法二

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                 Jul 22, 2024 10:15 UTC   364d            ca                      no      
apiserver                  Jul 22, 2024 10:14 UTC   364d            ca                      no      
apiserver-etcd-client      Jul 22, 2024 10:15 UTC   364d            etcd-ca                 no      
apiserver-kubelet-client   Jul 22, 2024 10:14 UTC   364d            ca                      no      
controller-manager.conf    Jul 22, 2024 10:15 UTC   364d            ca                      no      
etcd-healthcheck-client    Jul 22, 2024 10:15 UTC   364d            etcd-ca                 no      
etcd-peer                  Jul 22, 2024 10:15 UTC   364d            etcd-ca                 no      
etcd-server                Jul 22, 2024 10:14 UTC   364d            etcd-ca                 no      
front-proxy-client         Jul 22, 2024 10:14 UTC   364d            front-proxy-ca          no      
scheduler.conf             Jul 22, 2024 10:15 UTC   364d            ca                      no      

CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Jul 20, 2033 10:14 UTC   9y              no      
etcd-ca                 Jul 20, 2033 10:14 UTC   9y              no      
front-proxy-ca          Jul 20, 2033 10:14 UTC   9y              no  

证书有效期修改方法

安装go环境

下载

官网 https://go.dev/dl/

安装

tar xf go1.20.6.linux-amd64.tar.gz -C /usr/local/

添加环境变量

echo "export PATH=$PATH:/usr/local/go/bin" >>/etc/profile
source /etc/profile

验证

go version
go version go1.20.6 linux/amd64

Kubernetes源码下载

官方的github上下载 https://github.com/kubernetes/kubernetes/releases

查看版本

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.4", GitCommit:"fa3d7990104d7c1f16943a67f11b154b71f6a132", GitTreeState:"clean", BuildDate:"2023-07-19T12:20:54Z", GoVersion:"go1.20.6", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v5.0.1

下载

可以看到,我上面的版本是1.27.4

解压

tar xf kubernetes-1.27.4.tar.gz 
cd kubernetes-1.27.4/

修改源文件

修改 constants.go 文件

vim cmd/kubeadm/app/constants/constants.go
....
         // CertificateValidity defines the validity for all the signed certificates generated by kubeadm
        //CertificateValidity = time.Hour * 24 * 365
        // 将1年改成100年
        CertificateValidity = time.Hour * 24 * 365*100
...

修改cert.go文件

vim staging/src/k8s.io/client-go/util/cert/cert.go

.....
                //NotAfter:              now.Add(duration365d * 10).UTC(),                                                                                                                                                                                                                                                           
                NotAfter:              now.Add(duration365d * 100).UTC(),
.....

编译源代码

make WHAT=cmd/kubeadm GOFLAGS=-v

等待编译完成

编译完后查看结果,就可以看到生成的kubeadm二进文件

ll _output/bin/
总用量 46M
-rwxr-xr-x 1 root root 46M  7月 23 18:40 kubeadm

替换旧文件

更换kubeadm

备份原文件

cp /usr/bin/kubeadm /usr/bin/kubeadm.bak

替换

cp _output/bin/kubeadm /usr/bin/

备份证书

cd /etc/kubernetes
cp -R pki pki.bak

更新所有证书

kubeadm certs renew all

[renew] Reading configuration from the cluster...
[renew] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o 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 healthcheck 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
 
Done renewing certificates. You must restart the kube-apiserver, kube-controller-manager, kube-scheduler and etcd, so that they can use the new certificates.

需要更具体是重启服务,这里选择直接重启主机

查看证书

重启后查看新的证书,看结果除了ca以外的证书都变100年了,这是因为ca证书是不会被更新的,所以还是保持10年,不过对于一般的生产环境,十年绝对是够了

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                 Jun 29, 2123 08:08 UTC   99y             ca                      no      
apiserver                  Jun 29, 2123 08:08 UTC   99y             ca                      no      
apiserver-etcd-client      Jun 29, 2123 08:08 UTC   99y             etcd-ca                 no      
apiserver-kubelet-client   Jun 29, 2123 08:08 UTC   99y             ca                      no      
controller-manager.conf    Jun 29, 2123 08:08 UTC   99y             ca                      no      
etcd-healthcheck-client    Jun 29, 2123 08:08 UTC   99y             etcd-ca                 no      
etcd-peer                  Jun 29, 2123 08:08 UTC   99y             etcd-ca                 no      
etcd-server                Jun 29, 2123 08:08 UTC   99y             etcd-ca                 no      
front-proxy-client         Jun 29, 2123 08:08 UTC   99y             front-proxy-ca          no      
scheduler.conf             Jun 29, 2123 08:08 UTC   99y             ca                      no
 
CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Jun 29, 2033 07:47 UTC   9y              no
etcd-ca                 Jun 29, 2033 07:47 UTC   9y              no
front-proxy-ca          Jun 29, 2033 07:47 UTC   9y              no

如果有其他master,就直接把编译好的kubeadm文件,从第一台更新过的scp过去,然后按上面步骤,重新生成新的证书文件就可以了。更新过需要重启一下服务或者服务器。

这就是修改过程

补充

如果希望根证书也100年,重新生成ca证书太麻烦了,而且问题太多,服务会挂掉,所以不建议生产环境下更换ca证书

这边的方法是部署k8s的时候就直接让证书100年,也就不存在后面的问题了

编译源文件

同上面一样,最后编译完,获得kubeadm二进制文件

直接在安装完kubelet kubeadm kubectl 之后,就直接将编译好的文件替换掉kubeadm

cp _output/bin/kubeadm /usr/bin/

同样也要复制到其他节点上,无论master还是work

之后的操作就是正常部署就好了,而部署完了,证书就全都变成100年了

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                 Jun 29, 2123 08:08 UTC   99y             ca                      no      
apiserver                  Jun 29, 2123 08:08 UTC   99y             ca                      no      
apiserver-etcd-client      Jun 29, 2123 08:08 UTC   99y             etcd-ca                 no      
apiserver-kubelet-client   Jun 29, 2123 08:08 UTC   99y             ca                      no      
controller-manager.conf    Jun 29, 2123 08:08 UTC   99y             ca                      no      
etcd-healthcheck-client    Jun 29, 2123 08:08 UTC   99y             etcd-ca                 no      
etcd-peer                  Jun 29, 2123 08:08 UTC   99y             etcd-ca                 no      
etcd-server                Jun 29, 2123 08:08 UTC   99y             etcd-ca                 no      
front-proxy-client         Jun 29, 2123 08:08 UTC   99y             front-proxy-ca          no      
scheduler.conf             Jun 29, 2123 08:08 UTC   99y             ca                      no      

CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Jun 29, 2123 08:08 UTC   99y             no      
etcd-ca                 Jun 29, 2123 08:08 UTC   99y             no      
front-proxy-ca          Jun 29, 2123 08:08 UTC   99y             no     

标签:UTC,有效期,证书,ca,no,etcd,k8s,08,10
From: https://www.cnblogs.com/guangdelw/p/17575730.html

相关文章

  • Java生成SSL自签名证书及解析(keytool方式和源码方式)
    一:序当需要在Java应用程序中使用SSL/TLS加密通信或进行身份验证时,证书是必不可少的。证书可以用来验证服务器的身份,并确保通信的安全性。在Java开发中,可以使用JDK自带的keytool工具生成自签名证书。而本文将介绍如何使用JDK的keytool工具生成自签名证书以及相......
  • 修改k8s的数据目录
    修改kubelet工作目录1、创建kubelet的数据目录mkdir-p/data/k8s/kubelet2、根据/usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf加载/etc/sysconfig/kubelet文件vim/etc/sysconfig/kubeletKUBELET_EXTRA_ARGS="--root-dir=/data/k8s/kubelet"#命令sed......
  • SSL证书种类功能对比:企业该如何选择DV、 OV、EV
    选择一款合适的SSL证书是企业网站进行HTTPS访问的第一步。不同类型SSL证书在功能和验证要求上有所不同,企业应根据自身需求选择合适的证书。本文将介绍DV、OV、EV三种SSL证书的区别,以及企业选择SSL证书的建议。企业对SSL证书的要求要远高于个人网站,因为大多数企业网站可能存在着......
  • k8s基础之概念讲解
    目录1Kubernetes1.1简介1.2特性1.3架构1.4组件1.4.1MasterNode1.4.2WorkNode1.4.3service1.4.4Namespace1.4.5Volume1.5Pod控制器1.5.1pod1.5.2Pod控制器1Kubernetes1.1简介Kubernetes是一个全新的基于容器技术的分布式架构解决方案,是Google开源的一个容器......
  • Windows签发本地SSL证书
    Windows签发本地SSL证书按顺序使用以下命令行,遇到输入提示请根据状况输入wingetopensslopensslreq-newkeyrsa:2048-nodes-keyoutserver.key-x509-days3650-outserver.ceropensslpkcs12-export-inserver.cer-inkeyserver.key-outserver.pfx将得到的ser......
  • K8S初始化报错:CRI v1 runtime API is not implemented for endpoint \"unix:///var/r
    报错具体内容:[preflight]Somefatalerrorsoccurred:[ERRORCRI]:containerruntimeisnotrunning:output:time="2023-07-21T09:20:07Z"level=fatalmsg="validateserviceconnection:CRIv1runtimeAPIisnotimplementedforendpoint\"un......
  • 云原生第八周--k8s网络组件
    一网络通信1二层通信基于目标mac地址通信不可跨局域网通信通常通过交换机实现报文转发2三层网络通信-VlanVLAN(VirtualLocalAreaNetwork)即虚拟局域网,是将一个物理(交换机)的网络在逻辑上划分成多个广播域的通信技术,VLAN内的主机间可以直接通信,而VLAN网络外的主机需要......
  • k8s临时容器
    1.环境依赖k8s版本:v1.25+#低于1.25自行百度开启临时容器功能创建一个测试poddebug容器镜像2.创建临时容器#debugger:容器名称--copy-to:pod名称--image:临时容器的镜像--same-node:是否和被debug容器在同一节点上--share-processes:是否和被debug容......
  • k8s 学习笔记之搭建 nginx 服务测试搭建的环境
    服务部署接下来在kubernetes集群中部署一个nginx基础程序,测试集群是否正常工作。#部署nginx[root@master~]#kubectlcreatedeploymentnginx--image=nginx:1.14-alpine#暴露端口[root@master~]#kubectlexposedeploymentnginx--port=80--type=NodePort#......
  • k8s 学习笔记之集群网络插件安装
    我们在安装完集群后,通过kubectlgetnodes命令获取节点,可以看到所有节点都处于NotReady的状态,这是没有安装网络插件导致的。安装网络插件kubernetes支持多种网络插件,比如flannel、calico、canal等等,任选一种使用即可,本次选择flannel下面操作只需在master节点执行即可,插件......