|
| 1 | +--- |
| 2 | +title: Rotate certificates in Azure Kubernetes Service (AKS) |
| 3 | +description: Learn how to rotate your certificates in an Azure Kubernetes Service (AKS) cluster. |
| 4 | +services: container-service |
| 5 | +author: zr-msft |
| 6 | + |
| 7 | +ms.service: container-service |
| 8 | +ms.topic: article |
| 9 | +ms.date: 11/15/2019 |
| 10 | +ms.author: zarhoads |
| 11 | +--- |
| 12 | + |
| 13 | +# Rotate certificates in Azure Kubernetes Service (AKS) |
| 14 | + |
| 15 | +Azure Kubernetes Service (AKS) uses certificates for authentication with many of its components. 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. |
| 16 | + |
| 17 | +This article shows you how to rotate the certificates in your AKS cluster. |
| 18 | + |
| 19 | +## Before you begin |
| 20 | + |
| 21 | +This article requires that you are running the Azure CLI version 2.0.76 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][azure-cli-install]. |
| 22 | + |
| 23 | + |
| 24 | +### Install aks-preview CLI extension |
| 25 | + |
| 26 | +To use this feature, you need the *aks-preview* CLI extension version 0.4.21 or higher. Install the *aks-preview* Azure CLI extension using the [az extension add][az-extension-add] command, then check for any available updates using the [az extension update][az-extension-update] command: |
| 27 | + |
| 28 | +```azurecli-interactive |
| 29 | +# Install the aks-preview extension |
| 30 | +az extension add --name aks-preview |
| 31 | +
|
| 32 | +# Update the extension to make sure you have the latest version installed |
| 33 | +az extension update --name aks-preview |
| 34 | +``` |
| 35 | + |
| 36 | +## AKS certificates, Certificate Authorities, and Service Accounts |
| 37 | + |
| 38 | +AKS generates and uses the following certificates, Certificate Authorities, and Service Accounts: |
| 39 | + |
| 40 | +* The AKS API server creates a Certificate Authority (CA) called the Cluster CA. |
| 41 | +* The API server has a Cluster CA, which signs certificates for one-way communication from the API server to kubelets. |
| 42 | +* 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. |
| 43 | +* The etcd key value store has a certificate signed by the Cluster CA for communication from etcd to the API server. |
| 44 | +* The etcd key value store creates a CA that signs certificates to authenticate and authorize data replication between etcd replicas in the AKS cluster. |
| 45 | +* The API aggregator uses the Cluster CA to issue certificates for communication with other APIs, such as Open Service Broker for Azure. The API aggregator can also have its own CA for issuing those certificates, but it currently uses the Cluster CA. |
| 46 | +* Each node uses a Service Account (SA) token, which is signed by the Cluster CA. |
| 47 | +* The `kubectl` client has a certificate for communicating with the AKS cluster. |
| 48 | + |
| 49 | +> [!NOTE] |
| 50 | +> AKS clusters created prior to March 2019 have certificates that expire after two years. Any cluster created after March 2019 or any cluster that has its certificates rotated have certificates that expire after 30 years. |
| 51 | +
|
| 52 | +## Rotate your cluster certificates |
| 53 | + |
| 54 | +> [!WARNING] |
| 55 | +> Rotating your certificates using `az aks rotate-certs` can cause up to 30 minutes of downtime for your AKS cluster. |
| 56 | +
|
| 57 | +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. |
| 58 | + |
| 59 | +```console |
| 60 | +az aks get-credentials -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME |
| 61 | +``` |
| 62 | + |
| 63 | +Use `az aks rotate-certs` to rotate all certificates, CAs, and SAs on your cluster. |
| 64 | + |
| 65 | +```console |
| 66 | +az aks rotate-certs -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME |
| 67 | +``` |
| 68 | + |
| 69 | +> [!IMPORTANT] |
| 70 | +> 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. |
| 71 | +
|
| 72 | +Verify that the old certificates are no longer valid by running a `kubectl` command. Since you have not updated the certificates used by `kubectl`, you will see an error. For example: |
| 73 | + |
| 74 | +```console |
| 75 | +$ kubectl get no |
| 76 | +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") |
| 77 | +``` |
| 78 | + |
| 79 | +Update the certificate used by `kubectl` by running `az aks get-credentials`. |
| 80 | + |
| 81 | +```console |
| 82 | +az aks get-credentials -g $RESOURCE_GROUP_NAME -n $CLUSTER_NAME --overwrite-existing |
| 83 | +``` |
| 84 | + |
| 85 | +Verify the certificates have been updated by running a `kubectl` command, which will now succeed. For example: |
| 86 | + |
| 87 | +```console |
| 88 | +kubectl get no |
| 89 | +``` |
| 90 | + |
| 91 | +## Next steps |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | + |
| 96 | +[azure-cli-install]: /cli/azure/install-azure-cli |
| 97 | +[az-aks-get-credentials]: /cli/azure/aks?view=azure-cli-latest#az-aks-get-credentials |
| 98 | +[az-extension-add]: /cli/azure/extension#az-extension-add |
| 99 | +[az-extension-update]: /cli/azure/extension#az-extension-update |
| 100 | +[aks-best-practices-security-upgrades]: operator-best-practices-cluster-security.md |
0 commit comments