温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用kubeadm管理证书?

发布时间:2020-06-20 16:31:58 来源:网络 阅读:8851 作者:Flywithmeto 栏目:云计算

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中。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI