简体   繁体   English

轮换 Kubernetes 证书的最佳做法是什么

[英]What is the best practice to rotate Kubernetes certificates

Currently I am using a script to renew Kubernetes certificates before they expire.目前,我正在使用脚本在 Kubernetes 证书到期之前对其进行续订。 But this is a manual process.但这是一个手动过程。 I have to monitor expiration dates carefully and run this script beforehand.我必须仔细监控到期日期并事先运行此脚本。 What's the recommended way to update all control plane certificates automatically without updating control plane?在不更新控制平面的情况下自动更新所有控制平面证书的推荐方法是什么? Do kubelet's --rotate* flags rotate all components (eg controller) or it is just for kubelet? kubelet 的 --rotate* 标志是旋转所有组件(例如控制器)还是仅用于 kubelet? PS: Kubernetes cluster was created with kubeadm. PS:Kubernetes集群是用kubeadm创建的。

As of kubernetes 1.8 certificate rotation is added.从 kubernetes 开始,添加了 1.8 证书轮换。 You can read about it here, https://kubernetes.io/docs/tasks/tls/certificate-rotation/你可以在这里阅读它, https://kubernetes.io/docs/tasks/tls/certificate-rotation/

Answering following question:回答以下问题:

What's the recommended way to update all control plane certificates automatically without updating control plane在不更新控制平面的情况下自动更新所有控制平面证书的推荐方法是什么

According to the k8s docs and best practices the best practice is to use "Automatic certificate renewal" with control plane upgrade:根据 k8s 文档和最佳实践,最佳实践是在控制平面升级中使用“自动证书更新”:

Automatic certificate renewal自动证书更新

This feature is designed for addressing the simplest use cases;此功能旨在解决最简单的用例; if you don't have specific requirements on certificate renewal and perform Kubernetes version upgrades regularly (less than 1 year in between each upgrade), kubeadm will take care of keeping your cluster up to date and reasonably secure.如果您对证书续订没有特定要求并定期执行 Kubernetes 版本升级(每次升级之间间隔不到 1 年),kubeadm 将负责使您的集群保持最新并合理安全。

Note: It is a best practice to upgrade your cluster frequently in order to stay secure.注意:最佳实践是经常升级集群以保持安全。

-- Kubernetes.io: Administer cluster: Kubeadm certs: Automatic certificate renewal -- Kubernetes.io:管理集群:Kubeadm 证书:自动证书更新

Why this is the recommended way:为什么这是推荐的方式:

From the best practices standpoint you should be upgrading your control-plane to patch vulnerabilities, add features and use the version that is currently supported.从最佳实践的角度来看,您应该升级您的control-plane以修补漏洞、添加功能并使用当前支持的版本。

Each control-plane upgrade will renew the certificates as described (defaults to true ):每次control-plane升级都将按照描述更新证书(默认为true ):

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

You can also check the expiration of the control-plane certificates by running:您还可以通过运行以下命令检查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  

A side note!旁注!

kubelet.conf is not included in the list above because kubeadm configures kubelet for automatic certificate renewal. kubelet.conf未包含在上面的列表中,因为kubeadmkubelet配置为自动更新证书。

From what it can be seen by default:从默认情况下可以看出:

  • Client certificates generated by kubeadm expire after 1 year. kubeadm生成的客户端证书在 1 年后过期。
  • CA created by kubeadm are set to expire after 10 years. kubeadm创建的 CA 设置为 10 年后到期。

There are other features that allows you to rotate the certificates in a "semi automatic" way.还有其他功能允许您以“半自动”方式轮换证书。

You can opt for a manual certificate renewal with the:您可以通过以下方式选择手动证书续订:

  • $ kubeadm certs renew

where you can automatically (with the command) renew the specified (or all) certificates:您可以在其中自动(使用命令)更新指定(或所有)证书:

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

Please take a specific look on the output:请具体看一下 output:

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

As pointed, you will need to restart the components of your control-plane to use new certificate but remember:正如所指出的,您需要重新启动control-plane的组件才能使用新证书,但请记住:

  • $ kubectl delete pod -n kube-system kube-scheduler-ubuntu will not work . $ kubectl delete pod -n kube-system kube-scheduler-ubuntu将不起作用

You will need to restart the docker container responsible for the component:您将需要重新启动负责该组件的 docker 容器:

  • $ docker ps | grep -i "scheduler"
  • $ docker restart 8c361562701b (example) $ 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

As pointed in below link, kubelet can automatically renew it's certificate ( kubeadm configures the cluster in a way that this option is enabled):正如下面链接中所指出的, kubelet可以自动更新它的证书( kubeadm以启用此选项的方式配置集群):

Depending on the version used in your environment, this can be disabled.根据您的环境中使用的版本,可以禁用此功能。 Currently in the newest version of k8s managed by kubeadm this option is enabled by default according to my knowledge.目前在 kubeadm 管理的最新版本的kubeadm中,据我所知,此选项默认启用。


Please keep in mind that before you start with any kubernetes node/control plane/update/upgrade to read "Urgent Upgrade Notes" specific to your k8s version (example):请记住,在开始使用任何 kubernetes 节点/控制平面/更新/升级之前,请阅读特定于您的 k8s 版本的“紧急升级说明” (示例):


Defining the automatic way of certificate rotation could go in either way but you can use already mentioned commands to automate this process.定义证书轮换的自动方式可以 go 以任何一种方式,但您可以使用已经提到的命令来自动化这个过程。 You would need to create a script (which you already have) that would be put in cron that would fire after some time and renew them.您需要创建一个脚本(您已经拥有),该脚本将放入 cron 中,一段时间后将触发并更新它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM