Proposed Pull Request Change

title description author ms.topic ms.custom ms.date ms.author
Deploy an AKS cluster using the Bicep Kubernetes extension Learn how to quickly deploy a Kubernetes cluster using the Bicep Kubernetes extension and deploy an application in Azure Kubernetes Service (AKS). schaffererin quickstart devx-track-bicep 01/11/2024 schaffererin
📄 Document Links
GitHub View on GitHub Microsoft Learn View on Microsoft Learn
Content Truncation Detected
The generated rewrite appears to be incomplete.
Original lines: -
Output lines: -
Ratio: -
Raw New Markdown
Generating updated version of doc...
Rendered New Markdown
Generating updated version of doc...
+0 -0
+0 -0
--- title: Deploy an AKS cluster using the Bicep Kubernetes extension description: Learn how to quickly deploy a Kubernetes cluster using the Bicep Kubernetes extension and deploy an application in Azure Kubernetes Service (AKS). author: schaffererin ms.topic: quickstart ms.custom: devx-track-bicep ms.date: 01/11/2024 ms.author: schaffererin # Customer intent: As a developer or cluster operator, I want to quickly deploy an AKS cluster and deploy an application so that I can see how to run applications using the managed Kubernetes service in Azure. --- # Quickstart: Deploy an Azure Kubernetes Service (AKS) cluster using the Bicep Kubernetes extension (preview) Azure Kubernetes Service (AKS) is a managed Kubernetes service that lets you quickly deploy and manage clusters. In this quickstart, you: - Deploy an AKS cluster using the Bicep Kubernetes extension (preview). - Run a sample multi-container application with a group of microservices and web front ends simulating a retail scenario. > [!IMPORTANT] > The Bicep Kubernetes extension is currently in preview. You can enable the feature from the [Bicep configuration file](/azure/azure-resource-manager/bicep/bicep-config#enable-experimental-features) by adding: > > ```json > { > "experimentalFeaturesEnabled": { > "extensibility": true, > } > } > ``` > [!NOTE] > To get started with quickly provisioning an AKS cluster, this article includes steps to deploy a cluster with default settings for evaluation purposes only. Before deploying a production-ready cluster, we recommend that you familiarize yourself with our [baseline reference architecture][baseline-reference-architecture] to consider how it aligns with your business requirements. ## Before you begin This quickstart assumes a basic understanding of Kubernetes concepts. For more information, see [Kubernetes core concepts for Azure Kubernetes Service (AKS)][kubernetes-concepts]. - [!INCLUDE [quickstarts-free-trial-note](~/reusable-content/ce-skilling/azure/includes/quickstarts-free-trial-note.md)] - Make sure that the identity you use to create your cluster has the appropriate minimum permissions. For more details on access and identity for AKS, see [Access and identity options for Azure Kubernetes Service (AKS)](../concepts-identity.md). [!INCLUDE [About Bicep](~/reusable-content/ce-skilling/azure/includes/resource-manager-quickstart-bicep-introduction.md)] - To set up your environment for Bicep development, see [Install Bicep tools](/azure/azure-resource-manager/bicep/install). After completing the steps, you have [Visual Studio Code](https://code.visualstudio.com/) and the [Bicep extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep). You also have either the latest [Azure CLI](/cli/azure/) version or the latest [Azure PowerShell module](/powershell/azure/new-azureps-module-az). - To create an AKS cluster using a Bicep file, you provide an SSH public key. If you need this resource, see the following section. Otherwise, skip to [Review the Bicep file](#review-the-bicep-file). - To deploy a Bicep file, you need write access on the resources you deploy and access to all operations on the `Microsoft.Resources/deployments` resource type. For example, to deploy a virtual machine, you need `Microsoft.Compute/virtualMachines/write` and `Microsoft.Resources/deployments/*` permissions. For a list of roles and permissions, see [Azure built-in roles](/azure/role-based-access-control/built-in-roles). ### Create an SSH key pair 1. Go to [https://shell.azure.com](https://shell.azure.com) to open Cloud Shell in your browser. 1. Create an SSH key pair using the [az sshkey create][az-sshkey-create] Azure CLI command or the `ssh-keygen` command. ```azurecli # Create an SSH key pair using Azure CLI az sshkey create --name "mySSHKey" --resource-group "myResourceGroup" # Create an SSH key pair using ssh-keygen ssh-keygen -t rsa -b 4096 ``` For more information about creating SSH keys, see [Create and manage SSH keys for authentication in Azure][ssh-keys]. ## Review the Bicep file The Bicep file used to create an AKS cluster is from [Azure Quickstart Templates](https://azure.microsoft.com/resources/templates/aks/). For more AKS samples, see [AKS quickstart templates][aks-quickstart-templates]. :::code language="bicep" source="~/quickstart-templates/quickstarts/microsoft.kubernetes/aks/main.bicep"::: The resource defined in the Bicep file is [**Microsoft.ContainerService/managedClusters**](/azure/templates/microsoft.containerservice/managedclusters?tabs=bicep&pivots=deployment-language-bicep). Save a copy of the file as `main.bicep` to your local computer. ## Add the application definition To deploy the application, you use a manifest file to create all the objects required to run the [AKS Store application](https://github.com/Azure-Samples/aks-store-demo). A [Kubernetes manifest file][kubernetes-deployment] defines a cluster's desired state, such as which container images to run. The manifest includes the following Kubernetes deployments and services: :::image type="content" source="media/quick-kubernetes-deploy-bicep-kubernetes-extension/aks-store-architecture.png" alt-text="Screenshot of Azure Store sample architecture." lightbox="media/quick-kubernetes-deploy-bicep-kubernetes-extension/aks-store-architecture.png"::: - **Store front**: Web application for customers to view products and place orders. - **Product service**: Shows product information. - **Order service**: Places orders. - **Rabbit MQ**: Message queue for an order queue. > [!NOTE] > We don't recommend running stateful containers, such as Rabbit MQ, without persistent storage for production. These are used here for simplicity, but we recommend using managed services, such as Azure CosmosDB or Azure Service Bus. 1. Create a file named `aks-store-quickstart.yaml` in the same folder as `main.bicep` and copy in the following manifest: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: rabbitmq spec: replicas: 1 selector: matchLabels: app: rabbitmq template: metadata: labels: app: rabbitmq spec: nodeSelector: "kubernetes.io/os": linux containers: - name: rabbitmq image: mcr.microsoft.com/mirror/docker/library/rabbitmq:3.10-management-alpine ports: - containerPort: 5672 name: rabbitmq-amqp - containerPort: 15672 name: rabbitmq-http env: - name: RABBITMQ_DEFAULT_USER value: "username" - name: RABBITMQ_DEFAULT_PASS value: "password" resources: requests: cpu: 10m memory: 128Mi limits: cpu: 250m memory: 256Mi volumeMounts: - name: rabbitmq-enabled-plugins mountPath: /etc/rabbitmq/enabled_plugins subPath: enabled_plugins volumes: - name: rabbitmq-enabled-plugins configMap: name: rabbitmq-enabled-plugins items: - key: rabbitmq_enabled_plugins path: enabled_plugins --- apiVersion: v1 data: rabbitmq_enabled_plugins: | [rabbitmq_management,rabbitmq_prometheus,rabbitmq_amqp1_0]. kind: ConfigMap metadata: name: rabbitmq-enabled-plugins --- apiVersion: v1 kind: Service metadata: name: rabbitmq spec: selector: app: rabbitmq ports: - name: rabbitmq-amqp port: 5672 targetPort: 5672 - name: rabbitmq-http port: 15672 targetPort: 15672 type: ClusterIP --- apiVersion: apps/v1 kind: Deployment metadata: name: order-service spec: replicas: 1 selector: matchLabels: app: order-service template: metadata: labels: app: order-service spec: nodeSelector: "kubernetes.io/os": linux containers: - name: order-service image: ghcr.io/azure-samples/aks-store-demo/order-service:latest ports: - containerPort: 3000 env: - name: ORDER_QUEUE_HOSTNAME value: "rabbitmq" - name: ORDER_QUEUE_PORT value: "5672" - name: ORDER_QUEUE_USERNAME value: "username" - name: ORDER_QUEUE_PASSWORD value: "password" - name: ORDER_QUEUE_NAME value: "orders" - name: FASTIFY_ADDRESS value: "0.0.0.0" resources: requests: cpu: 1m memory: 50Mi limits: cpu: 75m memory: 128Mi initContainers: - name: wait-for-rabbitmq image: busybox command: ['sh', '-c', 'until nc -zv rabbitmq 5672; do echo waiting for rabbitmq; sleep 2; done;'] resources: requests: cpu: 1m memory: 50Mi limits: cpu: 75m memory: 128Mi --- apiVersion: v1 kind: Service metadata: name: order-service spec: type: ClusterIP ports: - name: http port: 3000 targetPort: 3000 selector: app: order-service --- apiVersion: apps/v1 kind: Deployment metadata: name: product-service spec: replicas: 1 selector: matchLabels: app: product-service template: metadata: labels: app: product-service spec: nodeSelector: "kubernetes.io/os": linux containers: - name: product-service image: ghcr.io/azure-samples/aks-store-demo/product-service:latest ports: - containerPort: 3002 resources: requests: cpu: 1m memory: 1Mi limits: cpu: 1m memory: 7Mi --- apiVersion: v1 kind: Service metadata: name: product-service spec: type: ClusterIP ports: - name: http port: 3002 targetPort: 3002 selector: app: product-service --- apiVersion: apps/v1 kind: Deployment metadata: name: store-front spec: replicas: 1 selector: matchLabels: app: store-front template: metadata: labels: app: store-front spec: nodeSelector: "kubernetes.io/os": linux containers: - name: store-front image: ghcr.io/azure-samples/aks-store-demo/store-front:latest ports: - containerPort: 8080 name: store-front env: - name: VUE_APP_ORDER_SERVICE_URL value: "http://order-service:3000/" - name: VUE_APP_PRODUCT_SERVICE_URL value: "http://product-service:3002/" resources: requests: cpu: 1m memory: 200Mi limits: cpu: 1000m memory: 512Mi --- apiVersion: v1 kind: Service metadata: name: store-front spec: ports: - port: 80 targetPort: 8080 selector: app: store-front type: LoadBalancer ``` For a breakdown of YAML manifest files, see [Deployments and YAML manifests](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/). If you create and save the YAML file locally, then you can upload the manifest file to your default directory in CloudShell by selecting the **Upload/Download files** button and selecting the file from your local file system. 2. Open `main.bicep` in Visual Studio Code. 3. Press <kbd>Ctrl+Shift+P</kbd> to open **Command Palette**. 4. Search for **bicep**, and then select **Bicep: Import Kubernetes Manifest**. :::image type="content" source="./media/quick-kubernetes-deploy-bicep-kubernetes-extension/bicep-kubernetes-extension-import-kubernetes-manifest.png" alt-text="Screenshot of Visual Studio Code import Kubernetes Manifest." lightbox="./media/quick-kubernetes-deploy-bicep-kubernetes-extension/bicep-kubernetes-extension-import-kubernetes-manifest.png"::: 5. Select `aks-store-quickstart.yaml` from the prompt. This process creates an `aks-store-quickstart.bicep` file in the same folder. 6. Open `main.bicep` and add the following Bicep at the end of the file to reference the newly created `aks-store-quickstart.bicep` module: ```bicep module kubernetes './aks-store-quickstart.bicep' = { name: 'buildbicep-deploy' params: { kubeConfig: aks.listClusterAdminCredential().kubeconfigs[0].value } } ``` 7. Save both `main.bicep` and `aks-store-quickstart.bicep`. ## Deploy the Bicep file ### [Azure CLI](#tab/azure-cli) 1. Create an Azure resource group using the [az group create][az-group-create] command. ```azurecli az group create --name myResourceGroup --location eastus ``` 1. Deploy the Bicep file using the [az deployment group create][az-deployment-group-create] command. ```azurecli az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters clusterName=<cluster-name> dnsPrefix=<dns-previs> linuxAdminUsername=<linux-admin-username> sshRSAPublicKey='<ssh-key>' ``` ### [Azure PowerShell](#tab/azure-powershell) 1. Create an Azure resource group using the [New-AzResourceGroup][new-azresourcegroup] cmdlet. ```azurepowershell New-AzResourceGroup -Name myResourceGroup -Location eastus ``` 1. Deploy the Bicep file using the [New-AzResourceGroupDeployment][new-azresourcegroupdeployment] cmdlet. ```azurepowershell New-AzResourceGroupDeployment -ResourceGroupName myResourceGroup -TemplateFile ./main.bicep -clusterName=<cluster-name> -dnsPrefix=<dns-prefix> -linuxAdminUsername=<linux-admin-username> -sshRSAPublicKey="<ssh-key>" ``` --- Provide the following values in the commands: - **Cluster name**: Enter a unique name for the AKS cluster, such as *myAKSCluster*. - **DNS prefix**: Enter a unique DNS prefix for your cluster, such as *myakscluster*. - **Linux Admin Username**: Enter a username to connect using SSH, such as *azureuser*. - **SSH RSA Public Key**: Copy and paste the *public* part of your SSH key pair (by default, the contents of *~/.ssh/id_rsa.pub*). It takes a few minutes to create the AKS cluster. Wait for the cluster successfully deploy before you move on to the next step. ## Validate the Bicep deployment 1. Sign in to the [Azure portal](https://portal.azure.com/). 1. On the Azure portal menu or from the **Home** page, navigate to your AKS cluster. 1. Under **Kubernetes resources**, select **Services and ingresses**. 1. Find the **store-front** service and copy the value for **External IP**. 1. Open a web browser to the external IP address of your service to see the Azure Store app in action. :::image type="content" source="media/quick-kubernetes-deploy-bicep-kubernetes-extension/aks-store-application.png" alt-text="Screenshot of AKS Store sample application." lightbox="media/quick-kubernetes-deploy-bicep-kubernetes-extension/aks-store-application.png"::: ## Delete the cluster If you don't plan on going through the [AKS tutorial][aks-tutorial], clean up unnecessary resources to avoid Azure charges. ### [Azure CLI](#tab/azure-cli) Remove the resource group, container service, and all related resources using the [az group delete][az-group-delete] command. ```azurecli az group delete --name myResourceGroup --yes --no-wait ``` ### [Azure PowerShell](#tab/azure-powershell) Remove the resource group, container service, and all related resources using the [Remove-AzResourceGroup][remove-azresourcegroup] cmdlet. ```azurepowershell Remove-AzResourceGroup -Name myResourceGroup ``` --- > [!NOTE] > The AKS cluster was created with a system-assigned managed identity, which is the default identity option used in this quickstart. The platform manages this identity so you don't need to manually remove it. ## Next steps In this quickstart, you deployed a Kubernetes cluster and then deployed a simple multi-container application to it. This sample application is for demo purposes only and doesn't represent all the best practices for Kubernetes applications. For guidance on creating full solutions with AKS for production, see [AKS solution guidance][aks-solution-guidance]. To learn more about AKS and walk through a complete code-to-deployment example, continue to the Kubernetes cluster tutorial. > [!div class="nextstepaction"] > [AKS tutorial][aks-tutorial] <!-- LINKS - external --> [aks-quickstart-templates]: https://azure.microsoft.com/resources/templates/?term=Azure+Kubernetes+Service <!-- LINKS - internal --> [kubernetes-concepts]: ../concepts-clusters-workloads.md [aks-tutorial]: ../tutorial-kubernetes-prepare-app.md [az-group-create]: /cli/azure/group#az-group-create [az-group-delete]: /cli/azure/group#az-group-delete [remove-azresourcegroup]: /powershell/module/az.resources/remove-azresourcegroup [kubernetes-deployment]: ../concepts-clusters-workloads.md#deployments-and-yaml-manifests [ssh-keys]: /azure/virtual-machines/linux/create-ssh-keys-detailed [az-deployment-group-create]: /cli/azure/deployment/group#az-deployment-group-create [new-azresourcegroup]: /powershell/module/az.resources/new-azresourcegroup [new-azresourcegroupdeployment]: /powershell/module/az.resources/new-azresourcegroupdeployment [az-sshkey-create]: /cli/azure/sshkey#az-sshkey-create [baseline-reference-architecture]: /azure/architecture/reference-architectures/containers/aks/baseline-aks?toc=/azure/aks/toc.json&bc=/azure/aks/breadcrumb/toc.json [aks-solution-guidance]: /azure/architecture/reference-architectures/containers/aks-start-here?toc=/azure/aks/toc.json&bc=/azure/aks/breadcrumb/toc.json
Success! Branch created successfully. Create Pull Request on GitHub
Error: