Skip to content

Commit c0377ff

Browse files
Merge pull request #241595 from TomArcherMsft/UserStory100154
User Story 100154
2 parents 8dc877f + ef54c10 commit c0377ff

File tree

5 files changed

+207
-3
lines changed

5 files changed

+207
-3
lines changed

articles/aks/TOC.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
- name: Use ARM template
4141
displayName: Resource Manager
4242
href: learn/quick-kubernetes-deploy-rm-template.md
43+
- name: Use Terraform
44+
href: learn/quick-kubernetes-deploy-terraform.md
4345
- name: Deploy a Windows-based AKS Cluster
4446
items:
4547
- name: Use the Azure CLI
@@ -287,9 +289,6 @@
287289
href: resize-node-pool.md
288290
- name: Use the Azure Linux container host for AKS
289291
href: use-azure-linux.md
290-
- name: Deploy AKS with Terraform
291-
href: /azure/developer/terraform/create-k8s-cluster-with-tf-and-aks
292-
maintainContext: true
293292
- name: Azure portal Kubernetes resource view
294293
href: kubernetes-portal.md
295294
- name: Use tags

articles/aks/index.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ landingContent:
8484
url: ./learn/quick-kubernetes-deploy-portal.md
8585
- text: Resource Manager template
8686
url: ./learn/quick-kubernetes-deploy-rm-template.md
87+
- text: Terraform
88+
url: ./learn/quick-kubernetes-deploy-terraform.md
8789

