|
| 1 | +--- |
| 2 | +title: Deploy an application with the Dapr cluster extension (preview) for Azure Kubernetes Service (AKS) |
| 3 | +description: Use the Dapr cluster extension (Preview) for Azure Kubernetes Service (AKS) to deploy an application |
| 4 | +author: nickomang |
| 5 | +ms.author: nickoman |
| 6 | +ms.service: container-service |
| 7 | +ms.topic: quickstart |
| 8 | +ms.date: 11/01/2021 |
| 9 | +ms.custom: template-quickstart |
| 10 | +--- |
| 11 | + |
| 12 | +# Quickstart: Deploy an application using the Dapr cluster extension (preview) for Azure Kubernetes Service (AKS) |
| 13 | + |
| 14 | +In this quickstart, you will get familiar with using the [Dapr cluster extension][dapr-overview] in an AKS cluster. You will be deploying a hello world example, consisting of a Python application that generates messages and a Node application that consumes and persists them. |
| 15 | + |
| 16 | +[!INCLUDE [preview features callout](./includes/preview/preview-callout.md)] |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +* An Azure subscription. If you don't have an Azure subscription, you can create a [free account](https://azure.microsoft.com/free). |
| 21 | +* [Azure CLI installed](/cli/azure/install-azure-cli). |
| 22 | +* An AKS cluster with the [Dapr cluster extension][dapr-overview] enabled |
| 23 | + |
| 24 | +## Clone the repository |
| 25 | + |
| 26 | +To obtain the files you'll be using to deploy the sample application, clone the [Quickstarts repository][hello-world-gh] and change to the `hello-kubernetes` directory: |
| 27 | + |
| 28 | +```bash |
| 29 | +git clone https://github.com/dapr/quickstarts.git |
| 30 | +cd quickstarts/hello-kubernetes |
| 31 | +``` |
| 32 | + |
| 33 | +## Create and configure a state store |
| 34 | + |
| 35 | +Dapr can use a number of different state stores (Redis, CosmosDB, DynamoDB, Cassandra, etc.) to persist and retrieve state. For this example, we will use Redis. |
| 36 | + |
| 37 | +### Create a Redis store |
| 38 | + |
| 39 | +1. Open the [Azure portal][azure-portal-cache] to start the Azure Redis Cache creation flow. |
| 40 | +2. Fill out the necessary information |
| 41 | +3. Click “Create” to kickoff deployment of your Redis instance. |
| 42 | +4. Take note of the hostname of your Redis instance, which you can retrieve from the “Overview” in Azure. It should look like `xxxxxx.redis.cache.windows.net:6380`. |
| 43 | +5. Once your instance is created, you’ll need to grab your access key. Navigate to “Access Keys” under “Settings” and create a Kubernetes secret to store your Redis password: |
| 44 | + |
| 45 | +```bash |
| 46 | +kubectl create secret generic redis --from-literal=redis-password=<your-redis-password> |
| 47 | +``` |
| 48 | + |
| 49 | +### Configure the Dapr components |
| 50 | + |
| 51 | +Once your store is created, you will need to add the keys to the redis.yaml file in the deploy directory of the Hello World repository. Replace the `redisHost` value with your own Redis master address, and the `redisPassword` with your own Secret. You can learn more [here][dapr-component-secrets]. |
| 52 | + |
| 53 | +You will also need to add the following two lines below `redisPassword` to enable connection over TLS: |
| 54 | + |
| 55 | +```yml |
| 56 | +- name: redisPassword |
| 57 | + secretKeyRef: |
| 58 | + name: redis |
| 59 | + key: redis-password |
| 60 | +- name: enableTLS |
| 61 | + value: true |
| 62 | +``` |
| 63 | +
|
| 64 | +### Apply the configuration |
| 65 | +
|
| 66 | +Apply the `redis.yaml` file: |
| 67 | + |
| 68 | +```bash |
| 69 | +kubectl apply -f ./deploy/redis.yaml |
| 70 | +``` |
| 71 | + |
| 72 | +And verify that your state store was successfully configured in the output: |
| 73 | + |
| 74 | +```bash |
| 75 | +component.dapr.io/statestore created |
| 76 | +``` |
| 77 | + |
| 78 | +## Deploy the Node.js app with the Dapr sidecar |
| 79 | + |
| 80 | +Apply the Node.js app's deployment to your cluster: |
| 81 | + |
| 82 | +```bash |
| 83 | +kubectl apply -f ./deploy/node.yaml |
| 84 | +``` |
| 85 | + |
| 86 | +> [!NOTE] |
| 87 | +> Kubernetes deployments are asynchronous. This means you'll need to wait for the deployment to complete before moving on to the next steps. You can do so with the following command: |
| 88 | +> ```bash |
| 89 | +> kubectl rollout status deploy/nodeapp |
| 90 | +> ``` |
| 91 | + |
| 92 | +This will deploy the Node.js app to Kubernetes. The Dapr control plane will automatically inject the Dapr sidecar to the Pod. If you take a look at the `node.yaml` file, you will see how Dapr is enabled for that deployment: |
| 93 | + |
| 94 | +* `dapr.io/enabled: true` - this tells the Dapr control plane to inject a sidecar to this deployment. |
| 95 | + |
| 96 | +* `dapr.io/app-id: nodeapp` - this assigns a unique ID or name to the Dapr application, so it can be sent messages to and communicated with by other Dapr apps. |
| 97 | + |
| 98 | +To access your service, obtain and make note of the `EXTERNAL-IP` via `kubectl`: |
| 99 | + |
| 100 | +```bash |
| 101 | +kubectl get svc nodeapp |
| 102 | +``` |
| 103 | + |
| 104 | +### Verify the service |
| 105 | + |
| 106 | +To call the service, run: |
| 107 | + |
| 108 | +```bash |
| 109 | +curl $EXTERNAL_IP/ports |
| 110 | +``` |
| 111 | + |
| 112 | +You should see output similar to the following: |
| 113 | + |
| 114 | +```bash |
| 115 | +{"DAPR_HTTP_PORT":"3500","DAPR_GRPC_PORT":"50001"} |
| 116 | +``` |
| 117 | + |
| 118 | +Next, submit an order to the application: |
| 119 | + |
| 120 | +```bash |
| 121 | +curl --request POST --data "@sample.json" --header Content-Type:application/json $EXTERNAL_IP/neworder |
| 122 | +``` |
| 123 | + |
| 124 | +Confirm the order has been persisted by requesting it: |
| 125 | + |
| 126 | +```bash |
| 127 | +curl $EXTERNAL_IP/order |
| 128 | +``` |
| 129 | + |
| 130 | +You should see output similar to the following: |
| 131 | + |
| 132 | +```bash |
| 133 | +{ "orderId": "42" } |
| 134 | +``` |
| 135 | + |
| 136 | +> [!TIP] |
| 137 | +> This is a good time to get acquainted with the Dapr dashboard- a convenient interface to check status, information and logs of applications running on Dapr. The following command will make it available on `http://localhost:8080/`: |
| 138 | +> ```bash |
| 139 | +> kubectl port-forward svc/dapr-dashboard -n dapr-system 8080:8080 |
| 140 | +> ``` |
| 141 | + |
| 142 | +## Deploy the Python app with the Dapr sidecar |
| 143 | + |
| 144 | +Take a quick look at the Python app. Navigate to the Python app directory in the `hello-kubernetes` quickstart and open `app.py`. |
| 145 | + |
| 146 | +This is a basic Python app that posts JSON messages to `localhost:3500`, which is the default listening port for Dapr. You can invoke the Node.js application's `neworder` endpoint by posting to `v1.0/invoke/nodeapp/method/neworder`. The message contains some data with an `orderId` that increments once per second: |
| 147 | + |
| 148 | +```python |
| 149 | +n = 0 |
| 150 | +while True: |
| 151 | + n += 1 |
| 152 | + message = {"data": {"orderId": n}} |
| 153 | +
|
| 154 | + try: |
| 155 | + response = requests.post(dapr_url, json=message) |
| 156 | + except Exception as e: |
| 157 | + print(e) |
| 158 | +
|
| 159 | + time.sleep(1) |
| 160 | +``` |
| 161 | + |
| 162 | +Deploy the Python app to your Kubernetes cluster: |
| 163 | + |
| 164 | +```bash |
| 165 | +kubectl apply -f ./deploy/python.yaml |
| 166 | +``` |
| 167 | + |
| 168 | +> [!NOTE] |
| 169 | +> As with above, the following command will wait for the deployment to complete: |
| 170 | +> ```bash |
| 171 | +> kubectl rollout status deploy/pythonapp |
| 172 | +> ``` |
| 173 | + |
| 174 | +## Observe messages and confirm persistence |
| 175 | + |
| 176 | +Now that both the Node.js and Python applications are deployed, watch messages come through. |
| 177 | + |
| 178 | +Get the logs of the Node.js app: |
| 179 | + |
| 180 | +```bash |
| 181 | +kubectl logs --selector=app=node -c node --tail=-1 |
| 182 | +``` |
| 183 | + |
| 184 | +If the deployments were successful, you should see logs like this: |
| 185 | + |
| 186 | +```bash |
| 187 | +Got a new order! Order ID: 1 |
| 188 | +Successfully persisted state |
| 189 | +Got a new order! Order ID: 2 |
| 190 | +Successfully persisted state |
| 191 | +Got a new order! Order ID: 3 |
| 192 | +Successfully persisted state |
| 193 | +``` |
| 194 | + |
| 195 | +Call the Node.js app's order endpoint to get the latest order. Grab the external IP address that you saved before and, append "/order" and perform a GET request against it (enter it into your browser, use Postman, or `curl` it!): |
| 196 | + |
| 197 | +```bash |
| 198 | +curl $EXTERNAL_IP/order |
| 199 | +{"orderID":"42"} |
| 200 | +``` |
| 201 | + |
| 202 | +You should see the latest JSON in the response. |
| 203 | + |
| 204 | +## Clean up resources |
| 205 | + |
| 206 | +Use the [az group delete][az-group-delete] command to remove the resource group, the AKS cluster, namespace, and all related resources. |
| 207 | + |
| 208 | +```azurecli-interactive |
| 209 | +az group delete --name MyResourceGroup |
| 210 | +``` |
| 211 | + |
| 212 | +## Next steps |
| 213 | + |
| 214 | +After successfully deploying this sample application: |
| 215 | +> [!div class="nextstepaction"] |
| 216 | +> [Learn more about other cluster extensions][cluster-extensions] |
| 217 | + |
| 218 | +<!-- LINKS --> |
| 219 | +<!-- INTERNAL --> |
| 220 | +[cluster-extensions]: ./cluster-extensions.md |
| 221 | +[dapr-overview]: ./dapr.md |
| 222 | +[az-group-delete]: /cli/azure/group#az_group_delete |
| 223 | + |
| 224 | +<!-- EXTERNAL --> |
| 225 | +[hello-world-gh]: https://github.com/dapr/quickstarts/tree/v1.4.0/hello-kubernetes |
| 226 | +[azure-portal-cache]: https://ms.portal.azure.com/#create/Microsoft.Cache |
| 227 | +[dapr-component-secrets]: https://docs.dapr.io/operations/components/component-secrets/ |
0 commit comments