kubernetes集群证书期限修改(三)

news/2024/7/3 10:48:12/文章来源:https://www.cnblogs.com/Shawnyi/p/18279137

kubernetes集群证书期限修改

1查看证书截止时间

查看证书截止时间,该命令显示 /etc/kubernetes/pki 文件夹中的客户端证书以及 kubeadm 使用的 KUBECONFIG 文件中嵌入的客户端证书的到期时间/剩余时间。
新版本命令:kubeadm certs check-expiration
旧版命令:kubeadm alpha certs check-expiration

[root@k8s-master01 ~]# 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                 May 09, 2025 17:44 UTC   312d            ca                      no      
apiserver                  May 09, 2025 17:44 UTC   312d            ca                      no      
apiserver-etcd-client      May 09, 2025 17:44 UTC   312d            etcd-ca                 no      
apiserver-kubelet-client   May 09, 2025 17:44 UTC   312d            ca                      no      
controller-manager.conf    May 09, 2025 17:44 UTC   312d            ca                      no      
etcd-healthcheck-client    May 09, 2025 17:44 UTC   312d            etcd-ca                 no      
etcd-peer                  May 09, 2025 17:44 UTC   312d            etcd-ca                 no      
etcd-server                May 09, 2025 17:44 UTC   312d            etcd-ca                 no      
front-proxy-client         May 09, 2025 17:44 UTC   312d            front-proxy-ca          no      
scheduler.conf             May 09, 2025 17:44 UTC   312d            ca                      no      CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      May 07, 2034 17:44 UTC   9y              no      
etcd-ca                 May 07, 2034 17:44 UTC   9y              no      
front-proxy-ca          May 07, 2034 17:44 UTC   9y              no

2 命令renew使用

renew:Renew certificates for a Kubernetes cluster(更新 Kubernetes 集群的证书,注:不涉及 CA 根证书)。使用的 renew 命令,来进行重新生成集群证书。
查看重新生成证书下有哪些命令:

#kubeadm certs renew -h[root@k8s-master01 ~]# kubeadm certs renew -h
This command is not meant to be run on its own. See list of available subcommands.
Usage:kubeadm certs renew [flags]kubeadm certs renew [command]
Available Commands:admin.conf               Renew the certificate embedded in the kubeconfig file for the admin to use and for kubeadm itselfall                      Renew all available certificatesapiserver                Renew the certificate for serving the Kubernetes APIapiserver-etcd-client    Renew the certificate the apiserver uses to access etcdapiserver-kubelet-client Renew the certificate for the API server to connect to kubeletcontroller-manager.conf  Renew the certificate embedded in the kubeconfig file for the controller manager to useetcd-healthcheck-client  Renew the certificate for liveness probes to healthcheck etcdetcd-peer                Renew the certificate for etcd nodes to communicate with each otheretcd-server              Renew the certificate for serving etcdfront-proxy-client       Renew the certificate for the front proxy clientscheduler.conf           Renew the certificate embedded in the kubeconfig file for the scheduler manager to use
Flags:-h, --help   help for renew
Global Flags:--add-dir-header           If true, adds the file directory to the header of the log messages--log-file string          If non-empty, use this log file (no effect when -logtostderr=true)--log-file-max-size uint   Defines the maximum size a log file can grow to (no effect when -logtostderr=true). Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)--one-output               If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)--rootfs string            [EXPERIMENTAL] The path to the 'real' host root filesystem.--skip-headers             If true, avoid header prefixes in the log messages--skip-log-headers         If true, avoid headers when opening log files (no effect when -logtostderr=true)-v, --v Level                  number for the log level verbosity
Use "kubeadm certs renew [command] --help" for more information about a command.
[root@k8s-master01 ~]# 

2.1 重要参数说明

all: 更新所有可用证书,一般可以直接使用这个更新所有。
apiserver: 单独更新服务 Kubernetes API 的证书,相当于针对单个组件进行生成。
apiserver-etcd-client: 更新 apiserver 用来访问 etcd 的证书。
其它都类似。

