k8s环境部署的集成arthas-spring-boot-starter spingboot项目无法访问控制台

本文主要是介绍k8s环境部署的集成arthas-spring-boot-starter spingboot项目无法访问控制台,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

k8s环境部署的集成arthas-spring-boot-starter项目无法访问控制台,springboot项目集成arthas-spring-boot-starter 会自带个控制台 供我们访问 但是当使用k8s环境部署后 这个页面就无法访问了

分析

首先看下arthas对应的配置 arthas-spring-boot-starter 中配置类 见 com.alibaba.arthas.spring.ArthasProperties

@ConfigurationProperties(prefix = "arthas")
public class ArthasProperties {private String ip;private int telnetPort;private int httpPort;private String tunnelServer;private String agentId;private String appName;/*** report executed command*/private String statUrl;/*** session timeout seconds*/private long sessionTimeout;private String username;private String password;private String home;/*** when arthas agent init error will throw exception by default.*/private boolean slientInit = false;/*** disabled commands,default disable stop command*/private String disabledCommands;private static final String DEFAULT_DISABLEDCOMMANDS = "stop";
}	

可以看到 的IP 以及端口配置 都是空的

实际的初始化 是在 com.taobao.arthas.agent.attach.ArthasAgent#init类中进行初始化的

public void init() throws IllegalStateException {// 尝试判断arthas是否已在运行,如果是的话,直接就退出try {Class.forName("java.arthas.SpyAPI"); // 加载不到会抛异常if (SpyAPI.isInited()) {return;}} catch (Throwable e) {// ignore}try {if (instrumentation == null) {instrumentation = ByteBuddyAgent.install();}// 检查 arthasHomeif (arthasHome == null || arthasHome.trim().isEmpty()) {// 解压出 arthasHomeURL coreJarUrl = this.getClass().getClassLoader().getResource("arthas-bin.zip");if (coreJarUrl != null) {File tempArthasDir = createTempDir();ZipUtil.unpack(coreJarUrl.openStream(), tempArthasDir);arthasHome = tempArthasDir.getAbsolutePath();} else {throw new IllegalArgumentException("can not getResources arthas-bin.zip from classloader: "+ this.getClass().getClassLoader());}}// find arthas-core.jarFile arthasCoreJarFile = new File(arthasHome, ARTHAS_CORE_JAR);if (!arthasCoreJarFile.exists()) {throw new IllegalStateException("can not find arthas-core.jar under arthasHome: " + arthasHome);}AttachArthasClassloader arthasClassLoader = new AttachArthasClassloader(new URL[] { arthasCoreJarFile.toURI().toURL() });/*** <pre>* ArthasBootstrap bootstrap = ArthasBootstrap.getInstance(inst);* </pre>*/Class<?> bootstrapClass = arthasClassLoader.loadClass(ARTHAS_BOOTSTRAP);Object bootstrap = bootstrapClass.getMethod(GET_INSTANCE, Instrumentation.class, Map.class).invoke(null,instrumentation, configMap);boolean isBind = (Boolean) bootstrapClass.getMethod(IS_BIND).invoke(bootstrap);if (!isBind) {String errorMsg = "Arthas server port binding failed! Please check $HOME/logs/arthas/arthas.log for more details.";throw new RuntimeException(errorMsg);}} catch (Throwable e) {errorMessage = e.getMessage();if (!slientInit) {throw new IllegalStateException(e);}}
}

可以看到会直接通过类加载器加载ARTHAS_BOOTSTRAP 常量 对应的 com.taobao.arthas.core.server.ArthasBootstrap 类 获取并调用其getInstance方法 然后在获取反射调用其bind方法

断点可以看到配置ip 为127.0.0.1
在这里插入图片描述
而127.0.0.1 对应的 host 即为localhost
当绑定的ip是localhost时 只能使用localhost 或者127.0.0.1 访问
没配置绑定ip的话 就会导致我们只能使用localhost 或者127.0.0.1 访问
在这里插入图片描述

另外 Arthas 默认启用了基于 HTTP Basic Authentication 的简单访问控制,目的是防止将 Arthas 的 Web 控制台暴露在公网上,避免安全隐患。

当你使用 arthas-spring-boot-starter 时,它会自动为你启用 Arthas 的访问控制功能。如果你没有显式配置用户名和密码,Arthas 会自动生成一个随机的用户名和密码,并将其打印在启动日志中。
这个是官网文档中并没有找到说明。当IP没进行配置时 默认是不需要鉴权的 使用本机的IP 也无须鉴权,当k8s 应用部署后 每次IP的地址都不一样 最简单的就是将IP配置成0.0.0.0 但是需要配置默认的密码 不然就会默认生成一个密码 打印到控制台 参见 SecurityAuthenticatorImpl的构造方法 密码不为空 就会设置一个默认的账号 。

SecurityAuthenticatorImpl的构造中有两个逻辑

  • 用户名不为空 密码为空 生成一个32位的随机密码
  • 用户为空 密码不为空 使用默认的用户名

源码位置 com.taobao.arthas.core.server.ArthasBootstrap#bind

if (IPUtils.isAllZeroIP(configure.getIp()) && StringUtils.isBlank(configure.getPassword())) {// 当 listen 0.0.0.0 时,强制生成密码,防止被远程连接String errorMsg = "Listening on 0.0.0.0 is very dangerous! External users can connect to your machine! "+ "No password is currently configured. " + "Therefore, a default password is generated, "+ "and clients need to use the password to connect!";AnsiLog.error(errorMsg);configure.setPassword(StringUtils.randomString(64));AnsiLog.error("Generated arthas password: " + configure.getPassword());logger().error(errorMsg);logger().info("Generated arthas password: " + configure.getPassword());
}this.securityAuthenticator = new SecurityAuthenticatorImpl(configure.getUsername(), configure.getPassword());

源码位 com.taobao.arthas.core.security.SecurityAuthenticatorImpl#SecurityAuthenticatorImpl

public SecurityAuthenticatorImpl(String username, String password) {if (username != null && password == null) {password = StringUtils.randomString(32);logger.info("\nUsing generated security password: {}\n", password);}if (username == null && password != null) {username = ArthasConstants.DEFAULT_USERNAME;}this.username = username;this.password = password;subject = new Subject();
}

解决

  • 针对IP 绑定不上的位置 我们可以直接设置绑定的IP 为0.0.0.0 同时显示的设置用户名和密码 (如果未配置账号密码 就会使用默认的用户名 和 随机生成64位的密码) 由于k8s部署 每次重启后IP会发生变更 这样导致使用的地址得频繁修改 (推荐使用入下方式)
arthas:ip: 0.0.0.0  username: adminpassword: xxx
  • 使用k8s中 service (服务(Service)提供一种抽象的方法,将运行在容器组(Pod)上的应用程序公开为网络服务。
    arthas配置也是需要添加的
arthas:ip: 0.0.0.0  username: adminpassword: xxx

在这里插入图片描述
提供内部或者外部访问 具体使用可以直接=使用图形化工具创建
内部访问: 虚拟IP + 具体的端口 默认8563
外部访问: nodeIp+ 外部访问端口

good day !!!

这篇关于k8s环境部署的集成arthas-spring-boot-starter spingboot项目无法访问控制台的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/989052

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程