首页 > 其他分享 >更换K8S证书可用期

更换K8S证书可用期

时间:2022-11-02 09:58:59浏览次数:76  
标签:K8S kubernetes 证书 app cmd etcd kubeadm k8s 更换

帮助文档:https://zealous-cricket-cfa.notion.site/kubeadm-k8s-24611be9607c4b3193012de58860535e

解决:

1.安装GO语言环境:

[root@k8s-master software]# wget https://studygolang.com/dl/golang/go1.19.1.linux-amd64.tar.gz
[root@k8s-master software]# tar xf go1.19.1.linux-amd64.tar.gz -C /usr/local
[root@k8s-master software]# vim /etc/profile   # 最后面添加如下信息
# go语言环境变量
export PATH=$PATH:/usr/local/go/bin
[root@k8s-master software]# source /etc/profile

2.Kubernetes源码下载与更改证书策略:(保证跟当前版本一样,我这里是1.23.1,可以先用kubectl version查看当前版本)

wget https://github.com/kubernetes/kubernetes/archive/refs/tags/v1.23.1.zip (修改版本号直接下载对应版本即可,tag里可能没有,但不影响下载)
mkdir k8s
unzip v1.23.1.zip -d /root/k8s
cd /root/k8s/kubernetes-1.23.1/
cd cmd/kubeadm/app/util/pkiutil
#更改配置文件并备份:
cp pki_helpers.go pki_helpers.go.bak
vim pki_helpers.go
#在636行左右
func NewSignedCert(cfg *CertConfig, key crypto.Signer, caCert *x509.Certificate, caKey crypto.Signer, isCA bool) (*x509.Certificate, error) {
     const effectyear = time.Hour * 24 * 365 * 50 #添加此行,我这里是改成50年
     serial, err := cryptorand.Int(cryptorand.Reader, new(big.Int).SetInt64(math.MaxInt64))
     if err != nil {
             return nil, err
     }
     if len(cfg.CommonName) == 0 {
             return nil, errors.New("must specify a CommonName")
     }

     keyUsage := x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature
     if isCA {
             keyUsage |= x509.KeyUsageCertSign
     }

     RemoveDuplicateAltNames(&cfg.AltNames)

//      notAfter := time.Now().Add(kubeadmconstants.CertificateValidity).UTC()#在go语言中,变量如果没引用会报错,所以需要注释此变量及下面的判断
//      if cfg.NotAfter != nil {
//              notAfter = *cfg.NotAfter
//      }

     certTmpl := x509.Certificate{
             Subject: pkix.Name{
                     CommonName:   cfg.CommonName,
                     Organization: cfg.Organization,
             },
             DNSNames:              cfg.AltNames.DNSNames,
             IPAddresses:           cfg.AltNames.IPs,
             SerialNumber:          serial,
             NotBefore:             caCert.NotBefore,
           //  NotAfter:              notAfter,  #注释此行
             NotAfter:              time.Now().Add(effectyear).UTC(),#添加此行
             KeyUsage:              keyUsage,
             ExtKeyUsage:           cfg.Usages,


# 注意路径,开始编译
root@master01:~/k8s/kubernetes-1.23.1/cmd/kubeadm/app/util/pkiutil# cd /root/k8s/kubernetes-1.23.1/
root@master01:~/k8s/kubernetes-1.23.1# make WHAT=cmd/kubeadm GOFLAGS=-v
+++ [0914 11:30:04] Building go targets for linux/amd64:
 cmd/kubeadm
> static build CGO_ENABLED=0: k8s.io/kubernetes/cmd/kubeadm
k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil
k8s.io/kubernetes/cmd/kubeadm/app/phases/certs
k8s.io/kubernetes/cmd/kubeadm/app/util/staticpod
k8s.io/kubernetes/cmd/kubeadm/app/phases/kubeconfig
k8s.io/kubernetes/cmd/kubeadm/app/phases/certs/renewal
k8s.io/kubernetes/cmd/kubeadm/app/phases/controlplane
k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/init
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/join
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/reset
k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade
k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/upgrade/node
k8s.io/kubernetes/cmd/kubeadm/app/cmd/upgrade
k8s.io/kubernetes/cmd/kubeadm/app/cmd
k8s.io/kubernetes/cmd/kubeadm/app
k8s.io/kubernetes/cmd/kubeadm



#备份之前的证书
cp -r /etc/kubernetes/pki /etc/kubernetes/pki.old

#备份之前的kubeadm
mv /usr/bin/kubeadm /usr/bin/kubeadm.old

#把编译过的kubeadm拷贝过来
root@master01:/etc/kubernetes/pki# cd /root/k8s/kubernetes-1.23.1/
root@master01:~/k8s/kubernetes-1.23.1# cp _output/bin/kubeadm /usr/bin/
#添加执行权限
root@master01:~/k8s/kubernetes-1.23.1# chmod 755 /usr/bin/kubeadm

#kubeadm的版本不一样,编译的命令也不一样,参考链接:https://www.cnblogs.com/sysin/p/15675772.html
root@master01:~/k8s/kubernetes-1.23.1# 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'
W0914 12:18:40.867177  165896 utils.go:69] The recommended value for "resolvConf" in "KubeletConfiguration" is: /run/systemd/resolve/resolv.conf; the provided value is: /run/systemd/resolve/resolv.conf

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.
   
#重启master组件容器并验证(这步可以不执行,生产环境不要执行!会重启服务。)
root@master01:~/k8s/kubernetes-1.23.1# docker ps |grep -E 'k8s_kube-apiserver|k8s_kube-controller-manager|k8s_kube-scheduler|k8s_etcd_etcd' | awk -F ' ' '{print $1}' |xargs docker restart
019ecea9f270
0bde6fe9ea90
3cd6f8f17ae6
58abb3209def
root@master01:~/k8s/kubernetes-1.23.1# 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'
W0914 13:10:19.423400  182208 utils.go:69] The recommended value for "resolvConf" in "KubeletConfiguration" is: /run/systemd/resolve/resolv.conf; the provided value is: /run/systemd/resolve/resolv.conf