2.2 备份旧证书文件目录

主节点上操作,生成新的证书之前最好备份一下数据:

cp -rp /var/lib/etcd /var/lib/etcd.bak
cp -rp /etc/kubernetes /etc/kubernetes.bak

3 配置1年期限证书

以下命令是批量生成1年期的新证书,需要在所有主节点上执行。

#kubeadm certs renew all

 注意:这种自动更新所有证书后,为期也只是1年。

此时所有证书都已重新生成,在 /etc/kubernetes/ 目录下的相关文件都会被更新。

/etc/kubernetes/admin.conf
/etc/kubernetes/controller-manager.conf
/etc/kubernetes/scheduler.conf 

警告: 如果你运行了一个 HA 集群,这个命令需要在所有主节点节点上执行。
说明: certs renew 使用现有的证书作为属性 (Common Name、Organization、SAN 等) 的权威来源, 而不是 kubeadm-config ConfigMap 。强烈建议使它们保持同步。

3.1 查看1年证书

再次查看证书时间就延续了一年:

#kubeadm certs check-expiration

最后重启服务即可。

4 配置50年期限证书

下面我们里生成50年的证书,证书有效期修改方法。

4.1 部署go环境

4.1.2 解压安装包

上传并解压安装包,执行一下命令:

cd /root/
wget  https://studygolang.com/dl/golang/go1.22.4.linux-amd64.tar.gz
tar zxf go1.22.4.linux-amd64.tar.gz -C /usr/local/

4.1.3 配置环境变量

echo "export PATH=$PATH:/usr/local/go/bin" >> .bash_profile
source .bash_profile

4.1.4 验证GO环境

# go version
[root@k8s-master01 ~]# go version
go version go1.22.4 linux/amd64
[root@k8s-master01 ~]#

5 Kubernetes源码下载

首先查看一下k8s的版本,源码要下载跟你当前系统对应的版本,所以我这边下载 v1.28.2就可以。

5.1 上传并解压文件

cd /root/
unzip -q kubernetes-v1.28.2.zip

5.2 修改源代码文件

证书的有效期是需要修改两个文件 constants.go 和 cert.go。

5.3 修改配置constants.go

修改查找 CertificateValidity 字段:

