|
| 1 | +--- |
| 2 | +title: "Tutorial: Create a Highly Available Eureka Service in Azure Container Apps" |
| 3 | +description: Learn to create a highly available Eureka service in Azure Container Apps. |
| 4 | +services: container-apps |
| 5 | +author: wenhaozhang |
| 6 | +ms.service: azure-container-apps |
| 7 | +ms.custom: devx-track-extended-java |
| 8 | +ms.topic: conceptual |
| 9 | +ms.date: 08/05/2024 |
| 10 | +ms.author: wenhaozhang |
| 11 | +--- |
| 12 | + |
| 13 | +In this tutorial, we will walk you through the process of creating a highly available Eureka service using in Container Apps. By leveraging Azure Container Apps, you can ensure that your Eureka service is highly available. |
| 14 | + |
| 15 | +A Highly Available service is one designed to be operational and accessible without significant downtime. For Eureka, this means ensuring the service registry is always available to client services for both registering themselves and discovering other services. Achieving HA for Eureka involves running multiple instances of the Eureka server and configuring them to be aware of each other, forming a cluster. This setup ensures that if one Eureka server fails, the others continue to operate, preventing a single point of failure. |
| 16 | + |
| 17 | +In this tutorial, you will learn to: |
| 18 | + |
| 19 | +1. Create Eureka Server for Spring components. |
| 20 | +2. Bind two Eureka Server for Spring components together to provide high available service. |
| 21 | +3. Bind applications to both two Eureka Server for high available service discovery. |
| 22 | + |
| 23 | +# Prerequisites |
| 24 | + |
| 25 | +To complete this project, you need the following items: |
| 26 | + |
| 27 | +| Requirement | Instructions | |
| 28 | +|--|--| |
| 29 | +| Azure account | An active subscription is required. If you don't have one, you [can create one for free](https://azure.microsoft.com/free/). | |
| 30 | +| Azure CLI | Install the [Azure CLI](/cli/azure/install-azure-cli).| |
| 31 | + |
| 32 | +## Considerations |
| 33 | + |
| 34 | +When running managed Java components in Azure Container Apps, be aware of the following details: |
| 35 | + |
| 36 | +[!INCLUDE [container-apps/component-considerations.md](../../includes/container-apps/component-considerations.md)] |
| 37 | + |
| 38 | +## Setup |
| 39 | + |
| 40 | +Before you begin, create the necessary resources by executing the following commands. |
| 41 | + |
| 42 | +1. Create variables to support your application configuration. These values are provided for you for the purposes of this lesson. |
| 43 | + |
| 44 | + ```bash |
| 45 | + export LOCATION=eastus |
| 46 | + export RESOURCE_GROUP=my-services-resource-group |
| 47 | + export ENVIRONMENT=my-environment |
| 48 | + export EUREKA_COMPONENT_NAME_01=eureka01 |
| 49 | + export EUREKA_COMPONENT_NAME_02=eureka02 |
| 50 | + export APP_NAME=sample-service-eureka-client |
| 51 | + export IMAGE="mcr.microsoft.com/javacomponents/samples/sample-service-eureka-client:latest" |
| 52 | + ``` |
| 53 | +2. Log in to Azure with the Azure CLI. |
| 54 | + |
| 55 | + ```azurecli |
| 56 | + az login |
| 57 | + ``` |
| 58 | + |
| 59 | +3. Create a resource group. |
| 60 | + |
| 61 | + ```azurecli |
| 62 | + az group create --name $RESOURCE_GROUP --location $LOCATION |
| 63 | + ``` |
| 64 | + |
| 65 | +4. Create your container apps environment. |
| 66 | + |
| 67 | + ```azurecli |
| 68 | + az containerapp env create \ |
| 69 | + --name $ENVIRONMENT \ |
| 70 | + --resource-group $RESOURCE_GROUP \ |
| 71 | + --location $LOCATION |
| 72 | + ``` |
| 73 | + |
| 74 | +## Create two Eureka Server for Spring Java component |
| 75 | + |
| 76 | +Now that you have an existing environment, to create high available Eureka service, you should at least create two Eureka Server for Spring java components. These components will form the core of your Eureka service. |
| 77 | + |
| 78 | +1. Create two Eureka Server for Spring component. |
| 79 | + |
| 80 | +```azurecli |
| 81 | +az containerapp env java-component eureka-server-for-spring create \ |
| 82 | + --environment $ENVIRONMENT \ |
| 83 | + --resource-group $RESOURCE_GROUP \ |
| 84 | + --name $EUREKA_COMPONENT_NAME_01 |
| 85 | +``` |
| 86 | + |
| 87 | +```azurecli |
| 88 | +az containerapp env java-component eureka-server-for-spring create \ |
| 89 | + --environment $ENVIRONMENT \ |
| 90 | + --resource-group $RESOURCE_GROUP \ |
| 91 | + --name $EUREKA_COMPONENT_NAME_02 |
| 92 | +``` |
| 93 | + |
| 94 | +## Bind the components with each other |
| 95 | + |
| 96 | +For the Eureka servers to work in a high-availability configuration, they must be aware of each other. This is achieved by binding the two Eureka components together. |
| 97 | + |
| 98 | +1. Bind the Eureka Server for Spring components with each other. |
| 99 | + |
| 100 | +```azurecli |
| 101 | +az containerapp env java-component eureka-server-for-spring update \ |
| 102 | + --environment $ENVIRONMENT \ |
| 103 | + --resource-group $RESOURCE_GROUP \ |
| 104 | + --name $EUREKA_COMPONENT_NAME_01 \ |
| 105 | + --bind $EUREKA_COMPONENT_NAME_02 |
| 106 | +``` |
| 107 | + |
| 108 | +```azurecli |
| 109 | +az containerapp env java-component eureka-server-for-spring update \ |
| 110 | + --environment $ENVIRONMENT \ |
| 111 | + --resource-group $RESOURCE_GROUP \ |
| 112 | + --name $EUREKA_COMPONENT_NAME_02 \ |
| 113 | + --bind $EUREKA_COMPONENT_NAME_01 |
| 114 | +``` |
| 115 | + |
| 116 | +## Deploy and bind your application |
| 117 | + |
| 118 | +Finally, deploy your application as a container app and bind it to the Eureka servers. This step involves creating the container app and binding it to the two Eureka components to ensure it can register with and discover services from the Eureka service. |
| 119 | + |
| 120 | +1. Create the container app |
| 121 | + |
| 122 | +```azurecli |
| 123 | +az containerapp create \ |
| 124 | + --name $APP_NAME \ |
| 125 | + --resource-group $RESOURCE_GROUP \ |
| 126 | + --environment $ENVIRONMENT \ |
| 127 | + --image $IMAGE \ |
| 128 | + --min-replicas 1 \ |
| 129 | + --max-replicas 1 \ |
| 130 | + --ingress external \ |
| 131 | + --target-port 8080 |
| 132 | +``` |
| 133 | + |
| 134 | +1. Bind the container app to the two Eureka Server for Spring components. |
| 135 | + |
| 136 | +```azurecli |
| 137 | +az containerapp update \ |
| 138 | + --name $APP_NAME \ |
| 139 | + --resource-group $RESOURCE_GROUP \ |
| 140 | + --bind $EUREKA_COMPONENT_NAME_01 |
| 141 | +``` |
| 142 | + |
| 143 | +```azurecli |
| 144 | +az containerapp update \ |
| 145 | + --name $APP_NAME \ |
| 146 | + --resource-group $RESOURCE_GROUP \ |
| 147 | + --bind $EUREKA_COMPONENT_NAME_02 |
| 148 | +``` |
| 149 | + |
| 150 | +## View the dashboards |
| 151 | + |
| 152 | +> [!IMPORTANT] |
| 153 | +> 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. |
| 154 | + |
| 155 | +1. Create the custom role definition. |
| 156 | + |
| 157 | + ```azurecli |
| 158 | + az role definition create --role-definition '{ |
| 159 | + "Name": "Java Component Dashboard Access", |
| 160 | + "IsCustom": true, |
| 161 | + "Description": "Can access managed Java Component dashboards in managed environments", |
| 162 | + "Actions": [ |
| 163 | + "Microsoft.App/managedEnvironments/write" |
| 164 | + ], |
| 165 | + "AssignableScopes": ["/subscriptions/<SUBSCRIPTION_ID>"] |
| 166 | + }' |
| 167 | + ``` |
| 168 | + |
| 169 | + Make sure to replace placeholder in between the `<>` brackets in the `AssignableScopes` value with your subscription ID. |
| 170 | + |
| 171 | +1. Assign the custom role to your account on managed environment resource. |
| 172 | + |
| 173 | + Get the resource id of the managed environment. |
| 174 | + |
| 175 | + ```azurecli |
| 176 | + export ENVIRONMENT_ID=$(az containerapp env show \ |
| 177 | + --name $ENVIRONMENT --resource-group $RESOURCE_GROUP \ |
| 178 | + --query id -o tsv) |
| 179 | + ``` |
| 180 | + |
| 181 | +1. Assign the role to your account. |
| 182 | + |
| 183 | + Before running this command, replace the placeholder in between the `<>` brackets with your user or service principal ID. |
| 184 | + |
| 185 | + ```azurecli |
| 186 | + az role assignment create \ |
| 187 | + --assignee <USER_OR_SERVICE_PRINCIPAL_ID> \ |
| 188 | + --role "Java Component Dashboard Access" \ |
| 189 | + --scope $ENVIRONMENT_ID |
| 190 | + ``` |
| 191 | + |
| 192 | +1. Get the URL of the Eureka Server for Spring dashboard. |
| 193 | + |
| 194 | + ```azurecli |
| 195 | + az containerapp env java-component eureka-server-for-spring show \ |
| 196 | + --environment $ENVIRONMENT \ |
| 197 | + --resource-group $RESOURCE_GROUP \ |
| 198 | + --name $EUREKA_COMPONENT_NAME_01 \ |
| 199 | + --query properties.ingress.fqdn -o tsv |
| 200 | + ``` |
| 201 | + |
| 202 | + This command returns the URL you can use to access the Eureka Server for Spring dashboard. Through the dashboard, you could find the eureka server setup consists of two replicas. |
| 203 | + |
| 204 | + :::image type="content" source="media/java-components/eureka-ha.png" alt-text="Screenshot of the High available Eureka Server for Spring dashboard." lightbox="media/java-components/eureka-ha.png"::: |
| 205 | + |
| 206 | +## Clean up resources |
| 207 | + |
| 208 | +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. |
| 209 | +
|
| 210 | +```azurecli |
| 211 | +az group delete \ |
| 212 | + --resource-group $RESOURCE_GROUP |
| 213 | +``` |
| 214 | +
|
| 215 | +## Next steps |
| 216 | +
|
| 217 | +> [!div class="nextstepaction"] |
| 218 | +> [Configure Eureka Server for Spring settings](java-eureka-server-usage.md) |
0 commit comments