CERTIFICATE                EXPIRES                  RESIDUAL TIME   CERTIFICATE AUTHORITY   EXTERNALLY MANAGED
admin.conf                 Sep 01, 2072 04:18 UTC   49y             ca                      no      
apiserver                  Sep 01, 2072 04:18 UTC   49y             ca                      no      
apiserver-etcd-client      Sep 01, 2072 04:18 UTC   49y             etcd-ca                 no      
apiserver-kubelet-client   Sep 01, 2072 04:18 UTC   49y             ca                      no      
controller-manager.conf    Sep 01, 2072 04:18 UTC   49y             ca                      no      
etcd-healthcheck-client    Sep 01, 2072 04:18 UTC   49y             etcd-ca                 no      
etcd-peer                  Sep 01, 2072 04:18 UTC   49y             etcd-ca                 no      
etcd-server                Sep 01, 2072 04:18 UTC   49y             etcd-ca                 no      
front-proxy-client         Sep 01, 2072 04:18 UTC   49y             front-proxy-ca          no      
scheduler.conf             Sep 01, 2072 04:18 UTC   49y             ca                      no      

CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Sep 11, 2032 02:07 UTC   9y              no      
etcd-ca                 Sep 11, 2032 02:07 UTC   9y              no      
front-proxy-ca          Sep 11, 2032 02:07 UTC   9y              no      

标签:K8S,kubernetes,证书,app,cmd,etcd,kubeadm,k8s,更换
From: https://www.cnblogs.com/anslinux/p/16850002.html

相关文章

  • 记一次node节点无法加入K8S集群
    #问题现象:root@small-virtual-machine:~#kubeadmjoin10.0.0.133:6443--tokend2hyl5.5qt5fzjsdbxm2k5o   --discovery-token-ca-cert-hashsha256:d02674df33b......
  • 一个ssl证书到期引起的事故
    今天早上起来打开公众号,发现页面打不开了。到腾讯云控制台一看,发现一个ssl证书过期。急忙申请新的免费证书,部署到nginx,但页面上显示证书还是原来的到期时间。问题找了好久......
  • 系统架构与设计(5)- Kubernetes(K8s)
    Kubernetes,也被称为K8s或Kube,是谷歌推出的业界最受欢迎的容器管理/运维工具(容器编排器)。它是一套自动化容器管理/运维的开源平台,包括部署、调度和节点集群的扩展等。使......
  • ubuntu20.04 更换清华源报错
    GVfsmetadataisnotsupported.FallbacktoTeplMetadataManager.EitherGVfsisnotcorrectlyinstalledorGVfsmetadataarenotsupportedonthisplatform.In......
  • Jumpserver添加K8S集群管理
    为加强内部运维权限管控,现在内网搭建jumpserver用于权限管理,操作记录追踪,现将K8S管理纳入堡垒机。步骤如下:1.添加K8S集群管理权限的SA,并且绑定cluster-admin角色2.获取tok......
  • Docker不香吗?为什么还要用k8s
    其实Docker和k8s并非直接的竞争对手,它俩相互依存。Docker是一个容器化平台,而k8s是Docker等容器平台的协调器。虚拟化技术已经走过了三个时代,没有容器化技术的演......
  • k8s.HPA.使用自定义指标Pod扩容
    k8s.HPA.使用自定义指标Pod扩容环境env:kubernetesv1.22metrics-server0.6.1prometheusv2.36.1prometheus-adapter.v0.10.0概述弹性伸缩1、资源层弹性,如......
  • k8s排空节点升级
    场景:指定节点node0030升级内存(8G-->16G)升级流程1.排空节点并停止调度kubectldrainnode0030--force由于节点中存在使用localpv的pod以及由DaemonSet控制的pod,无法直接排空......
  • Kubeadm部署k8s单点master
    Kubeadm部署k8s单点master1、环境准备:主机名IP说明宿主机系统master10.0.0.17Kubernetes集群的master节点CentOS7.9node110.0.0.27Kubernetes集群......
  • 最全XCode 开发证书配置(证书过期后的恢复)教程
    3月底的时候,准备更新苹果版,却发现,几个月没有开发,XCode上证书都过期了。怎么弄全忘了,简直没有勇气面对。过了一个多月,今天终于有点空闲时间,打算更新下证书,又发现,苹果开发......