8890
# Card
8991
- title: Develop and debug applications
29.7 KB
Loading
9.04 KB
Loading
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
---
2+
title: 'Quickstart: Create an Azure Kubernetes Service (AKS) cluster by using Terraform'
3+
description: In this article, you learn how to quickly create a Kubernetes cluster using Terraform and deploy an application in Azure Kubernetes Service (AKS)
4+
ms.topic: quickstart
5+
ms.date: 06/13/2023
6+
ms.custom: devx-track-terraform, ai-gen-docs
7+
#Customer intent: As a developer or cluster operator, I want to quickly create an AKS cluster and deploy an application so that I can see how to run applications using the managed Kubernetes service in Azure.
8+
---
9+
10+
# Quickstart: Create an Azure Kubernetes Service (AKS) cluster by using Terraform
11+
12+
Azure Kubernetes Service (AKS) is a managed Kubernetes service that lets you quickly deploy and manage clusters. In this quickstart, you:
13+
14+
* Deploy an AKS cluster using Terraform. The sample code is fully encapsulated such that it automatically creates a service principal and SSH key pair (using the [AzAPI provider](/azure/developer/terraform/overview-azapi-provider)).
15+
* Run a sample multi-container application with a web front-end and a Redis instance in the cluster.
16+
17+
:::image type="content" source="media/quick-kubernetes-deploy-terraform/azure-voting-application.png" alt-text="Preview of Azure Vote sample application.":::
18+
19+
[!INCLUDE [Terraform abstract](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)]
20+
21+
In this article, you learn how to:
22+
23+
> [!div class="checklist"]
24+
> * Create a random value for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet).
25+
> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group).
26+
> * Access the configuration of the AzureRM provider to get the Azure Object ID using [azurerm_client_config](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/client_config).
27+
> * Create a Kubernetes cluster using [azurerm_kubernetes_cluster](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster).
28+
> * Create an AzAPI resource [azapi_resource](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource).
29+
> * Create an AzAPI resource to generate an SSH key pair using [azapi_resource_action](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource_action).
30+
31+
[!INCLUDE [AI attribution](../../../includes/ai-generated-attribution.md)]
32+
33+
## Prerequisites
34+
35+
- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure)
36+
37+
- **Kubernetes command-line tool (kubectl):** [Download kubectl](https://kubernetes.io/releases/download/).
38+
39+
## Implement the Terraform code
40+
41+
> [!NOTE]
42+
> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/201-k8s-cluster-with-tf-and-aks). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/201-k8s-cluster-with-tf-and-aks/TestRecord.md).
43+
>
44+
> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
45+
46+
1. Create a directory in which to test the sample Terraform code and make it the current directory.
47+
48+
1. Create a file named `providers.tf` and insert the following code:
49+
50+
[!code-terraform[master](~/terraform_samples/quickstart/201-k8s-cluster-with-tf-and-aks/providers.tf)]
51+
52+
1. Create a file named `ssh.tf` and insert the following code:
53+
54+
[!code-terraform[master](~/terraform_samples/quickstart/201-k8s-cluster-with-tf-and-aks/ssh.tf)]
55+
56+
1. Create a file named `main.tf` and insert the following code:
57+
58+
[!code-terraform[master](~/terraform_samples/quickstart/201-k8s-cluster-with-tf-and-aks/main.tf)]
59+
60+
1. Create a file named `variables.tf` and insert the following code:
61+
62+
[!code-terraform[master](~/terraform_samples/quickstart/201-k8s-cluster-with-tf-and-aks/variables.tf)]
63+
64+
1. Create a file named `outputs.tf` and insert the following code:
65+
66+
[!code-terraform[master](~/terraform_samples/quickstart/201-k8s-cluster-with-tf-and-aks/outputs.tf)]
67+
68+
## Initialize Terraform
69+
70+
[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)]
71+
72+
## Create a Terraform execution plan
73+
74+
[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)]
75+
76+
## Apply a Terraform execution plan
77+
78+
[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)]
79+
80+
## Verify the results
81+
82+
#### [Azure CLI](#tab/azure-cli)
83+
84+
1. Get the Azure resource group name.
85+
86+
```console
87+
resource_group_name=$(terraform output -raw resource_group_name)
88+
```
89+
90+
1. Run [az aks list](/cli/azure/aks#az-aks-list) to display the name of the new Kubernetes cluster.
91+
92+
```azurecli
93+
az aks list \
94+
--resource-group $resource_group_name \
95+
--query "[].{\"K8s cluster name\":name}" \
96+
--output table
97+
```
98+
99+
1. Get the Kubernetes configuration from the Terraform state and store it in a file that kubectl can read.
100+
101+
```console
102+
echo "$(terraform output kube_config)" > ./azurek8s
103+
```
104+
105+
1. Verify the previous command didn't add an ASCII EOT character.
106+
107+
```console
108+
cat ./azurek8s
109+
```
110+
111+
**Key points:**
112+
113+
- If you see `<< EOT` at the beginning and `EOT` at the end, remove these characters from the file. Otherwise, you could receive the following error message: `error: error loading config file "./azurek8s": yaml: line 2: mapping values are not allowed in this context`
114+
115+
1. Set an environment variable so that kubectl picks up the correct config.
116+
117+
```console
118+
export KUBECONFIG=./azurek8s
119+
```
120+
121+
1. Verify the health of the cluster.
122+
123+
```console
124+
kubectl get nodes
125+
```
126+
127+
![Screenshot showing how the kubectl tool allows you to verify the health of your Kubernetes cluster.](./media/quick-kubernetes-deploy-terraform/kubectl-get-nodes.png)
128+
129+
**Key points:**
130+
131+
- When the AKS cluster was created, monitoring was enabled to capture health metrics for both the cluster nodes and pods. These health metrics are available in the Azure portal. For more information on container health monitoring, see [Monitor Azure Kubernetes Service health](/azure/azure-monitor/insights/container-insights-overview).
132+
- Several key values were output when you applied the Terraform execution plan. For example, the host address, AKS cluster user name, and AKS cluster password are output.
133+
134+
## Deploy the application
135+
136+
A [Kubernetes manifest file](/azure/aks/concepts-clusters-workloads#deployments-and-yaml-manifests) defines a cluster's desired state, such as which container images to run.
137+
138+
In this quickstart, you use a manifest to create all the objects needed to run the [Azure Vote application](https://github.com/Azure-Samples/azure-voting-app-redis.git). This manifest includes two [Kubernetes deployments](/azure/aks/concepts-clusters-workloads#deployments-and-yaml-manifests):
139+
140+
* The sample Azure Vote Python applications.
141+
* A Redis instance.
142+
143+
Two [Kubernetes Services](/azure/aks/concepts-network#services) are created:
144+
145+
* An internal service for the Redis instance.
146+
* An external service to access the Azure Vote application from the internet.
147+
148+
1. Create a file named `azure-vote.yaml` and insert the following code:
149+
150+
[!code-terraform[master](~/terraform_samples/quickstart/201-k8s-cluster-with-tf-and-aks/azure-vote.yaml)]
151+
152+
**Key points:**
153+
154+
- For more information about YAML manifest files, see [Deployments and YAML manifests](/azure/aks/concepts-clusters-workloads#deployments-and-yaml-manifests).
155+
156+
1. Run [kubectl apply](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) to deploy the application.
157+
158+
```console
159+
kubectl apply -f azure-vote.yaml
160+
```
161+
162+
### Test the application
163+
164+
1. When the application runs, a Kubernetes service exposes the application front end to the internet. This process can take a few minutes to complete. Run [kubectl get service](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get) with the `--watch` argument to monitor progress.
165+
166+
```console
167+
kubectl get service azure-vote-front --watch
168+
```
169+
170+
1. The **EXTERNAL-IP** output for the `azure-vote-front` service initially shows as *pending*. Once the **EXTERNAL-IP** address displays an IP address, use `CTRL-C` to stop the `kubectl` watch process.
171+
172+
1. To see the **Azure Vote** app in action, open a web browser to the external IP address of your service.
173+
174+
:::image type="content" source="media/quick-kubernetes-deploy-terraform/azure-voting-application.png" alt-text="Screenshot of Azure Vote sample application.":::
175+
176+
## Clean up resources
177+
178+
### Delete AKS resources
179+
180+
[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)]
181+
182+
### Delete service principal
183+
184+
1. Get the service principal ID.
185+
186+
```azurecli
187+
sp=$(terraform output -raw sp)
188+
```
189+
190+
1. Run [az ad sp delete](/cli/azure/ad/sp#az-ad-sp-delete) to delete the service principal.
191+
192+
```azurecli
193+
az ad sp delete --id $sp
194+
```
195+
196+
## Troubleshoot Terraform on Azure
197+
198+
[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot)
199+
200+
## Next steps
201+
202+
> [!div class="nextstepaction"]
203+
> [Learn more about using AKS](/azure/aks)

0 commit comments

Comments
 (0)