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如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一