Manual Rotation of CA Certificates
Kubernetes Docs · نسخهٔ f284a9e7c87f · CC BY 4.0 برای مستندات و مجوزهای همراه
<!-- overview -->
This page shows how to manually rotate the certificate authority (CA) certificates.
{{% heading "prerequisites" %}}
{{< include "task-tutorial-prereqs.md" >}}
- For more information about authentication in Kubernetes, see
Authenticating.
- For more information about best practices for CA certificates, see
Single root CA.
<!-- steps -->
Rotate the CA certificates manually
{{< caution >}} Make sure to back up your certificate directory along with configuration files and any other necessary files.
This approach assumes operation of the Kubernetes control plane in an HA configuration with multiple API servers. Graceful termination of the API server is also assumed, so clients can cleanly disconnect from one API server and reconnect to another.
Configurations with a single API server will experience unavailability while the API server is being restarted. {{< /caution >}}
- Distribute the new CA certificates and private keys (for example:
ca.crt,ca.key,front-proxy-ca.crt,
and front-proxy-ca.key) to all your control plane nodes in the Kubernetes certificates directory.
- Update the
--root-ca-fileflag for the {{< glossary_tooltip term_id="kube-controller-manager" >}} to include
both old and new CA, then restart the kube-controller-manager.
Any {{< glossary_tooltip text="ServiceAccount" term_id="service-account" >}} created after this point will get Secrets that include both old and new CAs.
{{< note >}} The files specified by the kube-controller-manager flags --client-ca-file and --cluster-signing-cert-file cannot be CA bundles. If these flags and --root-ca-file point to the same ca.crt file, which is now a bundle (includes both old and new CA), you will face an error. To workaround this problem, you can copy the new CA to a separate file and make the flags --client-ca-file and --cluster-signing-cert-file point to the copy. Once ca.crt is no longer a bundle, you can restore the problem flags to point to ca.crt and delete the copy.
Issue 1350 for kubeadm tracks an bug with the kube-controller-manager being unable to accept a CA bundle. {{< /note >}}
- Wait for the controller manager to update
ca.crtin the service account Secrets to include both old and new CA certificates.
If any Pods are started before the new CA is used by API servers, the new Pods get this update and will trust both old and new CAs.
- Restart all pods using in-cluster configurations (for example: kube-proxy, CoreDNS, etc) so they can use the
updated certificate authority data from Secrets that link to ServiceAccounts.
- Make sure CoreDNS, kube-proxy, and other Pods using in-cluster configurations are working as expected.
- Append both old and new CA to the file against
--client-ca-fileand--kubelet-certificate-authority
flag in the kube-apiserver configuration.
- Append both old and new CA to the file against the
--client-ca-fileflag in thekube-schedulerconfiguration.
- Update certificates for user accounts by replacing the content of
client-certificate-dataandclient-key-data
respectively.
For information about creating certificates for individual user accounts, see Configure certificates for user accounts.
Additionally, update the certificate-authority-data section in the kubeconfig files, respectively with Base64-encoded old and new certificate authority data
- Update the
--root-ca-fileflag for the {{< glossary_tooltip term_id="cloud-controller-manager" >}} to include
both old and new CA, then restart the cloud-controller-manager.
{{< note >}} If your cluster does not have a cloud-controller-manager, you can skip this step. {{< /note >}}
- Follow the steps below in a rolling fashion.
- Restart any other
aggregated API servers or webhook handlers to trust the new CA certificates.
- Restart the kubelet by updating the file against
clientCAFilein the kubelet configuration and
certificate-authority-data in kubelet.conf to use both the old and new CA on all nodes.
If your kubelet is not using client certificate rotation, update client-certificate-data and client-key-data in kubelet.conf on all nodes, along with the kubelet client certificate file usually found in /var/lib/kubelet/pki.
- Restart API servers with the certificates (
apiserver.crt,apiserver-kubelet-client.crt, and
front-proxy-client.crt), signed by the new CA. You can use the existing private keys or new private keys. If you changed the private keys, then update these in the Kubernetes certificates directory as well.
Since the Pods in your cluster trust both old and new CAs, there will be a momentary disconnection after which, pods' Kubernetes clients reconnect to the new API server. The new API server uses a certificate signed by the new CA.
- Restart the {{< glossary_tooltip term_id="kube-scheduler" text="kube-scheduler" >}} to use and
trust the new CAs.
- Make sure control plane components logs no TLS errors.
{{< note >}} To generate certificates and private keys for your cluster using the openssl command line tool, see [Certificates (openssl)](/docs/tasks/administer-cluster/certificates/#openssl). You can also use [cfssl](/docs/tasks/administer-cluster/certificates/#cfssl). {{< /note >}}
- Annotate any DaemonSets and Deployments to trigger pod replacement in a safer rolling fashion.
for namespace in $(kubectl get namespace -o jsonpath='{.items[*].metadata.name}'); do
for name in $(kubectl get deployments -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
kubectl patch deployment -n ${namespace} ${name} -p '{"spec":{"template":{"metadata":{"annotations":{"ca-rotation": "1"}}}}}';
done
for name in $(kubectl get daemonset -n $namespace -o jsonpath='{.items[*].metadata.name}'); do
kubectl patch daemonset -n ${namespace} ${name} -p '{"spec":{"template":{"metadata":{"annotations":{"ca-rotation": "1"}}}}}';
done
done{{< note >}} To limit the number of concurrent disruptions that your application experiences, see configure pod disruption budget. {{< /note >}}
Depending on how you use StatefulSets, you may also need to perform a similar rolling replacement.
- If your cluster is using bootstrap tokens to join nodes, update the ConfigMap
cluster-infoin thekube-public
namespace with new CA.
base64_encoded_ca="$(base64 -w0 /etc/kubernetes/pki/ca.crt)"
kubectl get cm/cluster-info --namespace kube-public -o yaml | \
/bin/sed "s/\(certificate-authority-data:\).*/\1 ${base64_encoded_ca}/" | \
kubectl apply -f -- Verify the cluster functionality.
- Check the logs from control plane components, along with the kubelet and the kube-proxy.
Ensure those components are not reporting any TLS errors; see looking at the logs for more details.
- Validate logs from any aggregated api servers and pods using in-cluster config.
- Once the cluster functionality is successfully verified:
- Update all service account tokens to include new CA certificate only.
- All pods using an in-cluster kubeconfig will eventually need to be restarted to pick up the new Secret,
so that no Pods are relying on the old cluster CA.
- Restart the control plane components by removing the old CA from the kubeconfig files and the files against it
--client-ca-file, --root-ca-file flags resp.
- On each node, restart the kubelet by removing the old CA from the file against the
clientCAFileflag
and from the kubelet kubeconfig file. You should carry this out as a rolling update.
If your cluster lets you make this change, you can also roll it out by replacing nodes rather than reconfiguring them.