Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Deploying Azure Cosmos DB state store to Azure Kubernetes Service

Table of contents

In this bonus assignment, you will deploy the Azure Cosmos DB state store to Azure Kubernetes Service (AKS). You will use the Azure Cosmos DB state store component provided by Dapr.

Pre-requisites

Step 1: Deploy Azure Cosmos DB state store to AKS

  1. Create Kubernetes secret for the Azure Cosmos DB account URL and the master key using the following command:

     kubectl create secret generic cosmos-db-secret \
         --from-literal=accountUrl=<cosmos-db-account-url>
         --from-literal=masterKey=<cosmos-db-master-key>
    

    Where <cosmos-db-account-url> is the account URL of the Azure Cosmos DB instance and <cosmos-db-master-key> is the master key of the Azure Cosmos DB instance. Both are set when the Azure Cosmos DB instance is created in the first part of this assignment.

  2. Copy this file dapr/azure-cosmosdb-statestore.yaml to deploy/ folder.

  3. Update the deploy/azure-cosmosdb-statestore.yaml file to use the Azure Cosmos DB instance deployed in the first part of this assignment. The file should look like this:

     apiVersion: dapr.io/v1alpha1
     kind: Component
     metadata:
       name: statestore
     spec:
       type: state.azure.cosmosdb
       version: v1
       metadata:
       - name: url
         secretKeyRef:
           name: cosmos-db-secret
           key: accountUrl
       - name: masterKey
         secretKeyRef:
           name: cosmos-db-secret
           key: masterKey
       - name: database
         value: dapr-workshop-java-database
       - name: collection
         value: vehicle-state
       - name: actorStateStore
         value: "true"
     scopes:
       - trafficcontrolservice
    

    Cosmos DB account URL url and master key masterKey are set to the secret created in the previous step. The database is set to dapr-workshop-java-database and the collection is set to vehicle-state.

    Never integrate secret in a kubernetes manifest directly, use kubernetes secret instead.

  4. Delete the image from local docker and from the Azure Container Registry:

     docker rmi traffic-control-service:1.0-SNAPSHOT
     az acr repository delete -n $CONTAINER_REGISTRY --image traffic-control-service:latest
    

    Where $CONTAINER_REGISTRY is the name of the Azure Container Registry.

  5. In the root folder of TrafficControlService microservice, run the following command:

     mvn spring-boot:build-image
     docker tag traffic-control-service:1.0-SNAPSHOT $CONTAINER_REGISTRY.azurecr.io/traffic-control-service:latest
     docker push $CONTAINER_REGISTRY.azurecr.io/traffic-control-service:latest
    

    Where $CONTAINER_REGISTRY is the name of the Azure Container Registry.

  6. From the root folder of the repo, run the following command:

     kubectl apply -k deploy
    

Step 2. Test the applications running in AKS

  1. Run the following command to identify the name of each microservice pod:

     kubectl get pods
    
  2. Look at the log file of each application pod to see the same output as seen when running on your laptop. For example:

     kubectl logs finecollectionservice-ccf8c9cf5-vr8hr -c fine-collection-service
    
  3. Delete all application deployments:

     kubectl delete -k deploy
    

Cleanup

When the workshop is done, please follow the cleanup instructions to delete the resources created in this workshop.

< Cosmos DB as a state store