简体   繁体   English

更新多个EC2实例上的日志级别

[英]Updating log level on multiple EC2 instances

My app runs on multiple EC2 instances to ensure high availability. 我的应用程序在多个EC2实例上运行,以确保高可用性。 The default log level is INFO for the app. 应用程序的默认日志级别为INFO But sometimes for debugging purposes, I want to update the log level to DEBUG . 但有时出于调试目的,我想将日志级别更新为DEBUG The request to update the log level passes through the ElasticLoadBalancer which delegates the request to any one of the multiple EC2 instances. 更新日志级别的请求通过ElasticLoadBalancer ,后者将请求委托给多个EC2实例中的任何一个。 The log level for the app running on that instance is updated but apps on the other instances will still log at level INFO . 在该实例上运行的应用程序的日志级别已更新,但其他实例上的应用程序仍将记录在INFO级别。 I want all the apps to log at DEBUG level. 我希望所有应用程序都能在DEBUG级别登录。

I am using Spring, SLF4J and Logback. 我正在使用Spring,SLF4J和Logback。

If I somehow make the log level information to be centralized, and the request will update the level on the centralized location, but still someone has to intimate apps on all instances about the change as app will never be requesting the log level. 如果我以某种方式使日志级别信息集中,并且请求将更新集中位置上的级别,但仍然有人必须在所有实例上关于更改的应用程序,因为应用程序永远不会请求日志级别。

If you want an AWS solution you can utilize sns. 如果您需要AWS解决方案,可以使用sns。

Once your app gets instantiated, register its endpoint (using it's private ip) to an sns topic for a http notification. 一旦您的应用程序被实例化,请将其端点(使用它的私有IP)注册到sns主题以获取http通知。 Thus instead of changing your LOG level through the load balancer you can issue a sns message and the message shall be sent to the endpoints registered. 因此,您可以发出sns消息而不是通过负载均衡器更改LOG级别,并将消息发送到已注册的端点。

Keep in mind to deregister the http endpoint from sns,once the app gets terminated. 一旦应用程序终止,请记住从sns取消注册http端点。

You might want to take a look at Zookeeper : 你可能想看看Zookeeper

ZooKeeper is a centralized service for maintaining configuration information , naming, providing distributed synchronization, and providing group services. ZooKeeper是一种集中式服务,用于维护配置信息 ,命名,提供分布式同步和提供组服务。

It's quite easy to setup and start small. 它很容易设置并从小处开始。 The app running on your EC2 nodes just needs to implement a "listener/watcher" interface. 在EC2节点上运行的应用程序只需要实现“监听器/观察器”界面。 This will notify your app when some configuration changed (eg. you decided you want to set the global log level to DEBUG ). 这将在某些配置发生更改时通知您的应用程序(例如,您决定要将全局日志级别设置为DEBUG )。

Based on this configuration-change, all of your nodes will update the local log-level without you having to come up with all kinds of ELB-bypassing manual REST-calls to tell each node to update - exactly what zookeeper is solving: 基于此配置更改,您的所有节点都将更新本地日志级别,而无需您提供各种ELB旁路手动REST调用来告知每个节点更新 - 确切地说是zookeeper正在解决的问题:

Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. 每次实施它们都需要做很多工作来修复不可避免的错误和竞争条件。 Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. 由于难以实现这些类型的服务,应用程序最初通常会吝啬它们,这使得它们在变化的情况下变得脆弱并且难以管理。 Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed. 即使正确完成,这些服务的不同实现也会在部署应用程序时导致管理复杂性。


When this works for you, you can add additional configuration to the zookeeper if needed, limiting the amount of configuration you need to package in the deployed apps or copied alongside them. 当这对您有用时,您可以根据需要向zookeeper添加其他配置,限制在已部署的应用程序中打包或与其一起复制所需的配置量。

Amazons Remote Management (Run Command) allows you to run commands on your instances. 亚马逊远程管理 (运行命令)允许您在实例上运行命令。 You just need a simple script to change the loglevel. 您只需要一个简单的脚本来更改日志级别。

But it is not easy to set it up and set grant all the needed IAM rights : 但要设置它并设置授予所有必需的IAM权限并不容易:

There are tags for an instance. 有一个实例的标签。 Some tags exist by default and you can create your own tags. 默认情况下存在一些标记,您可以创建自己的标记。 So, if we add a tag which identifies all those instances on which app is currently running, we can very easily fetch all those instances' IP addresses. 因此,如果我们添加一个标记来标识当前正在运行应用程序的所有实例,我们可以非常轻松地获取所有这些实例的IP地址。

DescribeInstancesRequest request = new DescribeInstancesRequest();

Filter filter1 = new Filter("tag:Environment", Collections.singletonList("Sandbox"));
Filter filter2 = new Filter("tag:Application", Collections.singletonList("xxxxx"));
Filter filter3 = new Filter("tag:Platform", Collections.singletonList("xxxx"));

InstanceProfileCredentialsProvider mInstanceProfileCredentialsProvider =
                new InstanceProfileCredentialsProvider();
AWSCredentials credentials = mInstanceProfileCredentialsProvider.getCredentials();

AmazonEC2 ec2Client = new AmazonEC2Client(credentials);
List<String> privateIps = new ArrayList<>();

ec2Client.describeInstances(request.withFilters(filter1, filter2, filter3)).getReservations().forEach(
                reservation -> reservation
                        .getInstances()
                        .forEach(instance -> privateIps.add(instance.getPrivateIpAddress())));

for (String privateIp : privateIps) {
     hitTheInstance(privateIp);
}

Here, I have used 3 tags to filter out the instances. 在这里,我使用了3个标签来过滤掉实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM