k8s证书到期实际操作

文章目录

  • [1. 百年的kubeadm](#1. 百年的kubeadm)
    • [1.1 源码修改](#1.1 源码修改)
    • [1.2 准备镜像](#1.2 准备镜像)
    • [1.3 编译](#1.3 编译)
    • [1.4 成品备份](#1.4 成品备份)
  • [2. 生成证书](#2. 生成证书)
    • [2.1 master主节点](#2.1 master主节点)
    • [2.2 其他master节点](#2.2 其他master节点)

1. 百年的kubeadm

1.1 源码修改

  • 源码下载
shell 复制代码
cd /usr/local/src/
git clone -b release-1.15 https://github.com/kubernetes/kubernetes.git
  • 再次确认分支
shell 复制代码
cd  kubernetes
git branch -a
  • 修改cert.go
shell 复制代码
vim ./staging/src/k8s.io/client-go/util/cert/cert.go
                NotBefore:             now.UTC(),
                #修改下边的10为100
                NotAfter:              now.Add(duration365d * 10).UTC(),
                KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
                BasicConstraintsValid: true,
                IsCA:  
  • 修改constants.go
shell 复制代码
vim ./cmd/kubeadm/app/constants/constants.go
        // CertificateValidity defines the validity for all the signed certificates generated by kubeadm
        #将下边time.Hour * 100
        CertificateValidity = time.Hour * 24 * 365

修改后如下:

shell 复制代码
vim ./cmd/kubeadm/app/constants/constants.go
        // CertificateValidity defines the validity for all the signed certificates generated by kubeadm
        CertificateValidity = time.Hour * 24 * 365 * 100

1.2 准备镜像

shell 复制代码
docker pull mirrorgooglecontainers/kube-cross:v1.12.10-1

官方提供的编译镜像,无论如何弄到服务器上就行了

1.3 编译

shell 复制代码
docker run --rm -it -v /usr/local/src/k8s.io/kubernetes:/go/src/k8s.io/kubernetes \
mirrorgooglecontainers/kube-cross:v1.12.10-1 bash

cd /go/src/k8s.io/kubernetes
make all WHAT=cmd/kubeadm GOFLAGS=-v
exit

1.4 成品备份

  • 成品位置

    编译好的成品在: _output/local/bin/linux/amd64/kubeadm

  • 成品测试

shell 复制代码
cp _output/local/bin/linux/amd64/kubeadm /usr/bin/
chmod a+x /usr/bin/kubeadm
kubeadm version

2. 生成证书

2.1 master主节点

  • 将前边准备的kubeadm 拷贝到服务器
shell 复制代码
cd /usr/local/src
wget  ftp://10.252.97.213/soft/kubeadm
  • 替换原来kubeadm文件
shell 复制代码
cp /usr/bin/kubeadm /usr/bin/kubeadm_back
cp /usr/local/src/kubeadm /usr/bin/kubeadm
chmod 755 /usr/bin/kubeadm
  • 备份配置文件和证书
shell 复制代码
cp -ra /etc/kubernetes /etc/kubernetes_back
  • 查看证书到期时间
shell 复制代码
kubeadm alpha certs check-expiration
  • 更新证书和配置文件
shell 复制代码
 kubeadm alpha certs renew all
  • 确认证书到期时间
shell 复制代码
[root@AiK8sM2 ~]# kubeadm alpha certs check-expiration
CERTIFICATE                EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
admin.conf                 Apr 03, 2120 06:46 UTC   99y             no
apiserver                  Apr 03, 2120 09:36 UTC   99y             no
apiserver-etcd-client      Apr 03, 2120 09:36 UTC   99y             no
apiserver-kubelet-client   Apr 03, 2120 09:36 UTC   99y             no
controller-manager.conf    Apr 03, 2120 09:36 UTC   99y             no
etcd-healthcheck-client    Apr 03, 2120 09:36 UTC   99y             no
etcd-peer                  Apr 03, 2120 09:36 UTC   99y             no
etcd-server                Apr 03, 2120 09:36 UTC   99y             no
front-proxy-client         Apr 03, 2120 06:47 UTC   99y             no
scheduler.conf             Apr 03, 2120 09:36 UTC   99y             no
  • 重启docker

说明:主要为了重启proxy和etcd

shell 复制代码
service docker restart
  • 重启kubelet
shell 复制代码
service kubelet restart

2.2 其他master节点

  • 拷贝100年的kubeadmin 文件
  • 将前边准备的kubeadm 拷贝到服务器
shell 复制代码
cd /usr/local/src
wget  ftp://10.252.97.213/soft/kubeadm
  • 替换原来kubeadm文件
shell 复制代码
cp /usr/bin/kubeadm /usr/bin/kubeadm_back
cp /usr/local/src/kubeadm /usr/bin/kubeadm
chmod 755 /usr/bin/kubeadm
  • 备份配置文件和证书
shell 复制代码
cp -ra /etc/kubernetes /etc/kubernetes_back
  • 查看证书到期时间
shell 复制代码
kubeadm alpha certs check-expiration
  • 更新证书和配置文件
shell 复制代码
 kubeadm alpha certs renew all
  • 确认证书到期时间
shell 复制代码
[root@AiK8sM2 ~]# kubeadm alpha certs check-expiration
CERTIFICATE                EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
admin.conf                 Apr 03, 2120 06:46 UTC   99y             no
apiserver                  Apr 03, 2120 09:36 UTC   99y             no
apiserver-etcd-client      Apr 03, 2120 09:36 UTC   99y             no
apiserver-kubelet-client   Apr 03, 2120 09:36 UTC   99y             no
controller-manager.conf    Apr 03, 2120 09:36 UTC   99y             no
etcd-healthcheck-client    Apr 03, 2120 09:36 UTC   99y             no
etcd-peer                  Apr 03, 2120 09:36 UTC   99y             no
etcd-server                Apr 03, 2120 09:36 UTC   99y             no
front-proxy-client         Apr 03, 2120 06:47 UTC   99y             no
scheduler.conf             Apr 03, 2120 09:36 UTC   99y             no
  • 从master-01 上拷贝证书文件
    从master01 上拷贝证书
shell 复制代码
ssh 10.251.137.187 "mkdir -p /etc/kubernetes/pki/etcd"
ssh 10.251.137.188 "mkdir -p /etc/kubernetes/pki/etcd"

scp -r /etc/kubernetes/admin.conf 10.251.137.187:/etc/kubernetes/admin.conf
scp -r /etc/kubernetes/admin.conf 10.251.137.188:/etc/kubernetes/admin.conf

scp -r /etc/kubernetes/pki/{ca.*,sa.*,front*}  10.251.137.187:/etc/kubernetes/pki/
scp -r /etc/kubernetes/pki/{ca.*,sa.*,front*}  10.251.137.188:/etc/kubernetes/pki/

scp -r /etc/kubernetes/pki/etcd/ca.*  10.251.137.187:/etc/kubernetes/pki/etcd/ 
scp -r /etc/kubernetes/pki/etcd/ca.*  10.251.137.188:/etc/kubernetes/pki/etcd/
  • 重启docker

说明:主要为了重启proxy和etcd

shell 复制代码
service docker restart
  • 重启kubelet
shell 复制代码
service kubelet restart
相关推荐
heimeiyingwang14 小时前
【架构实战】可观测性三支柱:日志、指标、链路的融合
elasticsearch·架构·kubernetes
CodexDave20 小时前
数据库连接池耗尽:排查顺序与三层兜底
服务器·前端·数据库·git·云原生·容器·kubernetes
名字还没想好☜1 天前
Kubernetes Ingress 实战:域名路由、TLS 证书与 502/404 排查
运维·云原生·容器·kubernetes·ingress
Zhu7581 天前
在k8s部署alist 3.62.0
云原生·容器·kubernetes
XUHUOJUN2 天前
AKS 不是安装在 Windows Server 上,而是运行在 Windows Server 之上的 Azure 平台能力
windows·架构·k8s·azure local·azure stack
Geek-Chow2 天前
Connecting kubectl to a Private EKS Cluster Over an Internal Domain
kubernetes·k8s·aws
人间凡尔赛2 天前
Post-Kubernetes 时代:2026 云原生架构从 Cloud Native 到 AI Native 的范式跃迁
云原生·架构·kubernetes
运维大师2 天前
【K8S 运维实战】03-网络模型实战CNI
运维·网络·kubernetes
运维大师2 天前
【K8S 运维实战】04-存储体系梳理CSI
运维·容器·kubernetes
heimeiyingwang2 天前
【架构实战】GitOps实践:Kubernetes上的声明式交付
elasticsearch·架构·kubernetes