|
| 1 | +--- |
| 2 | +title: "Tutorial: Create a highly available Eureka server component cluster in Azure Container Apps" |
| 3 | +description: Learn to create a highly available Eureka service in Azure Container Apps. |
| 4 | +services: container-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: azure-container-apps |
| 7 | +ms.custom: devx-track-extended-java |
| 8 | +ms.topic: tutorial |
| 9 | +ms.date: 08/16/2024 |
| 10 | +ms.author: cshoe |
| 11 | +#customer intent: As a developer, I want to create an Eureka server cluster so that I can ensure there is no downtime of my service registries regardless of load and failures. |
| 12 | +--- |
| 13 | + |
| 14 | +# Tutorial: Create a highly available Eureka server component cluster in Azure Container Apps |
| 15 | + |
| 16 | +In this tutorial, you learn to create a Eureka service designed to remain operational in the face of failures and high demand. Building a highly available Eureka service ensures the service registry is always available to clients regardless of demand. |
| 17 | + |
| 18 | +Achieving high availability status for Eureka includes linking multiple Eureka server instances together forming a cluster. The cluster provides resources so that if one Eureka server fails, the other services remain available for requests. |
| 19 | + |
| 20 | +In this tutorial, you: |
| 21 | + |
| 22 | +> [!div class="checklist"] |
| 23 | +> * Create a Eureka server for Spring components. |
| 24 | +> * Bind two Eureka servers for Spring components together into a cluster. |
| 25 | +> * Bind applications to both Eureka servers for highly available service discovery. |
| 26 | +
|
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +To complete this project, you need the following items: |
| 30 | + |
| 31 | +| Requirement | Instructions | |
| 32 | +|--|--| |
| 33 | +| Azure account | An active subscription is required. If you don't have one, you [can create one for free](https://azure.microsoft.com/free/). | |
| 34 | +| Azure CLI | Install the [Azure CLI](/cli/azure/install-azure-cli).| |
| 35 | + |
| 36 | +## Considerations |
| 37 | + |
| 38 | +When running managed Java components in Azure Container Apps, be aware of the following details: |
| 39 | + |
| 40 | +[!INCLUDE [container-apps/component-considerations.md](../../includes/container-apps/component-considerations.md)] |
| 41 | + |
| 42 | +## Setup |
| 43 | + |
| 44 | +Use the following steps to create your Eureka service cluster. |
| 45 | + |
| 46 | +1. Create variables that hold application configuration values. |
| 47 | + |
| 48 | + ```bash |
| 49 | + export LOCATION=eastus |
| 50 | + export RESOURCE_GROUP=my-services-resource-group |
| 51 | + export ENVIRONMENT=my-environment |
| 52 | + export EUREKA_COMPONENT_FIRST=eureka01 |
| 53 | + export EUREKA_COMPONENT_SECOND=eureka02 |
| 54 | + export APP_NAME=sample-service-eureka-client |
| 55 | + export IMAGE="mcr.microsoft.com/javacomponents/samples/sample-service-eureka-client:latest" |
| 56 | + ``` |
| 57 | + |
| 58 | +1. Sign in to Azure with the Azure CLI. |
| 59 | + |
| 60 | + ```azurecli |
| 61 | + az login |
| 62 | + ``` |
| 63 | + |
| 64 | +1. Create a resource group. |
| 65 | + |
| 66 | + ```azurecli |
| 67 | + az group create --name $RESOURCE_GROUP --location $LOCATION |
| 68 | + ``` |
| 69 | + |
| 70 | +1. Create your Container Apps environment. |
| 71 | + |
| 72 | + ```azurecli |
| 73 | + az containerapp env create \ |
| 74 | + --name $ENVIRONMENT \ |
| 75 | + --resource-group $RESOURCE_GROUP \ |
| 76 | + --location $LOCATION |
| 77 | + ``` |
| 78 | + |
| 79 | +## Create a cluster |
| 80 | + |
| 81 | +Next, create two Eureka server instances and link them together as a cluster. |
| 82 | + |
| 83 | +1. Create two Eureka Server for Spring components. |
| 84 | + |
| 85 | + ```azurecli |
| 86 | + az containerapp env java-component eureka-server-for-spring create \ |
| 87 | + --environment $ENVIRONMENT \ |
| 88 | + --resource-group $RESOURCE_GROUP \ |
| 89 | + --name $EUREKA_COMPONENT_FIRST |
| 90 | + ``` |
| 91 | + |
| 92 | + ```azurecli |
| 93 | + az containerapp env java-component eureka-server-for-spring create \ |
| 94 | + --environment $ENVIRONMENT \ |
| 95 | + --resource-group $RESOURCE_GROUP \ |
| 96 | + --name $EUREKA_COMPONENT_SECOND |
| 97 | + ``` |
| 98 | + |
| 99 | +## Bind components together |
| 100 | + |
| 101 | +For the Eureka servers to work in a high-availability configuration, they need to be linked together. |
| 102 | + |
| 103 | +1. Bind the first Eureka server to the second. |
| 104 | + |
| 105 | + ```azurecli |
| 106 | + az containerapp env java-component eureka-server-for-spring update \ |
| 107 | + --environment $ENVIRONMENT \ |
| 108 | + --resource-group $RESOURCE_GROUP \ |
| 109 | + --name $EUREKA_COMPONENT_FIRST \ |
| 110 | + --bind $EUREKA_COMPONENT_SECOND |
| 111 | + ``` |
| 112 | + |
| 113 | +1. Bind the second Eureka server to the first. |
| 114 | + |
| 115 | + ```azurecli |
| 116 | + az containerapp env java-component eureka-server-for-spring update \ |
| 117 | + --environment $ENVIRONMENT \ |
| 118 | + --resource-group $RESOURCE_GROUP \ |
| 119 | + --name $EUREKA_COMPONENT_SECOND \ |
| 120 | + --bind $EUREKA_COMPONENT_FIRST |
| 121 | + ``` |
| 122 | + |
| 123 | +## Deploy and bind the application |
| 124 | + |
| 125 | +With the server components linked together, you can create the container app and binding it to the two Eureka components. |
| 126 | + |
| 127 | +1. Create the container app. |
| 128 | + |
| 129 | + ```azurecli |
| 130 | + az containerapp create \ |
| 131 | + --name $APP_NAME \ |
| 132 | + --resource-group $RESOURCE_GROUP \ |
| 133 | + --environment $ENVIRONMENT \ |
| 134 | + --image $IMAGE \ |
| 135 | + --min-replicas 1 \ |
| 136 | + --max-replicas 1 \ |
| 137 | + --ingress external \ |
| 138 | + --target-port 8080 |
| 139 | + ``` |
| 140 | + |
| 141 | +1. Bind the container app to the first Eureka server component. |
| 142 | + |
| 143 | + ```azurecli |
| 144 | + az containerapp update \ |
| 145 | + --name $APP_NAME \ |
| 146 | + --resource-group $RESOURCE_GROUP \ |
| 147 | + --bind $EUREKA_COMPONENT_FIRST |
| 148 | + ``` |
| 149 | + |
| 150 | +1. Bind the container app to the second Eureka server component. |
| 151 | + |
| 152 | + ```azurecli |
| 153 | + az containerapp update \ |
| 154 | + --name $APP_NAME \ |
| 155 | + --resource-group $RESOURCE_GROUP \ |
| 156 | + --bind $EUREKA_COMPONENT_SECOND |
| 157 | + ``` |
| 158 | + |
| 159 | +## View the dashboards |
| 160 | + |
| 161 | +> [!IMPORTANT] |
| 162 | +> To view the dashboard, you need to have at least the `Microsoft.App/managedEnvironments/write` role assigned to your account on the managed environment resource. You can either explicitly assign `Owner` or `Contributor` role on the resource or follow the steps to create a custom role definition and assign it to your account. |
| 163 | + |
| 164 | +1. Create the custom role definition. |
| 165 | + |
| 166 | + ```azurecli |
| 167 | + az role definition create --role-definition '{ |
| 168 | + "Name": "Java Component Dashboard Access", |
| 169 | + "IsCustom": true, |
| 170 | + "Description": "Can access managed Java Component dashboards in managed environments", |
| 171 | + "Actions": [ |
| 172 | + "Microsoft.App/managedEnvironments/write" |
| 173 | + ], |
| 174 | + "AssignableScopes": ["/subscriptions/<SUBSCRIPTION_ID>"] |
| 175 | + }' |
| 176 | + ``` |
| 177 | + |
| 178 | + Make sure to replace placeholder in between the `<>` brackets in the `AssignableScopes` value with your subscription ID. |
| 179 | + |
| 180 | +1. Assign the custom role to your account on managed environment resource. |
| 181 | + |
| 182 | + Get the resource ID of the managed environment. |
| 183 | + |
| 184 | + ```azurecli |
| 185 | + export ENVIRONMENT_ID=$(az containerapp env show \ |
| 186 | + --name $ENVIRONMENT --resource-group $RESOURCE_GROUP \ |
| 187 | + --query id -o tsv) |
| 188 | + ``` |
| 189 | + |
| 190 | +1. Assign the role to your account. |
| 191 | + |
| 192 | + Before running this command, replace the placeholder in between the `<>` brackets with your user or service principal ID. |
| 193 | + |
| 194 | + ```azurecli |
| 195 | + az role assignment create \ |
| 196 | + --assignee <USER_OR_SERVICE_PRINCIPAL_ID> \ |
| 197 | + --role "Java Component Dashboard Access" \ |
| 198 | + --scope $ENVIRONMENT_ID |
| 199 | + ``` |
| 200 | + |
| 201 | +1. Get the URL of the Eureka Server for Spring dashboard. |
| 202 | + |
| 203 | + ```azurecli |
| 204 | + az containerapp env java-component eureka-server-for-spring show \ |
| 205 | + --environment $ENVIRONMENT \ |
| 206 | + --resource-group $RESOURCE_GROUP \ |
| 207 | + --name $EUREKA_COMPONENT_FIRST \ |
| 208 | + --query properties.ingress.fqdn -o tsv |
| 209 | + ``` |
| 210 | + |
| 211 | + This command returns the URL you can use to access the Eureka Server for Spring dashboard. Through the dashboard, you can verify that the Eureka server setup consists of two replicas. |
| 212 | + |
| 213 | + :::image type="content" source="media/java-components/eureka-highly-available.png" alt-text="Screenshot of a highly available Eureka Server for Spring dashboard."::: |
| 214 | + |
| 215 | +## Clean up resources |
| 216 | + |
| 217 | +The resources created in this tutorial have an effect on your Azure bill. If you aren't going to use these services long-term, run the following command to remove everything created in this tutorial. |
| 218 | +
|
| 219 | +```azurecli |
| 220 | +az group delete \ |
| 221 | + --resource-group $RESOURCE_GROUP |
| 222 | +``` |
| 223 | +
|
| 224 | +## Next steps |
| 225 | +
|
| 226 | +> [!div class="nextstepaction"] |
| 227 | +> [Configure Eureka Server for Spring settings](java-eureka-server-usage.md) |
0 commit comments