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
- The first part Use Azure Cosmos DB to store the state of a vehicle using Dapr is a pre-requisite for this assignment. The account URL and the master key of the Azure Cosmos DB instance are required for this assignment. Please use the same Azure Cosmos DB instance as used in the first part of this assignment.
- Assignment 5 - Deploying to Azure Kubernetes Service is also a pre-requisite for this assignment.
Step 1: Deploy Azure Cosmos DB state store to AKS
-
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. -
Copy this file
dapr/azure-cosmosdb-statestore.yaml
todeploy/
folder. -
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 keymasterKey
are set to the secret created in the previous step. Thedatabase
is set todapr-workshop-java-database
and thecollection
is set tovehicle-state
.Never integrate secret in a kubernetes manifest directly, use kubernetes secret instead.
-
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. -
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. -
From the root folder of the repo, run the following command:
kubectl apply -k deploy
Step 2. Test the applications running in AKS
-
Run the following command to identify the name of each microservice pod:
kubectl get pods
-
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
-
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.