eureka icon indicating copy to clipboard operation
eureka copied to clipboard

Eureka with AWS ECS Fargate

Open dulimitta opened this issue 7 years ago • 22 comments

I am deploying spring cloud application onto AWS ECS. I updated my eureka client with overriding EurekaInstanceConfigBean as to register with IP address. It is working well with launch type EC2 but failing with Fargate. Here is my configuration. I can see null values in the system logs.

@Bean
	public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils) {
		  EurekaInstanceConfigBean b = new EurekaInstanceConfigBean(inetUtils);
		  AmazonInfo info = AmazonInfo.Builder.newBuilder().autoBuild("eureka");
		  b.setDataCenterInfo(info);
		  info.getMetadata().put(AmazonInfo.MetaDataKey.publicHostname.getName(), info.get(AmazonInfo.MetaDataKey.publicIpv4));
		  System.out.println("*********************Public Host Name*******************************   " + info.get(AmazonInfo.MetaDataKey.publicHostname));
		  System.out.println("*********************Public IPV4 *******************************   " + info.get(AmazonInfo.MetaDataKey.publicIpv4));
		  System.out.println("*********************Local IPV4*******************************   " + info.get(AmazonInfo.MetaDataKey.localIpv4));
		  b.setHostname(info.get(AmazonInfo.MetaDataKey.publicHostname));
		  b.setIpAddress(info.get(AmazonInfo.MetaDataKey.publicIpv4));
		  b.setNonSecurePort(port);
		  return b;
	}

dulimitta avatar Jan 18 '18 03:01 dulimitta

@dulimitta by default eureka client queries the EC2 metadata endpoint for system information. I have not used Fargate yet, but I suspect perhaps this endpoint is not available within Fargate.

If that is the case, instead of autoBuild(), you'll want to manually create the AmazonInfo object with system information provided in other ways.

qiangdavidliu avatar Jan 30 '18 18:01 qiangdavidliu

Sorry for the late reply. How would I get system information in other ways? Can you please suggest.

dulimitta avatar Feb 22 '18 11:02 dulimitta

Fargate platform version 1.1 seems to support metadata for ECS. See anouncement.

I think that eureka-client does not support this. See AmazonInfo.java

In case anyone already has implemented this could you please point me to that. Anyway, it would be great to have eureka-client to support ECS (and especially Fargate).

One way to get metadata is to do it as described here.

jussiseppala avatar Jul 27 '18 09:07 jussiseppala

I created a sample how to do this. Hope this helps!

jussiseppala avatar Aug 17 '18 10:08 jussiseppala

Thank you @jussiseppala. I will look into it. We are using ECS with EC2 for now.

dulimitta avatar Aug 21 '18 15:08 dulimitta

Was this case resolved, can somebody post on how to get the container ip and port from the ecs containers (services) and send it to Eureka Server (ecs container).

Sridharc20 avatar Mar 19 '19 07:03 Sridharc20

@Sridharc20 we are using ECS with EC2. You can refer to @jussiseppala the last comment to get the container IP.

dulimitta avatar Mar 19 '19 13:03 dulimitta

Can we reopen this please? I would like to use Eureka on a Fargate cluster and it would be nice if Eureka supported this out of the box without me having to do hackity hacks.

I think I almost have it figured out, I could open a PR once I do. I am using Spring Cloud though...but my understanding is that configuration for integrating Eureka with Fargate should live here and not as a part of Spring Cloud project.

mljohns89 avatar Feb 22 '20 16:02 mljohns89

Reopened, happy to accept a PR with any examples.

troshko111 avatar Mar 23 '20 22:03 troshko111

@mjohns39 did you made it?

chrisegb avatar Apr 29 '20 09:04 chrisegb

@troshko111 I am facing same issue when I register my micro service with euraka it pick wrong ip address. I saw the sample created by @jussiseppala but that will make the code platform specific and I have to replicate that configuration in every micro service. Please let me know how we can fix this issue. If it's already fix please let me know the fix version.

shahbaz07 avatar Jul 01 '20 22:07 shahbaz07

@shahbaz07 did you get it?

chrisegb avatar Sep 27 '20 22:09 chrisegb

@ChristianEGB I didn't check this but I fixed this by adding below code in application class. But if it's fixed already please let me know the fixed version.

@Bean open fun eurekaInstanceConfig(inetUtils: InetUtils?): EurekaInstanceConfigBean? { inetUtils?.let { val config = EurekaInstanceConfigBean(inetUtils) var ip: String? = null try { ip = InetAddress.getLocalHost().hostAddress } catch (e: UnknownHostException) { e.printStackTrace() } config.ipAddress = ip config.nonSecurePort = getPort() config.isPreferIpAddress = true return config } return null }

open fun getPort(): Int { return 0 }

shahbaz07 avatar Sep 28 '20 19:09 shahbaz07

@shahbaz07 Will check and I will let yoo know, thanks.

chrisegb avatar Sep 28 '20 19:09 chrisegb

@ChristianEGB Thanks.

shahbaz07 avatar Sep 29 '20 01:09 shahbaz07

@ChristianEGB is there any update on this?

shahbaz07 avatar Nov 16 '20 11:11 shahbaz07

Did anyone figure out how to fix this issue?

anirbandutta9 avatar May 03 '22 13:05 anirbandutta9

@shahbaz07 @anirbandutta9 in my case I removed Eureka from my infra and I replaced that with the service discovery that AWS ECS already has.

chrisegb avatar May 03 '22 13:05 chrisegb

Yes. I figured it out. Apologies, I completely forgot to push a PR. I will try and do a gist or something at least today...and try to push PR this weekend

mljohns89 avatar May 03 '22 13:05 mljohns89

Yes. I figured it out. Apologies, I completely forgot to push a PR. I will try and do a gist or something at least today...and try to push PR this weekend

@mjohns39 Much appreciate ..thanks

anirbandutta9 avatar May 03 '22 13:05 anirbandutta9

@shahbaz07 @anirbandutta9 in my case I removed Eureka from my infra and I replaced that with the service discovery that AWS ECS already has.

@shahbaz07 We will see if we can implement service discovery in our environment .. Did you get any reference articles on how to do it?

anirbandutta9 avatar May 03 '22 13:05 anirbandutta9

Here is a gist: https://gist.github.com/mjohns39/e1f8881620292329b408465a982dc7b8. I also added the POJOs you'll need. Worth mentioning that it's basically identical to jussiseppala solution here. The minor difference is that I didn't need to use AmazonInfo

Edit: The tricky part is you essentially need to grab the private IP Address from the ECS Task Metadata and then override the destination IP Address with that private IP Address. You'll only know what that IP address is though at runtime since it is Fargate

mljohns89 avatar May 03 '22 15:05 mljohns89