如何使用kubeadm管理证书?
admin
2023-03-30 21:01:59
0

kubeadm管理证书

在管理证书之前,你需要了解kubernetes如何使用PKI证书的相关知识:官方文档

检查证书到期时间

check-expiration 可用于检查证书过期时间:

kubeadm alpha certs check-expiration

输出如下内容;

CERTIFICATE                EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
admin.conf                 Oct 06, 2020 03:56 UTC   364d            no      
apiserver                  Oct 06, 2020 10:41 UTC   364d            no      
apiserver-etcd-client      Oct 06, 2020 03:55 UTC   364d            no      
apiserver-kubelet-client   Oct 06, 2020 03:55 UTC   364d            no      
controller-manager.conf    Oct 06, 2020 03:56 UTC   364d            no      
etcd-healthcheck-client    Oct 02, 2020 12:14 UTC   361d            no      
etcd-peer                  Oct 02, 2020 12:14 UTC   361d            no      
etcd-server                Oct 02, 2020 12:14 UTC   361d            no      
front-proxy-client         Oct 06, 2020 03:55 UTC   364d            no      
scheduler.conf             Oct 06, 2020 03:56 UTC   364d            no     

该命令显示了 所有证书的到期/剩余时间,包括在etc/kubernetes/pki目录下的客户端证书及由kubeadm嵌入到KUBECONFIG文件中的客户端证书(admin.conf,controller-manager.conf和scheduler.conf)。

注意:

  1. kubelet.conf未包含在上面的列表中,因为kubeadm将已将其配置为自动更新。
  2. kubeadm无法管理由外部CA签名的证书。

自动续订证书

自动续订指的是,在用kubeadm升级控制平面时 自动更新所有证书。

如果对证书续约没有要求,并定期升级kubernetes版本,每次升级间隔时间少于1年,最佳做法是经常升级集群以确保安全。

如果不想在升级集群时续约证书,则给 kubeadm upgrade apply 或 kubeadm upgrade node 传递参数:--certificate-renewal=false

手动续订证书

使用 kubeadm alpha certs renew 命令 可以随时手动续订证书,该命令使用存储在/etc/kubernetes/pki中的 CA (or front-proxy-CA)证书和密钥来更新证书。

如果是HA集群,则在所有控制平面执行

kubeadm alpha certs 命令详解:

Available Commands:
  certificate-key  生成证书和key
  check-expiration  检测证书过期时间
  renew            续订Kubernetes集群的证书

用的最多的续订证书的 renew子命令,现在来看下该命令帮助:

root@k8s-master:~# kubeadm  alpha certs renew -h
This command is not meant to be run on its own. See list of available subcommands.

Usage:
  kubeadm alpha certs renew [flags]
  kubeadm alpha certs renew [command]

Available Commands:
  admin.conf               Renew the certificate embedded in the kubeconfig file for the admin to use and for kubeadm itself
  all                      Renew all available certificates
  apiserver                Renew the certificate for serving the Kubernetes API
  apiserver-etcd-client    Renew the certificate the apiserver uses to access etcd
  apiserver-kubelet-client Renew the certificate for the API server to connect to kubelet
  controller-manager.conf  Renew the certificate embedded in the kubeconfig file for the controller manager to use
  etcd-healthcheck-client  Renew the certificate for liveness probes to healtcheck etcd
  etcd-peer                Renew the certificate for etcd nodes to communicate with each other
  etcd-server              Renew the certificate for serving etcd
  front-proxy-client       Renew the certificate for the front proxy client
  scheduler.conf           Renew the certificate embedded in the kubeconfig file for the scheduler manager to use

Flags:
  -h, --help   help for renew

Global Flags:
      --log-file string          If non-empty, use this log file
      --log-file-max-size uint   Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
      --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
  -v, --v Level                  number for the log level verbosity

如上所知,指定某个证书就能续订该证书,指定 all
则续订所有证书。

命令执行后,注意:

  1. 无论证书的到期时间如何,都会无条件地续订一年。
  2. 证书的SAN等信息基于原证书,无需再次提供。
  3. renew执行后,为使更改生效,需要重启控制平面组件。

kubeadm alpha certs命令仅支持v1.15及其以上的版本。

示例: 手动续订apiserver的证书-apiserver.crt

从上面检测中知道,当前 apiserver.crt 到期时间是 Oct 06, 2020 03:55 UTC ,剩余364d。

1. 执行renew更新:

root@k8s-master:~# kubeadm  alpha certs renew apiserver
certificate for serving the Kubernetes API renewed

2. 重启控制平面:

重启kubelet会自动重新创建核心组件

systemctl restart kubelet

3. 验证:

root@k8s-master:~# kubeadm alpha certs check-expiration
CERTIFICATE                EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
apiserver                  Oct 06, 2020 10:41 UTC   364d            no  

apiserver证书到期时间发生了变化, 不过不是顺延一年, 而是 从你 执行renew成功的时间开始续签一年。

如果要将所有证书续签一年,则执行:

kubeadm  alpha certs renew all

使用外部CA续订证书

通过外部CA签发证书,需要kubeadm 生成一个CSR提交给CA。

1. 生成CSR和私钥:

kubeadm alpha certs renew apiserver --csr-only  --csr-dir /tmp/apiserver.csr
  • --csr-only:仅生成CSR。
  • --csr-dir:生成的CSR和私钥文件保存在哪里,默认保存在/etc/kubernetes/pki

2. 查看CSR和私钥:

命令输出结果中提供了CSR和私钥。

root@k8s-master:~# ls /tmp/apiserver.csr/
apiserver.csr  apiserver.key

3. 使用该私钥到CA上请求签发证书。

将颁发的证书及私钥复制到PKI目录/etc/kubernetes/pki中。

相关内容

热门资讯

河南760亿省级财政资金定存招... 【大河财立方消息】5月11日消息,河南省财政厅公布2026年度第1期河南省省级财政专户资金定期存款代...
无忧传媒宣布与“孕妇泰国坠崖案... 据北京商报消息,5月11日,记者从无忧传媒方面获悉,目前公司与签约达人王暖暖经友好协商已确定解约。5...
米哈游:编造“皮套论”谣言构成... 【大河财立方消息】 5月11日,据米哈游法务部,近日米哈游诉网络博主许某鹏(网名“自由人米八”)、罗...
外交部介绍特朗普访华具体安排和... 5月11日,外交部发言人郭嘉昆主持例行记者会。总台央视记者提问:中方已经发布美国总统特朗普来华进行国...
61家!郑州市科技类校外培训机... 根据《河南省科技类校外培训机构设置标准和管理指南(试行)》有关规定,为落实“双减”政策要求,经科技类...
夫妻酒后各自驾车,路上相撞,双... 5月10日晚,四川夹江县公安局发布消息,该局交通管理大队于近日处理一起追尾事故时,查获一对夫妻双双酒...
顾客称用餐时石锅上爬满虫,餐厅... 近日,有网友反映,在福建省晋江市池店镇的“钱头大自然餐厅”用餐时,顾客吃到一半竟发现石锅外壁上爬满了...
公职人员纠集他人私闯民宅殴打重... 5月10日,中共安康高新区空港新城委员会发布情况说明,内容如下:5月10日,网络出现反映“安康高新区...
中美联合侦破跨国走私贩毒案,抓... 4月初,中国公安部禁毒局和美国司法部缉毒署成功联合侦破郭某等人走私贩毒案,同步在中国辽宁、广东,美国...
多名网友收到广东地震局短信,官... 5月11日上午,多名广东网友发帖称,自己收到了广东地震局的短信,短信内容如下:【广东省地震局】温馨提...