|
| 1 | +--- |
| 2 | +title: 'Quickstart: Deploy an Azure Kubernetes Service cluster' |
| 3 | +description: Learn how to quickly create a Kubernetes cluster, deploy an application, and monitor performance in Azure Kubernetes Service (AKS) using PowerShell. |
| 4 | +services: container-service |
| 5 | +ms.topic: quickstart |
| 6 | +ms.date: 05/12/2020 |
| 7 | + |
| 8 | + |
| 9 | +#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 and monitor applications using the managed Kubernetes service in Azure. |
| 10 | +--- |
| 11 | + |
| 12 | +# Quickstart: Deploy an Azure Kubernetes Service cluster using PowerShell |
| 13 | + |
| 14 | +In this quickstart, you deploy an Azure Kubernetes Service (AKS) cluster using PowerShell. AKS is a |
| 15 | +managed Kubernetes service that lets you quickly deploy and manage clusters. A multi-container |
| 16 | +application that includes a web frontend and a Redis instance is run in the cluster. You then see |
| 17 | +how to monitor the health of the cluster and pods that run your application. |
| 18 | + |
| 19 | +To learn more about creating a Windows Server node pool, see |
| 20 | +[Create an AKS cluster that supports Windows Server containers][windows-container-powershell]. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +This quickstart assumes a basic understanding of Kubernetes concepts. For more information, see |
| 25 | +[Kubernetes core concepts for Azure Kubernetes Service (AKS)][kubernetes-concepts]. |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account |
| 30 | +before you begin. |
| 31 | + |
| 32 | +If you choose to use PowerShell locally, this article requires that you install the Az PowerShell |
| 33 | +module and connect to your Azure account using the |
| 34 | +[Connect-AzAccount](/powershell/module/az.accounts/Connect-AzAccount) cmdlet. For more information |
| 35 | +about installing the Az PowerShell module, see |
| 36 | +[Install Azure PowerShell][install-azure-powershell]. |
| 37 | + |
| 38 | +> [!IMPORTANT] |
| 39 | +> This article uses a preview version of the Az.AKS PowerShell module. You must install it |
| 40 | +> separately from the Az PowerShell module using the following command: |
| 41 | +> `Install-Module -Name Az.AKS -AllowPrerelease -RequiredVersion 1.1.0-preview`. Once the preview |
| 42 | +> version the Az.AKS PowerShell module is generally available, it becomes part of future Az |
| 43 | +> PowerShell module releases and available natively from within Azure Cloud Shell. |
| 44 | +
|
| 45 | +[!INCLUDE [cloud-shell-try-it](../../includes/cloud-shell-try-it.md)] |
| 46 | + |
| 47 | +If you have multiple Azure subscriptions, choose the appropriate subscription in which the resources |
| 48 | +should be billed. Select a specific subscription ID using the |
| 49 | +[Set-AzContext](/powershell/module/az.accounts/set-azcontext) cmdlet. |
| 50 | + |
| 51 | +```azurepowershell-interactive |
| 52 | +Set-AzContext -SubscriptionId 00000000-0000-0000-0000-000000000000 |
| 53 | +``` |
| 54 | + |
| 55 | +## Create a resource group |
| 56 | + |
| 57 | +An [Azure resource group](https://docs.microsoft.com/azure/azure-resource-manager/resource-group-overview) |
| 58 | +is a logical group in which Azure resources are deployed and managed. When you create a resource |
| 59 | +group, you are asked to specify a location. This location is where resource group metadata is |
| 60 | +stored, it is also where your resources run in Azure if you don't specify another region during |
| 61 | +resource creation. Create a resource group using the [New-AzResourceGroup][new-azresourcegroup] |
| 62 | +cmdlet. |
| 63 | + |
| 64 | +The following example creates a resource group named **myResourceGroup** in the **eastus** region. |
| 65 | + |
| 66 | +```azurepowershell-interactive |
| 67 | +New-AzResourceGroup -Name myResourceGroup -Location eastus |
| 68 | +``` |
| 69 | + |
| 70 | +The following example output shows the resource group created successfully: |
| 71 | + |
| 72 | +```Output |
| 73 | +ResourceGroupName : myResourceGroup |
| 74 | +Location : eastus |
| 75 | +ProvisioningState : Succeeded |
| 76 | +Tags : |
| 77 | +ResourceId : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup |
| 78 | +``` |
| 79 | + |
| 80 | +## Create AKS cluster |
| 81 | + |
| 82 | +Use the `ssh-keygen` command-line utility to generate an SSH key pair. For more details, see |
| 83 | +[Quick steps: Create and use an SSH public-private key pair for Linux VMs in Azure](/azure/virtual-machines/linux/mac-create-ssh-keys). |
| 84 | + |
| 85 | +Use the [New-AzAks][new-azaks] cmdlet to create an AKS cluster. The |
| 86 | +following example creates a cluster named **myAKSCluster** with one node. Azure Monitor for |
| 87 | +containers is also enabled by default. This takes several minutes to complete. |
| 88 | + |
| 89 | +> [!NOTE] |
| 90 | +> When creating an AKS cluster, a second resource group is automatically created to store the AKS |
| 91 | +> resources. For more information, see |
| 92 | +> [Why are two resource groups created with AKS?](https://docs.microsoft.com/azure/aks/faq#why-are-two-resource-groups-created-with-aks) |
| 93 | +
|
| 94 | +```azurepowershell-interactive |
| 95 | +New-AzAks -ResourceGroupName myResourceGroup -Name myAKSCluster -NodeCount 1 |
| 96 | +``` |
| 97 | + |
| 98 | +After a few minutes, the command completes and returns information about the cluster. |
| 99 | + |
| 100 | +## Connect to the cluster |
| 101 | + |
| 102 | +To manage a Kubernetes cluster, you use [kubectl][kubectl], the Kubernetes command-line client. If |
| 103 | +you use Azure Cloud Shell, `kubectl` is already installed. To install `kubectl` locally, use the |
| 104 | +`Install-AzAksKubectl` cmdlet: |
| 105 | + |
| 106 | +```azurepowershell |
| 107 | +Install-AzAksKubectl |
| 108 | +``` |
| 109 | + |
| 110 | +To configure `kubectl` to connect to your Kubernetes cluster, use the |
| 111 | +[Import-AzAksCredential][import-azakscredential] cmdlet. The following |
| 112 | +example downloads credentials and configures the Kubernetes CLI to use them. |
| 113 | + |
| 114 | +```azurepowershell-interactive |
| 115 | +Import-AzAksCredential -ResourceGroupName myResourceGroup -Name myAKSCluster |
| 116 | +``` |
| 117 | + |
| 118 | +To verify the connection to your cluster, use the [kubectl get][kubectl-get] command to return a |
| 119 | +list of the cluster nodes. |
| 120 | + |
| 121 | +```azurepowershell-interactive |
| 122 | +.\kubectl get nodes |
| 123 | +``` |
| 124 | + |
| 125 | +The following example output shows the single node created in the previous steps. Make sure that the |
| 126 | +status of the node is **Ready**: |
| 127 | + |
| 128 | +```Output |
| 129 | +NAME STATUS ROLES AGE VERSION |
| 130 | +aks-nodepool1-31718369-0 Ready agent 6m44s v1.15.10 |
| 131 | +``` |
| 132 | + |
| 133 | +## Run the application |
| 134 | + |
| 135 | +A Kubernetes manifest file defines a desired state for the cluster, such as what container images to |
| 136 | +run. In this quickstart, a manifest is used to create all objects needed to run the Azure Vote |
| 137 | +application. This manifest includes two [Kubernetes deployments][kubernetes-deployment] - one for |
| 138 | +the sample Azure Vote Python applications, and the other for a Redis instance. Two |
| 139 | +[Kubernetes Services is also created - an internal service for the Redis |
| 140 | +instance, and an external service to access the Azure Vote application from the internet. |
| 141 | + |
| 142 | +> [!TIP] |
| 143 | +> In this quickstart, you manually create and deploy your application manifests to the AKS cluster. |
| 144 | +> In more real-world scenarios, you can use [Azure Dev Spaces][azure-dev-spaces] to rapidly iterate |
| 145 | +> and debug your code directly in the AKS cluster. You can use Dev Spaces across OS platforms and |
| 146 | +> development environments, and work together with others on your team. |
| 147 | +
|
| 148 | +Create a file named `azure-vote.yaml` and copy in the following YAML definition. If you use the |
| 149 | +Azure Cloud Shell, this file can be created using `vi` or `nano` as if working on a virtual or |
| 150 | +physical system: |
| 151 | + |
| 152 | +```yaml |
| 153 | +apiVersion: apps/v1 |
| 154 | +kind: Deployment |
| 155 | +metadata: |
| 156 | + name: azure-vote-back |
| 157 | +spec: |
| 158 | + replicas: 1 |
| 159 | + selector: |
| 160 | + matchLabels: |
| 161 | + app: azure-vote-back |
| 162 | + template: |
| 163 | + metadata: |
| 164 | + labels: |
| 165 | + app: azure-vote-back |
| 166 | + spec: |
| 167 | + nodeSelector: |
| 168 | + "beta.kubernetes.io/os": linux |
| 169 | + containers: |
| 170 | + - name: azure-vote-back |
| 171 | + image: redis |
| 172 | + resources: |
| 173 | + requests: |
| 174 | + cpu: 100m |
| 175 | + memory: 128Mi |
| 176 | + limits: |
| 177 | + cpu: 250m |
| 178 | + memory: 256Mi |
| 179 | + ports: |
| 180 | + - containerPort: 6379 |
| 181 | + name: redis |
| 182 | +--- |
| 183 | +apiVersion: v1 |
| 184 | +kind: Service |
| 185 | +metadata: |
| 186 | + name: azure-vote-back |
| 187 | +spec: |
| 188 | + ports: |
| 189 | + - port: 6379 |
| 190 | + selector: |
| 191 | + app: azure-vote-back |
| 192 | +--- |
| 193 | +apiVersion: apps/v1 |
| 194 | +kind: Deployment |
| 195 | +metadata: |
| 196 | + name: azure-vote-front |
| 197 | +spec: |
| 198 | + replicas: 1 |
| 199 | + selector: |
| 200 | + matchLabels: |
| 201 | + app: azure-vote-front |
| 202 | + template: |
| 203 | + metadata: |
| 204 | + labels: |
| 205 | + app: azure-vote-front |
| 206 | + spec: |
| 207 | + nodeSelector: |
| 208 | + "beta.kubernetes.io/os": linux |
| 209 | + containers: |
| 210 | + - name: azure-vote-front |
| 211 | + image: microsoft/azure-vote-front:v1 |
| 212 | + resources: |
| 213 | + requests: |
| 214 | + cpu: 100m |
| 215 | + memory: 128Mi |
| 216 | + limits: |
| 217 | + cpu: 250m |
| 218 | + memory: 256Mi |
| 219 | + ports: |
| 220 | + - containerPort: 80 |
| 221 | + env: |
| 222 | + - name: REDIS |
| 223 | + value: "azure-vote-back" |
| 224 | +--- |
| 225 | +apiVersion: v1 |
| 226 | +kind: Service |
| 227 | +metadata: |
| 228 | + name: azure-vote-front |
| 229 | +spec: |
| 230 | + type: LoadBalancer |
| 231 | + ports: |
| 232 | + - port: 80 |
| 233 | + selector: |
| 234 | + app: azure-vote-front |
| 235 | +``` |
| 236 | +
|
| 237 | +Deploy the application using the [kubectl apply][kubectl-apply] command and specify the name of your |
| 238 | +YAML manifest: |
| 239 | +
|
| 240 | +```azurepowershell-interactive |
| 241 | +.\kubectl apply -f azure-vote.yaml |
| 242 | +``` |
| 243 | + |
| 244 | +The following example output shows the Deployments and Services created successfully: |
| 245 | + |
| 246 | +```Output |
| 247 | +deployment.apps/azure-vote-back created |
| 248 | +service/azure-vote-back created |
| 249 | +deployment.apps/azure-vote-front created |
| 250 | +service/azure-vote-front created |
| 251 | +``` |
| 252 | + |
| 253 | +## Test the application |
| 254 | + |
| 255 | +When the application runs, a Kubernetes service exposes the application frontend to the internet. |
| 256 | +This process can take a few minutes to complete. |
| 257 | + |
| 258 | +To monitor progress, use the [kubectl get service][kubectl-get] command with the `--watch` argument. |
| 259 | + |
| 260 | +```azurepowershell-interactive |
| 261 | +.\kubectl get service azure-vote-front --watch |
| 262 | +``` |
| 263 | + |
| 264 | +Initially the **EXTERNAL-IP** for the **azure-vote-front** service is shown as **pending**. |
| 265 | + |
| 266 | +```Output |
| 267 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 268 | +azure-vote-front LoadBalancer 10.0.37.27 <pending> 80:30572/TCP 6s |
| 269 | +``` |
| 270 | + |
| 271 | +When the **EXTERNAL-IP** address changes from **pending** to an actual public IP address, use `CTRL-C` |
| 272 | +to stop the `kubectl` watch process. The following example output shows a valid public IP address |
| 273 | +assigned to the service: |
| 274 | + |
| 275 | +```Output |
| 276 | +azure-vote-front LoadBalancer 10.0.37.27 52.179.23.131 80:30572/TCP 2m |
| 277 | +``` |
| 278 | + |
| 279 | +To see the Azure Vote app in action, open a web browser to the external IP address of your service. |
| 280 | + |
| 281 | + |
| 282 | + |
| 283 | +When the AKS cluster was created, |
| 284 | +[Azure Monitor for containers](../azure-monitor/insights/container-insights-overview.md) was enabled |
| 285 | +to capture health metrics for both the cluster nodes and pods. These health metrics are available in |
| 286 | +the Azure portal. |
| 287 | + |
| 288 | +## Delete the cluster |
| 289 | + |
| 290 | +To avoid Azure charges, you should clean up unneeded resources. When the cluster is no longer |
| 291 | +needed, use the [Remove-AzResourceGroup][remove-azresourcegroup] cmdlet to remove the resource |
| 292 | +group, container service, and all related resources. |
| 293 | + |
| 294 | +```azurepowershell-interactive |
| 295 | +Remove-AzResourceGroup -Name myResourceGroup |
| 296 | +``` |
| 297 | + |
| 298 | +> [!NOTE] |
| 299 | +> When you delete the cluster, the Azure Active Directory service principal used by the AKS cluster |
| 300 | +> is not removed. For steps on how to remove the service principal, see |
| 301 | +> [AKS service principal considerations and deletion][sp-delete]. If you used a managed identity, |
| 302 | +> the identity is managed by the platform and does not require removal. |
| 303 | +
|
| 304 | +## Get the code |
| 305 | + |
| 306 | +In this quickstart, pre-created container images were used to create a Kubernetes deployment. The |
| 307 | +related application code, Dockerfile, and Kubernetes manifest file are available on GitHub. |
| 308 | + |
| 309 | +[https://github.com/Azure-Samples/azure-voting-app-redis][azure-vote-app] |
| 310 | + |
| 311 | +## Next steps |
| 312 | + |
| 313 | +In this quickstart, you deployed a Kubernetes cluster and deployed a multi-container application to |
| 314 | +it. You can also [access the Kubernetes web dashboard][kubernetes-dashboard] for your AKS cluster. |
| 315 | + |
| 316 | +To learn more about AKS, and walk through a complete code to deployment example, continue to the |
| 317 | +Kubernetes cluster tutorial. |
| 318 | + |
| 319 | +> [!div class="nextstepaction"] |
| 320 | +> [AKS tutorial][aks-tutorial] |
| 321 | +
|
| 322 | +<!-- LINKS - external --> |
| 323 | +[kubectl]: https://kubernetes.io/docs/user-guide/kubectl/ |
| 324 | +[kubectl-get]: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get |
| 325 | +[azure-dev-spaces]: https://docs.microsoft.com/azure/dev-spaces/ |
| 326 | +[kubectl-apply]: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply |
| 327 | +[azure-vote-app]: https://github.com/Azure-Samples/azure-voting-app-redis.git |
| 328 | + |
| 329 | +<!-- LINKS - internal --> |
| 330 | +[windows-container-powershell]: windows-container-powershell.md |
| 331 | +[kubernetes-concepts]: concepts-clusters-workloads.md |
| 332 | +[install-azure-powershell]: /powershell/azure/install-az-ps |
| 333 | +[new-azresourcegroup]: /powershell/module/az.resources/new-azresourcegroup |
| 334 | +[new-azaks]: /powershell/module/az.aks/new-azaks |
| 335 | +[import-azakscredential]: /powershell/module/az.aks/import-azakscredential |
| 336 | +[kubernetes-deployment]: concepts-clusters-workloads.md#deployments-and-yaml-manifests |
| 337 | +[kubernetes-service]: concepts-network.md#services |
| 338 | +[remove-azresourcegroup]: /powershell/module/az.resources/remove-azresourcegroup |
| 339 | +[sp-delete]: kubernetes-service-principal.md#additional-considerations |
| 340 | +[kubernetes-dashboard]: kubernetes-dashboard.md |
| 341 | +[aks-tutorial]: ./tutorial-kubernetes-prepare-app.md |
0 commit comments