【问题标题】:What is the best practice to rotate Kubernetes certificates轮换 Kubernetes 证书的最佳实践是什么
【发布时间】:2021-08-11 01:49:19
【问题描述】:

目前我正在使用脚本在 Kubernetes 证书过期之前更新它们。但这是一个手动过程。我必须仔细监控到期日期并事先运行此脚本。在不更新控制平面的情况下自动更新所有控制平面证书的推荐方法是什么? kubelet 的 --rotate* 标志是旋转所有组件(例如控制器)还是仅用于 kubelet? PS:Kubernetes集群是用kubeadm创建的。

【问题讨论】:

  • 嗨,正如docs 所说,这是kubelet 特有的。您是否还专门询问kubelet 或其他组件?从最佳实践的角度来看,您是否看过 automatic certificate renewal 上的文档?
  • 这个页面是关于使用 kubeadm 更新的,它仍然是手动的,不是吗?我的理解 --certificate-renewal 是 kubeadm 的一个功能,当您进行控制平面升级时,它也会升级证书。但我的问题更多是关于如何在不升级控制平面的情况下自动更新它。
  • 解决方案可能在很大程度上取决于您的设置。您能否详细介绍一下您的 Kubernetes 集群?它是如何创建的?你用过kubeadmkubespray这样的工具吗?
  • 它是用 kubeadm 创建的。没有托管服务。

标签: kubernetes certificate kubelet


【解决方案1】:

回答以下问题:

在不更新控制平面的情况下自动更新所有控制平面证书的推荐方法是什么

根据 k8s 文档和最佳实践,最佳实践是在控制平面升级中使用“自动证书更新”:

自动更新证书

此功能旨在解决最简单的用例;如果您对证书续订没有特定要求并定期执行 Kubernetes 版本升级(每次升级之间的时间少于 1 年),kubeadm 将负责使您的集群保持最新并相当安全。

注意:最好经常升级集群以确保安全。

-- Kubernetes.io: Administer cluster: Kubeadm certs: Automatic certificate renewal

为什么这是推荐的方式:

从最佳实践的角度来看,您应该升级您的 control-plane 以修补漏洞、添加功能并使用当前支持的版本。

每次control-plane 升级都会按照说明更新证书(默认为true):

  • $ kubeadm upgrade apply --help
--certificate-renewal    Perform the renewal of certificates used by component changed during upgrades. (default true)

您还可以通过运行检查control-plane 证书的到期情况:

  • $ 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 30, 2022 13:36 UTC   364d                                    no      
apiserver                  May 30, 2022 13:36 UTC   364d            ca                      no      
apiserver-etcd-client      May 30, 2022 13:36 UTC   364d            etcd-ca                 no      
apiserver-kubelet-client   May 30, 2022 13:36 UTC   364d            ca                      no      
controller-manager.conf    May 30, 2022 13:36 UTC   364d                                    no      
etcd-healthcheck-client    May 30, 2022 13:36 UTC   364d            etcd-ca                 no      
etcd-peer                  May 30, 2022 13:36 UTC   364d            etcd-ca                 no      
etcd-server                May 30, 2022 13:36 UTC   364d            etcd-ca                 no      
front-proxy-client         May 30, 2022 13:36 UTC   364d            front-proxy-ca          no      
scheduler.conf             May 30, 2022 13:36 UTC   364d                                    no      
 
CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      May 28, 2031 13:36 UTC   9y              no      
etcd-ca                 May 28, 2031 13:36 UTC   9y              no      
front-proxy-ca          May 28, 2031 13:36 UTC   9y              no  

附注!

kubelet.conf 未包含在上述列表中,因为kubeadmkubelet 配置为自动更新证书。

从默认情况下可以看到:

  • kubeadm 生成的客户端证书在 1 年后过期。
  • kubeadm 创建的 CA 将在 10 年后到期。

还有其他功能允许您以“半自动”方式轮换证书。

您可以选择手动更新证书:

  • $ kubeadm certs renew

您可以自动(使用命令)更新指定(或所有)证书:

  • $ 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.

请具体看一下输出:

You must restart the kube-apiserver, kube-controller-manager, kube-scheduler and etcd, so that they can use the new certificates.

如前所述,您需要重新启动 control-plane 的组件才能使用新证书,但请记住:

  • $ kubectl delete pod -n kube-system kube-scheduler-ubuntu 不起作用

您将需要重新启动负责该组件的 docker 容器:

  • $ docker ps | grep -i "scheduler"
  • $ docker restart 8c361562701b(示例)
8c361562701b   38f903b54010             "kube-scheduler --au…"   11 minutes ago      Up 11 minutes                k8s_kube-scheduler_kube-scheduler-ubuntu_kube-system_dbb97c1c9c802fa7cf2ad7d07938bae9_5
b709e8fb5e6c   k8s.gcr.io/pause:3.4.1   "/pause"                 About an hour ago   Up About an hour             k8s_POD_kube-scheduler-ubuntu_kube-system_dbb97c1c9c802fa7cf2ad7d07938bae9_0

如下链接所示,kubelet 可以自动更新其证书(kubeadm 以启用此选项的方式配置集群):

根据您的环境中使用的版本,可以禁用此功能。目前在kubeadm管理的最新版k8s中,据我所知,这个选项是默认开启的。


请记住,在开始任何 kubernetes 节点/控制平面/更新/升级之前,请阅读特定于您的 k8s 版本的“紧急升级说明”(示例):


定义证书轮换的自动方式可以采用任何一种方式,但您可以使用已经提到的命令来自动执行此过程。您需要创建一个脚本(您已经拥有),该脚本将被放入 cron 中,一段时间后会触发并更新它们。

【讨论】:

    【解决方案2】:

    从 kubernetes 1.8 开始,添加了证书轮换。你可以在这里阅读它,https://kubernetes.io/docs/tasks/tls/certificate-rotation/

    【讨论】:

    • 这只是 kubelet。不包括整个控制平面。
    猜你喜欢
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    相关资源
    最近更新 更多