Skip to content

Commit 56bd8bd

Browse files
authored
Merge pull request #252758 from schaffererin/aks-certificate-rotation
Freshness pass: Certificate rotation in AKS
2 parents a0079e1 + 0d8ce67 commit 56bd8bd

File tree

1 file changed

+88
-75
lines changed

1 file changed

+88
-75
lines changed

articles/aks/certificate-rotation.md

Lines changed: 88 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,141 +8,154 @@ ms.date: 01/19/2023
88

99
# Certificate rotation in Azure Kubernetes Service (AKS)
1010

11-
Azure Kubernetes Service (AKS) uses certificates for authentication with many of its components. If you have a RBAC-enabled cluster built after March 2022, it's enabled with certificate auto-rotation. Periodically, you may need to rotate those certificates for security or policy reasons. For example, you may have a policy to rotate all your certificates every 90 days.
11+
Azure Kubernetes Service (AKS) uses certificates for authentication with many of its components. RBAC-enabled clusters created after March 2022 are enabled with certificate auto-rotation. You may need to periodically rotate those certificates for security or policy reasons. For example, you may have a policy to rotate all your certificates every 90 days.
1212

1313
> [!NOTE]
14-
> Certificate auto-rotation will *only* be enabled by default for RBAC enabled AKS clusters.
14+
> Certificate auto-rotation is *only* enabled by default for RBAC enabled AKS clusters.
1515
1616
This article shows you how certificate rotation works in your AKS cluster.
1717

1818
## Before you begin
1919

20-
This article requires that you are running the Azure CLI version 2.0.77 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][azure-cli-install].
20+
This article requires the Azure CLI version 2.0.77 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][azure-cli-install].
2121

2222
## AKS certificates, Certificate Authorities, and Service Accounts
2323

24-
AKS generates and uses the following certificates, Certificate Authorities, and Service Accounts:
24+
AKS generates and uses the following certificates, Certificate Authorities (CA), and Service Accounts (SA):
2525

26-
* The AKS API server creates a Certificate Authority (CA) called the Cluster CA.
26+
* The AKS API server creates a CA called the Cluster CA.
2727
* The API server has a Cluster CA, which signs certificates for one-way communication from the API server to kubelets.
28-
* Each kubelet also creates a Certificate Signing Request (CSR), which is signed by the Cluster CA, for communication from the kubelet to the API server.
28+
* Each kubelet creates a Certificate Signing Request (CSR), which the Cluster CA signs, for communication from the kubelet to the API server.
2929
* The API aggregator uses the Cluster CA to issue certificates for communication with other APIs. The API aggregator can also have its own CA for issuing those certificates, but it currently uses the Cluster CA.
30-
* Each node uses a Service Account (SA) token, which is signed by the Cluster CA.
30+
* Each node uses an SA token, which the Cluster CA signs.
3131
* The `kubectl` client has a certificate for communicating with the AKS cluster.
3232

33-
Certificates mentioned above are maintained by Microsoft, except the cluster certificate, which you have to maintain.
33+
Microsoft maintains all certificates mentioned in this section, except for the cluster certificate.
3434

3535
> [!NOTE]
36-
> AKS clusters created prior to May 2019 have certificates that expire after two years. Any cluster created after May 2019 or any cluster that has its certificates rotated have Cluster CA certificates that expire after 30 years. All other AKS certificates, which use the Cluster CA for signing, will expire after two years and are automatically rotated during an AKS version upgrade which happened after 8/1/2021. To verify when your cluster was created, use `kubectl get nodes` to see the *Age* of your node pools.
3736
>
38-
> Additionally, you can check the expiration date of your cluster's certificate. For example, the following bash command displays the client certificate details for the *myAKSCluster* cluster in resource group *rg*:
39-
> ```console
40-
> kubectl config view --raw -o jsonpath="{.users[?(@.name == 'clusterUser_rg_myAKSCluster')].user.client-certificate-data}" | base64 -d | openssl x509 -text | grep -A2 Validity
41-
> ```
37+
> * **AKS clusters created *before* May 2019** have certificates that expire after two years.
38+
> * **AKS clusters created *after* May 2019** have Cluster CA certificates that expire after 30 years.
39+
>
40+
> You can verify when your cluster was created using the `kubectl get nodes` command, which shows you the *Age* of your node pools.
4241
43-
To check expiration date of apiserver certificate, run the following command:
42+
## Check certificate expiration dates
4443