cd /root/kubernetes-v1.28.2
vim ./cmd/kubeadm/app/constants/constants.go
const (// KubernetesDir is the directory Kubernetes owns for storing various configuration filesKubernetesDir = "/etc/kubernetes"// ManifestsSubDirName defines directory name to store manifestsManifestsSubDirName = "manifests"// TempDirForKubeadm defines temporary directory for kubeadm// should be joined with KubernetesDir.TempDirForKubeadm = "tmp"// CertificateBackdate defines the offset applied to notBefore for CA certificates generated by kubeadmCertificateBackdate = time.Minute * 5// CertificateValidity defines the validity for all the signed certificates generated by kubeadmCertificateValidity = time.Hour * 24 * 365 * 50// DefaultCertificateDir defines default certificate directoryDefaultCertificateDir = "pki"
----------------
#默认是1年,CertificateValidity的后面加个50就是50年。

5.4 修改配置cert.go

修改查找第二个 duration365d 字段:

cd cd /root/kubernetes-v1.28.2
vim ./staging/src/k8s.io/client-go/util/cert/cert.go
// NewSelfSignedCACert creates a CA certificate
func NewSelfSignedCACert(cfg Config, key crypto.Signer) (*x509.Certificate, error) {now := time.Now()// returns a uniform random value in [0, max-1), then add 1 to serial to make it a uniform random value in [1, max).serial, err := cryptorand.Int(cryptorand.Reader, new(big.Int).SetInt64(math.MaxInt64-1))if err != nil {return nil, err}serial = new(big.Int).Add(serial, big.NewInt(1))notBefore := now.UTC()if !cfg.NotBefore.IsZero() {notBefore = cfg.NotBefore.UTC()}tmpl := x509.Certificate{SerialNumber: serial,Subject: pkix.Name{CommonName:   cfg.CommonName,Organization: cfg.Organization,},DNSNames:              []string{cfg.CommonName},NotBefore:             notBefore,NotAfter:              now.Add(duration365d * 50).UTC(),KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,BasicConstraintsValid: true,IsCA:                  true,}certDERBytes, err := x509.CreateCertificate(cryptorand.Reader, &tmpl, &tmpl, key.Public(), key)if err != nil {return nil, err}       return x509.ParseCertificate(certDERBytes)
}       
------------------
#修改 duration365d 字段,默认是10年,修改成50。

5.5 编译源代码文件

cd /root/kubernetes-v1.28.2
make WHAT=cmd/kubeadm GOFLAGS=-v

5.5.1 编译过程

[root@k8s-master01 kubernetes-v1.28.2]# make WHAT=cmd/kubeadm GOFLAGS=-v
go version go1.20.8 linux/amd64
+++ [0702 03:08:31] Building go targets for linux/amd64k8s.io/kubernetes/cmd/kubeadm (static)
internal/goarch
encoding
internal/unsafeheader
internal/abi
internal/cpu
internal/coverage/rtcov
internal/goexperiment
internal/bytealg
internal/goos
.....
#确认编译是否有错提示,为 0 表示编译没有报错。
[root@k8s-master01 kubernetes-v1.28.2]# echo $?
0
[root@k8s-master01 kubernetes-v1.28.2]# 

5.5.2 检查编译结果

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

[root@k8s-master01 kubernetes-v1.28.2]# ls -l /root/kubernetes-v1.28.2/_output/bin/
total 49612
-rwxr-xr-x 1 root root 50802688 Jul  2 03:22 kubeadm
[root@k8s-master01 kubernetes-v1.28.2]# 

6 替换kubeadm

下面准备把新的kubeadm替换旧的,所以需要把旧的备份一下,记住有多台master都需要备份一下。

#备份
mv /usr/bin/kubeadm /usr/bin/kubeadm_20240702bak
#替换
cp /root/kubernetes-v1.28.2/_output/bin/kubeadm /usr/bin/

7 证书更新

记住如有多台master都需要执行刷新。

#kubeadm certs renew all
[root@k8s-master01 ~]# 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.
[root@k8s-master01 ~]# echo $?
0
[root@k8s-master01 ~]#

8 重启服务器节点master

#kubectl -n kube-system get cm kubeadm-config -o yaml
[root@k8s-master01 ~]# kubectl -n kube-system get cm kubeadm-config -o yaml
apiVersion: v1
data:ClusterConfiguration: |apiServer:extraArgs:authorization-mode: Node,RBACtimeoutForControlPlane: 4m0sapiVersion: kubeadm.k8s.io/v1beta3certificatesDir: /etc/kubernetes/pkiclusterName: kubernetescontrollerManager: {}dns: {}etcd:local:dataDir: /var/lib/etcdimageRepository: registry.k8s.iokind: ClusterConfigurationkubernetesVersion: v1.28.2networking:dnsDomain: cluster.localpodSubnet: 10.244.0.0/16serviceSubnet: 10.96.0.0/12scheduler: {}
kind: ConfigMap
metadata:creationTimestamp: "2024-05-09T17:44:43Z"name: kubeadm-confignamespace: kube-systemresourceVersion: "234"uid: fa84e3b1-a1f6-4e26-9b33-77c8c2218d70
[root@k8s-master01 ~]# 

重启后查看新的证书,看结果除了ca以外的证书都变50年了。

9 查看证书

[root@k8s-master01 ~]# 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 19, 2074 19:38 UTC   49y             ca                      no      
apiserver                  Jun 19, 2074 19:38 UTC   49y             ca                      no      
apiserver-etcd-client      Jun 19, 2074 19:38 UTC   49y             etcd-ca                 no      
apiserver-kubelet-client   Jun 19, 2074 19:38 UTC   49y             ca                      no      
controller-manager.conf    Jun 19, 2074 19:38 UTC   49y             ca                      no      
etcd-healthcheck-client    Jun 19, 2074 19:38 UTC   49y             etcd-ca                 no      
etcd-peer                  Jun 19, 2074 19:38 UTC   49y             etcd-ca                 no      
etcd-server                Jun 19, 2074 19:38 UTC   49y             etcd-ca                 no      
front-proxy-client         Jun 19, 2074 19:38 UTC   49y             front-proxy-ca          no      
scheduler.conf             Jun 19, 2074 19:38 UTC   49y             ca                      no      CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      May 07, 2034 17:44 UTC   9y              no      
etcd-ca                 May 07, 2034 17:44 UTC   9y              no      
front-proxy-ca          May 07, 2034 17:44 UTC   9y              no      
[root@k8s-master01 ~]# 

同理其他台的master,只需要把kubeadm文件,从第一台更新过的scp过去,然后按上面步骤,重新生成新的证书文件就可以了。记住更新过需要重启一下服务或者服务器。到此,证书期限更新完成。
 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/736109.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【github报错解决】Failed to connect to github.com port 443: Couldnt connect to server

今天使用github push时候报错的 解决办法: 1、查看本机代理 路径:设置->网络和Internet->代理->地址:端口 2、用git cmd修改配置,port就用刚刚代理端口值git config --global http.proxy http://127.0.0.1:[port] git config --global https.proxy http://127.0.0…

LVGL组件

LVGL组件的使用 目录LVGL组件的使用1 . 父和子对象2. 部件的基本属性及设置3. 图解:还有其他样式可以设置2. 组件分类 1 . 父和子对象2. 部件的基本属性及设置 /******** 设置打小 ********/ /******** 注意:设置部件位置时,坐标原点在父对象的左上角 ********/ lv_…

Maven 笔记

开发工具:Maven相关内容笔记# Maven简介 Maven的本质是一个项目管理工具,将项目开发和管理过程抽象成一个项目对象模型(POM)) 这玩意儿是使用Java开发的,所以采用的就是Java的思想:面向对象 POM (Project Object Model):项目对象模型Maven的作用:项目构建:提供标准的、…

202406月度小结

这个学期比上个学期有意思得多了。但是为什么我只写了6月小结,没有写3月4月5月小结呢?因为那时候还没有想到用博客写这种小结形式的随笔啦…… 横向对比,这四个月都挺有意思的。但是由于我太容易忘记事情了……我的记忆是有限的,先把6月发生的趣事赶紧记录下来比较好。 昨天…

大模型技术方向Task1笔记

赛题概要 一、赛事背景 在当今数字化时代,企业积累了丰富的对话数据,这些数据不仅是客户与企业之间交流的记录,更是隐藏着宝贵信息的宝库。在这个背景下,群聊对话分角色要素提取成为了企业营销和服务的一项重要策略。 群聊对话分角色要素提取的理念是基于企业对话数据的深度…

[LeetCode] 122. Best Time to Buy and Sell Stock II

medium是你的谎言. class Solution:def maxProfit(self, prices: List[int]) -> int:#1if len(prices) == 1:return 0#elsemax_profit = 0min_price = prices[0]for i, element in enumerate(prices):#find a min_price if element <= min_price:min_price = elementelse…

【esp32 学习笔记】esp-idf学会调用组件管理——以button

简单不看版——esp-idf组件管理步骤 在ESP-IDF 组件管理器网页(https://components.espressif.com/)搜索我们需要的组件,比如【button】,然后 点开相应的组件,比如 espressif/button 组件。 【关键步骤】复制相关组件界面上配置组件的命令 ,形如:idf.py add-dependency &…

idea创建javaweb项目

1.新建project项目 2.添加依赖<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.0.10</version> </dependency>…

Linguistics-English-Would, Should, and Could: How to Use Them Correctly

https://7esl.com/would-should-could/ Key Takeaways“Would” is used for hypotheticals and future possibilities that may not occur. “Should” implies advice, expectation, or probability. “Could” expresses past ability or present possibility.Home Knowled…

[LeetCode] 121. Best Time to Buy and Sell Stock

想清楚了确实算是简单题. class Solution:def maxProfit(self, prices: List[int]) -> int:#1if len(prices) == 1:return 0#elsemax_profit = 0min_price = prices[0]for i, element in enumerate(prices):if element <= min_price:min_price = elementelif element - m…

Druid数据库加密实现

Druid数据库连接池中的密码加密功能提供了以下几个主要好处: 增强安全性:最显著的好处是提高了数据库系统的安全性。明文存储数据库密码容易造成安全隐患,一旦配置文件泄露,数据库可能遭受未授权访问。通过加密数据库密码,即使配置文件落入未经授权的人员手中,他们也无法…

17-移动端适配

移动端适配01 单位的分类02 相对单位相对的对象 2.1 em 永远都是相对于自身的font-size <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, …

maven下载安装配置

Maven 是一款为 Java 项目管理构建、依赖管理的工具(软件),使用 Maven 可以自动化构建、测试、打包和发布项目,大大提高了开发效率和质量。 Maven的两大核心作用: 1.依赖管理: Maven 可以管理项目的依赖,包括自动下载所需依赖库、自动下载依赖需要的依赖并且保证版本没有…

回溯

理论基础 什么是回溯法回溯法也可以叫做回溯搜索法,它是一种搜索的方式。 在二叉树系列中,我们已经不止一次,提到了回溯,例如二叉树:以为使用了递归,其实还隐藏着回溯 (opens new window)。 回溯是递归的副产品,只要有递归就会有回溯。 所以以下讲解中,回溯函数也就是递…

day02 go的数据类型和运算符

day02 go的数据类型和运算符 前言动态语言类型 自动开辟数据空间,自动识别数据类型 python 静态语言类型 开辟内存空间,定义数据类型 c,java,gogo 语言中的数据类型 /*- go 基本数据类型:- 布尔类型- 数值类型- 整型 :- 有符号:int,int8,int16,int32,int64.- 无符号:ui…

16-视口的理解

视口的理解01 布局视口和视觉视口02 理想视口<meta name="viewport" content="width=device-width, initial-scale=1.0"> 通过width=device-width 即布局视口的宽度等于设备的宽度,以此来达到效果视口中的其它值

《痞子衡嵌入式半月刊》 第 103 期

痞子衡嵌入式半月刊: 第 103 期这里分享嵌入式领域有用有趣的项目/工具以及一些热点新闻,农历年分二十四节气,希望在每个交节之日准时发布一期。 本期刊是开源项目(GitHub: JayHeng/pzh-mcu-bi-weekly),欢迎提交 issue,投稿或推荐你知道的嵌入式那些事儿。 上期回顾 :《…

EasyDesktop 浏览器书签管理从未如此简单

作为一名软件开发从业人员, 每天80%的时间都在与浏览器打交道, 一半的时间在用浏览器开发调试, 另一半时间则是在互联网上搜寻各种知识和资源. 为此, 我的浏览器书签栏存储和很多非常棒的链接, 多到2k屏幕分辨率下一栏都放不下, 为此我只好把这些书签的名称改的短一点, 这样就能…

docker基本介绍

虚拟机的部署形式 一个物理机可以部署多个app 每个app独立运行在一个VM虚拟机里面 依然部署很慢 迁移和扩展可以使用VM的克隆 每一个虚拟机都是一个完整的操作系统,虚拟机多的时候,操作系统本身资源也就被消耗过多 企业使用的虚拟化工具为:vmware esxi虚拟化工具 个人学习使…

新生题

MISC kongfu 用winhex打开压缩包,下拉到最后发现keybase64解密后发现是栅栏密码audio 打开发现是一个wav文件,用Audacity打开根据电平,得到01100010 01100001 01101011 01100100 01101111 01110010 二进制转字符进行md5加密Smtpbase64解码foremost 用foremost分解图片分解成…