45-
```console
46-
curl https://{apiserver-fqdn} -k -v 2>&1 |grep expire
47-
```
44+
### Check cluster certificate expiration date
4845

49-
To check the expiration date of certificate on VMAS agent node, run the following command:
46+
* Check the expiration date of the cluster certificate using the `kubectl config view` command.
5047

51-
```azurecli
52-
az vm run-command invoke -g MC_rg_myAKSCluster_region -n vm-name --command-id RunShellScript --query 'value[0].message' -otsv --scripts "openssl x509 -in /etc/kubernetes/certs/apiserver.crt -noout -enddate"
53-
```
48+
```console
49+
kubectl config view --raw -o jsonpath="{.users[?(@.name == 'clusterUser_rg_myAKSCluster')].user.client-certificate-data}" | base64 -d | openssl x509 -text | grep -A2 Validity
50+
```
5451

55-
To check expiration date of certificate on one virtual machine scale set agent node, run the following command:
52+
### Check API server certificate expiration date
5653

57-
```azurecli
58-
az vmss run-command invoke --resource-group "MC_rg_myAKSCluster_region" --name "vmss-name " --command-id RunShellScript --instance-id 1 --scripts "openssl x509 -in /etc/kubernetes/certs/apiserver.crt -noout -enddate" --query "value[0].message"
59-
```
54+
* Check the expiration date of the API server certificate using the following `curl` command.
6055

61-
## Certificate Auto Rotation
56+
```console
57+
curl https://{apiserver-fqdn} -k -v 2>&1 |grep expire
58+
```
6259

63-
For AKS to automatically rotate non-CA certificates, the cluster must have [TLS Bootstrapping](https://kubernetes.io/docs/reference/access-authn-authz/kubelet-tls-bootstrapping/) which has been enabled by default in all Azure regions.
60+
### Check VMAS agent node certificate expiration date
6461

65-
> [!NOTE]
66-
> If you have an existing cluster you have to upgrade that cluster to enable Certificate Auto-Rotation.
67-
> Do not disable bootstrap to keep your auto-rotation enabled.
62+
* Check the expiration date of the VMAS agent node certificate using the `az vm run-command invoke` command.
6863

69-
> [!NOTE]
70-
> If the cluster is in a stopped state during the auto certificate rotation only the control plane certificates are rotated. In this case the nodepool should be recreated, after certificate rotation, in order to initiate the nodepool certificate rotation.
64+
```azurecli-interactive
65+
az vm run-command invoke -g MC_rg_myAKSCluster_region -n vm-name --command-id RunShellScript --query 'value[0].message' -otsv --scripts "openssl x509 -in /etc/kubernetes/certs/apiserver.crt -noout -enddate"
66+
```
7167

72-
For any AKS clusters created or upgraded after March 2022 Azure Kubernetes Service will automatically rotate non-CA certificates on both the control plane and agent nodes within 80% of the client certificate valid time, before they expire with no downtime for the cluster.
68+
### Check Virtual Machine Scale Set agent node certificate expiration date
7369

74-
### How to check whether current agent node pool is TLS Bootstrapping enabled?
70+
* Check the expiration date of the Virtual Machine Scale Set agent node certificate using the `az vm run-command invoke` command.
7571

76-
To verify if TLS Bootstrapping is enabled on your cluster browse to the following paths:
72+
```azurecli-interactive
73+
az vmss run-command invoke --resource-group "MC_rg_myAKSCluster_region" --name "vmss-name" --command-id RunShellScript --instance-id 1 --scripts "openssl x509 -in /etc/kubernetes/certs/apiserver.crt -noout -enddate" --query "value[0].message"
74+
```
7775

78-
* On a Linux node: */var/lib/kubelet/bootstrap-kubeconfig* or */host/var/lib/kubelet/bootstrap-kubeconfig*
79-
* On a Windows node: *C:\k\bootstrap-config*
76+
## Certificate Auto Rotation
8077

81-
To access agent nodes, see [Connect to Azure Kubernetes Service cluster nodes for maintenance or troubleshooting][aks-node-access] for more information.
78+
For AKS to automatically rotate non-CA certificates, the cluster must have [TLS Bootstrapping](https://kubernetes.io/docs/reference/access-authn-authz/kubelet-tls-bootstrapping/), which is enabled by default in all Azure regions.
8279

8380
> [!NOTE]
84-
> The file path may change as Kubernetes version evolves in the future.
81+
>
82+
> * If you have an existing cluster, you have to upgrade that cluster to enable Certificate Auto Rotation.
83+
> * Don't disable Bootstrap to keep auto rotation enabled.
84+
> * If the cluster is in a stopped state during the auto certificate rotation, only the control plane certificates are rotated. In this case, you should recreate the node pool after certificate rotation to initiate the node pool certificate rotation.
8585

86-
Once a region is configured, create a new cluster or upgrade an existing cluster with `az aks upgrade` to set that cluster for auto-certificate rotation. A control plane and node pool upgrade is needed to enable this feature.
86+
For any AKS clusters created or upgraded after March 2022, Azure Kubernetes Service automatically rotates non-CA certificates on both the control plane and agent nodes within 80% of the client certificate valid time before they expire with no downtime for the cluster.
8787

88-
```azurecli
89-
az aks upgrade -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME
90-
```
88+
### How to check whether current agent node pool is TLS Bootstrapping enabled?
9189

92-
### Limitation
90+
1. Verify if your cluster has TLS Bootstrapping enabled by browsing to one to the following paths:
9391

94-
Certificate auto-rotation will only be enabled by default for RBAC enabled AKS clusters.
92+
* On a Linux node: */var/lib/kubelet/bootstrap-kubeconfig* or */host/var/lib/kubelet/bootstrap-kubeconfig*
93+
* On a Windows node: *C:\k\bootstrap-config*
94+
95+
For more information, see [Connect to Azure Kubernetes Service cluster nodes for maintenance or troubleshooting][aks-node-access].
96+
97+
> [!NOTE]
98+
> The file path may change as Kubernetes versions evolve.
99+
100+
2. Once a region is configured, create a new cluster or upgrade an existing cluster to set auto rotation for the cluster certificate. You need to upgrade the control plane and node pool to enable this feature.
95101

96102
## Manually rotate your cluster certificates
97103

98104
> [!WARNING]
99-
> Rotating your certificates using `az aks rotate-certs` will recreate all of your nodes, VM scale set and their Disks and can cause up to 30 minutes of downtime for your AKS cluster.
105+
> Rotating your certificates using `az aks rotate-certs` recreates all of your nodes, Virtual Machine Scale Sets and Disks and can cause up to *30 minutes of downtime* for your AKS cluster.
100106

101-
Use [az aks get-credentials][az-aks-get-credentials] to sign in to your AKS cluster. This command also downloads and configures the `kubectl` client certificate on your local machine.
107+
1. Connect to your cluster using the [`az aks get-credentials`][az-aks-get-credentials] command.
102108

103-
```azurecli
104-
az aks get-credentials -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME
105-
```
109+
```azurecli-interactive
110+
az aks get-credentials -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME
111+
```
106112

107-
Use `az aks rotate-certs` to rotate all certificates, CAs, and SAs on your cluster.
113+
2. Rotate all certificates, CAs, and SAs on your cluster using the [`az aks rotate-certs`][az-aks-rotate-certs] command.
108114

109-
```azurecli
110-
az aks rotate-certs -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME
111-
```
115+
```azurecli-interactive
116+
az aks rotate-certs -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME
117+
```
112118

113-
> [!IMPORTANT]
114-
> It may take up to 30 minutes for `az aks rotate-certs` to complete. If the command fails before completing, use `az aks show` to verify the status of the cluster is *Certificate Rotating*. If the cluster is in a failed state, rerun `az aks rotate-certs` to rotate your certificates again.
119+
> [!IMPORTANT]
120+
> It may take up to 30 minutes for `az aks rotate-certs` to complete. If the command fails before completing, use `az aks show` to verify the status of the cluster is *Certificate Rotating*. If the cluster is in a failed state, rerun `az aks rotate-certs` to rotate your certificates again.
115121

116-
Verify that the old certificates aren't valid by running any `kubectl` command. If you haven't updated the certificates used by `kubectl`, you'll see an error similar to the following example:
122+
3. Verify the old certificates are no longer valid using any `kubectl` command, such as `kubectl get nodes`.
117123

118-
```console
119-
kubectl get nodes
120-
Unable to connect to the server: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "ca")
121-
```
124+
```azurecli-interactive
125+
kubectl get nodes
126+
```
122127

123-
To update the certificate used by `kubectl`, run the [az aks get-credentials][az-aks-get-credentials] command:
128+
If you haven't updated the certificates used by `kubectl`, you see an error similar to the following example output:
124129

125-
```azurecli
126-
az aks get-credentials -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME --overwrite-existing
127-
```
130+
```output
131+
Unable to connect to the server: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "ca")
132+
```
128133

129-
To verify the certificates have been updated, run the following [kubectl get][kubectl-get] command:
134+
4. Update the certificate used by `kubectl` using the [`az aks get-credentials`][az-aks-get-credentials] command with the `--overwrite-existing` flag.
130135

131-
```console
132-
kubectl get nodes
133-
```
136+
```azurecli-interactive
137+
az aks get-credentials -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME --overwrite-existing
138+
```
134139

135-
> [!NOTE]
136-
> If you have any services that run on top of AKS, you might need to update their certificates.
140+
5. Verify the certificates have been updated using the [`kubectl get`][kubectl-get] command.
141+
142+
```azurecli-interactive
143+
kubectl get nodes
144+
```
145+
146+
> [!NOTE]
147+
> If you have any services that run on top of AKS, you might need to update their certificates.
137148

138149
## Next steps
139150

140-
This article showed you how to automatically rotate your cluster's certificates, CAs, and SAs. You can see [Best practices for cluster security and upgrades in Azure Kubernetes Service (AKS)][aks-best-practices-security-upgrades] for more information on AKS security best practices.
151+
This article showed you how to automatically rotate your cluster certificates, CAs, and SAs. For more information, see [Best practices for cluster security and upgrades in Azure Kubernetes Service (AKS)][aks-best-practices-security-upgrades].
141152

153+
<!-- LINKS - internal -->
142154
[azure-cli-install]: /cli/azure/install-azure-cli
143155
[az-aks-get-credentials]: /cli/azure/aks#az_aks_get_credentials
144-
[az-get]: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get
145-
[az-extension-add]: /cli/azure/extension#az_extension_add
146-
[az-extension-update]: /cli/azure/extension#az_extension_update
147156
[aks-best-practices-security-upgrades]: operator-best-practices-cluster-security.md
148157
[aks-node-access]: ./node-access.md
158+
[az-aks-rotate-certs]: /cli/azure/aks#az_aks_rotate_certs
159+
160+
<!-- LINKS - external -->
161+
[kubectl-get]: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get

0 commit comments

Comments
 (0)